This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvoice_item.go
150 lines (129 loc) · 4.67 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
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 int `json:"amount"`
Currency string `json:"currency"`
Customer string `json:"customer"`
Date UnixTime `json:"date"`
Description string `json:"description,omitempty"`
Invoice string `json:"invoice,omitempty"`
Subscription string `json:"subscription,omitempty"`
Proration bool `json:"proration"`
Metadata map[string]string `json:"metadata,omitempty"`
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 int
// 3-letter ISO code for currency.
Currency string
// (Optional) An arbitrary string which you can attach to the invoice item.
// The description is displayed in the invoice for easy tracking.
Description 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
// (Optional) The ID of a subscription to add this invoice item to.
Subscription string
Metadata map[string]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 (InvoiceItemClient) Create(params *InvoiceItemParams) (*InvoiceItem, error) {
item := InvoiceItem{}
values := url.Values{
"amount": {strconv.Itoa(params.Amount)},
"currency": {params.Currency},
"customer": {params.Customer},
}
// add optional parameters
if params.Description != "" {
values.Add("description", params.Description)
}
if params.Invoice != "" {
values.Add("invoice", params.Invoice)
}
if params.Subscription != "" {
values.Add("subscription", params.Subscription)
}
appendMetadata(values, params.Metadata)
err := query("POST", "/invoiceitems", values, &item)
return &item, err
}
// Retrieves the Invoice Item with the given ID.
//
// see https://stripe.com/docs/api#retrieve_invoiceitem
func (InvoiceItemClient) Get(id string) (*InvoiceItem, error) {
item := InvoiceItem{}
path := "/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 (InvoiceItemClient) Update(id string, params *InvoiceItemParams) (*InvoiceItem, error) {
item := InvoiceItem{}
values := make(url.Values)
if params.Description != "" {
values.Add("description", params.Description)
}
if params.Amount != 0 {
values.Add("invoice", strconv.Itoa(params.Amount))
}
appendMetadata(values, params.Metadata)
err := query("POST", "/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 (InvoiceItemClient) Delete(id string) (bool, error) {
resp := DeleteResp{}
path := "/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 (c InvoiceItemClient) List(limit int, before, after string) ([]*InvoiceItem, error) {
return c.list("", limit, before, after)
}
// Returns a list of Invoice Items for the specified Customer ID.
//
// see https://stripe.com/docs/api#list_invoiceitems
func (c InvoiceItemClient) CustomerList(id string, limit int, before, after string) ([]*InvoiceItem, error) {
return c.list(id, limit, before, after)
}
func (InvoiceItemClient) list(id string, limit int, before, after string) ([]*InvoiceItem, error) {
res := struct{ Data []*InvoiceItem }{}
params := listParams(limit, before, after)
if id != "" {
params.Add("customer", id)
}
err := query("GET", "/invoiceitems", params, &res)
return res.Data, err
}