forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilling_test.go
522 lines (466 loc) · 17 KB
/
billing_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
package recurly_test
import (
"bytes"
"encoding/xml"
"fmt"
"net"
"net/http"
"testing"
"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
)
// TestBillingEncoding ensures structs are encoded to XML properly.
// Because Recurly supports partial updates, it's important that only defined
// fields are handled properly -- including types like booleans and integers which
// have zero values that we want to send.
func TestBilling_Encoding(t *testing.T) {
tests := []struct {
v recurly.Billing
expected string
}{
{v: recurly.Billing{}, expected: "<billing_info></billing_info>"},
{v: recurly.Billing{Token: "507c7f79bcf86cd7994f6c0e"}, expected: "<billing_info><token_id>507c7f79bcf86cd7994f6c0e</token_id></billing_info>"},
{v: recurly.Billing{FirstName: "Verena", LastName: "Example"}, expected: "<billing_info><first_name>Verena</first_name><last_name>Example</last_name></billing_info>"},
{v: recurly.Billing{Address: "123 Main St."}, expected: "<billing_info><address1>123 Main St.</address1></billing_info>"},
{v: recurly.Billing{Address2: "Unit A"}, expected: "<billing_info><address2>Unit A</address2></billing_info>"},
{v: recurly.Billing{City: "San Francisco"}, expected: "<billing_info><city>San Francisco</city></billing_info>"},
{v: recurly.Billing{State: "CA"}, expected: "<billing_info><state>CA</state></billing_info>"},
{v: recurly.Billing{Zip: "94105"}, expected: "<billing_info><zip>94105</zip></billing_info>"},
{v: recurly.Billing{Country: "US"}, expected: "<billing_info><country>US</country></billing_info>"},
{v: recurly.Billing{Phone: "555-555-5555"}, expected: "<billing_info><phone>555-555-5555</phone></billing_info>"},
{v: recurly.Billing{VATNumber: "abc"}, expected: "<billing_info><vat_number>abc</vat_number></billing_info>"},
{v: recurly.Billing{IPAddress: net.ParseIP("127.0.0.1")}, expected: "<billing_info><ip_address>127.0.0.1</ip_address></billing_info>"},
{v: recurly.Billing{Number: 4111111111111111, Month: 5, Year: 2020, VerificationValue: 111}, expected: "<billing_info><number>4111111111111111</number><month>5</month><year>2020</year><verification_value>111</verification_value></billing_info>"},
{v: recurly.Billing{RoutingNumber: "065400137", AccountNumber: "0123456789", AccountType: "checking"}, expected: "<billing_info><routing_number>065400137</routing_number><account_number>0123456789</account_number><account_type>checking</account_type></billing_info>"},
}
for _, tt := range tests {
var buf bytes.Buffer
if err := xml.NewEncoder(&buf).Encode(tt.v); err != nil {
t.Fatalf("unexpected error: %v", err)
} else if buf.String() != tt.expected {
t.Fatalf("unexpected value: %s", buf.String())
}
}
}
func TestBilling_Type(t *testing.T) {
b0 := recurly.Billing{
FirstSix: "011111",
LastFour: "1111",
Month: 11,
Year: 2020,
}
b1 := recurly.Billing{
NameOnAccount: "Acme, Inc",
RoutingNumber: "123456780",
AccountNumber: "111111111",
}
var b2 recurly.Billing
if b0.Type() != "card" {
t.Fatalf("unexpected type: %s", b0.Type())
} else if b1.Type() != "bank" {
t.Fatalf("unexpected type: %s", b1.Type())
} else if b2.Type() != "" {
t.Fatalf("unexpected type: %s", b2.Type())
}
}
func TestBilling_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/accounts/1/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Fatalf("unexpected method: %s", r.Method)
}
w.WriteHeader(200)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?>
<billing_info href="http://api.test.host/v2/accounts/1/billing_info" type="credit_card">
<account href="http://api.test.host/v2/accounts/1"/>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<company nil="nil"></company>
<address1>123 Main St.</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>94105</zip>
<country>US</country>
<phone nil="nil"></phone>
<vat_number>US1234567890</vat_number>
<ip_address>127.0.0.1</ip_address>
<ip_address_country>US</ip_address_country>
<card_type>Visa</card_type>
<year type="integer">2015</year>
<month type="integer">11</month>
<first_six>411111</first_six>
<last_four>1111</last_four>
</billing_info>`)
})
resp, b, err := client.Billing.Get("1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected get billing info to return OK")
} else if diff := cmp.Diff(b, &recurly.Billing{
XMLName: xml.Name{Local: "billing_info"},
FirstName: "Verena",
LastName: "Example",
Address: "123 Main St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
VATNumber: "US1234567890",
IPAddress: net.ParseIP("127.0.0.1"),
IPAddressCountry: "US",
CardType: "Visa",
Year: 2015,
Month: 11,
FirstSix: "411111",
LastFour: "1111",
}); diff != "" {
t.Fatal(diff)
}
}
// ACH customers may not have billing info. This asserts that nil values for
// many of the fields are safely ignored without parse errors.
func TestBilling_Get_ACH(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/accounts/1/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Fatalf("unexpected method: %s", r.Method)
}
w.WriteHeader(200)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?>
<billing_info type="ach">
<first_name>Verena</first_name>
<last_name>Example</last_name>
<address1 nil="nil"></address1>
<address2 nil="nil"></address2>
<city nil="nil"></city>
<state nil="nil"></state>
<zip nil="nil"></zip>
<country nil="nil"></country>
<phone nil="nil"></phone>
<vat_number nil="nil"></vat_number>
<account_type nil="nil"></account_type>
<last_four nil="nil"></last_four>
<routing_number nil="nil"></routing_number>
</billing_info>`)
})
resp, b, err := client.Billing.Get("1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected get billing info to return OK")
} else if diff := cmp.Diff(b, &recurly.Billing{
XMLName: xml.Name{Local: "billing_info"},
FirstName: "Verena",
LastName: "Example",
}); diff != "" {
t.Fatal(diff)
}
}
func TestBilling_Get_ErrNotFound(t *testing.T) {
setup()
defer teardown()
var invoked bool
mux.HandleFunc("/v2/accounts/1/billing_info", func(w http.ResponseWriter, r *http.Request) {
invoked = true
w.WriteHeader(http.StatusNotFound)
})
_, billing, err := client.Billing.Get("1")
if !invoked {
t.Fatal("handler not invoked")
} else if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if billing != nil {
t.Fatalf("expected billing to be nil: %#v", billing)
}
}
func TestBilling_Create_WithToken(t *testing.T) {
setup()
defer teardown()
token := "tok-woueh7LtsBKs8sE20"
mux.HandleFunc("/v2/accounts/1/billing_info", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if r.Method != "POST" {
t.Fatalf("unexpected method: %s", r.Method)
}
var given bytes.Buffer
given.ReadFrom(r.Body)
expected := fmt.Sprintf("<billing_info><token_id>%s</token_id></billing_info>", token)
if expected != given.String() {
t.Fatalf("unexpected input: %v", given.String())
}
w.WriteHeader(200)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?>
<billing_info href="http://api.test.host/v2/accounts/1/billing_info" type="credit_card">
<account href="http://api.test.host/v2/accounts/1"/>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<company nil="nil"></company>
<address1>123 Main St.</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>94105</zip>
<country>US</country>
<phone nil="nil"></phone>
<vat_number>US1234567890</vat_number>
<ip_address>127.0.0.1</ip_address>
<ip_address_country>US</ip_address_country>
<card_type>Visa</card_type>
<year type="integer">2015</year>
<month type="integer">11</month>
<first_six>411111</first_six>
<last_four>1111</last_four>
</billing_info>`)
})
resp, b, err := client.Billing.CreateWithToken("1", token)
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected creating billing info to return OK")
} else if diff := cmp.Diff(b, &recurly.Billing{
XMLName: xml.Name{Local: "billing_info"},
FirstName: "Verena",
LastName: "Example",
Address: "123 Main St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
VATNumber: "US1234567890",
IPAddress: net.ParseIP("127.0.0.1"),
IPAddressCountry: "US",
CardType: "Visa",
Year: 2015,
Month: 11,
FirstSix: "411111",
LastFour: "1111",
}); diff != "" {
t.Fatal(diff)
}
}
func TestBilling_Create_WithCC(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/accounts/1/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Fatalf("unexpected status: %s", r.Method)
}
var given bytes.Buffer
given.ReadFrom(r.Body)
expected := "<billing_info><first_name>Verena</first_name><last_name>Example</last_name><address1>123 Main St.</address1><city>San Francisco</city><state>CA</state><zip>94105</zip><country>US</country><number>4111111111111111</number><month>10</month><year>2020</year></billing_info>"
if expected != given.String() {
t.Fatalf("unexpected input: %v", given.String())
}
w.WriteHeader(200)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><billing_info></billing_info>`)
})
resp, _, err := client.Billing.Create("1", recurly.Billing{
FirstName: "Verena",
LastName: "Example",
Address: "123 Main St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
Number: 4111111111111111,
Month: 10,
Year: 2020,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected creating billing info to return OK")
}
}
func TestBilling_Create_WithBankAccount(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/accounts/134/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Fatalf("unexpected method: %s", r.Method)
}
var given bytes.Buffer
given.ReadFrom(r.Body)
expected := "<billing_info><first_name>Verena</first_name><last_name>Example</last_name><address1>123 Main St.</address1><city>San Francisco</city><state>CA</state><zip>94105</zip><country>US</country><name_on_account>Acme, Inc</name_on_account><routing_number>123456780</routing_number><account_number>111111111</account_number><account_type>checking</account_type></billing_info>"
if expected != given.String() {
t.Fatalf("unexpected input: %v", given.String())
}
w.WriteHeader(200)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><billing_info></billing_info>`)
})
resp, _, err := client.Billing.Create("134", recurly.Billing{
FirstName: "Verena",
LastName: "Example",
Address: "123 Main St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
NameOnAccount: "Acme, Inc",
RoutingNumber: "123456780",
AccountNumber: "111111111",
AccountType: "checking",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected creating billing info to return OK")
}
}
func TestBilling_Update_WithToken(t *testing.T) {
setup()
defer teardown()
token := "tok-UgLus845alBogoKRsiGw92vzos"
mux.HandleFunc("/v2/accounts/abceasf/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
t.Fatalf("unexpected method: %s", r.Method)
}
var given bytes.Buffer
given.ReadFrom(r.Body)
expected := fmt.Sprintf("<billing_info><token_id>%s</token_id></billing_info>", token)
if expected != given.String() {
t.Fatalf("unexpected input: %v", given.String())
}
w.WriteHeader(200)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><billing_info></billing_info>`)
})
resp, _, err := client.Billing.UpdateWithToken("abceasf", token)
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected updating billing info to return OK")
}
}
func TestBilling_Update_InvalidToken(t *testing.T) {
setup()
defer teardown()
token := "tok-UgLus845alBogoKRsiGw92vzos"
mux.HandleFunc("/v2/accounts/abceasf/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
t.Fatalf("unexpected method: %s", r.Method)
}
w.WriteHeader(404)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><error><symbol>token_invalid</symbol><description>Token is either invalid or expired</description></error>`)
})
resp, billing, err := client.Billing.UpdateWithToken("abceasf", token)
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if billing != nil {
t.Fatalf("unexpected billing to be nil: %#v", billing)
}
if resp.IsOK() {
t.Fatal("expected updating billing info with invalid token to return error")
} else if diff := cmp.Diff(resp.Errors, []recurly.Error{
{
Symbol: "token_invalid",
Message: "Token is either invalid or expired",
},
}); diff != "" {
t.Fatal(diff)
}
}
func TestBilling_Update_WithCC(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/accounts/1/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
t.Fatalf("unexpected method: %s", r.Method)
}
var given bytes.Buffer
given.ReadFrom(r.Body)
expected := "<billing_info><first_name>Verena</first_name><last_name>Example</last_name><address1>123 Main St.</address1><city>San Francisco</city><state>CA</state><zip>94105</zip><country>US</country><number>4111111111111111</number><month>10</month><year>2020</year></billing_info>"
if expected != given.String() {
t.Fatalf("unexpected input: %v", given.String())
}
w.WriteHeader(200)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><billing_info></billing_info>`)
})
resp, _, err := client.Billing.Update("1", recurly.Billing{
FirstName: "Verena",
LastName: "Example",
Address: "123 Main St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
Number: 4111111111111111,
Month: 10,
Year: 2020,
// Add additional fields that should be removed
Token: "abc",
IPAddressCountry: "US",
FirstSix: "411111",
LastFour: "1111",
CardType: "visa",
PaypalAgreementID: "ppl",
AmazonAgreementID: "asdfb",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected creating billing info to return OK")
}
}
func TestBilling_Update_WithBankAccount(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/accounts/134/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
t.Fatalf("unexpected method: %s", r.Method)
}
var given bytes.Buffer
given.ReadFrom(r.Body)
expected := "<billing_info><first_name>Verena</first_name><last_name>Example</last_name><address1>123 Main St.</address1><city>San Francisco</city><state>CA</state><zip>94105</zip><country>US</country><name_on_account>Acme, Inc</name_on_account><routing_number>123456780</routing_number><account_number>111111111</account_number><account_type>checking</account_type></billing_info>"
if expected != given.String() {
t.Fatalf("unexpected input: %v", given.String())
}
w.WriteHeader(200)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><billing_info></billing_info>`)
})
resp, _, err := client.Billing.Update("134", recurly.Billing{
FirstName: "Verena",
LastName: "Example",
Address: "123 Main St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
NameOnAccount: "Acme, Inc",
RoutingNumber: "123456780",
AccountNumber: "111111111",
AccountType: "checking",
// Add additional fields that should be removed
Token: "abc",
IPAddressCountry: "US",
FirstSix: "111111",
LastFour: "1111",
CardType: "visa",
PaypalAgreementID: "ppl",
AmazonAgreementID: "asdfb",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected creating billing info to return OK")
}
}
func TestBilling_Clear(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/accounts/[email protected]/billing_info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "DELETE" {
t.Fatalf("unexpected method: %s", r.Method)
}
w.WriteHeader(204)
})
resp, err := client.Billing.Clear("[email protected]")
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if resp.IsError() {
t.Fatal("expected deleting billing_info to return OK")
}
}