forked from BoltApp/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit_card_integration_test.go
355 lines (292 loc) · 8 KB
/
credit_card_integration_test.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// +build integration
package braintree
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/BoltApp/braintree-go/testhelpers"
)
func TestCreditCard(t *testing.T) {
t.Parallel()
ctx := context.Background()
cust, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
g := testGateway.CreditCard()
card, err := g.Create(ctx, &CreditCard{
CustomerId: cust.Id,
Number: testCardVisa,
ExpirationDate: "05/14",
CVV: "100",
Options: &CreditCardOptions{
VerifyCard: testhelpers.BoolPtr(true),
},
})
if err != nil {
t.Fatal(err)
}
t.Log(card)
if card.Token == "" {
t.Fatal("invalid token")
}
if card.ProductID != "Unknown" { // Unknown appears to be reported from the Braintree Sandbox environment for all cards.
t.Errorf("got product id %q, want %q", card.ProductID, "Unknown")
}
// Update
card2, err := g.Update(ctx, &CreditCard{
Token: card.Token,
Number: testCardMastercard,
ExpirationDate: "05/14",
CVV: "100",
Options: &CreditCardOptions{
VerifyCard: testhelpers.BoolPtr(true),
},
})
if err != nil {
t.Fatal(err)
}
t.Log(card2)
if card2.Token != card.Token {
t.Fatal("tokens do not match")
}
if card2.CardType != "MasterCard" {
t.Fatal("card type does not match")
}
// Delete
err = g.Delete(ctx, card2)
if err != nil {
t.Fatal(err)
}
}
func TestCreditCardFailedAutoVerification(t *testing.T) {
t.Parallel()
ctx := context.Background()
cust, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
g := testGateway.CreditCard()
card, err := g.Create(ctx, &CreditCard{
CustomerId: cust.Id,
PaymentMethodNonce: FakeNonceProcessorDeclinedVisa,
})
if err == nil {
t.Fatal("Got no error, want error")
}
if g, w := err.(*BraintreeError).ErrorMessage, "Do Not Honor"; g != w {
t.Fatalf("Got error %q, want error %q", g, w)
}
t.Logf("%#v\n", err)
t.Logf("%#v\n", card)
}
func TestCreditCardForceNotVerified(t *testing.T) {
t.Parallel()
ctx := context.Background()
cust, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
g := testGateway.CreditCard()
card, err := g.Create(ctx, &CreditCard{
CustomerId: cust.Id,
PaymentMethodNonce: FakeNonceProcessorDeclinedVisa,
Options: &CreditCardOptions{
VerifyCard: testhelpers.BoolPtr(false),
},
})
if err != nil {
t.Fatal(err)
}
t.Logf("%#v\n", card)
}
func TestCreateCreditCardWithExpirationMonthAndYear(t *testing.T) {
t.Parallel()
ctx := context.Background()
customer, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
card, err := testGateway.CreditCard().Create(ctx, &CreditCard{
CustomerId: customer.Id,
Number: testCardVisa,
ExpirationMonth: "05",
ExpirationYear: "2014",
CVV: "100",
})
if err != nil {
t.Fatal(err)
}
if card.Token == "" {
t.Fatal("invalid token")
}
}
func TestCreateCreditCardInvalidInput(t *testing.T) {
t.Parallel()
ctx := context.Background()
card, err := testGateway.CreditCard().Create(ctx, &CreditCard{
Number: testCardVisa,
ExpirationDate: "05/14",
})
t.Log(card)
// This test should fail because customer id is required
if err == nil {
t.Fatal("expected to get error creating card because of required fields, but did not")
}
// TODO: validate fields
}
func TestFindCreditCard(t *testing.T) {
t.Parallel()
ctx := context.Background()
customer, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
card, err := testGateway.CreditCard().Create(ctx, &CreditCard{
CustomerId: customer.Id,
Number: testCardVisa,
ExpirationDate: "05/14",
CVV: "100",
Options: &CreditCardOptions{
VerifyCard: testhelpers.BoolPtr(true),
},
})
t.Log(card)
if err != nil {
t.Fatal(err)
}
if card.Token == "" {
t.Fatal("invalid token")
}
card2, err := testGateway.CreditCard().Find(ctx, card.Token)
t.Log(card2)
if err != nil {
t.Fatal(err)
}
if card2.Token != card.Token {
t.Fatal("tokens do not match")
}
}
func TestFindCreditCardBadData(t *testing.T) {
t.Parallel()
ctx := context.Background()
card, err := testGateway.CreditCard().Find(ctx, "invalid_token")
t.Log(card)
if err == nil {
t.Fatal("expected to get error because the token is invalid")
}
}
func TestSaveCreditCardWithVenmoSDKPaymentMethodCode(t *testing.T) {
t.Parallel()
ctx := context.Background()
customer, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
card, err := testGateway.CreditCard().Create(ctx, &CreditCard{
CustomerId: customer.Id,
VenmoSDKPaymentMethodCode: "stub-" + testCardVisa,
})
if err != nil {
t.Fatal(err)
}
if card.VenmoSDK {
t.Fatal("venmo card marked")
}
}
func TestSaveCreditCardWithVenmoSDKSession(t *testing.T) {
t.Parallel()
ctx := context.Background()
customer, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
card, err := testGateway.CreditCard().Create(ctx, &CreditCard{
CustomerId: customer.Id,
Number: testCardVisa,
ExpirationDate: "05/14",
Options: &CreditCardOptions{
VenmoSDKSession: "stub-session",
},
})
if err != nil {
t.Fatal(err)
}
if card.VenmoSDK {
t.Fatal("venmo card marked")
}
}
func TestGetExpiringBetweenCards(t *testing.T) {
now := time.Now()
t.Parallel()
ctx := context.Background()
customer, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
card1, err := testGateway.CreditCard().Create(ctx, &CreditCard{
CustomerId: customer.Id,
Number: testCardVisa,
ExpirationDate: now.AddDate(0, -2, 0).Format("01/2006"),
})
if err != nil {
t.Fatal(err)
}
t.Log("card1", card1.Token)
card2, err := testGateway.CreditCard().Create(ctx, &CreditCard{
CustomerId: customer.Id,
Number: testCardVisa,
ExpirationDate: now.Format("01/2006"),
})
if err != nil {
t.Fatal(err)
}
t.Log("card2", card2.Token)
card3, err := testGateway.CreditCard().Create(ctx, &CreditCard{
CustomerId: customer.Id,
Number: testCardVisa,
ExpirationDate: now.AddDate(0, 2, 0).Format("01/2006"),
})
if err != nil {
t.Fatal(err)
}
t.Log("card3", card3.Token)
fromDate := now.AddDate(0, -1, 0)
toDate := now.AddDate(0, 1, 0)
expiringCards := map[string]bool{}
results, err := testGateway.CreditCard().ExpiringBetweenIDs(ctx, fromDate, toDate)
if err != nil {
t.Fatal(err)
}
for page := 1; page <= results.PageCount; page++ {
results, err := testGateway.CreditCard().ExpiringBetweenPage(ctx, fromDate, toDate, results, page)
if err != nil {
t.Fatal(err)
}
t.Logf("Iterating page %d (page size: %d, total items: %d)", results.CurrentPageNumber, results.PageSize, results.TotalItems)
for _, card := range results.CreditCards {
expiringCards[card.Token] = true
}
}
if expiringCards[card1.Token] {
t.Fatalf("expiringCards contains card1 (%s), it shouldn't be returned in expiring cards results", card1.Token)
}
if !expiringCards[card2.Token] {
t.Fatalf("expiringCards does not contain card2 (%s), it should be returned in expiring cards results", card2.Token)
}
if expiringCards[card3.Token] {
t.Fatalf("expiringCards contains card3 (%s), it shouldn't be returned in expiring cards results", card3.Token)
}
_, err = testGateway.CreditCard().ExpiringBetweenPage(ctx, fromDate, toDate, results, 0)
t.Logf("%#v", err)
if err == nil || !strings.Contains(err.Error(), "page 0 out of bounds") {
t.Errorf("requesting page 0 should result in out of bounds error, but got %#v", err)
}
_, err = testGateway.CreditCard().ExpiringBetweenPage(ctx, fromDate, toDate, results, results.PageCount+1)
t.Logf("%#v", err)
if err == nil || !strings.Contains(err.Error(), fmt.Sprintf("page %d out of bounds", results.PageCount+1)) {
t.Errorf("requesting page %d should result in out of bounds error, but got %v", results.PageCount+1, err)
}
}