This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
invoice_item.go
166 lines (142 loc) · 4.92 KB
/
invoice_item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package stripe
import (
"net/url"
"strconv"
)
// InvoiceItem represents a charge (or credit) that should be applied to the
// customer at the end of a billing cycle.
//
// see https://stripe.com/docs/api#invoiceitem_object
type InvoiceItem struct {
Id string `json:"id"`
Amount int64 `json:"amount"`
Currency string `json:"currency"`
Customer string `json:"customer"`
Date int64 `json:"date"`
Desc String `json:"description"`
Invoice String `json:"invoice"`
Livemode bool `json:"livemode"`
}
// InvoiceItemParams encapsulates options for creating a new Invoice Items.
type InvoiceItemParams struct {
// The ID of the customer who will be billed when this invoice item is
// billed.
Customer string
// The integer amount in cents of the charge to be applied to the upcoming
// invoice. If you want to apply a credit to the customer's account, pass a
// negative amount.
Amount int64
// 3-letter ISO code for currency. Currently, only 'usd' is supported.
Currency string
// (Optional) An arbitrary string which you can attach to the invoice item.
// The description is displayed in the invoice for easy tracking.
Desc string
// (Optional) The ID of an existing invoice to add this invoice item to.
// When left blank, the invoice item will be added to the next upcoming
// scheduled invoice.
Invoice string
}
// InvoiceItemClient encapsulates operations for creating, updating, deleting
// and querying invoices using the Stripe REST API.
type InvoiceItemClient struct{}
// Create adds an arbitrary charge or credit to the customer's upcoming invoice.
//
// see https://stripe.com/docs/api#invoiceitem_object
func (self *InvoiceItemClient) Create(params *InvoiceItemParams) (*InvoiceItem, error) {
item := InvoiceItem{}
values := url.Values{
"amount": {strconv.FormatInt(params.Amount, 10)},
"currency": {params.Currency},
"customer": {params.Customer},
}
// add optional parameters
if len(params.Desc) != 0 {
values.Add("description", params.Desc)
}
if len(params.Invoice) != 0 {
values.Add("invoice", params.Invoice)
}
err := query("POST", "/v1/invoiceitems", values, &item)
return &item, err
}
// Retrieves the Invoice Item with the given ID.
//
// see https://stripe.com/docs/api#retrieve_invoiceitem
func (self *InvoiceItemClient) Retrieve(id string) (*InvoiceItem, error) {
item := InvoiceItem{}
path := "/v1/invoiceitems/" + url.QueryEscape(id)
err := query("GET", path, nil, &item)
return &item, err
}
// Update changes the amount or description of an Invoice Item on an upcoming
// invoice, using the given Invoice Item ID.
//
// see https://stripe.com/docs/api#update_invoiceitem
func (self *InvoiceItemClient) Update(id string, params *InvoiceItemParams) (*InvoiceItem, error) {
item := InvoiceItem{}
values := url.Values{}
if len(params.Desc) != 0 {
values.Add("description", params.Desc)
}
if params.Amount != 0 {
values.Add("invoice", strconv.FormatInt(params.Amount, 10))
}
err := query("POST", "/v1/invoiceitems/"+url.QueryEscape(id), values, &item)
return &item, err
}
// Removes an Invoice Item with the given ID.
//
// see https://stripe.com/docs/api#delete_invoiceitem
func (self *InvoiceItemClient) Delete(id string) (bool, error) {
resp := DeleteResp{}
path := "/v1/invoiceitems/" + url.QueryEscape(id)
if err := query("DELETE", path, nil, &resp); err != nil {
return false, err
}
return resp.Deleted, nil
}
// Returns a list of Invoice Items.
//
// see https://stripe.com/docs/api#list_invoiceitems
func (self *InvoiceItemClient) List() ([]*InvoiceItem, error) {
return self.list("", 10, 0)
}
// Returns a list of Invoice Items at the specified range.
//
// see https://stripe.com/docs/api#list_invoiceitems
func (self *InvoiceItemClient) ListN(count int, offset int) ([]*InvoiceItem, error) {
return self.list("", count, offset)
}
// Returns a list of Invoice Items for the specified Customer ID.
//
// see https://stripe.com/docs/api#list_invoiceitems
func (self *InvoiceItemClient) CustomerList(id string) ([]*InvoiceItem, error) {
return self.list(id, 10, 0)
}
// Returns a list of Invoice Items for the specified Customer ID, at the
// specified range.
//
// see https://stripe.com/docs/api#list_invoiceitems
func (self *InvoiceItemClient) CustomerListN(id string, count int, offset int) ([]*InvoiceItem, error) {
return self.list(id, count, offset)
}
func (self *InvoiceItemClient) list(id string, count int, offset int) ([]*InvoiceItem, error) {
// define a wrapper function for the Invoice Items List, so that we can
// cleanly parse the JSON
type listInvoiceItemsResp struct{ Data []*InvoiceItem }
resp := listInvoiceItemsResp{}
// add the count and offset to the list of url values
values := url.Values{
"count": {strconv.Itoa(count)},
"offset": {strconv.Itoa(offset)},
}
// query for customer id, if provided
if id != "" {
values.Add("customer", id)
}
err := query("GET", "/v1/invoiceitems", values, &resp)
if err != nil {
return nil, err
}
return resp.Data, nil
}