forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purchases_test.go
431 lines (419 loc) · 18 KB
/
purchases_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
package recurly_test
import (
"bytes"
"encoding/xml"
"fmt"
"net/http"
"strconv"
"testing"
"github.com/blacklightcms/recurly"
)
func TestPurchases_Purchase_Encoding(t *testing.T) {
tests := []struct {
v recurly.Purchase
expected string
}{
{
expected: "<purchase><account></account><adjustments></adjustments><currency></currency><gift_card></gift_card><coupon_codes></coupon_codes><subscriptions></subscriptions></purchase>",
},
{
// From Recurly examples: https://dev.recurly.com/docs/create-purchase
v: recurly.Purchase{
CollectionMethod: "automatic",
Currency: "USD",
CustomerNotes: "Some notes for the customer.",
TermsAndConditions: "Our company terms and conditions.",
VATReverseChargeNotes: "Vat reverse charge notes.",
GatewayCode: "test-gateway-code",
Account: recurly.Account{
Code: "c442b36c-c64f-41d7-b8e1-9c04e7a6ff82",
ShippingAddresses: &[]recurly.ShippingAddress{
recurly.ShippingAddress{
FirstName: "Lon",
LastName: "Doner",
Address: "221B Baker St.",
City: "London",
Zip: "W1K 6AH",
Country: "GB",
Nickname: "Home",
},
},
BillingInfo: &recurly.Billing{
Address: "400 Alabama St",
City: "San Francisco",
Country: "US",
FirstName: "Benjamin",
LastName: "Du Monde",
Month: 12,
Number: 4111111111111111,
State: "CA",
Year: 2019,
Zip: "94110",
},
},
Adjustments: []recurly.Adjustment{
recurly.Adjustment{
ProductCode: "4549449c-5870-4845-b672-1d07f15e87dd",
Quantity: 1,
// RevenueScheduleType: "at_invoice",
UnitAmountInCents: 1000,
Description: "Description of this adjustment",
},
},
Subscriptions: []recurly.NewSubscription{
recurly.NewSubscription{PlanCode: "plan1"},
},
CouponCodes: []string{"coupon1", "coupon2"},
GiftCard: "ABC1234",
},
expected: "<purchase>" +
"<account><account_code>c442b36c-c64f-41d7-b8e1-9c04e7a6ff82</account_code>" +
"<billing_info><first_name>Benjamin</first_name><last_name>Du Monde</last_name><address1>400 Alabama St</address1><city>San Francisco</city><state>CA</state><zip>94110</zip><country>US</country><number>4111111111111111</number><month>12</month><year>2019</year></billing_info>" +
"<shipping_addresses><shipping_address><first_name>Lon</first_name><last_name>Doner</last_name><nickname>Home</nickname><address1>221B Baker St.</address1><address2></address2><city>London</city><state></state><zip>W1K 6AH</zip><country>GB</country></shipping_address></shipping_addresses>" +
"</account>" +
"<adjustments><adjustment>" +
"<description>Description of this adjustment</description><product_code>4549449c-5870-4845-b672-1d07f15e87dd</product_code><unit_amount_in_cents>1000</unit_amount_in_cents><quantity>1</quantity>" +
// TODO: RevenueScheduleType not yet modeled
// "<revenue_schedule_type>at_invoice</revenue_schedule_type>"+
"</adjustment></adjustments>" +
"<collection_method>automatic</collection_method>" +
"<currency>USD</currency>" +
"<gift_card><redemption_code>ABC1234</redemption_code></gift_card>" +
"<coupon_codes><coupon_code>coupon1</coupon_code><coupon_code>coupon2</coupon_code></coupon_codes>" +
"<subscriptions><subscription><plan_code>plan1</plan_code><account></account><currency></currency></subscription></subscriptions>" +
"<customer_notes>Some notes for the customer.</customer_notes>" +
"<terms_and_conditions>Our company terms and conditions.</terms_and_conditions>" +
"<vat_reverse_charge_notes>Vat reverse charge notes.</vat_reverse_charge_notes>" +
"<gateway_code>test-gateway-code</gateway_code>" +
"</purchase>",
},
{
v: recurly.Purchase{ShippingAddressID: 2438622711411416831},
expected: "<purchase><account></account><adjustments></adjustments><currency></currency><gift_card></gift_card><coupon_codes></coupon_codes><subscriptions></subscriptions><shipping_address_id>2438622711411416831</shipping_address_id></purchase>",
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var given bytes.Buffer
if err := xml.NewEncoder(&given).Encode(tt.v); err != nil {
t.Fatalf("(%d) unexpected encode error: %v", i, err)
} else if tt.expected != given.String() {
t.Fatalf("(%d) unexpected value: \n%s\n%s", i, given.String(), tt.expected)
}
})
}
}
func TestPurchases_Create(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/purchases", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Fatalf("unexpected method: %s", r.Method)
}
w.WriteHeader(201)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?>
<invoice_collection>
<charge_invoice href="https://your-subdomain.recurly.com/v2/invoices/2531">
<account href="https://your-subdomain.recurly.com/v2/accounts/1"/>
<address>
<address1>123 Main St.</address1>
<address2></address2>
<city>SF</city>
<state>CA</state>
<zip>94105</zip>
<country>US</country>
<phone></phone>
</address>
<uuid>458465c8bd2035928b441940d1a17179</uuid>
<state>paid</state>
<invoice_number_prefix></invoice_number_prefix>
<invoice_number type="integer">2531</invoice_number>
<vat_number></vat_number>
<tax_in_cents type="integer">111</tax_in_cents>
<total_in_cents type="integer">1411</total_in_cents>
<currency>USD</currency>
<created_at type="datetime">2018-06-19T01:13:26Z</created_at>
<updated_at type="datetime">2018-06-19T01:13:26Z</updated_at>
<attempt_next_collection_at nil="nil"></attempt_next_collection_at>
<closed_at type="datetime">2018-06-19T01:13:26Z</closed_at>
<customer_notes nil="nil"></customer_notes>
<recovery_reason nil="nil"></recovery_reason>
<subtotal_before_discount_in_cents type="integer">1300</subtotal_before_discount_in_cents>
<subtotal_in_cents type="integer">1300</subtotal_in_cents>
<discount_in_cents type="integer">0</discount_in_cents>
<due_on type="datetime">2018-06-19T01:13:26Z</due_on>
<net_terms type="integer">0</net_terms>
<collection_method>automatic</collection_method>
<po_number nil="nil"></po_number>
<terms_and_conditions nil="nil"></terms_and_conditions>
<tax_type>usst</tax_type>
<tax_region>CA</tax_region>
<tax_rate type="float">0.085</tax_rate>
<line_items type="array">
<adjustment href="https://your-subdomain.recurly.com/v2/adjustments/458465c8a977efb18125714c5abc4119" type="charge">
<account href="https://your-subdomain.recurly.com/v2/accounts/1"/>
<invoice href="https://your-subdomain.recurly.com/v2/invoices/2531"/>
<uuid>458465c8a977efb18125714c5abc4119</uuid>
<state>invoiced</state>
<description>Description for Adjustment</description>
<accounting_code nil="nil"></accounting_code>
<product_code>freeform_98765</product_code>
<origin>debit</origin>
<unit_amount_in_cents type="integer">100</unit_amount_in_cents>
<quantity type="integer">13</quantity>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">111</tax_in_cents>
<total_in_cents type="integer">1411</total_in_cents>
<currency>USD</currency>
<proration_rate nil="nil"></proration_rate>
<taxable type="boolean">false</taxable>
<tax_type>usst</tax_type>
<tax_region>CA</tax_region>
<tax_rate type="float">0.085</tax_rate>
<tax_exempt type="boolean">false</tax_exempt>
<start_date type="datetime">2018-06-19T01:13:26Z</start_date>
<end_date nil="nil"></end_date>
<created_at type="datetime">2018-06-19T01:13:26Z</created_at>
<updated_at type="datetime">2018-06-19T01:13:26Z</updated_at>
<revenue_schedule_type></revenue_schedule_type>
</adjustment>
</line_items>
<transactions type="array">
<transaction href="https://your-subdomain.recurly.com/v2/transactions/458465c9016060dbde60734789869a2d" type="credit_card">
<account href="https://your-subdomain.recurly.com/v2/accounts/1"/>
<invoice href="https://your-subdomain.recurly.com/v2/invoices/2531"/>
<uuid>458465c9016060dbde60734789869a2d</uuid>
<action>purchase</action>
<amount_in_cents type="integer">1411</amount_in_cents>
<tax_in_cents type="integer">111</tax_in_cents>
<currency>USD</currency>
<status>success</status>
<payment_method>credit_card</payment_method>
<reference>8001561</reference>
<source>transaction</source>
<recurring type="boolean">false</recurring>
<test type="boolean">true</test>
<voidable type="boolean">true</voidable>
<refundable type="boolean">true</refundable>
<ip_address nil="nil"></ip_address>
<gateway_type>test</gateway_type>
<origin>api</origin>
<description>Transaction Description</description>
<message>Successful test transaction</message>
<approval_code nil="nil"></approval_code>
<failure_type nil="nil"></failure_type>
<gateway_error_codes nil="nil"></gateway_error_codes>
<cvv_result code="" nil="nil"></cvv_result>
<avs_result code="D">Street address and postal code match.</avs_result>
<avs_result_street nil="nil"></avs_result_street>
<avs_result_postal nil="nil"></avs_result_postal>
<created_at type="datetime">2018-06-19T01:13:26Z</created_at>
<collected_at type="datetime">2018-06-19T01:13:26Z</collected_at>
<updated_at type="datetime">2018-06-19T01:13:26Z</updated_at>
<details>
<account>
<account_code>1</account_code>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<company nil="nil"></company>
<email>verena@example.com</email>
<billing_info type="credit_card">
<first_name>Verena</first_name>
<last_name>Example</last_name>
<address1>123 Main St.</address1>
<address2></address2>
<city>SF</city>
<state>CA</state>
<zip>94105</zip>
<country>US</country>
<phone></phone>
<vat_number></vat_number>
<card_type>Visa</card_type>
<year type="integer">2020</year>
<month type="integer">6</month>
<first_six>411111</first_six>
<last_four>1111</last_four>
</billing_info>
</account>
</details>
</transaction>
</transactions>
<a name="refund" href="https://your-subdomain.recurly.com/v2/invoices/2531/refund" method="post"/>
</charge_invoice>
<credit_invoices type="array">
</credit_invoices>
</invoice_collection>`)
})
expectedInvoiceNumber := 2531
expectedTax := 111
expectedTotal := 1411
r, invs, err := client.Purchases.Create(recurly.Purchase{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if r.IsError() {
t.Fatal("expected create purchase to return OK")
} else if invs.ChargeInvoice.InvoiceNumber != expectedInvoiceNumber {
t.Fatalf("expected invoice number %d; got %d\n", expectedInvoiceNumber, invs.ChargeInvoice.InvoiceNumber)
} else if invs.ChargeInvoice.TaxInCents != expectedTax {
t.Fatalf("expected tax %d; got %d\n", expectedTax, invs.ChargeInvoice.TaxInCents)
} else if invs.ChargeInvoice.TotalInCents != expectedTotal {
t.Fatalf("expected total %d; got %d\n", expectedTotal, invs.ChargeInvoice.TotalInCents)
}
}
func TestPurchases_Preview(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/purchases/preview", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Fatalf("unexpected method: %s", r.Method)
}
w.WriteHeader(201)
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?>
<invoice_collection>
<charge_invoice href="https://your-subdomain.recurly.com/v2/invoices/2531">
<account href="https://your-subdomain.recurly.com/v2/accounts/1"/>
<address>
<address1>123 Main St.</address1>
<address2></address2>
<city>SF</city>
<state>CA</state>
<zip>94105</zip>
<country>US</country>
<phone></phone>
</address>
<uuid>458465c8bd2035928b441940d1a17179</uuid>
<state>paid</state>
<invoice_number_prefix></invoice_number_prefix>
<invoice_number type="integer">2531</invoice_number>
<vat_number></vat_number>
<tax_in_cents type="integer">111</tax_in_cents>
<total_in_cents type="integer">1411</total_in_cents>
<currency>USD</currency>
<created_at type="datetime">2018-06-19T01:13:26Z</created_at>
<updated_at type="datetime">2018-06-19T01:13:26Z</updated_at>
<attempt_next_collection_at nil="nil"></attempt_next_collection_at>
<closed_at type="datetime">2018-06-19T01:13:26Z</closed_at>
<customer_notes nil="nil"></customer_notes>
<recovery_reason nil="nil"></recovery_reason>
<subtotal_before_discount_in_cents type="integer">1300</subtotal_before_discount_in_cents>
<subtotal_in_cents type="integer">1300</subtotal_in_cents>
<discount_in_cents type="integer">0</discount_in_cents>
<due_on type="datetime">2018-06-19T01:13:26Z</due_on>
<net_terms type="integer">0</net_terms>
<collection_method>automatic</collection_method>
<po_number nil="nil"></po_number>
<terms_and_conditions nil="nil"></terms_and_conditions>
<tax_type>usst</tax_type>
<tax_region>CA</tax_region>
<tax_rate type="float">0.085</tax_rate>
<line_items type="array">
<adjustment href="https://your-subdomain.recurly.com/v2/adjustments/458465c8a977efb18125714c5abc4119" type="charge">
<account href="https://your-subdomain.recurly.com/v2/accounts/1"/>
<invoice href="https://your-subdomain.recurly.com/v2/invoices/2531"/>
<uuid>458465c8a977efb18125714c5abc4119</uuid>
<state>invoiced</state>
<description>Description for Adjustment</description>
<accounting_code nil="nil"></accounting_code>
<product_code>freeform_98765</product_code>
<origin>debit</origin>
<unit_amount_in_cents type="integer">100</unit_amount_in_cents>
<quantity type="integer">13</quantity>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">111</tax_in_cents>
<total_in_cents type="integer">1411</total_in_cents>
<currency>USD</currency>
<proration_rate nil="nil"></proration_rate>
<taxable type="boolean">false</taxable>
<tax_type>usst</tax_type>
<tax_region>CA</tax_region>
<tax_rate type="float">0.085</tax_rate>
<tax_exempt type="boolean">false</tax_exempt>
<start_date type="datetime">2018-06-19T01:13:26Z</start_date>
<end_date nil="nil"></end_date>
<created_at type="datetime">2018-06-19T01:13:26Z</created_at>
<updated_at type="datetime">2018-06-19T01:13:26Z</updated_at>
<revenue_schedule_type></revenue_schedule_type>
</adjustment>
</line_items>
<transactions type="array">
<transaction href="https://your-subdomain.recurly.com/v2/transactions/458465c9016060dbde60734789869a2d" type="credit_card">
<account href="https://your-subdomain.recurly.com/v2/accounts/1"/>
<invoice href="https://your-subdomain.recurly.com/v2/invoices/2531"/>
<uuid>458465c9016060dbde60734789869a2d</uuid>
<action>purchase</action>
<amount_in_cents type="integer">1411</amount_in_cents>
<tax_in_cents type="integer">111</tax_in_cents>
<currency>USD</currency>
<status>success</status>
<payment_method>credit_card</payment_method>
<reference>8001561</reference>
<source>transaction</source>
<recurring type="boolean">false</recurring>
<test type="boolean">true</test>
<voidable type="boolean">true</voidable>
<refundable type="boolean">true</refundable>
<ip_address nil="nil"></ip_address>
<gateway_type>test</gateway_type>
<origin>api</origin>
<description>Transaction Description</description>
<message>Successful test transaction</message>
<approval_code nil="nil"></approval_code>
<failure_type nil="nil"></failure_type>
<gateway_error_codes nil="nil"></gateway_error_codes>
<cvv_result code="" nil="nil"></cvv_result>
<avs_result code="D">Street address and postal code match.</avs_result>
<avs_result_street nil="nil"></avs_result_street>
<avs_result_postal nil="nil"></avs_result_postal>
<created_at type="datetime">2018-06-19T01:13:26Z</created_at>
<collected_at type="datetime">2018-06-19T01:13:26Z</collected_at>
<updated_at type="datetime">2018-06-19T01:13:26Z</updated_at>
<details>
<account>
<account_code>1</account_code>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<company nil="nil"></company>
<email>verena@example.com</email>
<billing_info type="credit_card">
<first_name>Verena</first_name>
<last_name>Example</last_name>
<address1>123 Main St.</address1>
<address2></address2>
<city>SF</city>
<state>CA</state>
<zip>94105</zip>
<country>US</country>
<phone></phone>
<vat_number></vat_number>
<card_type>Visa</card_type>
<year type="integer">2020</year>
<month type="integer">6</month>
<first_six>411111</first_six>
<last_four>1111</last_four>
</billing_info>
</account>
</details>
</transaction>
</transactions>
<a name="refund" href="https://your-subdomain.recurly.com/v2/invoices/2531/refund" method="post"/>
</charge_invoice>
<credit_invoices type="array">
</credit_invoices>
</invoice_collection>`)
})
expectedInvoiceNumber := 2531
expectedTax := 111
expectedTotal := 1411
r, invs, err := client.Purchases.Preview(recurly.Purchase{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
} else if r.IsError() {
t.Fatal("expected preview purchase to return OK")
} else if invs.ChargeInvoice.InvoiceNumber != expectedInvoiceNumber {
t.Fatalf("expected invoice number %d; got %d\n", expectedInvoiceNumber, invs.ChargeInvoice.InvoiceNumber)
} else if invs.ChargeInvoice.TaxInCents != expectedTax {
t.Fatalf("expected tax %d; got %d\n", expectedTax, invs.ChargeInvoice.TaxInCents)
} else if invs.ChargeInvoice.TotalInCents != expectedTotal {
t.Fatalf("expected total %d; got %d\n", expectedTotal, invs.ChargeInvoice.TotalInCents)
}
}