This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathClient_Payments_test.go
408 lines (369 loc) · 13.3 KB
/
Client_Payments_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
package connectsdk
import (
"net/url"
"strings"
"testing"
"time"
"github.com/Ingenico-ePayments/connect-sdk-go/communicator"
"github.com/Ingenico-ePayments/connect-sdk-go/communicator/communication"
"github.com/Ingenico-ePayments/connect-sdk-go/defaultimpl"
"github.com/Ingenico-ePayments/connect-sdk-go/domain/payment"
"github.com/Ingenico-ePayments/connect-sdk-go/errors"
"github.com/Ingenico-ePayments/connect-sdk-go/logging"
)
func CheckSuccess(cv *TestConnection, resp payment.CreateResponse, err error) string {
if err != nil {
return "Error during request: " + err.Error()
}
if payment := resp.Payment; payment == nil || payment.ID == nil || *payment.ID != "000002000020142549460000100001" {
return "The ID of the payment is not equal to 000002000020142549460000100001"
}
if payment := resp.Payment; payment == nil || payment.Status == nil || *payment.Status != "PENDING_APPROVAL" {
return "The status of the payment is unequal to PENDING_APPROVAL"
}
return ""
}
func CheckRejected(cv *TestConnection, res payment.CreateResponse, err error) string {
if err == nil {
return "Error expected"
}
if dpe, ok := err.(*errors.DeclinedPaymentError); ok {
res := dpe.PaymentResult()
if res == nil {
return "PaymentResult is nil"
}
if res.Payment.ID == nil {
return "PaymentResult.ID is nil"
}
if *res.Payment.ID != "000002000020142544360000100001" {
return "PaymentResult.ID is not 000002000020142544360000100001"
}
if res.Payment.Status == nil {
return "PaymentResult.Status is nil"
}
if *res.Payment.Status != "REJECTED" {
return "PaymentResult.Status is not equal to " + "REJECTED"
}
} else {
return "Expected DeclinedPaymentError, but got different error"
}
estr := err.Error()
if !strings.Contains(estr, "payment '000002000020142544360000100001'") {
return "Error message does not contain" + "payment '000002000020142544360000100001'"
}
if !strings.Contains(estr, "status 'REJECTED'") {
return "Error message: " + estr + " does not contain " + "status 'REJECTED'"
}
if !strings.Contains(estr, rejectedJSON) {
return "Error message: " + estr + " does not contain response body"
}
return ""
}
func CheckInvalidRequest(cv *TestConnection, res payment.CreateResponse, err error) string {
if err == nil {
return "Error expected"
}
if _, ok := err.(*errors.ValidateError); !ok {
return "Expected ValidateError, but got different error"
}
estr := err.Error()
if !strings.Contains(estr, invalidRequestJSON) {
return "Error message: " + estr + " does not contain response body"
}
return ""
}
func CheckInvalidAuthorization(cv *TestConnection, res payment.CreateResponse, err error) string {
if err == nil {
return "Error expected"
}
if _, ok := err.(errors.APIError); !ok {
return "Expected APIError, but got different error"
}
estr := err.Error()
if !strings.Contains(estr, invalidAuthorizationJSON) {
return "Error message: " + estr + " does not contain response body"
}
return ""
}
func CheckReferenceError(cv *TestConnection, res payment.CreateResponse, err error) string {
if err == nil {
return "Error expected"
}
if _, ok := err.(*errors.ReferenceError); !ok {
return "Expected ReferenceError, but got different error"
}
estr := err.Error()
if !strings.Contains(estr, duplicateRequestJSON) {
return "Error message: " + estr + " does not contain response body"
}
return ""
}
func CheckIdempotenceError(cv *TestConnection, res payment.CreateResponse, err error) string {
if err == nil {
return "Error expected"
}
if ie, ok := err.(*errors.IdempotenceError); ok {
if ie.IdempotenceKey() != cv.idempotenceKey {
return "Returned wrong idempotenceKey"
}
} else {
return "Expected IdempotenceError, but got different error"
}
estr := err.Error()
if !strings.Contains(estr, duplicateRequestJSON) {
return "Error message: " + estr + " does not contain response body"
}
return ""
}
func CheckNotFound(cv *TestConnection, res payment.CreateResponse, err error) string {
if err == nil {
return "Error expected"
}
if nfe, ok := err.(*errors.NotFoundError); ok {
if nfe.InternalError() == nil {
return "Returned NotFoundError without internal error"
}
if _, ok := nfe.InternalError().(*errors.ResponseError); !ok {
return "NotFoundError has a different internal error than ResponseError"
}
if !strings.Contains(nfe.InternalError().Error(), notFoundHTML) {
return "Error message: " + nfe.InternalError().Error() + " does not contain response body"
}
} else {
return "Expected NotFoundError, but got different error"
}
return ""
}
func CheckMethodNotAllowed(cv *TestConnection, res payment.CreateResponse, err error) string {
if err == nil {
return "Error expected"
}
if nfe, ok := err.(*errors.CommunicationError); ok {
if nfe.InternalError() == nil {
return "Returned CommunicationError without internal error"
}
if _, ok := nfe.InternalError().(*errors.ResponseError); !ok {
return "CommunicationError has a different internal error than ResponseError"
}
if !strings.Contains(nfe.InternalError().Error(), methodNotAllowedHTML) {
return "Error message: " + nfe.InternalError().Error() + " does not contain response body"
}
} else {
return "Expected CommunicationError, but got different error"
}
return ""
}
var table = []TestConnection{
// Tests that a non-failure response will not throw an exception.
{pendingApprovalJSON, 201, nil, "", CheckSuccess},
//Tests that a failure response with a payment result will return a DeclinedPaymentError.
{rejectedJSON, 400, nil, "", CheckRejected},
// Tests that a 400 failure response without a payment result will return a ValidateError.
{invalidRequestJSON, 400, nil, "", CheckInvalidRequest},
// Tests that a 401 failure response without a payment result will return a APIError.
{invalidAuthorizationJSON, 401, nil, "", CheckInvalidAuthorization},
// Tests that a 409 failure response with a duplicate request code but without an idempotence key will return a ReferenceError
{duplicateRequestJSON, 409, nil, "", CheckReferenceError},
// Tests that a 409 failure response with a duplicate request code and an idempotence key will return a IdempotenceError.
{duplicateRequestJSON, 409, nil, "key", CheckIdempotenceError},
// Tests that a 404 response with a non-JSON response will throw a NotFoundException.
{notFoundHTML, 404, []communication.Header{newHeader("content-type", "text/html")}, "", CheckNotFound},
// Tests that a 405 response with a non-JSON response will throw a CommunicationException.
{methodNotAllowedHTML, 405, []communication.Header{newHeader("content-type", "text/html")}, "", CheckMethodNotAllowed},
}
func newHeader(name, value string) communication.Header {
h, _ := communication.NewHeader(name, value)
return *h
}
type TestConnection struct {
body string
statusCode int
headers []communication.Header
idempotenceKey string
checkf CheckResult
}
const pendingApprovalJSON string = `{
"creationOutput": {
"additionalReference": "00000200002014254946",
"externalReference": "000002000020142549460000100001"
},
"payment": {
"id": "000002000020142549460000100001",
"paymentOutput": {
"amountOfMoney": {
"amount": 2345,
"currencyCode": "CAD"
},
"references": {
"paymentReference": "0"
},
"paymentMethod": "card",
"cardPaymentMethodSpecificOutput": {
"paymentProductId": 1,
"authorisationCode": "OK1131",
"card": {
"cardNumber": "************9176",
"expiryDate": "1220"
},
"fraudResults": {
"fraudServiceResult": "error",
"avsResult": "X",
"cvvResult": "M"
}
}
},
"status": "PENDING_APPROVAL",
"statusOutput": {
"isCancellable": true,
"statusCode": 600,
"statusCodeChangeDateTime": "20150331120036",
"isAuthorized": true
}
}
}`
const rejectedJSON = `{
"errorId": "2c164323-20d3-4e9e-8578-dc562cd7506c-0000003c",
"errors": [
{
"code": "21000020",
"requestId": "2001798",
"message": "VALUE **************** OF FIELD CREDITCARDNUMBER DID NOT PASS THE LUHNCHECK"
}
],
"paymentResult": {
"creationOutput": {
"additionalReference": "00000200002014254436",
"externalReference": "000002000020142544360000100001"
},
"payment": {
"id": "000002000020142544360000100001",
"paymentOutput": {
"amountOfMoney": {
"amount": 2345,
"currencyCode": "CAD"
},
"references": {
"paymentReference": "0"
},
"paymentMethod": "card",
"cardPaymentMethodSpecificOutput": {
"paymentProductId": 1
}
},
"status": "REJECTED",
"statusOutput": {
"errors": [
{
"code": "21000020",
"requestId": "2001798",
"message": "VALUE **************** OF FIELD CREDITCARDNUMBER DID NOT PASS THE LUHNCHECK"
}
],
"isCancellable": false,
"statusCode": 100,
"statusCodeChangeDateTime": "20150330173151",
"isAuthorized": false
}
}
}
}`
const invalidRequestJSON string = `{
"errorId": "2c164323-20d3-4e9e-8578-dc562cd7506c-00000058",
"errors": [
{
"code": "21000120",
"requestId": "2001803",
"propertyName": "cardPaymentMethodSpecificInput.card.expiryDate",
"message": "paymentMethodSpecificInput.card.expiryDate (1210) IS IN THE PAST OR NOT IN CORRECT MMYY FORMAT"
}
]
}`
const invalidAuthorizationJSON string = `{
"errorId": "fbd8d914-c889-45d3-a396-9e0d9ff9db88-0000006f",
"errors": [
{
"code": "9002",
"message": "MISSING_OR_INVALID_AUTHORIZATION"
}
]
}`
const duplicateRequestJSON string = `{
"errorId" : "75b0f13a-04a5-41b3-83b8-b295ddb23439-000013c6",
"errors" : [ {
"code" : "1409",
"message" : "DUPLICATE REQUEST IN PROGRESS",
"httpStatusCode" : 409
} ]
}`
const notFoundHTML string = "Not Found"
const methodNotAllowedHTML string = "Not Allowed"
type CheckResult func(cv *TestConnection, resp payment.CreateResponse, err error) string
func (t *TestConnection) CloseExpiredConnections() {
}
func (t *TestConnection) CloseIdleConnections(num time.Duration) {
}
func (t *TestConnection) Close() error {
return nil
}
func (t *TestConnection) Get(resourceURI url.URL, requestHeaders []communication.Header, respHandler communication.ResponseHandler) (interface{}, error) {
return nil, nil
}
func (t *TestConnection) Delete(resourceURI url.URL, requestHeaders []communication.Header, respHandler communication.ResponseHandler) (interface{}, error) {
return nil, nil
}
func (t *TestConnection) Put(resourceURI url.URL, requestHeaders []communication.Header, body string, respHandler communication.ResponseHandler) (interface{}, error) {
return nil, nil
}
func (t *TestConnection) PutMultipart(resourceURI url.URL, requestHeaders []communication.Header, body *communication.MultipartFormDataObject, respHandler communication.ResponseHandler) (interface{}, error) {
return nil, nil
}
func (t *TestConnection) Post(resourceURI url.URL, requestHeaders []communication.Header, body string, respHandler communication.ResponseHandler) (interface{}, error) {
return respHandler.Handle(t.statusCode, t.headers, strings.NewReader(t.body))
}
func (t *TestConnection) PostMultipart(resourceURI url.URL, requestHeaders []communication.Header, body *communication.MultipartFormDataObject, respHandler communication.ResponseHandler) (interface{}, error) {
return nil, nil
}
func (t *TestConnection) DisableLogging() {
}
func (t *TestConnection) EnableLogging(l logging.CommunicatorLogger) {
}
func createRequestT() payment.CreateRequest {
p := payment.NewCreateRequest()
if p != nil {
return *p
}
panic("Cannot Create Request")
}
func TestPayment(t *testing.T) {
for _, tv := range table {
connectionMock := tv
apiEndpoint, _ := url.Parse("http://localhost")
// Error not possible
auth, _ := defaultimpl.NewDefaultAuthenticator(defaultimpl.V1HMAC, "test", "test")
// Error not possible
mp, err := communicator.NewMetaDataProviderBuilder("ingenico").Build()
if err != nil {
t.Fatalf("Cannot create MetaDataProvider: %s", err)
}
session, err := communicator.NewSession(apiEndpoint, &connectionMock, auth, mp)
if err != nil {
t.Fatal("Cannot create Session")
}
client, err := CreateClientFromSession(session)
if err != nil {
t.Fatal("Cannot create Client: ", err)
}
body := createRequestT()
cc := &CallContext{}
var resp payment.CreateResponse
if tv.idempotenceKey != "" {
cc.IdempotenceKey = tv.idempotenceKey
resp, err = client.Merchant("merchantId").Payments().Create(body, cc)
} else {
resp, err = client.Merchant("merchantId").Payments().Create(body, nil)
}
if str := tv.checkf(&tv, resp, err); str != "" {
t.Fatal(str)
}
}
}