-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment_test.go
196 lines (178 loc) · 6.06 KB
/
payment_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
package veritrans
import (
"fmt"
"log"
"os"
"testing"
"github.com/joho/godotenv"
assert "github.com/stretchr/testify/require"
)
var paymentService *PaymentService
func init() {
if err := godotenv.Load(); err != nil {
for _, envItem := range EnvVariables {
if os.Getenv(envItem) == "" {
log.Fatal("No env file for testing")
}
}
}
config := ConnectionConfig{
MerchantCCID: os.Getenv("MERCHANT_CCID"),
MerchantPassword: os.Getenv("MERCHANT_PASSWORD"),
AccountAPIURL: os.Getenv("ACCOUNT_API_URL"),
PaymentAPIURL: os.Getenv("PAYMENT_API_URL"),
SearchAPIURL: os.Getenv("SEARCH_API_URL"),
TxnVersion: os.Getenv("TXN_VERSION"),
DummyRequest: os.Getenv("DUMMY_REQUEST"),
}
paymentService, _ = NewPaymentService(config)
accountService = NewAccountService(config)
}
func TestPayment(t *testing.T) {
testAccountID := "PAYMENT_ACCOUNT_01"
accountParam := &AccountParam{
AccountID: testAccountID,
}
// Create Account
account, err := accountService.GetAccount(accountParam)
if err == nil {
// Assert if the account exists
assert.Equal(t, testAccountID, account.AccountID)
} else {
assert.Equal(t, "未登録の会員です。", err.Error())
account, err := accountService.CreateAccount(accountParam)
// Create if the account doesn't exist
assert.Nil(t, err)
assert.Equal(t, testAccountID, account.AccountID)
}
account, err = accountService.GetCard(accountParam)
assert.Nil(t, err)
if len(account.CardInfo) == 0 {
// Add Card
firstTestCardNumber := "4111111111111111"
expiredAt := GetAfterOneYear()
accountParam.CardParam = &CardParam{
CardNumber: firstTestCardNumber,
CardExpire: expiredAt,
DefaultCard: "1",
}
_, err = accountService.CreateCard(accountParam)
assert.Nil(t, err)
}
// Authorize without capture
testOrderID, err := findNewOrderID()
assert.Nil(t, err)
fmt.Printf("Found New Order ID: %s\n", testOrderID)
payAmount := "100"
authorizeParam := Params{
OrderID: testOrderID,
Amount: payAmount,
JPO: "10",
WithCapture: "false",
PayNowIDParam: &PayNowIDParam{
AccountParam: &AccountParam{
AccountID: testAccountID,
},
},
}
_, err = paymentService.Authorize(&authorizeParam, PaymentServiceType(PayCard))
assert.Nil(t, err)
searchParam := Params{
ContainDummyFlag: "1",
ServiceTypeCd: []string{"card"},
NewerFlag: "true",
SearchParam: &SearchParam{
Common: OrderParam{
OrderID: testOrderID,
},
},
}
result, err := paymentService.Search(&searchParam, PaymentServiceType(Search))
assert.Nil(t, err)
assert.Equal(t, "success", result.MStatus)
assert.NotNil(t, result.OrderInfos)
assert.GreaterOrEqual(t, len(result.OrderInfos.OrderInfo), 1)
firstOrderInfo := result.OrderInfos.OrderInfo[0]
assert.Equal(t, firstOrderInfo.AccountID, testAccountID)
assert.Equal(t, firstOrderInfo.LastSuccessTxnType, "Authorize")
assert.NotNil(t, firstOrderInfo.TransactionInfos)
assert.Equal(t, len(firstOrderInfo.TransactionInfos.TransactionInfo), 1)
transactionInfo := firstOrderInfo.TransactionInfos.TransactionInfo[0]
assert.Equal(t, transactionInfo.Amount, payAmount)
assert.Equal(t, transactionInfo.Command, "Authorize")
assert.Equal(t, transactionInfo.MStatus, "success")
assert.Equal(t, transactionInfo.ProperInfo.ReqWithCapture, "false")
fmt.Println("Authorize Without Capture Passed")
// Cancel
cancelParam := Params{
OrderID: testOrderID,
Amount: payAmount,
}
_, err = paymentService.Cancel(&cancelParam, PaymentServiceType(PayCard))
assert.Nil(t, err)
result, err = paymentService.Search(&searchParam, PaymentServiceType(Search))
assert.Nil(t, err)
assert.Equal(t, "success", result.MStatus)
assert.NotNil(t, result.OrderInfos)
assert.GreaterOrEqual(t, len(result.OrderInfos.OrderInfo), 1)
firstOrderInfo = result.OrderInfos.OrderInfo[0]
assert.Equal(t, firstOrderInfo.AccountID, testAccountID)
assert.Equal(t, firstOrderInfo.LastSuccessTxnType, "Cancel")
assert.NotNil(t, firstOrderInfo.TransactionInfos)
assert.Equal(t, len(firstOrderInfo.TransactionInfos.TransactionInfo), 1)
transactionInfo = firstOrderInfo.TransactionInfos.TransactionInfo[0]
assert.Equal(t, transactionInfo.Amount, payAmount)
assert.Equal(t, transactionInfo.Command, "Cancel")
assert.Equal(t, transactionInfo.MStatus, "success")
fmt.Println("Cancel Passed")
// Authorize with capture
testOrderID, err = findNewOrderID()
assert.Nil(t, err)
fmt.Printf("Found New Order ID: %s\n", testOrderID)
authorizeParam.WithCapture = "true"
authorizeParam.OrderID = testOrderID
_, err = paymentService.Authorize(&authorizeParam, PaymentServiceType(PayCard))
assert.Nil(t, err)
searchParam.SearchParam.Common.OrderID = testOrderID
result, err = paymentService.Search(&searchParam, PaymentServiceType(Search))
assert.Nil(t, err)
assert.Equal(t, "success", result.MStatus)
assert.NotNil(t, result.OrderInfos)
assert.GreaterOrEqual(t, len(result.OrderInfos.OrderInfo), 1)
firstOrderInfo = result.OrderInfos.OrderInfo[0]
assert.Equal(t, firstOrderInfo.AccountID, testAccountID)
assert.Equal(t, firstOrderInfo.LastSuccessTxnType, "Authorize")
assert.NotNil(t, firstOrderInfo.TransactionInfos)
assert.Equal(t, len(firstOrderInfo.TransactionInfos.TransactionInfo), 1)
transactionInfo = firstOrderInfo.TransactionInfos.TransactionInfo[0]
assert.Equal(t, transactionInfo.Amount, payAmount)
assert.Equal(t, transactionInfo.Command, "Authorize")
assert.Equal(t, transactionInfo.MStatus, "success")
assert.Equal(t, transactionInfo.ProperInfo.ReqWithCapture, "true")
fmt.Println("Authorize With Capture Passed")
}
func findNewOrderID() (string, error) {
testOrderID := ""
for {
randomID := GetRandomID(8)
testOrderID = fmt.Sprintf("PAYMENT_TEST_ORDER_%d", randomID)
searchParam := Params{
ContainDummyFlag: "1",
ServiceTypeCd: []string{"card"},
NewerFlag: "true",
SearchParam: &SearchParam{
Common: OrderParam{
OrderID: testOrderID,
},
},
}
result, err := paymentService.Search(&searchParam, PaymentServiceType(Search))
if err != nil {
return "", err
}
if len(result.OrderInfos.OrderInfo) == 0 {
break
}
}
return testOrderID, nil
}