-
Notifications
You must be signed in to change notification settings - Fork 25
/
client_test.go
289 lines (248 loc) · 8.5 KB
/
client_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
package coreapi
import (
"encoding/json"
"github.com/midtrans/midtrans-go"
assert "github.com/stretchr/testify/require"
"testing"
"time"
)
const sandboxClientKey = "SB-Mid-client-yUgKb__vX_zH2TMN"
const sandboxServerKey = "SB-Mid-server-TvgWB_Y9s81-rbMBH7zZ8BHW"
const sampleCardNumber = "4811111111111114"
const bniCardNumber = "4105058689481467"
const bcaBinNumber = "45563300"
func timestamp() string {
return time.Now().UTC().Format("2006010215040105")
}
func getCardToken(cardNumber string) string {
year := time.Now().Year() + 1
midtrans.ClientKey = sandboxClientKey
res, _ := CardToken(cardNumber, 12, year, "123")
return res.TokenID
}
func createPayload(orderId string, paymentType CoreapiPaymentType, cardToken string) *ChargeReq {
if paymentType == PaymentTypeCreditCard {
return &ChargeReq{
PaymentType: paymentType,
TransactionDetails: midtrans.TransactionDetails{
OrderID: orderId,
GrossAmt: 10000,
},
CreditCard: &CreditCardDetails{
TokenID: cardToken,
},
}
}
return &ChargeReq{
PaymentType: paymentType,
TransactionDetails: midtrans.TransactionDetails{
OrderID: orderId,
GrossAmt: 10000,
},
}
}
func TestRegisterCard(t *testing.T) {
year := time.Now().Year() + 1
midtrans.ClientKey = sandboxClientKey
resp1, _ := RegisterCard(sampleCardNumber, 12, year)
assert.Equal(t, resp1.StatusCode, "200")
assert.Equal(t, resp1.MaskCard, "48111111-1114")
c := Client{}
c.New(sandboxServerKey, midtrans.Sandbox)
resp2, _ := c.RegisterCard(bniCardNumber, 12, year, sandboxClientKey)
assert.Equal(t, resp2.StatusCode, "200")
assert.Equal(t, resp2.MaskCard, "41050586-1467")
}
func TestCardToken(t *testing.T) {
year := time.Now().Year() + 1
midtrans.ClientKey = sandboxClientKey
resp1, _ := CardToken(sampleCardNumber, 12, year, "123")
assert.Equal(t, resp1.StatusCode, "200")
c := Client{}
c.New(sandboxServerKey, midtrans.Sandbox)
resp2, _ := c.CardToken(bniCardNumber, 12, year, "123", sandboxClientKey)
assert.Equal(t, resp2.StatusCode, "200")
}
func TestChargeTransactionWithMap(t *testing.T) {
req1 := &ChargeReqWithMap{
"payment_type": "gopay",
"transaction_details": map[string]interface{}{
"order_id": "MID-GO-UNIT_TEST-3" + timestamp(),
"gross_amount": 10000,
},
}
midtrans.ServerKey = sandboxServerKey
resp, _ := ChargeTransactionWithMap(req1)
assert.Equal(t, resp["status_code"], "201")
assert.Equal(t, resp["payment_type"], "gopay")
req2 := &ChargeReqWithMap{
"payment_type": PaymentTypeBankTransfer,
"transaction_details": map[string]interface{}{
"order_id": "MID-GO-UNIT_TEST-4" + timestamp(),
"gross_amount": 10000,
},
}
c := Client{}
c.New(sandboxServerKey, midtrans.Sandbox)
resp2, _ := c.ChargeTransactionWithMap(req2)
assert.Equal(t, resp2["status_code"], "201")
assert.Equal(t, resp2["payment_type"], "bank_transfer")
}
func TestChargeTransaction(t *testing.T) {
midtrans.ServerKey = sandboxServerKey
resp1, _ := ChargeTransaction(createPayload("MID-GO-UNIT_TEST-1"+timestamp(), PaymentTypeGopay, ""))
assert.Equal(t, resp1.StatusCode, "201")
assert.Equal(t, resp1.PaymentType, "gopay")
c := Client{}
c.New(sandboxServerKey, midtrans.Sandbox)
resp2, _ := c.ChargeTransaction(createPayload("MID-GO-UNIT_TEST-2"+timestamp(), PaymentTypeGopay, ""))
assert.Equal(t, resp2.StatusCode, "201")
assert.Equal(t, resp2.PaymentType, "gopay")
}
func TestChargeTransactionWithIdempotencyKey(t *testing.T) {
req := &ChargeReq{
PaymentType: PaymentTypeGopay,
TransactionDetails: midtrans.TransactionDetails{
OrderID: "MID-GO-UNIT_TEST-" + timestamp(),
GrossAmt: 10000,
},
}
c := Client{}
c.New(sandboxServerKey, midtrans.Sandbox)
c.Options.SetPaymentIdempotencyKey(timestamp())
resp1, _ := c.ChargeTransaction(req)
resp2, _ := c.ChargeTransaction(req)
assert.Equal(t, resp2, resp1)
}
func TestCardPointInquiry(t *testing.T) {
midtrans.ServerKey = sandboxServerKey
resp, _ := CardPointInquiry(getCardToken(bniCardNumber))
assert.Equal(t, resp.StatusCode, "200")
}
// Failure test case
func TestRegisterCardFailure(t *testing.T) {
midtrans.ClientKey = sandboxClientKey
resp1, _ := RegisterCard(sampleCardNumber, 12, 2020)
assert.Equal(t, resp1.StatusCode, "400")
assert.Equal(t, resp1.StatusMessage, "One or more parameters in the payload is invalid.")
c := Client{}
c.New(sandboxServerKey, midtrans.Sandbox)
resp2, _ := c.RegisterCard(bniCardNumber, 12, 2020, sandboxClientKey)
assert.Equal(t, resp2.StatusCode, "400")
assert.Equal(t, resp2.StatusMessage, "One or more parameters in the payload is invalid.")
}
func TestCardTokenFailure(t *testing.T) {
midtrans.ClientKey = sandboxClientKey
res, _ := CardToken(sampleCardNumber, 12, 2020, "123")
assert.Equal(t, res.StatusCode, "400")
assert.Equal(t, res.StatusMessage, "One or more parameters in the payload is invalid.")
c := Client{}
c.New(sandboxServerKey, midtrans.Sandbox)
resp2, _ := c.CardToken(bniCardNumber, 12, 2020, "123", sandboxClientKey)
assert.Equal(t, resp2.StatusCode, "400")
assert.Equal(t, resp2.StatusMessage, "One or more parameters in the payload is invalid.")
}
func TestChargeTransactionNilParam(t *testing.T) {
midtrans.ServerKey = sandboxServerKey
_, err := ChargeTransaction(nil)
assert.Equal(t, err.GetStatusCode(), 500)
assert.Contains(t, err.GetMessage(), "Midtrans API is returning API error.")
}
func TestChargeTransactionWithMapNilParam(t *testing.T) {
midtrans.ServerKey = sandboxServerKey
_, err := ChargeTransactionWithMap(nil)
assert.Equal(t, err.GetStatusCode(), 500)
assert.Contains(t, err.GetMessage(), "Midtrans API is returning API error.")
}
func TestChargeWrongServerKey(t *testing.T) {
midtrans.ServerKey = "DUMMY"
_, err := ChargeTransaction(&ChargeReq{})
assert.Equal(t, err.GetStatusCode(), 401)
c := Client{}
c.New("DUMMY", midtrans.Sandbox)
c.ChargeTransaction(&ChargeReq{})
assert.Equal(t, err.GetStatusCode(), 401)
}
func TestChargeTransactionWithQRISIncludesQRString(t *testing.T) {
midtrans.ServerKey = sandboxServerKey
resp1, _ := ChargeTransaction(createPayload("MID-GO-UNIT_TEST-1"+timestamp(), PaymentTypeQris, ""))
assert.Equal(t, resp1.StatusCode, "201")
assert.Equal(t, resp1.PaymentType, "qris")
assert.NotEmpty(t, resp1.QRString)
c := Client{}
c.New(sandboxServerKey, midtrans.Sandbox)
resp2, _ := c.ChargeTransaction(createPayload("MID-GO-UNIT_TEST-2"+timestamp(), PaymentTypeQris, ""))
assert.Equal(t, resp2.StatusCode, "201")
assert.Equal(t, resp2.PaymentType, "qris")
assert.NotEmpty(t, resp2.QRString)
}
func TestGetBIN(t *testing.T) {
midtrans.ClientKey = sandboxClientKey
resp, _ := GetBIN(bcaBinNumber)
assert.Equal(t, resp.Data.BankCode, "BCA")
assert.Equal(t, resp.Data.RegistrationRequired, false)
}
type mockHTTPClient struct {
// Define a field to hold the dummy response
dummyResponseJSON []byte
}
// Implement the GetBIN method of the Client interface for the mock client
func (m *mockHTTPClient) GetBIN(binNumber string) (*BinResponse, error) {
// Return the stored dummy response
var binResponse BinResponse
err := json.Unmarshal(m.dummyResponseJSON, &binResponse)
if err != nil {
return nil, err
}
return &binResponse, nil
}
func TestGetBINWithRegistrationRequiredIsNullFromResponse(t *testing.T) {
dummyResponseJSON := []byte(`{
"data": {
"registration_required": null,
"country_name": "INDONESIA",
"country_code": "ID",
"channel": "online_offline",
"brand": "VISA",
"bin_type": "CREDIT",
"bin_class": "GOLD",
"bin": "45563300",
"bank_code": "BCA",
"bank": "BANK CENTRAL ASIA"
}
}`)
// Create an instance of the mock HTTP client with the dummy response
mockClient := &mockHTTPClient{
dummyResponseJSON: dummyResponseJSON,
}
// Call the GetBIN function with the mock client
resp, err := mockClient.GetBIN(bcaBinNumber)
// Check if there's no error
assert.NoError(t, err)
// Check if the response matches the expected values
assert.Equal(t, resp.Data.BankCode, "BCA")
assert.Equal(t, resp.Data.RegistrationRequired, false)
}
func TestGetBINWithRegistrationRequiredIsTrueFromResponse(t *testing.T) {
dummyResponseJSON := []byte(`{
"data": {
"registration_required": true,
"country_name": "INDONESIA",
"country_code": "ID",
"channel": "online_offline",
"brand": "VISA",
"bin_type": "CREDIT",
"bin_class": "GOLD",
"bin": "45563300",
"bank_code": "BCA",
"bank": "BANK CENTRAL ASIA"
}
}`)
mockClient := &mockHTTPClient{
dummyResponseJSON: dummyResponseJSON,
}
resp, err := mockClient.GetBIN(bcaBinNumber)
assert.NoError(t, err)
assert.Equal(t, resp.Data.BankCode, "BCA")
assert.Equal(t, resp.Data.RegistrationRequired, true)
}