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 pathcharge.go
189 lines (165 loc) · 6.16 KB
/
charge.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package stripe
import (
"net/url"
"strconv"
)
// ISO 3-digit Currency Codes for major currencies (not the full list).
const (
USD = "usd" // US Dollar ($)
EUR = "eur" // Euro (€)
GBP = "gbp" // British Pound Sterling (UK£)
JPY = "jpy" // Japanese Yen (¥)
CAD = "cad" // Canadian Dollar (CA$)
HKD = "hkd" // Hong Kong Dollar (HK$)
CNY = "cny" // Chinese Yuan (CN¥)
AUD = "aud" // Australian Dollar (A$)
)
// Charge represents details about a credit card charge in Stripe.
//
// see https://stripe.com/docs/api#charge_object
type Charge struct {
ID string `json:"id"`
Description string `json:"description,omitempty"`
Amount int `json:"amount"`
Card *Card `json:"card"`
Currency string `json:"currency"`
Created UnixTime `json:"created"`
Customer string `json:"customer,omitempty"`
Invoice string `json:"invoice,omitempty"`
Paid bool `json:"paid"`
Refunded bool `json:"refunded,omitempty"`
AmountRefunded int `json:"amount_refunded,omitempty"`
BalanceTransaction string `json:"balance_transaction"`
Dispute *Dispute `json:"dispute,omitempty"`
FailureMessage string `json:"failure_message,omitempty"`
FailureCode string `json:"failure_code,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Livemode bool `json:"livemode"`
}
type Dispute struct {
Charge string `json:"charge"`
Livemode bool `json:"livemode"`
Amount int `json:"amount"`
Created UnixTime `json:"created"`
Currency string `json:"currency"`
Reason string `json:"reason"`
Status string `json:"status"`
BalanceTransaction string `json:"balance_transaction"`
Evidence string `json:"evidence,omitempty"`
EvidenceDueBy *UnixTime `json:"evidence_due_by,omitempty"`
Protected bool `json:"is_protected,omitempty"`
}
// ChargeParams encapsulates options for creating a new Charge.
type ChargeParams struct {
// A positive integer in cents representing how much to charge the card.
// The minimum amount is 50 cents.
Amount int
// 3-letter ISO code for currency. Currently, only 'usd' is supported.
Currency string
// (Optional) Either customer or card is required, but not both The ID of an
// existing customer that will be charged in this request.
Customer string
// (Optional) Credit Card that should be charged.
Card *CardParams
// (Optional) Credit Card token that should be charged.
Token string
// An arbitrary string which you can attach to a charge object. It is
// displayed when in the web interface alongside the charge. It's often a
// good idea to use an email address as a description for tracking later.
Description string
// Whether or not to immediately capture the charge. Default is true.
Capture *bool
// An arbitrary string to be displayed alongside your company name on your
// customer's credit card statement. This may be up to 15 characters.
StatementDescription string
Metadata map[string]string
}
// ChargeClient encapsulates operations for creating, updating, deleting and
// querying charges using the Stripe REST API.
type ChargeClient struct{}
// Creates a new credit card Charge.
//
// see https://stripe.com/docs/api#create_charge
func (ChargeClient) Create(params *ChargeParams) (*Charge, error) {
charge := Charge{}
values := url.Values{
"amount": {strconv.Itoa(params.Amount)},
"currency": {params.Currency},
}
if params.Description != "" {
values.Add("description", params.Description)
}
if params.Capture != nil && !*params.Capture {
values.Add("capture", "false")
}
if params.StatementDescription != "" {
values.Add("statement_description", params.StatementDescription)
}
appendMetadata(values, params.Metadata)
// add optional credit card details, if specified
if params.Card != nil {
appendCardParams(values, true, params.Card)
} else if len(params.Token) > 0 {
values.Add("card", params.Token)
} else {
// if no credit card is provide we need to specify the customer
values.Add("customer", params.Customer)
}
err := query("POST", "/charges", values, &charge)
return &charge, err
}
// Retrieves the details of a charge with the given ID.
//
// see https://stripe.com/docs/api#retrieve_charge
func (ChargeClient) Get(id string) (*Charge, error) {
charge := Charge{}
path := "/charges/" + url.QueryEscape(id)
err := query("GET", path, nil, &charge)
return &charge, err
}
// Refunds a charge for the full amount.
//
// see https://stripe.com/docs/api#refund_charge
func (ChargeClient) Refund(id string) (*Charge, error) {
values := url.Values{}
charge := Charge{}
path := "/charges/" + url.QueryEscape(id) + "/refund"
err := query("POST", path, values, &charge)
return &charge, err
}
// Refunds a charge for the specified amount.
//
// see https://stripe.com/docs/api#refund_charge
func (ChargeClient) RefundAmount(id string, amt int) (*Charge, error) {
values := url.Values{
"amount": {strconv.Itoa(amt)},
}
charge := Charge{}
path := "/charges/" + url.QueryEscape(id) + "/refund"
err := query("POST", path, values, &charge)
return &charge, err
}
// Returns a list of your Charges with the specified range.
//
// see https://stripe.com/docs/api#list_charges
func (c ChargeClient) List(limit int, before, after string) ([]*Charge, bool, error) {
return c.list("", limit, before, after)
}
// Returns a list of your Charges with the given Customer ID.
//
// see https://stripe.com/docs/api#list_charges
func (c ChargeClient) CustomerList(id string, limit int, before, after string) ([]*Charge, bool, error) {
return c.list(id, limit, before, after)
}
func (ChargeClient) list(id string, limit int, before, after string) ([]*Charge, bool, error) {
res := struct {
ListObject
Data []*Charge
}{}
params := listParams(limit, before, after)
if id != "" {
params.Add("customer", id)
}
err := query("GET", "/charges", params, &res)
return res.Data, res.More, err
}