forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_source.go
207 lines (181 loc) · 5.56 KB
/
payment_source.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
package stripe
import (
"encoding/json"
"fmt"
)
// SourceParams is a union struct used to describe an
// arbitrary payment source.
type SourceParams struct {
Token string
Card *CardParams
}
// AppendDetails adds the source's details to the query string values.
// For cards: when creating a new one, the parameters are passed as a dictionary, but
// on updates they are simply the parameter name.
func (sp *SourceParams) AppendDetails(values *RequestValues, creating bool) {
if len(sp.Token) > 0 {
values.Add("source", sp.Token)
} else if sp.Card != nil {
sp.Card.AppendDetails(values, creating)
}
}
// CustomerSourceParams are used to manipulate a given Stripe
// Customer object's payment sources.
// For more details see https://stripe.com/docs/api#sources
type CustomerSourceParams struct {
Params
Customer string
Source *SourceParams
}
// SourceVerifyParams are used to verify a customer source
// For more details see https://stripe.com/docs/guides/ach-beta
type SourceVerifyParams struct {
Params
Customer string
Amounts [2]uint8
}
// SetSource adds valid sources to a CustomerSourceParams object,
// returning an error for unsupported sources.
func (cp *CustomerSourceParams) SetSource(sp interface{}) error {
source, err := SourceParamsFor(sp)
cp.Source = source
return err
}
// SourceParamsFor creates SourceParams objects around supported
// payment sources, returning errors if not.
//
// Currently supported source types are Card (CardParams) and
// Tokens/IDs (string), where Tokens could be single use card
// tokens or bitcoin receiver ids
func SourceParamsFor(obj interface{}) (*SourceParams, error) {
var sp *SourceParams
var err error
switch p := obj.(type) {
case *CardParams:
sp = &SourceParams{
Card: p,
}
case string:
sp = &SourceParams{
Token: p,
}
default:
err = fmt.Errorf("Unsupported source type %s", p)
}
return sp, err
}
// Displayer provides a human readable representation of a struct
type Displayer interface {
Display() string
}
// PaymentSourceType consts represent valid payment sources.
type PaymentSourceType string
const (
// PaymentSourceBankAccount is a constant representing a payment source
// which is a bank account.
PaymentSourceBankAccount PaymentSourceType = "bank_account"
// PaymentSourceBitcoinReceiver is a constant representing a payment source
// which is a Bitcoin receiver.
PaymentSourceBitcoinReceiver PaymentSourceType = "bitcoin_receiver"
// PaymentSourceObject is a constant representing a payment source which
// is a top level source object (/v1/sources).
PaymentSourceObject PaymentSourceType = "source"
// PaymentSourceCard is a constant representing a payment source which is a
// card.
PaymentSourceCard PaymentSourceType = "card"
)
// PaymentSource describes the payment source used to make a Charge.
// The Type should indicate which object is fleshed out (eg. BitcoinReceiver or Card)
// For more details see https://stripe.com/docs/api#retrieve_charge
type PaymentSource struct {
Type PaymentSourceType `json:"object"`
ID string `json:"id"`
BankAccount *BankAccount `json:"-"`
BitcoinReceiver *BitcoinReceiver `json:"-"`
Card *Card `json:"-"`
SourceObject *Source `json:"-"`
Deleted bool `json:"deleted"`
}
// SourceList is a list object for cards.
type SourceList struct {
ListMeta
Values []*PaymentSource `json:"data"`
}
// SourceListParams are used to enumerate the payment sources that are attached
// to a Customer.
type SourceListParams struct {
ListParams
Customer string
}
// Display human readable representation of source.
func (s *PaymentSource) Display() string {
switch s.Type {
case PaymentSourceBankAccount:
return s.BankAccount.Display()
case PaymentSourceBitcoinReceiver:
return s.BitcoinReceiver.Display()
case PaymentSourceCard:
return s.Card.Display()
case PaymentSourceObject:
return s.SourceObject.Display()
}
return ""
}
// UnmarshalJSON handles deserialization of a PaymentSource.
// This custom unmarshaling is needed because the specific
// type of payment instrument it refers to is specified in the JSON
func (s *PaymentSource) UnmarshalJSON(data []byte) error {
type source PaymentSource
var ss source
err := json.Unmarshal(data, &ss)
if err == nil {
*s = PaymentSource(ss)
switch s.Type {
case PaymentSourceBankAccount:
json.Unmarshal(data, &s.BankAccount)
case PaymentSourceBitcoinReceiver:
json.Unmarshal(data, &s.BitcoinReceiver)
case PaymentSourceCard:
json.Unmarshal(data, &s.Card)
case PaymentSourceObject:
json.Unmarshal(data, &s.SourceObject)
}
} else {
// the id is surrounded by "\" characters, so strip them
s.ID = string(data[1 : len(data)-1])
}
return nil
}
// MarshalJSON handles serialization of a PaymentSource.
// This custom marshaling is needed because the specific type
// of payment instrument it represents is specified by the PaymentSourceType
func (s *PaymentSource) MarshalJSON() ([]byte, error) {
var target interface{}
switch s.Type {
case PaymentSourceBitcoinReceiver:
target = struct {
Type PaymentSourceType `json:"object"`
*BitcoinReceiver
}{
Type: s.Type,
BitcoinReceiver: s.BitcoinReceiver,
}
case PaymentSourceCard:
var customerID *string
if s.Card.Customer != nil {
customerID = &s.Card.Customer.ID
}
target = struct {
Type PaymentSourceType `json:"object"`
Customer *string `json:"customer"`
*Card
}{
Type: s.Type,
Customer: customerID,
Card: s.Card,
}
case "":
target = s.ID
}
return json.Marshal(target)
}