forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transactions_test.go
226 lines (207 loc) · 6.76 KB
/
transactions_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
package recurly_test
import (
"context"
"encoding/xml"
"net"
"net/http"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/splice/recurly"
)
func TestTransactions_List(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
var invocations int
s.HandleFunc("GET", "/v2/transactions", func(w http.ResponseWriter, r *http.Request) {
invocations++
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("transactions.xml"))
}, t)
pager := client.Transactions.List(nil)
for pager.Next() {
var transactions []recurly.Transaction
if err := pager.Fetch(context.Background(), &transactions); err != nil {
t.Fatal(err)
} else if !s.Invoked {
t.Fatal("expected s to be invoked")
} else if diff := cmp.Diff(transactions, []recurly.Transaction{*NewTestTransaction()}); diff != "" {
t.Fatal(diff)
}
}
if invocations != 1 {
t.Fatalf("unexpected number of invocations: %d", invocations)
}
}
func TestTransactions_ListAccount(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
var invocations int
s.HandleFunc("GET", "/v2/accounts/1/transactions", func(w http.ResponseWriter, r *http.Request) {
invocations++
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("transactions.xml"))
}, t)
pager := client.Transactions.ListAccount("1", nil)
for pager.Next() {
var transactions []recurly.Transaction
if err := pager.Fetch(context.Background(), &transactions); err != nil {
t.Fatal(err)
} else if !s.Invoked {
t.Fatal("expected s to be invoked")
} else if diff := cmp.Diff(transactions, []recurly.Transaction{*NewTestTransaction()}); diff != "" {
t.Fatal(diff)
}
}
if invocations != 1 {
t.Fatalf("unexpected number of invocations: %d", invocations)
}
}
func TestTransactions_Get(t *testing.T) {
t.Run("OK", func(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("GET", "/v2/transactions/a13acd8fe4294916b79aec87b7ea441f", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("transaction.xml"))
}, t)
if transaction, err := client.Transactions.Get(context.Background(), "a13acd8f-e429-4916-b79a-ec87b7ea441f"); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(transaction, NewTestTransaction()); diff != "" {
t.Fatal(diff)
} else if !s.Invoked {
t.Fatal("expected fn invocation")
}
})
// Retrieving a failed transaction should hold the transaction errors.
t.Run("TransactionFailed", func(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("GET", "/v2/transactions/a13acd8fe4294916b79aec87b7ea441f", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("transaction_failed.xml"))
}, t)
if transaction, err := client.Transactions.Get(context.Background(), "a13acd8f-e429-4916-b79a-ec87b7ea441f"); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(transaction, NewTestTransactionFailed()); diff != "" {
t.Fatal(diff)
} else if !s.Invoked {
t.Fatal("expected fn invocation")
}
})
// Ensure a 404 returns nil values.
t.Run("ErrNotFound", func(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("GET", "/v2/transactions/a13acd8fe4294916b79aec87b7ea441f", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}, t)
if transaction, err := client.Transactions.Get(context.Background(), "a13acd8f-e429-4916-b79a-ec87b7ea441f"); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if transaction != nil {
t.Fatalf("expected nil: %#v", transaction)
}
})
}
// Returns a Transaction corresponding to testdata/transaction.xml.
func NewTestTransaction() *recurly.Transaction {
return &recurly.Transaction{
InvoiceNumber: 1108,
UUID: "a13acd8fe4294916b79aec87b7ea441f", // UUID has been sanitized
Action: "purchase",
AmountInCents: 1000,
TaxInCents: 0,
Currency: "USD",
Status: "success",
Description: "Order #717",
PaymentMethod: "credit_card",
Reference: "5416477",
Source: "subscription",
Recurring: recurly.NewBool(true),
Test: true,
Voidable: recurly.NewBool(true),
Refundable: recurly.NewBool(true),
IPAddress: net.ParseIP("127.0.0.1"),
CVVResult: recurly.CVVResult{
Code: "M",
Message: "Match",
},
AVSResult: recurly.AVSResult{
Code: "D",
Message: "Street address and postal code match.",
},
CreatedAt: recurly.NewTime(time.Date(2015, time.June, 10, 15, 25, 6, 0, time.UTC)),
Account: recurly.Account{
XMLName: xml.Name{Local: "account"},
Code: "1",
FirstName: "Verena",
LastName: "Example",
Email: "[email protected]",
BillingInfo: &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",
CardType: "Visa",
Year: 2017,
Month: 11,
FirstSix: "411111",
LastFour: "1111",
},
},
}
}
// Returns a Transaction corresponding to testdata/transaction_failed.xml
// as well as the transaction portion of testdata/errors_transaction_failed.xml.
func NewTestTransactionFailed() *recurly.Transaction {
return &recurly.Transaction{
UUID: "3d1c6aa86e3d447eb0f3b4a6e3e074d9",
Action: "purchase",
AmountInCents: 4900,
TaxInCents: 0,
Currency: "USD",
Status: "declined",
Test: true,
Voidable: recurly.NewBool(false),
Refundable: recurly.NewBool(false),
TransactionError: &recurly.TransactionError{
XMLName: xml.Name{Local: "transaction_error"},
ErrorCode: "fraud_security_code",
ErrorCategory: "fraud",
MerchantMessage: "The payment gateway declined the transaction because the security code (CVV) did not match.",
CustomerMessage: "The security code you entered does not match. Please update the CVV and try again.",
GatewayErrorCode: "301",
ThreeDSecureActionTokenID: "ABCDEFGHIJKL012345",
},
CVVResult: recurly.CVVResult{
Code: "N",
Message: "No Match",
},
AVSResult: recurly.AVSResult{
Code: "D",
Message: "Street address and postal code match.",
},
AVSResultStreet: "Y",
AVSResultPostal: "Y",
CreatedAt: recurly.NewTime(MustParseTime("2011-10-17T17:24:53Z")),
Account: recurly.Account{
XMLName: xml.Name{Local: "account"},
Code: "1",
Email: "[email protected]",
BillingInfo: &recurly.Billing{
XMLName: xml.Name{Local: "billing_info"},
CardType: "Visa",
Year: 2015,
Month: 11,
FirstSix: "400000",
LastFour: "0101",
},
},
}
}