forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction_integration_test.go
1188 lines (1028 loc) · 32.4 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package braintree
import (
"math/rand"
"net/http"
"reflect"
"strconv"
"testing"
"time"
"github.com/lionelbarrow/braintree-go/testhelpers"
)
func randomAmount() *Decimal {
return NewDecimal(rand.Int63n(10000), 2)
}
func TestTransactionCreateSubmitForSettlementAndVoid(t *testing.T) {
t.Parallel()
tx, err := testGateway.Transaction().Create(&TransactionRequest{
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 != TransactionStatusAuthorized {
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 != TransactionStatusSubmittedForSettlement {
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 != TransactionStatusVoided {
t.Fatal(x)
}
}
func TestTransactionSearch(t *testing.T) {
t.Parallel()
txg := testGateway.Transaction()
createTx := func(amount *Decimal, customerName string) error {
_, err := txg.Create(&TransactionRequest{
Type: "sale",
Amount: amount,
Customer: &Customer{
FirstName: customerName,
},
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
},
})
return err
}
unique := testhelpers.RandomString()
name0 := "Erik-" + unique
if err := createTx(randomAmount(), name0); err != nil {
t.Fatal(err)
}
name1 := "Lionel-" + unique
if err := createTx(randomAmount(), name1); err != nil {
t.Fatal(err)
}
query := new(SearchQuery)
f := query.AddTextField("customer-first-name")
f.Is = name0
result, err := txg.Search(query)
if err != nil {
t.Fatal(err)
}
if result.TotalItems != 1 {
t.Fatal(result.Transactions)
}
tx := result.Transactions[0]
if x := tx.Customer.FirstName; x != name0 {
t.Log(name0)
t.Fatal(x)
}
}
func TestTransactionSearchTime(t *testing.T) {
txg := testGateway.Transaction()
createTx := func(amount *Decimal, customerName string) error {
_, err := txg.Create(&TransactionRequest{
Type: "sale",
Amount: amount,
Customer: &Customer{
FirstName: customerName,
},
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
},
})
return err
}
unique := testhelpers.RandomString()
name0 := "Erik-" + unique
if err := createTx(randomAmount(), name0); err != nil {
t.Fatal(err)
}
name1 := "Lionel-" + unique
if err := createTx(randomAmount(), name1); err != nil {
t.Fatal(err)
}
{ // test: txn is returned if querying for created at before now
query := new(SearchQuery)
f1 := query.AddTextField("customer-first-name")
f1.Is = name0
f2 := query.AddTimeField("created-at")
f2.Max = time.Now()
result, err := txg.Search(query)
if err != nil {
t.Fatal(err)
}
if result.TotalItems != 1 {
t.Fatal(result.Transactions)
}
tx := result.Transactions[0]
if x := tx.Customer.FirstName; x != name0 {
t.Log(name0)
t.Fatal(x)
}
}
{ // test: txn is not returned if querying for created at before 1 hour ago
query := new(SearchQuery)
f1 := query.AddTextField("customer-first-name")
f1.Is = name0
f2 := query.AddTimeField("created-at")
f2.Max = time.Now().Add(-time.Hour)
result, err := txg.Search(query)
if err != nil {
t.Fatal(err)
}
if result.TotalItems != 0 {
t.Fatal(result.Transactions)
}
}
}
// This test will fail unless you set up your Braintree sandbox account correctly. See TESTING.md for details.
func TestTransactionCreateWhenGatewayRejected(t *testing.T) {
t.Parallel()
_, err := testGateway.Transaction().Create(&TransactionRequest{
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)
}
if err.(*BraintreeError).Transaction.ProcessorResponseCode != 2010 {
t.Fatalf("expected err.Transaction.ProcessorResponseCode to be 2010, but got %d", err.(*BraintreeError).Transaction.ProcessorResponseCode)
}
if err.(*BraintreeError).Transaction.AdditionalProcessorResponse != "2010 : Card Issuer Declined CVV" {
t.Fatalf("expected err.Transaction.ProcessorResponseCode to be `2010 : Card Issuer Declined CVV`, but got %s", err.(*BraintreeError).Transaction.AdditionalProcessorResponse)
}
}
func TestTransactionCreateWhenGatewayRejectedFraud(t *testing.T) {
t.Parallel()
_, err := testGateway.Transaction().Create(&TransactionRequest{
Type: "sale",
Amount: NewDecimal(201000, 2),
PaymentMethodNonce: FakeNonceGatewayRejectedFraud,
})
if err == nil {
t.Fatal("Did not receive error when creating invalid transaction")
}
if err.Error() != "Gateway Rejected: fraud" {
t.Fatal(err)
}
txn := err.(*BraintreeError).Transaction
if txn.Status != TransactionStatusGatewayRejected {
t.Fatalf("Got status %q, want %q", txn.Status, TransactionStatusGatewayRejected)
}
if txn.GatewayRejectionReason != GatewayRejectionReasonFraud {
t.Fatalf("Got gateway rejection reason %q, wanted %q", txn.GatewayRejectionReason, GatewayRejectionReasonFraud)
}
if txn.ProcessorResponseCode != 0 {
t.Fatalf("Got processor response code %q, want %q", txn.ProcessorResponseCode, 0)
}
}
func TestTransactionCreatedWhenCVVDoesNotMatch(t *testing.T) {
t.Parallel()
_, err := testGateway.Transaction().Create(&TransactionRequest{
Type: "sale",
Amount: randomAmount(),
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
CVV: "200", // Should cause CVV does not match response
},
})
if err.Error() != "Gateway Rejected: cvv" {
t.Fatal(err)
}
txn := err.(*BraintreeError).Transaction
if txn.Status != TransactionStatusGatewayRejected {
t.Fatalf("Got status %q, want %q", txn.Status, TransactionStatusGatewayRejected)
}
if txn.GatewayRejectionReason != GatewayRejectionReasonCVV {
t.Fatalf("Got gateway rejection reason %q, wanted %q", txn.GatewayRejectionReason, GatewayRejectionReasonCVV)
}
if txn.CVVResponseCode != CVVResponseCodeDoesNotMatch {
t.Fatalf("Got CVV Response Code %q, wanted %q", txn.CVVResponseCode, CVVResponseCodeDoesNotMatch)
}
}
func TestTransactionCreatedWhenAVSBankDoesNotSupport(t *testing.T) {
t.Parallel()
_, err := testGateway.Transaction().Create(&TransactionRequest{
MerchantAccountId: avsAndCVVTestMerchantAccountId,
Type: "sale",
Amount: randomAmount(),
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
CVV: "100",
},
BillingAddress: &Address{
StreetAddress: "1 E Main St",
Locality: "Chicago",
Region: "IL",
PostalCode: "30001", // Should cause AVS bank does not support error response.
},
})
if err == nil {
t.Fatal("Did not receive error when creating invalid transaction")
}
if err.Error() != "Gateway Rejected: avs" {
t.Fatal(err)
}
txn := err.(*BraintreeError).Transaction
if txn.Status != TransactionStatusGatewayRejected {
t.Fatalf("Got status %q, want %q", txn.Status, TransactionStatusGatewayRejected)
}
if txn.GatewayRejectionReason != GatewayRejectionReasonAVS {
t.Fatalf("Got gateway rejection reason %q, wanted %q", txn.GatewayRejectionReason, GatewayRejectionReasonAVS)
}
if txn.AVSErrorResponseCode != AVSResponseCodeNotSupported {
t.Fatalf("Got AVS Error Response Code %q, wanted %q", txn.AVSErrorResponseCode, AVSResponseCodeNotSupported)
}
}
func TestTransactionCreatedWhenAVSPostalDoesNotMatch(t *testing.T) {
t.Parallel()
_, err := testGateway.Transaction().Create(&TransactionRequest{
MerchantAccountId: avsAndCVVTestMerchantAccountId,
Type: "sale",
Amount: randomAmount(),
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
CVV: "100",
},
BillingAddress: &Address{
StreetAddress: "1 E Main St",
Locality: "Chicago",
Region: "IL",
PostalCode: "20000", // Should cause AVS postal code does not match response.
},
})
if err == nil {
t.Fatal("Did not receive error when creating invalid transaction")
}
if err.Error() != "Gateway Rejected: avs" {
t.Fatal(err)
}
txn := err.(*BraintreeError).Transaction
if txn.Status != TransactionStatusGatewayRejected {
t.Fatalf("Got status %q, want %q", txn.Status, TransactionStatusGatewayRejected)
}
if txn.GatewayRejectionReason != GatewayRejectionReasonAVS {
t.Fatalf("Got gateway rejection reason %q, wanted %q", txn.GatewayRejectionReason, GatewayRejectionReasonAVS)
}
if txn.AVSPostalCodeResponseCode != AVSResponseCodeDoesNotMatch {
t.Fatalf("Got AVS postal response code %q, wanted %q", txn.AVSPostalCodeResponseCode, AVSResponseCodeDoesNotMatch)
}
}
func TestTransactionCreatedWhenAVStreetAddressDoesNotMatch(t *testing.T) {
t.Parallel()
_, err := testGateway.Transaction().Create(&TransactionRequest{
MerchantAccountId: avsAndCVVTestMerchantAccountId,
Type: "sale",
Amount: randomAmount(),
CreditCard: &CreditCard{
Number: testCreditCards["visa"].Number,
ExpirationDate: "05/14",
CVV: "100",
},
BillingAddress: &Address{
StreetAddress: "201 E Main St", // Should cause AVS street address not verified response.
Locality: "Chicago",
Region: "IL",
PostalCode: "60637",
},
})
if err == nil {
t.Fatal("Did not receive error when creating invalid transaction")
}
if err.Error() != "Gateway Rejected: avs" {
t.Fatal(err)
}
txn := err.(*BraintreeError).Transaction
if txn.Status != TransactionStatusGatewayRejected {
t.Fatalf("Got status %q, want %q", txn.Status, TransactionStatusGatewayRejected)
}
if txn.GatewayRejectionReason != GatewayRejectionReasonAVS {
t.Fatalf("Got gateway rejection reason %q, wanted %q", txn.GatewayRejectionReason, GatewayRejectionReasonAVS)
}
if txn.AVSStreetAddressResponseCode != AVSResponseCodeNotVerified {
t.Fatalf("Got AVS street address response code %q, wanted %q", txn.AVSStreetAddressResponseCode, AVSResponseCodeNotVerified)
}
}
func TestFindTransaction(t *testing.T) {
t.Parallel()
createdTransaction, err := testGateway.Transaction().Create(&TransactionRequest{
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) {
t.Parallel()
_, 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)
}
if apiErr, ok := err.(APIError); !(ok && apiErr.StatusCode() == http.StatusNotFound) {
t.Fatal(err)
}
}
// This test will fail unless you set up your Braintree sandbox account correctly. See TESTING.md for details.
func TestTransactionDescriptorFields(t *testing.T) {
t.Parallel()
tx := &TransactionRequest{
Type: "sale",
Amount: randomAmount(),
PaymentMethodNonce: FakeNonceTransactable,
Options: &TransactionOptions{
SubmitForSettlement: true,
},
Descriptor: &Descriptor{
Name: "Company Name*Product 1",
Phone: "0000000000",
URL: "example.com",
},
}
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.Status != TransactionStatusSubmittedForSettlement {
t.Fatalf("expected tx2.Status to be %s, but got %s", TransactionStatusSubmittedForSettlement, tx2.Status)
}
if tx2.Descriptor.Name != "Company Name*Product 1" {
t.Fatalf("expected tx2.Descriptor.Name to be Company Name*Product 1, but got %s", tx2.Descriptor.Name)
}
if tx2.Descriptor.Phone != "0000000000" {
t.Fatalf("expected tx2.Descriptor.Phone to be 0000000000, but got %s", tx2.Descriptor.Phone)
}
if tx2.Descriptor.URL != "example.com" {
t.Fatalf("expected tx2.Descriptor.URL to be example.com, but got %s", tx2.Descriptor.URL)
}
}
// This test will fail unless you set up your Braintree sandbox account correctly. See TESTING.md for details.
func TestTransactionPaypalFields(t *testing.T) {
t.Parallel()
const (
PayeeEmail = "payee@payal.com"
Description = "One tasty sandwich"
CustomField = "foo"
)
subData := make(map[string]string)
subData["faz"] = "bar"
customer, err := testGateway.Customer().Create(&Customer{})
if err != nil {
t.Fatal(err)
}
nonce := FakeNoncePayPalFuturePayment
paymentMethod, err := testGateway.PaymentMethod().Create(&PaymentMethodRequest{
CustomerId: customer.Id,
PaymentMethodNonce: nonce,
})
if err != nil {
t.Fatal(err)
}
paypalAccount, ok := paymentMethod.(*PayPalAccount)
if !ok {
t.Fatal("Could not assert paypal account")
}
tx := &TransactionRequest{
Type: "sale",
Amount: randomAmount(),
PaymentMethodToken: paypalAccount.GetToken(),
OrderId: "123456ABC",
Options: &TransactionOptions{
SubmitForSettlement: true,
TransactionOptionsPaypalRequest: &TransactionOptionsPaypalRequest{
PayeeEmail: PayeeEmail,
Description: Description,
CustomField: CustomField,
SupplementaryData: subData,
},
},
}
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.Status != TransactionStatusSettling {
t.Fatalf("expected tx2.Status to be %s, but got %s", TransactionStatusSettling, tx2.Status)
}
if tx2.PayPalDetails.PayeeEmail != PayeeEmail {
t.Fatalf("expected tx2.PaypalDetails.PayeeEmail to be %s, but got %s", PayeeEmail, tx2.PayPalDetails.PayeeEmail)
}
if tx2.PayPalDetails.Description != Description {
t.Fatalf("expected tx2.PaypalDetails.Description to be %s, but got %s", Description, tx2.PayPalDetails.Description)
}
if tx2.PayPalDetails.CustomField != CustomField {
t.Fatalf("expected tx2.PayPalDetails.CustomField to be %s, but got %s", CustomField, tx2.PayPalDetails.CustomField)
}
}
func TestTransactionRiskDataFields(t *testing.T) {
t.Parallel()
tx := &TransactionRequest{
Type: "sale",
Amount: randomAmount(),
PaymentMethodNonce: FakeNonceTransactable,
RiskData: &RiskDataRequest{
CustomerBrowser: "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/540.0 (KHTML,like Gecko) Chrome/9.1.0.0 Safari/540.0",
CustomerIP: "127.0.0.1",
},
}
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)
}
}
func TestAllTransactionFields(t *testing.T) {
t.Parallel()
amount := randomAmount()
taxAmount := NewDecimal(amount.Unscaled/10, amount.Scale)
tx := &TransactionRequest{
Type: "sale",
Amount: amount,
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",
},
TaxAmount: taxAmount,
DeviceData: `{"device_session_id": "dsi_1234", "fraud_merchant_id": "fmi_1234", "correlation_id": "ci_1234"}`,
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.CurrencyISOCode != "USD" {
t.Fatalf("expected CurrencyISOCode to be %s but was %s", "USD", tx2.CurrencyISOCode)
}
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.TaxAmount == nil {
t.Fatalf("expected TaxAmount to be set, but was nil")
}
if tx2.TaxAmount.Cmp(tx.TaxAmount) != 0 {
t.Fatalf("expected TaxAmount to be equal, but %s was not %s", tx2.TaxAmount, tx.TaxAmount)
}
if tx2.TaxExempt != tx.TaxExempt {
t.Fatalf("expected TaxExempt to be equal, but %t was not %t", tx2.TaxExempt, tx.TaxExempt)
}
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 != TransactionStatusSubmittedForSettlement {
t.Fatalf("expected tx2.Status to be %s, but got %s", TransactionStatusSubmittedForSettlement, tx2.Status)
}
if tx2.PaymentInstrumentType != "credit_card" {
t.Fatalf("expected tx2.PaymentInstrumentType to be %s, but got %s", "credit_card", tx2.PaymentInstrumentType)
}
if tx2.AdditionalProcessorResponse != "" {
t.Fatalf("expected tx2.AdditionalProcessorResponse to be empty, but got %s", tx2.AdditionalProcessorResponse)
}
if tx2.RiskData == nil {
t.Fatal("expected tx2.RiskData not to be empty")
}
if tx2.RiskData.ID == "" {
t.Fatal("expected tx2.RiskData.ID not to be empty")
}
if tx2.RiskData.Decision != "Approve" {
t.Fatalf("expected tx2.RiskData.Decision to be Approve, but got %s", tx2.RiskData.Decision)
}
}
// This test will only pass on Travis. See TESTING.md for more details.
func TestTransactionDisbursementDetails(t *testing.T) {
t.Parallel()
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 {
t.Error("funds held doesn't match")
}
if !txn.DisbursementDetails.Success {
t.Error("success doesn't match")
}
}
func TestTransactionCreateFromPaymentMethodCode(t *testing.T) {
t.Parallel()
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(&TransactionRequest{
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 TestTrxPaymentMethodNonce(t *testing.T) {
t.Parallel()
txn, err := testGateway.Transaction().Create(&TransactionRequest{
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) {
t.Parallel()
amount := NewDecimal(20000, 2)
txn, err := testGateway.Transaction().Create(&TransactionRequest{
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.Testing().Settle(txn.Id)
if err != nil {
t.Fatal(err)
}
if txn.Status != TransactionStatusSettled {
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 != TransactionStatusSubmittedForSettlement {
t.Fatal(x)
}
refundTxn, err = testGateway.Testing().Settle(refundTxn.Id)
if err != nil {
t.Fatal(err)
}
if refundTxn.Status != TransactionStatusSettled {
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) {
t.Parallel()
amount := NewDecimal(10000, 2)
refundAmt1 := NewDecimal(5000, 2)
refundAmt2 := NewDecimal(5001, 2)
txn, err := testGateway.Transaction().Create(&TransactionRequest{
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.Testing().Settle(txn.Id)
if err != nil {
t.Fatal(err)
}
if txn.Status != TransactionStatusSettled {
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 != TransactionStatusSubmittedForSettlement {
t.Fatal(x)
}
refundTxn, err = testGateway.Testing().Settle(refundTxn.Id)
if err != nil {
t.Fatal(err)
}
if refundTxn.Status != TransactionStatusSettled {
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)
}
}
func TestTransactionCreateWithCustomFields(t *testing.T) {
t.Parallel()
customFields := map[string]string{
"custom_field_1": "custom value",
}
amount := NewDecimal(10000, 2)
txn, err := testGateway.Transaction().Create(&TransactionRequest{
Type: "sale",
Amount: amount,
PaymentMethodNonce: FakeNonceTransactable,
CustomFields: customFields,
})
if err != nil {
t.Fatal(err)
}
if x := map[string]string(txn.CustomFields); !reflect.DeepEqual(x, customFields) {
t.Fatalf("Returned custom fields doesn't match input, got %q, want %q", x, customFields)
}
txn, err = testGateway.Transaction().Find(txn.Id)
if err != nil {
t.Fatal(err)
}
if x := map[string]string(txn.CustomFields); !reflect.DeepEqual(x, customFields) {
t.Fatalf("Returned custom fields doesn't match input, got %q, want %q", x, customFields)
}
}
func TestTransactionTaxExempt(t *testing.T) {
t.Parallel()
amount := NewDecimal(10000, 2)
txn, err := testGateway.Transaction().Create(&TransactionRequest{
Type: "sale",
Amount: amount,
TaxExempt: true,
PaymentMethodNonce: FakeNonceTransactable,
})
if err != nil {
t.Fatal(err)
}
txn, err = testGateway.Transaction().Find(txn.Id)
if err != nil {
t.Fatal(err)
}
if !txn.TaxExempt {
t.Fatalf("Transaction did not return tax exempt")
}
if txn.TaxAmount != nil {
t.Fatalf("Transaction TaxAmount got %v, want nil", txn.TaxAmount)
}
}
func TestTransactionTaxFieldsNotProvided(t *testing.T) {
t.Parallel()
amount := NewDecimal(10000, 2)
txn, err := testGateway.Transaction().Create(&TransactionRequest{
Type: "sale",
Amount: amount,
PaymentMethodNonce: FakeNonceTransactable,
})
if err != nil {
t.Fatal(err)
}
txn, err = testGateway.Transaction().Find(txn.Id)
if err != nil {
t.Fatal(err)
}
if txn.TaxExempt {