-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkredivo_test.go
644 lines (512 loc) · 18.3 KB
/
kredivo_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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
package kredivo
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestKredivo(t *testing.T) {
//Order request payload
orderData := []byte(`{
"server_key":"8tLHIx8V0N6KtnSpS9Nbd6zROFFJH7",
"payment_type":"30_days",
"transaction_details": {
"amount":9505000,
"order_id":"736373K383",
"items": [
{
"id":"BB12345678",
"name":"iPhone 5S",
"price":6000000,
"type":"Smartphone",
"url":"http://merchant.com/cellphones/iphone5s_64g",
"quantity":1
},
{
"id":"AZ14565678",
"name":"Hailee Sneakers Blink Silver",
"price":250000,
"url":"http://merchant.com/fashion/shoes/sneakers-blink-shoes",
"type":"Sneakers",
"quantity":2,
"parent_type":"SELLER",
"parent_id":"SELLER456"
},
{
"id":"taxfee",
"name":"Tax Fee",
"price":1000,
"quantity":1
},
{
"id":"shippingfee",
"name":"Shipping Fee",
"price":90000,
"quantity":1,
"parent_type":"SELLER",
"parent_id":"SELLER456"
},
{
"id":"discount",
"name":"Discount",
"price":50000,
"quantity":1
}
]
},
"sellers":[
{
"id":"SELLER123",
"name":"Sunrise",
"email": "sunrise@gmail.com",
"address" : {
"first_name":"Irfan",
"last_name":"Sutandro",
"address":"Jalan Tentara Pelajar no 49",
"city":"Jakarta Utara",
"postal_code":"12960",
"phone":"08123456789",
"country_code":"IDN"
}
},
{
"id":"SELLER456",
"name":"Toko Bagus",
"email": "tokobagus@gmail.com",
"address" : {
"first_name":"Toto",
"last_name":"Wahyuni",
"address":"Jalan Krici raya IX",
"city":"Jakarta Selatan",
"postal_code":"12960",
"phone":"08123456789",
"country_code":"IDN"
}
}
],
"customer_details":{
"first_name":"Oemang",
"last_name":"Tandra",
"email":"alie@satuduatiga.com",
"phone":"081513114262"
},
"billing_address": {
"first_name":"Oemang",
"last_name":"Tandra",
"address":"Jalan Teknologi Indonesia No. 25",
"city":"Jakarta",
"postal_code":"12960",
"phone":"081513114262",
"country_code":"IDN"
},
"shipping_address": {
"first_name":"Oemang",
"last_name":"Tandra",
"address":"Jalan Teknologi Indonesia No. 25",
"city":"Jakarta",
"postal_code":"12960",
"phone":"081513114262",
"country_code":"IDN"
},
"push_uri":"https://api.merchant.com/push.php",
"back_to_store_uri":"https://merchant.com"
}`)
//Payment Request Payload
paymentRequestData := []byte(`{
"server_key":"8tLHIx8V0N6KtnSpS9Nbd6zROFFJH7",
"amount":2400000,
"items": [
{
"id":"BB12345678",
"name":"iPhone 5S",
"price":2400000,
"url":"http://merchant.com/cellphones/iphone5s_64g",
"type":"Smartphone",
"quantity":1
}
]
}`)
//Cancel Request Payload
cancelRequestData := []byte(`{
"server_key":"MEJ4FLRc74UU64cxCF8Z3HYSpPctD7",
"order_id":"8192K383",
"transaction_id":"f6e33997-9ea2-426c-991b-9421165b9343",
"cancellation_reason":"Out of stock",
"cancelled_by":"Althea",
"cancellation_date":"1501844808"
}`)
//partialCancelData
partialCancelData := []byte(`{
"server_key":"8tLHIx8V0N6KtnSpS9Nbd6zROFFJH7",
"transaction_details": {
"order_id":"KD14721",
"amount":1500100,
"items": [
{
"id":"11",
"name":"Mesin Cuci",
"cancellation_reason":"Out of stock"
}
]
},
"cancelled_by":"Althea",
"cancellation_date":"1501846306"
}`)
//TransactionStatus Request Payload
transactionStatusRequestData := []byte(`{
"server_key":"MEJ4FLRc74UU64cxCF8Z3HYSpPctD7",
"order_id":"8192K383"
}`)
t.Run("Test Kredivo Creation", func(t *testing.T) {
//construct kredivo
//and add required parameters
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
if kred == nil {
t.Error("Cannot Call Kredivo Constructor")
}
})
t.Run("Test Kredivo Checkout", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/checkout_url" {
t.Errorf("Expected request to ‘/checkout_url, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"status":"OK",
"message":"Message if any",
"redirect_url":"https://sandbox.kredivo.com/kredivo/v2/signin?tk=XXX"
}`))
}))
//close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var order Order
err := json.Unmarshal(orderData, &order)
if err != nil {
t.Error("Cannot Unmarshal Order JSON data")
}
kred.Env = ts.URL
checkoutResult := kred.Checkout(&order)
if checkoutResult.Error != nil {
t.Errorf("Checkout() returned an error: %s", checkoutResult.Error)
}
})
t.Run("Test Kredivo Checkout Error Empty Result", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/checkout_url" {
t.Errorf("Expected request to ‘/checkout_url, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`error`))
}))
//close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var order Order
err := json.Unmarshal([]byte("order error"), &order)
if err == nil {
t.Error("Should Error Unmarshal Order JSON data")
}
kred.Env = ts.URL
checkoutResult := kred.Checkout(&order)
if checkoutResult.Error == nil {
t.Errorf("Checkout() Should returned an error: %s", checkoutResult.Error)
}
})
t.Run("Test Kredivo Get Payments", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/payments" {
t.Errorf("Expected request to ‘/payments, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{
"status": "OK",
"message": "Available payment types are listed.",
"payments": [
{
"down_payment": 0,
"name": "Bayar dalam 30 hari",
"amount": 2400000,
"installment_amount": 2400000,
"rate": 0,
"monthly_installment": 2400000,
"discounted_monthly_installment": 0,
"tenure": 1,
"id": "30_days"
}
]}`))
}))
// close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var paymentRequest PaymentRequest
err := json.Unmarshal(paymentRequestData, &paymentRequest)
if err != nil {
t.Error("Cannot Unmarshal Payment Request JSON data")
}
//hit the get payment endpoint
kred.Env = ts.URL
paymentResult := kred.GetPayments(&paymentRequest)
if paymentResult.Error != nil {
t.Errorf("GetPayments() returned an error: %s", paymentResult.Error)
}
})
t.Run("Test Kredivo Get Payments Error Empty Result", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/payments" {
t.Errorf("Expected request to ‘/payments, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`"error"`))
}))
// close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var paymentRequest PaymentRequest
err := json.Unmarshal([]byte(""), &paymentRequest)
if err == nil {
t.Error("Should be Error Unmarshal Payment Request JSON data")
}
//hit the get payment endpoint
kred.Env = ts.URL
paymentResult := kred.GetPayments(&paymentRequest)
if paymentResult.Error == nil {
t.Errorf("GetPayments() Should returned an error: %s", paymentResult.Error)
}
})
t.Run("Test Kredivo Confirm Payment", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "GET" {
t.Errorf("Expected GET request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/update" {
t.Errorf("Expected request to ‘/update, got ‘%s’", r.URL.EscapedPath())
}
if r.URL.Query().Get("transaction_id") == "" && r.URL.Query().Get("signature_key") == "" {
t.Error("Expected request to ‘/update, got transaction id and signature key")
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{
"status": "OK",
"legal_name": "TANDRA",
"fraud_status": "accept",
"order_id": "KD125262",
"transaction_time": 1501846094,
"amount": "1500100.00",
"payment_type": "30_days",
"transaction_status": "settlement",
"message": "Confirmed order status. Valid!",
"transaction_id": "fadee4e5-99a2-48d6-952d-007f3fa508e8"
}`))
}))
// close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var confirmRequest ConfirmRequest
confirmRequest.TransactionID = "f6e33997-9ea2-426c-991b-9421165b9343"
confirmRequest.SignatureKey = "YmQBYtAODqlWkmVrkNY%2BRtHclC9yHMDsKwAJ%2BG4n%2BQ1m1DlgqtIfsjjQrUFEIs%2BnWlHhJahCmJuj%2FSOJ8YmPPuX9iKoquvfJl5n0V57Cess%3D"
//hit the confirm endpoint
kred.Env = ts.URL
confirmResult := kred.Confirm(&confirmRequest)
if confirmResult.Error != nil {
t.Errorf("Confirm() returned an error: %s", confirmResult.Error)
}
})
t.Run("Test Kredivo Cancel Transaction", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/cancel_transaction" {
t.Errorf("Expected request to ‘/cancel_transaction, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{
"status": "OK",
"fraud_status": "accept",
"order_id": "KD14721",
"transaction_time": 1501842660,
"amount": "1515521.00",
"payment_type": "30_days",
"transaction_status": "cancel",
"message": "Cancelled the transaction!",
"transaction_id": "6febc2b2-ac4f-462c-9e7e-56fc5da05d91"
}`))
}))
// close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var cancelRequest CancelRequest
err := json.Unmarshal(cancelRequestData, &cancelRequest)
if err != nil {
t.Error("Cannot Unmarshal Cancel Request JSON data")
}
//hit the cancel transaction endpoint
kred.Env = ts.URL
cancelResult := kred.Cancel(&cancelRequest)
if cancelResult.Error != nil {
t.Errorf("Cancel() returned an error: %s", cancelResult.Error)
}
})
t.Run("Test Kredivo Cancel Transaction Error Empty Result", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/cancel_transaction" {
t.Errorf("Expected request to ‘/cancel_transaction, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`error`))
}))
// close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var cancelRequest CancelRequest
err := json.Unmarshal([]byte(""), &cancelRequest)
if err == nil {
t.Error("Should Error Unmarshal Cancel Request JSON data")
}
//hit the cancel transaction endpoint
kred.Env = ts.URL
cancelResult := kred.Cancel(&cancelRequest)
if cancelResult.Error == nil {
t.Errorf("Cancel() Should returned an error: %s", cancelResult.Error)
}
})
t.Run("Test Kredivo Partial Cancel Transaction", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/partial_cancel_transaction" {
t.Errorf("Expected request to ‘/partial_cancel_transaction, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{
"status": "OK",
"message": "Processed Partial Cancellation successfully!",
"fraud_status": "accept"
}`))
}))
// close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var partialRequest PartialCancelRequest
err := json.Unmarshal(partialCancelData, &partialRequest)
if err != nil {
t.Error("Cannot Unmarshal Cancel Request JSON data")
}
//hit the partial cancel transaction endpoint
kred.Env = ts.URL
cancelResult := kred.PartialCancel(&partialRequest)
if cancelResult.Error != nil {
t.Errorf("PartialCancel() returned an error: %s", cancelResult.Error)
}
})
t.Run("Test Kredivo Partial Cancel Transaction Error Empty Result", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/partial_cancel_transaction" {
t.Errorf("Expected request to ‘/partial_cancel_transaction, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`error`))
}))
// close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var partialRequest PartialCancelRequest
err := json.Unmarshal([]byte(`error`), &partialRequest)
if err == nil {
t.Error("Should Return Error Unmarshal Cancel Request JSON data")
}
//hit the partial cancel transaction endpoint
kred.Env = ts.URL
cancelResult := kred.PartialCancel(&partialRequest)
if cancelResult.Error == nil {
t.Errorf("PartialCancel() Should returned an error: %s", cancelResult.Error)
}
})
t.Run("Test Kredivo Get Transaction Status", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "POST" {
t.Errorf("Expected POST request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/transaction/status" {
t.Errorf("Expected request to ‘/transaction/status, got ‘%s’", r.URL.EscapedPath())
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{
"status": "OK",
"legal_name": "ADITYA LUKMAN AFANDI",
"fraud_status": "accept",
"order_id": "KD14721",
"transaction_time": 1501842660,
"external_userid": "SbmEaX1fdr",
"amount": "1515521.00",
"payment_type": "30_days",
"transaction_status": "cancel",
"message": "Cancelled the transaction!",
"transaction_id": "6febc2b2-ac4f-462c-9e7e-56fc5da05d91"
}`))
}))
// close server
defer ts.Close()
//construct kredivo
kred := New("123456", "bhinneka.com/kredivo/notif", "bhinneka.com", 8*time.Second)
var transactionRequest TransactionStatusRequest
err := json.Unmarshal(transactionStatusRequestData, &transactionRequest)
if err != nil {
t.Error("Cannot Unmarshal Transaction Status Request JSON data")
}
//hit the transaction status endpoint
kred.Env = ts.URL
transactionStatusResult := kred.TransactionStatus(&transactionRequest)
if transactionStatusResult.Error != nil {
t.Errorf("TransactionStatus() returned an error: %s", transactionStatusResult.Error)
}
})
}