Skip to content

Commit 9430bb3

Browse files
committed
add feature Create Pay Account with customer's wallet/account (gopay)
1 parent 3c32c03 commit 9430bb3

File tree

8 files changed

+399
-5
lines changed

8 files changed

+399
-5
lines changed

gateway/midtrans/card_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,33 @@ func TestCreateCardChargeWithContext_Success(t *testing.T) {
395395
assert.Equal(t, &expectedResult, result)
396396
}
397397

398+
func TestCreateCardCharge_ErrorRequest(t *testing.T) {
399+
// mockApiRequest interface
400+
// for mocking Call API
401+
mockApiRequest := mocks.NewApiRequestInterface(t)
402+
403+
// mock request
404+
mockUrl := getMockUrlSandBox()
405+
mockParams := getMockParamsCardChargeBytes()
406+
expectedResult := midtrans.ChargeResponse{}
407+
408+
// doing mock call
409+
mockApiRequest.
410+
On("Call", mock.Anything, http.MethodPost, mockUrl, mock.Anything, mockParams, &expectedResult).Return(errors.New("error request"))
411+
412+
// mock options
413+
mockOptions := getMockOptionsSandBox()
414+
mockOptions.ApiCall = mockApiRequest
415+
416+
opts, _ := pg.NewOption(mockOptions)
417+
418+
cp := getMockParamsCardCharge()
419+
result, err := midtrans.CreateCardCharge(cp, opts)
420+
421+
assert.NotNil(t, err, "error should not to be nil")
422+
assert.Nil(t, result, "result should be nil")
423+
}
424+
398425
func TestCreateCardCharge_ErrorValidationTransactionDetailsIsNil(t *testing.T) {
399426
// mock options
400427
mockOptions := getMockOptionsSandBox()
@@ -464,3 +491,18 @@ func TestCreateCardCharge_ErrorValidationParamsTokenIdIsNil(t *testing.T) {
464491
assert.NotNil(t, err, "error should not to be nil")
465492
assert.Nil(t, result, "result should be nil")
466493
}
494+
495+
func TestCreateCardChargeWithContext_ErrorValidationParamsTokenIdIsNil(t *testing.T) {
496+
ctx := context.Background()
497+
// mock options
498+
mockOptions := getMockOptionsSandBox()
499+
500+
opts, _ := pg.NewOption(mockOptions)
501+
502+
cp := getMockParamsCardCharge()
503+
cp.CreditCard.TokenID = ""
504+
result, err := midtrans.CreateCardChargeWithContext(ctx, cp, opts)
505+
506+
assert.NotNil(t, err, "error should not to be nil")
507+
assert.Nil(t, result, "result should be nil")
508+
}

gateway/midtrans/link_account.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package midtrans
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"time"
7+
8+
pg "github.com/pandudpn/go-payment-gateway"
9+
"github.com/pandudpn/go-payment-gateway/utils"
10+
)
11+
12+
// CreateLinkPayAccount is to link the customer's account for payments
13+
func CreateLinkPayAccount(lap *LinkAccountPay, opts *pg.Options) (*LinkAccountPayResponse, error) {
14+
m, err := createChargeMidtrans(lap, opts)
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
20+
defer cancel()
21+
22+
return m.createLinkPayAccount(ctx)
23+
}
24+
25+
// CreateLinkPayAccountWithContext is to link the customer's account for payments
26+
func CreateLinkPayAccountWithContext(ctx context.Context, lap *LinkAccountPay, opts *pg.Options) (*LinkAccountPayResponse, error) {
27+
m, err := createChargeMidtrans(lap, opts)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
return m.createLinkPayAccount(ctx)
33+
}
34+
35+
func (m *midtrans) createLinkPayAccount(ctx context.Context) (*LinkAccountPayResponse, error) {
36+
var (
37+
res LinkAccountPayResponse
38+
err error
39+
header = make(http.Header)
40+
)
41+
42+
// set authorization basic header
43+
header.Set("Authorization", utils.SetBasicAuthorization(m.opts.ServerKey, ""))
44+
45+
m.uri += createPayAccountUri
46+
err = m.opts.ApiCall.Call(ctx, http.MethodPost, m.uri, header, m.params, &res)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
return &res, nil
52+
}

gateway/midtrans/link_account_test.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package midtrans_test
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
"testing"
8+
9+
pg "github.com/pandudpn/go-payment-gateway"
10+
"github.com/pandudpn/go-payment-gateway/gateway/midtrans"
11+
"github.com/pandudpn/go-payment-gateway/mocks"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/mock"
14+
)
15+
16+
func TestCreateLinkPayAccount_SuccessSandBox(t *testing.T) {
17+
// mockApiRequest interface
18+
// for mocking Call API
19+
mockApiRequest := mocks.NewApiRequestInterface(t)
20+
21+
// mock request
22+
mockParamLinkAccountPay := getMockParamsLinkAccountPayBytes()
23+
mockUrl := getMockUrlCreatePayAccountSandBox()
24+
mockHeader := getMockHeaderSandBox()
25+
expectedResult := midtrans.LinkAccountPayResponse{}
26+
27+
// doing mock call
28+
mockApiRequest.
29+
On("Call", mock.Anything, http.MethodPost, mockUrl, mockHeader, mockParamLinkAccountPay, &expectedResult).Return(nil)
30+
31+
// mock options
32+
mockOptions := getMockOptionsSandBox()
33+
mockOptions.ApiCall = mockApiRequest
34+
35+
opts, _ := pg.NewOption(mockOptions)
36+
37+
lap := getMockParamsLinkAccountPay()
38+
result, err := midtrans.CreateLinkPayAccount(lap, opts)
39+
40+
assert.Nil(t, err, "error should be nil")
41+
assert.Equal(t, &expectedResult, result)
42+
}
43+
44+
func TestCreateLinkPayAccountWithContext_SuccessSandBox(t *testing.T) {
45+
ctx := context.Background()
46+
// mockApiRequest interface
47+
// for mocking Call API
48+
mockApiRequest := mocks.NewApiRequestInterface(t)
49+
50+
// mock request
51+
mockParamLinkAccountPay := getMockParamsLinkAccountPayBytes()
52+
mockUrl := getMockUrlCreatePayAccountSandBox()
53+
mockHeader := getMockHeaderSandBox()
54+
expectedResult := midtrans.LinkAccountPayResponse{}
55+
56+
// doing mock call
57+
mockApiRequest.
58+
On("Call", mock.Anything, http.MethodPost, mockUrl, mockHeader, mockParamLinkAccountPay, &expectedResult).Return(nil)
59+
60+
// mock options
61+
mockOptions := getMockOptionsSandBox()
62+
mockOptions.ApiCall = mockApiRequest
63+
64+
opts, _ := pg.NewOption(mockOptions)
65+
66+
lap := getMockParamsLinkAccountPay()
67+
result, err := midtrans.CreateLinkPayAccountWithContext(ctx, lap, opts)
68+
69+
assert.Nil(t, err, "error should be nil")
70+
assert.Equal(t, &expectedResult, result)
71+
}
72+
73+
func TestCreateLinkPayAccount_ErrorRequest(t *testing.T) {
74+
// mockApiRequest interface
75+
// for mocking Call API
76+
mockApiRequest := mocks.NewApiRequestInterface(t)
77+
78+
// mock request
79+
mockParamLinkAccountPay := getMockParamsLinkAccountPayBytes()
80+
mockUrl := getMockUrlCreatePayAccountSandBox()
81+
mockHeader := getMockHeaderSandBox()
82+
expectedResult := midtrans.LinkAccountPayResponse{}
83+
84+
// doing mock call
85+
mockApiRequest.
86+
On("Call", mock.Anything, http.MethodPost, mockUrl, mockHeader, mockParamLinkAccountPay, &expectedResult).Return(errors.New("error request"))
87+
88+
// mock options
89+
mockOptions := getMockOptionsSandBox()
90+
mockOptions.ApiCall = mockApiRequest
91+
92+
opts, _ := pg.NewOption(mockOptions)
93+
94+
lap := getMockParamsLinkAccountPay()
95+
result, err := midtrans.CreateLinkPayAccount(lap, opts)
96+
97+
assert.NotNil(t, err, "error should not to be nil")
98+
assert.Nil(t, result, "result should be nil")
99+
}
100+
101+
func TestCreateLinkPayAccount_DefaultPaymentType(t *testing.T) {
102+
// mockApiRequest interface
103+
// for mocking Call API
104+
mockApiRequest := mocks.NewApiRequestInterface(t)
105+
106+
// mock request
107+
mockParamLinkAccountPay := getMockParamsLinkAccountPayBytes()
108+
mockUrl := getMockUrlCreatePayAccountSandBox()
109+
mockHeader := getMockHeaderSandBox()
110+
expectedResult := midtrans.LinkAccountPayResponse{}
111+
112+
// doing mock call
113+
mockApiRequest.
114+
On("Call", mock.Anything, http.MethodPost, mockUrl, mockHeader, mockParamLinkAccountPay, &expectedResult).Return(nil)
115+
116+
// mock options
117+
mockOptions := getMockOptionsSandBox()
118+
mockOptions.ApiCall = mockApiRequest
119+
120+
opts, _ := pg.NewOption(mockOptions)
121+
122+
lap := getMockParamsLinkAccountPay()
123+
lap.PaymentType = ""
124+
result, err := midtrans.CreateLinkPayAccount(lap, opts)
125+
126+
assert.Nil(t, err, "error should be nil")
127+
assert.Equal(t, &expectedResult, result)
128+
}
129+
130+
func TestCreateLinkPayAccount_ErrorValidationPaymentTypeNotMatch(t *testing.T) {
131+
mockOptions := getMockOptionsSandBox()
132+
133+
opts, _ := pg.NewOption(mockOptions)
134+
135+
lap := getMockParamsLinkAccountPay()
136+
lap.PaymentType = midtrans.PaymentTypeBCA
137+
result, err := midtrans.CreateLinkPayAccount(lap, opts)
138+
139+
assert.NotNil(t, err, "error should not to be nil")
140+
assert.Nil(t, result, "result should be nil")
141+
}
142+
143+
func TestCreateLinkPayAccountWithContext_ErrorValidationPaymentTypeNotMatch(t *testing.T) {
144+
ctx := context.Background()
145+
mockOptions := getMockOptionsSandBox()
146+
147+
opts, _ := pg.NewOption(mockOptions)
148+
149+
lap := getMockParamsLinkAccountPay()
150+
lap.PaymentType = midtrans.PaymentTypeBCA
151+
result, err := midtrans.CreateLinkPayAccountWithContext(ctx, lap, opts)
152+
153+
assert.NotNil(t, err, "error should not to be nil")
154+
assert.Nil(t, result, "result should be nil")
155+
}
156+
157+
func TestCreateLinkPayAccount_ErrorValidationParamsGopayPartnerIsNil(t *testing.T) {
158+
mockOptions := getMockOptionsSandBox()
159+
160+
opts, _ := pg.NewOption(mockOptions)
161+
162+
lap := getMockParamsLinkAccountPay()
163+
lap.GopayPartner = nil
164+
result, err := midtrans.CreateLinkPayAccount(lap, opts)
165+
166+
assert.NotNil(t, err, "error should not to be nil")
167+
assert.Nil(t, result, "result should be nil")
168+
}

gateway/midtrans/midtrans.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const (
2424

2525
// uri for register CardToken
2626
createRegisterCardUri = "/v2/card/register"
27+
28+
// uri for createPayAccount
29+
createPayAccountUri = "/v2/pay/account"
2730
)
2831

2932
type midtrans struct {

gateway/midtrans/midtrans_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func getMockUrlRegisterCardProduction() string {
4242
return "https://api.midtrans.com/v2/card/register"
4343
}
4444

45+
func getMockUrlCreatePayAccountSandBox() string {
46+
return "https://api.sandbox.midtrans.com/v2/pay/account"
47+
}
48+
49+
func getMockUrlCreatePayAccountProduction() string {
50+
return "https://api.midtrans.com/v2/pay/account"
51+
}
52+
4553
func getMockOptionsSandBox() *pg.Options {
4654
return &pg.Options{
4755
Environment: pg.SandBox,
@@ -98,6 +106,39 @@ func getMockParamsEWalletBytes() []byte {
98106
return b
99107
}
100108

109+
func getMockParamsLinkAccountPay() *midtrans.LinkAccountPay {
110+
return &midtrans.LinkAccountPay{
111+
PaymentType: midtrans.PaymentTypeGopay,
112+
GopayPartner: &midtrans.GopayPartner{
113+
PhoneNumber: "81212345678",
114+
CountryCode: "62",
115+
RedirectURL: "https://www.your-app.com",
116+
},
117+
}
118+
}
119+
120+
func getMockParamsLinkAccountPayBytes() []byte {
121+
b, _ := json.Marshal(getMockParamsLinkAccountPay())
122+
123+
return b
124+
}
125+
126+
func getMockLinkAccountPayResponse() *midtrans.LinkAccountPayResponse {
127+
return &midtrans.LinkAccountPayResponse{
128+
PaymentType: midtrans.PaymentTypeGopay,
129+
StatusCode: "201",
130+
AccountID: "account-id-testing",
131+
Actions: []*midtrans.Action{
132+
{
133+
Name: "activation-deeplink",
134+
Method: "GET",
135+
URL: "https://api.sandbox.midtrans.com/v2/pay/account/gpar_account-id-testing/link",
136+
},
137+
},
138+
AccountStatus: "PENDING",
139+
}
140+
}
141+
101142
func getMockChargeResponseEWallet() *midtrans.ChargeResponse {
102143
return &midtrans.ChargeResponse{
103144
ID: "id-test",

0 commit comments

Comments
 (0)