forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment_method_integration_test.go
218 lines (187 loc) · 6.37 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
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
// +build integration
package braintree
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
"github.com/dietdoctor/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
addr := &AddressRequest{
FirstName: "Robert",
LastName: "Smith",
Company: "The Cure",
StreetAddress: "39 Acacia Avenue",
ExtendedAddress: "SAV Studios",
Locality: "North End",
Region: "London",
PostalCode: "SW1A 0AA",
CountryCodeAlpha2: "GB",
CountryCodeAlpha3: "GBR",
CountryCodeNumeric: "826",
CountryName: "United Kingdom",
}
paymentMethod, err := g.Create(ctx, &PaymentMethodRequest{
CustomerId: cust.Id,
PaymentMethodNonce: FakeNonceTransactableVisa,
BillingAddress: addr,
})
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())
}
if card, ok := paymentMethod.(*CreditCard); ok {
ba := card.BillingAddress
if ba.FirstName != addr.FirstName {
t.Errorf("Got paymentMethod billing adress first name %#v, want %#v", ba.FirstName, addr.FirstName)
}
if ba.LastName != addr.LastName {
t.Errorf("Got paymentMethod billing adress last name %#v, want %#v", ba.LastName, addr.LastName)
}
if ba.Company != addr.Company {
t.Errorf("Got paymentMethod billing adress company %#v, want %#v", ba.Company, addr.Company)
}
if ba.StreetAddress != addr.StreetAddress {
t.Errorf("Got paymentMethod billing adress street address %#v, want %#v", ba.StreetAddress, addr.StreetAddress)
}
if ba.ExtendedAddress != addr.ExtendedAddress {
t.Errorf("Got paymentMethod billing adress extended address %#v, want %#v", ba.ExtendedAddress, addr.ExtendedAddress)
}
if ba.Locality != addr.Locality {
t.Errorf("Got paymentMethod billing adress locality %#v, want %#v", ba.Locality, addr.Locality)
}
if ba.Region != addr.Region {
t.Errorf("Got paymentMethod billing adress region %#v, want %#v", ba.Region, addr.Region)
}
if ba.PostalCode != addr.PostalCode {
t.Errorf("Got paymentMethod billing adress postal code %#v, want %#v", ba.PostalCode, addr.PostalCode)
}
if ba.CountryCodeAlpha2 != addr.CountryCodeAlpha2 {
t.Errorf("Got paymentMethod billing adress country alpha2 %#v, want %#v", ba.CountryCodeAlpha2, addr.CountryCodeAlpha2)
}
if ba.CountryCodeAlpha3 != addr.CountryCodeAlpha3 {
t.Errorf("Got paymentMethod billing adress country alpha3 %#v, want %#v", ba.CountryCodeAlpha3, addr.CountryCodeAlpha3)
}
if ba.CountryCodeNumeric != addr.CountryCodeNumeric {
t.Errorf("Got paymentMethod billing adress country numeric %#v, want %#v", ba.CountryCodeNumeric, addr.CountryCodeNumeric)
}
if ba.CountryName != addr.CountryName {
t.Errorf("Got paymentMethod billing adress country name %#v, want %#v", ba.CountryName, addr.CountryName)
}
} else {
t.Error("paymentMethod should have been a credit card")
}
// 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)
}