This repository was archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcard.go
More file actions
177 lines (168 loc) · 5.09 KB
/
card.go
File metadata and controls
177 lines (168 loc) · 5.09 KB
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
package stripe
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
// Card is a representation of a credit card, as reported by the Stripe API.
type Card struct {
Type string `json:"type"` // Card brand.
ExpYear int `json:"exp_year"`
CVC string `json:"cvc"`
CVCCheck string `json:"cvc_check"` // The result of a check: "pass", "fail", "unchecked", or nil
Country string `json:"country"` // Stripe's "best guess" at the country the card is in
Name string `json:"name"`
AddressCountry string `json:"address_country"` // The user-supplied country for the card's billing address
State string `json:"address_state"`
Zip string `json:"address_zip"`
AddressLine1 string `json:"address_line1"`
AddressLine2 string `json:"address_line2"`
LastFour string `json:"last4"`
Number string `json:"number"`
Object string `json:"object"` // Should always be "card"
ExpMonth int `json:"exp_month"`
Fingerprint string `json:"fingerprint"` // This uniquely identifies the card's number
AddressZipCheck string `json:"address_zip_check"` // The result of a zip code check: "pass", "fail", "unchecked", or "nil"
AddressLine1Check string `json:"address_line1_check"` // The result of an address check: "pass", "fail", "unchecked", or "nil"
ID string `json:"id"`
}
func (card *Card) String() string {
if card.Number != "" {
return card.Number
}
if card.ID != "" {
return card.ID
}
if card.Name != "" {
str := fmt.Sprintf("%s's", card.Name)
if card.Type != "" {
str = fmt.Sprintf("%s %s", str, card.Type)
} else {
str = fmt.Sprintf("%s card", str)
}
if card.LastFour != "" {
str = fmt.Sprintf("%s (%s)", str, card.LastFour)
}
return str
}
if card.Fingerprint != "" {
return fmt.Sprintf("Fingerprint: %s", card.Fingerprint)
}
return "Unknown card."
}
// ChargeValues sets *card's non-empty properties to their appropriate key in *values
// This is useful for constructing HTTP requests from Card objects
// This also satisfies the Chargeable interface, allowing Cards to be charged
func (card *Card) ChargeValues(values *url.Values) error {
if card == nil {
// TODO: Throw error
}
if card.Number != "" {
values.Set("card[number]", card.Number)
} else {
// TODO: Throw error
}
if card.ExpMonth != 0 {
values.Set("card[exp_month]", strconv.Itoa(card.ExpMonth))
} else {
// TODO: Throw error
}
if card.ExpYear != 0 {
values.Set("card[exp_year]", strconv.Itoa(card.ExpYear))
} else {
// TODO: throw error
}
if card.CVC != "" {
values.Set("card[cvc]", card.CVC)
}
if card.Name != "" {
values.Set("card[name]", card.Name)
}
if card.AddressLine1 != "" {
values.Set("card[address_line1]", card.AddressLine1)
}
if card.AddressLine2 != "" {
values.Set("card[address_line2]", card.AddressLine2)
}
if card.Zip != "" {
values.Set("card[address_zip]", card.Zip)
}
if card.State != "" {
values.Set("card[address_state]", card.State)
}
if card.AddressCountry != "" {
values.Set("card[address_country]", card.AddressCountry)
} else if card.Country != "" {
values.Set("card[address_country]", card.Country)
}
return nil
}
// Token is the representation of a credit card token, the one-use string generated by Stripe to be used as a credit card.
type Token struct {
LiveMode bool `json:"livemode"`
Created int64 `json:"created"`
Used bool `json:"used"`
Amount int64 `json:"amount"`
Object string `json:"object"` // Should always be "token"
Currency string `json:"currency"`
ID string `json:"id"`
Card *Card `json:"card"`
Error *RawError `json:"error"`
}
func (token *Token) String() string {
return fmt.Sprintf("%s (%s)", token.ID, token.Card)
}
// ChargeValues sets *token's ID to the appropriate key in *values.
// This is handy for constructing HTTP requests from Tokens.
// This also satisfies the Chargeable interface, allowing tokens to be charged.
func (token *Token) ChargeValues(values *url.Values) error {
if token == nil {
// TODO: throw an error
}
if token.ID != "" {
values.Set("card", token.ID)
} else {
// TODO: Throw an error
}
return nil
}
// GetToken retrieves the Token with an ID of id from Stripe
func (stripe *Stripe) GetToken(id string) (resp *Token, err error) {
r, err := stripe.request("GET", "tokens/"+id, "")
if err != nil {
return nil, err
}
err = json.Unmarshal(r, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
//TODO: Throw an error
}
return resp, err
}
// Create token swaps credit card details for a Token on Stripe's servers
func (stripe *Stripe) CreateToken(card *Card) (resp *Token, err error) {
values := make(url.Values)
if card == nil {
// TODO: throw an error
}
err = card.ChargeValues(&values)
if err != nil {
return nil, err
}
params := values.Encode()
r, err := stripe.request("POST", "tokens", params)
if err != nil {
return nil, err
}
err = json.Unmarshal(r, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
//TODO: Throw an error
}
return resp, nil
}