This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
211 lines (184 loc) · 5.92 KB
/
card.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package stripe
import (
"encoding/json"
"fmt"
"net/url"
)
// CardBrand is the list of allowed values for the card's brand.
// Allowed values are "Unknown", "Visa", "American Express", "MasterCard", "Discover"
// "JCB", "Diners Club".
type CardBrand string
// Verification is the list of allowed verification responses.
// Allowed values are "pass", "fail", "unchecked", "unavailabe".
type Verification string
// CardFunding is the list of allowed values for the card's funding.
// Allowed values are "credit", "debit", "prepaid", "unknown".
type CardFunding string
// CardParams is the set of parameters that can be used when creating or updating a card.
// For more details see https://stripe.com/docs/api#create_card and https://stripe.com/docs/api#update_card.
type CardParams struct {
Params
Token string
Default bool
Account, Customer, Recipient string
Name, Number, Month, Year, CVC, Currency string
Address1, Address2, City, State, Zip, Country string
}
// CardListParams is the set of parameters that can be used when listing cards.
// For more details see https://stripe.com/docs/api#list_cards.
type CardListParams struct {
ListParams
Account, Customer, Recipient string
}
// Card is the resource representing a Stripe credit/debit card.
// For more details see https://stripe.com/docs/api#cards.
type Card struct {
ID string `json:"id"`
Month uint8 `json:"exp_month"`
Year uint16 `json:"exp_year"`
Fingerprint string `json:"fingerprint"`
Funding CardFunding `json:"funding"`
LastFour string `json:"last4"`
Brand CardBrand `json:"brand"`
Currency Currency `json:"currency"`
Default bool `json:"default_for_currency"`
City string `json:"address_city"`
Country string `json:"address_country"`
Address1 string `json:"address_line1"`
Address1Check Verification `json:"address_line1_check"`
Address2 string `json:"address_line2"`
State string `json:"address_state"`
Zip string `json:"address_zip"`
ZipCheck Verification `json:"address_zip_check"`
CardCountry string `json:"country"`
Customer *Customer `json:"customer"`
CVCCheck Verification `json:"cvc_check"`
Meta map[string]string `json:"metadata"`
Name string `json:"name"`
Recipient *Recipient `json:"recipient"`
DynLastFour string `json:"dynamic_last4"`
Deleted bool `json:"deleted"`
// Description is a succinct summary of the card's information.
//
// Please note that this field is for internal use only and is not returned
// as part of standard API requests.
Description string `json:"description"`
// IIN is the card's "Issuer Identification Number".
//
// Please note that this field is for internal use only and is not returned
// as part of standard API requests.
IIN string `json:"iin"`
// Issuer is a bank or financial institution that provides the card.
//
// Please note that this field is for internal use only and is not returned
// as part of standard API requests.
Issuer string `json:"issuer"`
}
// CardList is a list object for cards.
type CardList struct {
ListMeta
Values []*Card `json:"data"`
}
// AppendDetails adds the card's details to the query string values.
// When creating a new card, the parameters are passed as a dictionary, but
// on updates they are simply the parameter name.
func (c *CardParams) AppendDetails(values *url.Values, creating bool) {
if creating {
if len(c.Token) > 0 && len(c.Account) > 0 {
values.Add("external_account", c.Token)
} else if len(c.Token) > 0 {
values.Add("card", c.Token)
} else {
values.Add("card[object]", "card")
values.Add("card[number]", c.Number)
if len(c.CVC) > 0 {
values.Add("card[cvc]", c.CVC)
}
if len(c.Currency) > 0 {
values.Add("card[currency]", c.Currency)
}
}
}
if len(c.Month) > 0 {
if creating {
values.Add("card[exp_month]", c.Month)
} else {
values.Add("exp_month", c.Month)
}
}
if len(c.Year) > 0 {
if creating {
values.Add("card[exp_year]", c.Year)
} else {
values.Add("exp_year", c.Year)
}
}
if len(c.Name) > 0 {
if creating {
values.Add("card[name]", c.Name)
} else {
values.Add("name", c.Name)
}
}
if len(c.Address1) > 0 {
if creating {
values.Add("card[address_line1]", c.Address1)
} else {
values.Add("address_line1", c.Address1)
}
}
if len(c.Address2) > 0 {
if creating {
values.Add("card[address_line2]", c.Address2)
} else {
values.Add("address_line2", c.Address2)
}
}
if len(c.City) > 0 {
if creating {
values.Add("card[address_city]", c.City)
} else {
values.Add("address_city", c.City)
}
}
if len(c.State) > 0 {
if creating {
values.Add("card[address_state]", c.State)
} else {
values.Add("address_state", c.State)
}
}
if len(c.Zip) > 0 {
if creating {
values.Add("card[address_zip]", c.Zip)
} else {
values.Add("address_zip", c.Zip)
}
}
if len(c.Country) > 0 {
if creating {
values.Add("card[address_country]", c.Country)
} else {
values.Add("address_country", c.Country)
}
}
}
// Display human readable representation of a Card.
func (c *Card) Display() string {
return fmt.Sprintf("%s ending in %s", c.Brand, c.LastFour)
}
// UnmarshalJSON handles deserialization of a Card.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (c *Card) UnmarshalJSON(data []byte) error {
type card Card
var cc card
err := json.Unmarshal(data, &cc)
if err == nil {
*c = Card(cc)
} else {
// the id is surrounded by "\" characters, so strip them
c.ID = string(data[1 : len(data)-1])
}
return nil
}