-
Notifications
You must be signed in to change notification settings - Fork 44
/
purchases_test.go
262 lines (239 loc) · 7.45 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
package recurly_test
import (
"bytes"
"context"
"encoding/xml"
"net/http"
"strconv"
"testing"
"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
)
// Ensure structs are encoded to XML properly.
func TestPurchases_Purchase_Encoding(t *testing.T) {
tests := []struct {
v recurly.Purchase
expected string
}{
{
expected: MustCompactString(`
<purchase>
<account></account>
<adjustments></adjustments>
<currency></currency>
<gift_card></gift_card>
<coupon_codes></coupon_codes>
<subscriptions></subscriptions>
</purchase>
`),
},
{
v: *NewTestPurchase(),
expected: string(MustOpenCompactXMLFile("purchase.xml")),
},
{
v: recurly.Purchase{ShippingAddressID: 2438622711411416831},
expected: MustCompactString(`
<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>
`),
},
{
v: recurly.Purchase{
ShippingFees: &[]recurly.ShippingFee{
{
ShippingMethodCode: "foo",
ShippingAmountInCents: recurly.NewInt(0),
},
{
ShippingMethodCode: "bar",
ShippingAmountInCents: recurly.NewInt(10),
},
},
ShippingAddressID: 1,
TransactionType: "moto",
},
expected: MustCompactString(`
<purchase>
<account></account>
<adjustments></adjustments>
<currency></currency>
<gift_card></gift_card>
<coupon_codes></coupon_codes>
<subscriptions></subscriptions>
<shipping_address_id>1</shipping_address_id>
<shipping_fees>
<shipping_fee>
<shipping_method_code>foo</shipping_method_code>
<shipping_amount_in_cents>0</shipping_amount_in_cents>
</shipping_fee>
<shipping_fee>
<shipping_method_code>bar</shipping_method_code>
<shipping_amount_in_cents>10</shipping_amount_in_cents>
</shipping_fee>
</shipping_fees>
<transaction_type>moto</transaction_type>
</purchase>
`),
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
buf := new(bytes.Buffer)
if err := xml.NewEncoder(buf).Encode(tt.v); err != nil {
t.Fatal(err)
} else if tt.expected != buf.String() {
t.Fatal(buf.String()+"\n\n", tt.expected)
}
})
}
}
func TestPurchases_Create(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("POST", "/v2/purchases", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
w.Write(MustOpenFile("invoice_collection.xml"))
}, t)
if collection, err := client.Purchases.Create(context.Background(), recurly.Purchase{}); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(collection, NewTestInvoiceCollection()); diff != "" {
t.Fatal(diff)
}
}
func TestPurchases_Preview(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("POST", "/v2/purchases/preview", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("invoice_collection.xml"))
}, t)
if collection, err := client.Purchases.Preview(context.Background(), recurly.Purchase{}); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(collection, NewTestInvoiceCollection()); diff != "" {
t.Fatal(diff)
}
}
func TestPurchases_Authorize(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("POST", "/v2/purchases/authorize", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("purchase.xml"))
}, t)
if purchase, err := client.Purchases.Authorize(context.Background(), recurly.Purchase{}); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(purchase, NewTestPurchase()); diff != "" {
t.Fatal(diff)
}
}
func TestPurchases_Pending(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("POST", "/v2/purchases/pending", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("purchase.xml"))
}, t)
if purchase, err := client.Purchases.Pending(context.Background(), recurly.Purchase{}); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(purchase, NewTestPurchase()); diff != "" {
t.Fatal(diff)
}
}
func TestPurchases_Capture(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("POST", "/v2/purchases/a13acd8fe4294916b79aec87b7ea441f/capture", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("invoice_collection.xml"))
}, t)
if collection, err := client.Purchases.Capture(context.Background(), "a13acd8f-e429-4916-b79a-ec87b7ea441f"); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(collection, NewTestInvoiceCollection()); diff != "" {
t.Fatal(diff)
}
}
func TestPurchases_Cancel(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("POST", "/v2/purchases/a13acd8fe4294916b79aec87b7ea441f/cancel", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("invoice_collection.xml"))
}, t)
if collection, err := client.Purchases.Cancel(context.Background(), "a13acd8f-e429-4916-b79a-ec87b7ea441f"); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(collection, NewTestInvoiceCollection()); diff != "" {
t.Fatal(diff)
}
}
// Returns a Purchase corresponding to testdata/purchase.xml.
func NewTestPurchase() *recurly.Purchase {
return &recurly.Purchase{
XMLName: xml.Name{Local: "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{
XMLName: xml.Name{Local: "account"},
Code: "c442b36c-c64f-41d7-b8e1-9c04e7a6ff82",
ShippingAddresses: &[]recurly.ShippingAddress{{
XMLName: xml.Name{Local: "shipping_address"},
FirstName: "Lon",
LastName: "Doner",
Address: "221B Baker St.",
City: "London",
Zip: "W1K 6AH",
Country: "GB",
Nickname: "Home",
}},
BillingInfo: &recurly.Billing{
XMLName: xml.Name{Local: "billing_info"},
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{{
XMLName: xml.Name{Local: "adjustment"},
ProductCode: "4549449c-5870-4845-b672-1d07f15e87dd",
Quantity: 1,
UnitAmountInCents: recurly.NewInt(1000),
Description: "Description of this adjustment",
}},
Subscriptions: []recurly.PurchaseSubscription{{
XMLName: xml.Name{Local: "subscription"},
PlanCode: "plan1",
}},
CouponCodes: []string{"coupon1", "coupon2"},
GiftCard: "ABC1234",
}
}