forked from manuelstofer/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
payment_method_integration_test.go
161 lines (131 loc) · 3.94 KB
/
payment_method_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
// +build integration
package braintree
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
"github.com/braintree-go/braintree-go/testhelpers"
)
func TestPaymentMethod(t *testing.T) {
t.Parallel()
ctx := context.Background()
cust, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
g := testGateway.PaymentMethod()
// Create using credit card
paymentMethod, err := g.Create(ctx, &PaymentMethodRequest{
CustomerId: cust.Id,
PaymentMethodNonce: FakeNonceTransactableVisa,
})
if err != nil {
t.Fatal(err)
}
if paymentMethod.GetCustomerId() != cust.Id {
t.Errorf("Got paymentMethod customer Id %#v, want %#v", paymentMethod.GetCustomerId(), cust.Id)
}
if paymentMethod.GetToken() == "" {
t.Errorf("Got paymentMethod token %#v, want a value", paymentMethod.GetToken())
}
// Update using different credit card
rand.Seed(time.Now().UTC().UnixNano())
token := fmt.Sprintf("btgo_test_token_%d", rand.Int()+1)
paymentMethod, err = g.Update(ctx, paymentMethod.GetToken(), &PaymentMethodRequest{
PaymentMethodNonce: FakeNonceTransactableMasterCard,
Token: token,
})
if err != nil {
t.Fatal(err)
}
if paymentMethod.GetToken() != token {
t.Errorf("Got paymentMethod token %#v, want %#v", paymentMethod.GetToken(), token)
}
// Updating with different payment method type should fail
if _, err = g.Update(ctx, token, &PaymentMethodRequest{PaymentMethodNonce: FakeNoncePayPalBillingAgreement}); err == nil {
t.Errorf("Updating with a different payment method type should have failed")
}
// Find credit card
paymentMethod, err = g.Find(ctx, token)
if err != nil {
t.Fatal(err)
}
if paymentMethod.GetCustomerId() != cust.Id {
t.Errorf("Got paymentMethod customer Id %#v, want %#v", paymentMethod.GetCustomerId(), cust.Id)
}
if paymentMethod.GetToken() != token {
t.Errorf("Got paymentMethod token %#v, want %#v", paymentMethod.GetToken(), token)
}
// Delete credit card
if err := g.Delete(ctx, token); err != nil {
t.Fatal(err)
}
// Create using PayPal
paymentMethod, err = g.Create(ctx, &PaymentMethodRequest{
CustomerId: cust.Id,
PaymentMethodNonce: FakeNoncePayPalBillingAgreement,
})
if err != nil {
t.Fatal(err)
}
// Find PayPal
_, err = g.Find(ctx, paymentMethod.GetToken())
if err != nil {
t.Fatal(err)
}
// Updating a PayPal account with a different payment method nonce of any kind should fail
if _, err = g.Update(ctx, paymentMethod.GetToken(), &PaymentMethodRequest{PaymentMethodNonce: FakeNoncePayPalOneTimePayment}); err == nil {
t.Errorf("Updating a PayPal account with a different nonce should have failed")
}
// Delete PayPal
if err := g.Delete(ctx, paymentMethod.GetToken()); err != nil {
t.Fatal(err)
}
// Cleanup
if err := testGateway.Customer().Delete(ctx, cust.Id); err != nil {
t.Fatal(err)
}
}
func TestPaymentMethodFailedAutoVerification(t *testing.T) {
t.Parallel()
ctx := context.Background()
cust, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
g := testGateway.PaymentMethod()
pm, err := g.Create(ctx, &PaymentMethodRequest{
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", pm)
}
func TestPaymentMethodForceNotVerified(t *testing.T) {
t.Parallel()
ctx := context.Background()
cust, err := testGateway.Customer().Create(ctx, &CustomerRequest{})
if err != nil {
t.Fatal(err)
}
g := testGateway.PaymentMethod()
pm, err := g.Create(ctx, &PaymentMethodRequest{
CustomerId: cust.Id,
PaymentMethodNonce: FakeNonceProcessorDeclinedVisa,
Options: &PaymentMethodRequestOptions{
VerifyCard: testhelpers.BoolPtr(false),
},
})
if err != nil {
t.Fatal(err)
}
t.Logf("%#v\n", pm)
}