forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_integration_test.go
492 lines (424 loc) · 12.8 KB
/
transaction_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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package braintree
import (
"math/rand"
"strconv"
"testing"
"time"
)
func randomAmount() *Decimal {
return NewDecimal(rand.Int63n(10000), 2)
}
func TestTransactionCreateSubmitForSettlementAndVoid(t *testing.T) {
tx, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
Amount: NewDecimal(2000, 2),
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
},
})
t.Log(tx)
if err != nil {
t.Fatal(err)
}
if tx.Id == "" {
t.Fatal("Received invalid ID on new transaction")
}
if tx.Status != "authorized" {
t.Fatal(tx.Status)
}
// Submit for settlement
ten := NewDecimal(1000, 2)
tx2, err := testGateway.Transaction().SubmitForSettlement(tx.Id, ten)
t.Log(tx2)
if err != nil {
t.Fatal(err)
}
if x := tx2.Status; x != "submitted_for_settlement" {
t.Fatal(x)
}
if amount := tx2.Amount; amount.Cmp(ten) != 0 {
t.Fatalf("transaction settlement amount (%s) did not equal amount requested (%s)", amount, ten)
}
// Void
tx3, err := testGateway.Transaction().Void(tx2.Id)
t.Log(tx3)
if err != nil {
t.Fatal(err)
}
if x := tx3.Status; x != "voided" {
t.Fatal(x)
}
}
func TestTransactionSearch(t *testing.T) {
txg := testGateway.Transaction()
createTx := func(amount *Decimal, customerName string) error {
_, err := txg.Create(&Transaction{
Type: "sale",
Amount: amount,
Customer: &Customer{
FirstName: customerName,
},
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
},
})
return err
}
ts := strconv.FormatInt(time.Now().Unix(), 10)
name := "Erik-" + ts
if err := createTx(randomAmount(), name); err != nil {
t.Fatal(err)
}
if err := createTx(randomAmount(), "Lionel-"+ts); err != nil {
t.Fatal(err)
}
query := new(SearchQuery)
f := query.AddTextField("customer-first-name")
f.Is = name
result, err := txg.Search(query)
if err != nil {
t.Fatal(err)
}
if !result.TotalItems.Valid || result.TotalItems.Int64 != 1 {
t.Fatal(result.Transactions)
}
tx := result.Transactions[0]
if x := tx.Customer.FirstName; x != name {
t.Log(name)
t.Fatal(x)
}
}
// This test will fail unless you set up your Braintree sandbox account correctly. See TESTING.md for details.
func TestTransactionCreateWhenGatewayRejected(t *testing.T) {
_, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
Amount: NewDecimal(201000, 2),
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
},
})
if err == nil {
t.Fatal("Did not receive error when creating invalid transaction")
}
if err.Error() != "Card Issuer Declined CVV" {
t.Fatal(err)
}
}
func TestFindTransaction(t *testing.T) {
createdTransaction, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
Amount: randomAmount(),
CreditCard: &CreditCard{
Number: testCreditCards["mastercard"].Number,
ExpirationDate: "05/14",
},
})
if err != nil {
t.Fatal(err)
}
foundTransaction, err := testGateway.Transaction().Find(createdTransaction.Id)
if err != nil {
t.Fatal(err)
}
if createdTransaction.Id != foundTransaction.Id {
t.Fatal("transaction ids do not match")
}
}
func TestFindNonExistantTransaction(t *testing.T) {
_, err := testGateway.Transaction().Find("bad_transaction_id")
if err == nil {
t.Fatal("Did not receive error when finding an invalid tx ID")
}
if err.Error() != "Not Found (404)" {
t.Fatal(err)
}
}
func TestAllTransactionFields(t *testing.T) {
tx := &Transaction{
Type: "sale",
Amount: randomAmount(),
OrderId: "my_custom_order",
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
CVV: "100",
},
Customer: &Customer{
FirstName: "Lionel",
},
BillingAddress: &Address{
StreetAddress: "1 E Main St",
Locality: "Chicago",
Region: "IL",
PostalCode: "60637",
},
ShippingAddress: &Address{
StreetAddress: "1 E Main St",
Locality: "Chicago",
Region: "IL",
PostalCode: "60637",
},
Options: &TransactionOptions{
SubmitForSettlement: true,
StoreInVault: true,
AddBillingAddressToPaymentMethod: true,
StoreShippingAddressInVault: true,
},
}
tx2, err := testGateway.Transaction().Create(tx)
if err != nil {
t.Fatal(err)
}
if tx2.Type != tx.Type {
t.Fatalf("expected Type to be equal, but %s was not %s", tx2.Type, tx.Type)
}
if tx2.Amount.Cmp(tx.Amount) != 0 {
t.Fatalf("expected Amount to be equal, but %s was not %s", tx2.Amount, tx.Amount)
}
if tx2.OrderId != tx.OrderId {
t.Fatalf("expected OrderId to be equal, but %s was not %s", tx2.OrderId, tx.OrderId)
}
if tx2.Customer.FirstName != tx.Customer.FirstName {
t.Fatalf("expected Customer.FirstName to be equal, but %s was not %s", tx2.Customer.FirstName, tx.Customer.FirstName)
}
if tx2.BillingAddress.StreetAddress != tx.BillingAddress.StreetAddress {
t.Fatalf("expected BillingAddress.StreetAddress to be equal, but %s was not %s", tx2.BillingAddress.StreetAddress, tx.BillingAddress.StreetAddress)
}
if tx2.BillingAddress.Locality != tx.BillingAddress.Locality {
t.Fatalf("expected BillingAddress.Locality to be equal, but %s was not %s", tx2.BillingAddress.Locality, tx.BillingAddress.Locality)
}
if tx2.BillingAddress.Region != tx.BillingAddress.Region {
t.Fatalf("expected BillingAddress.Region to be equal, but %s was not %s", tx2.BillingAddress.Region, tx.BillingAddress.Region)
}
if tx2.BillingAddress.PostalCode != tx.BillingAddress.PostalCode {
t.Fatalf("expected BillingAddress.PostalCode to be equal, but %s was not %s", tx2.BillingAddress.PostalCode, tx.BillingAddress.PostalCode)
}
if tx2.ShippingAddress.StreetAddress != tx.ShippingAddress.StreetAddress {
t.Fatalf("expected ShippingAddress.StreetAddress to be equal, but %s was not %s", tx2.ShippingAddress.StreetAddress, tx.ShippingAddress.StreetAddress)
}
if tx2.ShippingAddress.Locality != tx.ShippingAddress.Locality {
t.Fatalf("expected ShippingAddress.Locality to be equal, but %s was not %s", tx2.ShippingAddress.Locality, tx.ShippingAddress.Locality)
}
if tx2.ShippingAddress.Region != tx.ShippingAddress.Region {
t.Fatalf("expected ShippingAddress.Region to be equal, but %s was not %s", tx2.ShippingAddress.Region, tx.ShippingAddress.Region)
}
if tx2.ShippingAddress.PostalCode != tx.ShippingAddress.PostalCode {
t.Fatalf("expected ShippingAddress.PostalCode to be equal, but %s was not %s", tx2.ShippingAddress.PostalCode, tx.ShippingAddress.PostalCode)
}
if tx2.CreditCard.Token == "" {
t.Fatalf("expected CreditCard.Token to be equal, but %s was not %s", tx2.CreditCard.Token, tx.CreditCard.Token)
}
if tx2.Customer.Id == "" {
t.Fatalf("expected Customer.Id to be equal, but %s was not %s", tx2.Customer.Id, tx.Customer.Id)
}
if tx2.Status != "submitted_for_settlement" {
t.Fatalf("expected tx2.Status to be %s, but got %s", "submitted_for_settlement", tx2.Status)
}
}
// This test will only pass on Travis. See TESTING.md for more details.
func TestTransactionDisbursementDetails(t *testing.T) {
txn, err := testGateway.Transaction().Find("dskdmb")
if err != nil {
t.Fatal(err)
}
if txn.DisbursementDetails.DisbursementDate != "2013-06-27" {
t.Fatalf("expected disbursement date to be %s, was %s", "2013-06-27", txn.DisbursementDetails.DisbursementDate)
}
if txn.DisbursementDetails.SettlementAmount.Cmp(NewDecimal(10000, 2)) != 0 {
t.Fatalf("expected settlement amount to be %s, was %s", NewDecimal(10000, 2), txn.DisbursementDetails.SettlementAmount)
}
if txn.DisbursementDetails.SettlementCurrencyIsoCode != "USD" {
t.Fatalf("expected settlement currency code to be %s, was %s", "USD", txn.DisbursementDetails.SettlementCurrencyIsoCode)
}
if txn.DisbursementDetails.SettlementCurrencyExchangeRate.Cmp(NewDecimal(100, 2)) != 0 {
t.Fatalf("expected settlement currency exchange rate to be %s, was %s", NewDecimal(100, 2), txn.DisbursementDetails.SettlementCurrencyExchangeRate)
}
if !txn.DisbursementDetails.FundsHeld.Valid || txn.DisbursementDetails.FundsHeld.Bool {
t.Error("funds held doesn't match")
}
if !txn.DisbursementDetails.Success.Valid || !txn.DisbursementDetails.Success.Bool {
t.Error("success doesn't match")
}
}
func TestTransactionCreateFromPaymentMethodCode(t *testing.T) {
customer, err := testGateway.Customer().Create(&Customer{
CreditCard: &CreditCard{
Number: testCreditCards["discover"].Number,
ExpirationDate: "05/14",
},
})
if err != nil {
t.Fatal(err)
}
if customer.CreditCards.CreditCard[0].Token == "" {
t.Fatal("invalid token")
}
tx, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
CustomerID: customer.Id,
Amount: randomAmount(),
PaymentMethodToken: customer.CreditCards.CreditCard[0].Token,
})
if err != nil {
t.Fatal(err)
}
if tx.Id == "" {
t.Fatal("invalid tx id")
}
}
func TestSettleTransaction(t *testing.T) {
old_environment := testGateway.Environment
txn, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
Amount: randomAmount(),
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
},
})
if err != nil {
t.Fatal(err)
}
txn, err = testGateway.Transaction().SubmitForSettlement(txn.Id, txn.Amount)
if err != nil {
t.Fatal(err)
}
testGateway.Environment = Production
_, err = testGateway.Transaction().Settle(txn.Id)
if err.Error() != "Operation not allowed in production environment" {
t.Log(testGateway.Environment)
t.Fatal(err)
}
testGateway.Environment = old_environment
txn, err = testGateway.Transaction().Settle(txn.Id)
if err != nil {
t.Fatal(err)
}
if txn.Status != "settled" {
t.Fatal(txn.Status)
}
}
func TestTrxPaymentMethodNonce(t *testing.T) {
txn, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
Amount: randomAmount(),
PaymentMethodNonce: "fake-apple-pay-mastercard-nonce",
})
if err != nil {
t.Fatal(err)
}
txn, err = testGateway.Transaction().SubmitForSettlement(txn.Id, txn.Amount)
if err != nil {
t.Fatal(err)
}
}
func TestTransactionCreateSettleAndFullRefund(t *testing.T) {
amount := NewDecimal(20000, 2)
txn, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
Amount: amount,
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
},
})
if err != nil {
t.Fatal(err)
}
txn, err = testGateway.Transaction().SubmitForSettlement(txn.Id, txn.Amount)
if err != nil {
t.Fatal(err)
}
txn, err = testGateway.Transaction().Settle(txn.Id)
if err != nil {
t.Fatal(err)
}
if txn.Status != "settled" {
t.Fatal(txn.Status)
}
// Refund
refundTxn, err := testGateway.Transaction().Refund(txn.Id)
t.Log(refundTxn)
if err != nil {
t.Fatal(err)
}
if x := refundTxn.Status; x != "submitted_for_settlement" {
t.Fatal(x)
}
refundTxn, err = testGateway.Transaction().Settle(refundTxn.Id)
if err != nil {
t.Fatal(err)
}
if refundTxn.Status != "settled" {
t.Fatal(txn.Status)
}
if *refundTxn.RefundedTransactionId != txn.Id {
t.Fatal(*refundTxn.RefundedTransactionId)
}
// Check that the refund shows up in the original transaction
txn, err = testGateway.Transaction().Find(txn.Id)
if err != nil {
t.Fatal(err)
}
if txn.RefundIds != nil && (*txn.RefundIds)[0] != refundTxn.Id {
t.Fatal(*txn.RefundIds)
}
// Second refund should fail
refundTxn, err = testGateway.Transaction().Refund(txn.Id)
t.Log(refundTxn)
if err.Error() != "Transaction has already been completely refunded." {
t.Fatal(err)
}
}
func TestTransactionCreateSettleAndPartialRefund(t *testing.T) {
amount := NewDecimal(10000, 2)
refundAmt1 := NewDecimal(5000, 2)
refundAmt2 := NewDecimal(5001, 2)
txn, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
Amount: amount,
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
},
})
if err != nil {
t.Fatal(err)
}
txn, err = testGateway.Transaction().SubmitForSettlement(txn.Id, txn.Amount)
if err != nil {
t.Fatal(err)
}
txn, err = testGateway.Transaction().Settle(txn.Id)
if err != nil {
t.Fatal(err)
}
if txn.Status != "settled" {
t.Fatal(txn.Status)
}
// Refund
refundTxn, err := testGateway.Transaction().Refund(txn.Id, refundAmt1)
t.Log(refundTxn)
if err != nil {
t.Fatal(err)
}
if x := refundTxn.Status; x != "submitted_for_settlement" {
t.Fatal(x)
}
refundTxn, err = testGateway.Transaction().Settle(refundTxn.Id)
if err != nil {
t.Fatal(err)
}
if refundTxn.Status != "settled" {
t.Fatal(txn.Status)
}
// Refund amount too large
refundTxn, err = testGateway.Transaction().Refund(txn.Id, refundAmt2)
t.Log(refundTxn)
if err.Error() != "Refund amount is too large." {
t.Fatal(err)
}
}