-
Notifications
You must be signed in to change notification settings - Fork 3
/
aio_request.go
410 lines (363 loc) · 15 KB
/
aio_request.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
package ecpay
import (
"github.com/Laysi/go-ecpay-sdk/base"
"github.com/go-errors/errors"
"strconv"
"strings"
"time"
)
type AioOrderRequest struct {
ecpayBase.AioCheckOutGeneralOption
*ecpayBase.AioCheckOutAtmOption
*ecpayBase.AioCheckOutCvsBarcodeOption
*ecpayBase.AioCheckOutCreditOption
*ecpayBase.AioCheckOutCreditOnetimeOption
*ecpayBase.AioCheckOutCreditInstallmentOption
*ecpayBase.AioCheckOutCreditPeriodOption
*ecpayBase.AioCheckOutInvoiceOption
}
type AioOrderRequestWithClient struct {
err error
request *AioOrderRequest
client Client
}
type AioOrderRequestCreditWithClient struct {
*AioOrderRequestWithClient
}
type AioOrderRequestAtmWithClient struct {
*AioOrderRequestWithClient
}
type AioOrderRequestCvsBarcodeWithClient struct {
*AioOrderRequestWithClient
}
type AioOrderRequestAllWithClient struct {
*AioOrderRequestWithClient
}
type AioOrderRequestInvoiceWithClient struct {
*AioOrderRequestWithClient
}
func (c Client) CreateOrder(tradeNo string, tradeDate time.Time, amount int, description string, itemNames []string) *AioOrderRequestWithClient {
description = strings.ReplaceAll(description, "\n", " ")
description = strings.ReplaceAll(description, "\r", " ")
r := &AioOrderRequestWithClient{
request: &AioOrderRequest{
AioCheckOutGeneralOption: ecpayBase.AioCheckOutGeneralOption{
MerchantID: c.merchantID,
EncryptType: ecpayBase.ENCRYPTTYPEENUM_SHA256,
MerchantTradeNo: tradeNo,
MerchantTradeDate: ecpayBase.ECPayDateTime(tradeDate),
PaymentType: ecpayBase.AIOCHECKPAYMENTTYPEENUM_AIO,
TotalAmount: amount,
TradeDesc: description,
ItemName: strings.Join(itemNames, "#"),
ReturnURL: c.returnURL,
ChoosePayment: "",
},
},
client: c,
}
r.request.AioCheckOutGeneralOption.OrderResultURL = r.client.orderResultURL
r.request.AioCheckOutGeneralOption.ClientBackURL = r.client.clientBackURL
return r
}
func (r *AioOrderRequestWithClient) WithOptional(optional AioCheckOutGeneralOptional) *AioOrderRequestWithClient {
if r.err != nil {
return r
}
r.request.AioCheckOutGeneralOption.ChooseSubPayment = (*ecpayBase.ChooseSubPaymentEnum)(PtrNilString(string(optional.ChooseSubPayment)))
r.request.AioCheckOutGeneralOption.CustomField1 = PtrNilString(optional.CustomField1)
r.request.AioCheckOutGeneralOption.CustomField2 = PtrNilString(optional.CustomField2)
r.request.AioCheckOutGeneralOption.CustomField3 = PtrNilString(optional.CustomField3)
r.request.AioCheckOutGeneralOption.CustomField4 = PtrNilString(optional.CustomField4)
r.request.AioCheckOutGeneralOption.ItemURL = PtrNilString(optional.ItemURL)
r.request.AioCheckOutGeneralOption.PlatformID = PtrNilString(optional.PlatformID)
r.request.AioCheckOutGeneralOption.Remark = PtrNilString(optional.Remark)
r.request.AioCheckOutGeneralOption.StoreID = PtrNilString(optional.StoreID)
r.request.AioCheckOutGeneralOption.Language = (*ecpayBase.LanguageEnum)(PtrNilString(string(optional.Language)))
r.request.AioCheckOutGeneralOption.NeedExtraPaidInfo = (*ecpayBase.NeedExtraPaidInfoEnum)(PtrNilString(string(optional.NeedExtraPaidInfo)))
return r
}
// Atm Payment
func (r *AioOrderRequestWithClient) SetAtmPayment() *AioOrderRequestAtmWithClient {
if r.err != nil {
return &AioOrderRequestAtmWithClient{r}
}
r.request.ChoosePayment = ecpayBase.CHOOSEPAYMENTENUM_ATM
r.request.AioCheckOutAtmOption = &ecpayBase.AioCheckOutAtmOption{}
r.request.AioCheckOutAtmOption.ClientRedirectURL = r.client.clientRedirectURL
r.request.AioCheckOutAtmOption.PaymentInfoURL = r.client.paymentInfoURL
return &AioOrderRequestAtmWithClient{r}
}
// expireDate: **允許繳費有效天數** 若需設定最長 60 天,最短 1 天。 未設定此參數則預設為 3 天 注意事項:以天為單位
func (r *AioOrderRequestAtmWithClient) WithAtmOptional(expireDate int) *AioOrderRequestAtmWithClient {
if r.err != nil {
return r
}
r.request.AioCheckOutAtmOption.ExpireDate = ecpayBase.PtrInt(expireDate)
return r
}
// WebATM Payment
func (r *AioOrderRequestWithClient) SetWebAtmPayment() *AioOrderRequestWithClient {
if r.err != nil {
return r
}
r.request.ChoosePayment = ecpayBase.CHOOSEPAYMENTENUM_WEB_ATM
return r
}
// Cvs Barcode Payment
func (r *AioOrderRequestWithClient) SetCvsPayment() *AioOrderRequestCvsBarcodeWithClient {
if r.err != nil {
return &AioOrderRequestCvsBarcodeWithClient{r}
}
r.request.ChoosePayment = ecpayBase.CHOOSEPAYMENTENUM_CVS
r.request.AioCheckOutCvsBarcodeOption = &ecpayBase.AioCheckOutCvsBarcodeOption{}
r.request.AioCheckOutCvsBarcodeOption.PaymentInfoURL = r.client.paymentInfoURL
r.request.AioCheckOutCvsBarcodeOption.ClientRedirectURL = r.client.clientRedirectURL
return &AioOrderRequestCvsBarcodeWithClient{r}
}
func (r *AioOrderRequestWithClient) SetBarcodePayment() *AioOrderRequestCvsBarcodeWithClient {
if r.err != nil {
return &AioOrderRequestCvsBarcodeWithClient{r}
}
r.request.ChoosePayment = ecpayBase.CHOOSEPAYMENTENUM_BARCODE
r.request.AioCheckOutCvsBarcodeOption = &ecpayBase.AioCheckOutCvsBarcodeOption{}
r.request.AioCheckOutCvsBarcodeOption.PaymentInfoURL = r.client.paymentInfoURL
r.request.AioCheckOutCvsBarcodeOption.ClientRedirectURL = r.client.clientRedirectURL
return &AioOrderRequestCvsBarcodeWithClient{r}
}
func (r *AioOrderRequestCvsBarcodeWithClient) WithCvsBarcodeOptional(option AioCheckOutCvsBarcodeOptional) *AioOrderRequestCvsBarcodeWithClient {
if r.err != nil {
return r
}
r.request.AioCheckOutCvsBarcodeOption.Desc1 = PtrNilString(option.Desc1)
r.request.AioCheckOutCvsBarcodeOption.Desc2 = PtrNilString(option.Desc2)
r.request.AioCheckOutCvsBarcodeOption.Desc3 = PtrNilString(option.Desc3)
r.request.AioCheckOutCvsBarcodeOption.Desc4 = PtrNilString(option.Desc4)
r.request.AioCheckOutCvsBarcodeOption.StoreExpireDate = PtrNilInt(option.StoreExpireDate)
return r
}
// Credit Payment
func (r *AioOrderRequestWithClient) SetCreditPayment() *AioOrderRequestCreditWithClient {
if r.err != nil {
return &AioOrderRequestCreditWithClient{r}
}
r.request.ChoosePayment = ecpayBase.CHOOSEPAYMENTENUM_CREDIT
r.request.AioCheckOutCreditOption = &ecpayBase.AioCheckOutCreditOption{}
return &AioOrderRequestCreditWithClient{r}
}
func (r *AioOrderRequestCreditWithClient) WithCreditOptional(option AioCheckOutCreditOptional) *AioOrderRequestCreditWithClient {
if r.err != nil {
return r
}
r.request.AioCheckOutCreditOption.BindingCard = (*ecpayBase.BindingCardEnum)(PtrNilInt(int(option.BindingCard)))
r.request.AioCheckOutCreditOption.MerchantMemberID = PtrNilString(option.MerchantMemberID)
return r
}
func (r *AioOrderRequestCreditWithClient) WithCreditOnetimeOptional(option AioCheckOutCreditOnetimeOptional) *AioOrderRequestCreditWithClient {
if r.err != nil {
return r
}
r.request.AioCheckOutCreditOnetimeOption = &ecpayBase.AioCheckOutCreditOnetimeOption{
Redeem: (*ecpayBase.RedeemEnum)(PtrNilString(string(option.Redeem))),
UnionPay: (*ecpayBase.UnionPayEnum)(PtrNilInt(int(option.UnionPay))),
}
return r
}
type IntSliceConverter []int
func (i IntSliceConverter) ToStringSlice() []string {
var result []string
for _, val := range i {
result = append(result, strconv.Itoa(val))
}
return result
}
// 信用卡分期可用參數為:3,6,12,18,24
func (r *AioOrderRequestCreditWithClient) WithCreditInstallmentOptional(installments ...int) *AioOrderRequestCreditWithClient {
if r.err != nil {
return r
}
r.request.AioCheckOutCreditInstallmentOption = &ecpayBase.AioCheckOutCreditInstallmentOption{
CreditInstallment: strings.Join(IntSliceConverter(installments).ToStringSlice(), ","),
}
return r
}
func (r *AioOrderRequestCreditWithClient) WithCreditPeriodOptional(periodType ecpayBase.CreditPeriodTypeEnum, frequency int, execTimes int) *AioOrderRequestCreditWithClient {
if r.err != nil {
return r
}
if r.client.periodReturnURL == nil {
r.err = errors.New("Missing configure of PeriodReturnURL to use Period Credit Payment Option")
return r
}
r.request.AioCheckOutCreditPeriodOption = &ecpayBase.AioCheckOutCreditPeriodOption{
PeriodAmount: r.request.TotalAmount,
PeriodType: periodType,
Frequency: frequency,
ExecTimes: execTimes,
PeriodReturnURL: r.client.periodReturnURL,
}
return r
}
// all payment
func (r *AioOrderRequestWithClient) SetAllPayment(ignorePayment ...ecpayBase.ChoosePaymentEnum) *AioOrderRequestAllWithClient {
if r.err != nil {
return &AioOrderRequestAllWithClient{r}
}
r = r.SetCreditPayment().
SetAtmPayment().
SetCvsPayment().
SetBarcodePayment().AioOrderRequestWithClient
r.request.ChoosePayment = ecpayBase.CHOOSEPAYMENTENUM_ALL
var ignorePaymentValues []string
for _, v := range ignorePayment {
ignorePaymentValues = append(ignorePaymentValues, string(v))
}
if len(ignorePaymentValues) != 0 {
r.request.IgnorePayment = ecpayBase.PtrString(strings.Join(ignorePaymentValues, "#"))
}
return &AioOrderRequestAllWithClient{r}
}
// all options
func (r *AioOrderRequestAllWithClient) WithCreditOptional(option AioCheckOutCreditOptional) *AioOrderRequestAllWithClient {
return &AioOrderRequestAllWithClient{
(&AioOrderRequestCreditWithClient{r.AioOrderRequestWithClient}).
WithCreditOptional(option).
AioOrderRequestWithClient,
}
}
func (r *AioOrderRequestAllWithClient) WithCreditOnetimeOptional(option AioCheckOutCreditOnetimeOptional) *AioOrderRequestAllWithClient {
return &AioOrderRequestAllWithClient{
(&AioOrderRequestCreditWithClient{r.AioOrderRequestWithClient}).
WithCreditOnetimeOptional(option).
AioOrderRequestWithClient,
}
}
func (r *AioOrderRequestAllWithClient) WithCreditInstallmentOptional(installments ...int) *AioOrderRequestAllWithClient {
return &AioOrderRequestAllWithClient{
(&AioOrderRequestCreditWithClient{r.AioOrderRequestWithClient}).
WithCreditInstallmentOptional(installments...).
AioOrderRequestWithClient,
}
}
func (r *AioOrderRequestAllWithClient) WithCreditPeriodOptional(periodType ecpayBase.CreditPeriodTypeEnum, frequency int, execTimes int) *AioOrderRequestAllWithClient {
return &AioOrderRequestAllWithClient{
(&AioOrderRequestCreditWithClient{r.AioOrderRequestWithClient}).
WithCreditPeriodOptional(periodType, frequency, execTimes).
AioOrderRequestWithClient,
}
}
func (r *AioOrderRequestAllWithClient) WithAtmOptional(expireDate int) *AioOrderRequestAllWithClient {
return &AioOrderRequestAllWithClient{
(&AioOrderRequestAtmWithClient{r.AioOrderRequestWithClient}).
WithAtmOptional(expireDate).
AioOrderRequestWithClient,
}
}
func (r *AioOrderRequestAllWithClient) WithCvsBarcodeOptional(option AioCheckOutCvsBarcodeOptional) *AioOrderRequestAllWithClient {
return &AioOrderRequestAllWithClient{
(&AioOrderRequestCvsBarcodeWithClient{r.AioOrderRequestWithClient}).
WithCvsBarcodeOptional(option).
AioOrderRequestWithClient,
}
}
// Invoice
type InvoiceItem struct {
// Name **商品名稱**
Name string
// Count **商品數量**
Count int
// Word **商品單位**
Word string
// Price **商品價格**
Price int
// TaxType **商品課稅別** 當課稅類別 [TaxType] = 9 (MIXED) 時,此欄位不可為空
TaxType *ecpayBase.TaxTypeEnum
}
func (r *AioOrderRequestWithClient) SetInvoice(relateNumber string, taxType ecpayBase.TaxTypeEnum, donation ecpayBase.InvoiceDonationEunm, print ecpayBase.InvoicePrintEnum, items []InvoiceItem, delay int, invType string) *AioOrderRequestInvoiceWithClient {
if r.err != nil {
return &AioOrderRequestInvoiceWithClient{r}
}
r.request.AioCheckOutGeneralOption.InvoiceMark = ecpayBase.INVOICEMARKENUM_Y.Ptr()
var itemsName []string
var itemsCount []string
var itemsWord []string
var itemsPrice []string
for _, item := range items {
itemsName = append(itemsName, item.Name)
itemsCount = append(itemsCount, strconv.Itoa(item.Count))
itemsWord = append(itemsWord, item.Word)
itemsPrice = append(itemsPrice, strconv.Itoa(item.Price))
}
r.request.AioCheckOutInvoiceOption = &ecpayBase.AioCheckOutInvoiceOption{
RelateNumber: relateNumber,
TaxType: taxType,
Donation: donation,
Print: print,
InvoiceItemName: FormUrlEncode(strings.Join(itemsName, "|")),
InvoiceItemCount: strings.Join(itemsCount, "|"),
InvoiceItemWord: FormUrlEncode(strings.Join(itemsWord, "|")),
InvoiceItemPrice: strings.Join(itemsPrice, "|"),
DelayDay: delay,
InvType: invType,
}
if taxType == ecpayBase.TAXTYPEENUM_MIXED {
var itemsTaxType []string
for _, item := range items {
if taxType == ecpayBase.TAXTYPEENUM_MIXED {
itemsTaxType = append(itemsTaxType, string(*item.TaxType))
}
}
r.request.AioCheckOutInvoiceOption.InvoiceItemTaxType = ecpayBase.PtrString(strings.Join(itemsTaxType, "|"))
}
return &AioOrderRequestInvoiceWithClient{r}
}
func (r *AioOrderRequestInvoiceWithClient) WithInvoiceOptional(option AioCheckOutInvoiceOptional) *AioOrderRequestInvoiceWithClient {
if r.err != nil {
return r
}
//if r.AioCheckOutInvoiceOption == nil{
// r.err = errors.New("wrong must use SetInvoice before WithInvoiceOptional to set required parameter properly")
// return r
//}
r.request.AioCheckOutInvoiceOption.CustomerID = PtrNilString(option.CustomerID)
r.request.AioCheckOutInvoiceOption.CustomerIdentifier = PtrNilString(option.CustomerIdentifier)
r.request.AioCheckOutInvoiceOption.CustomerName = PtrNilString(FormUrlEncode(option.CustomerName))
r.request.AioCheckOutInvoiceOption.CustomerAddr = PtrNilString(FormUrlEncode(option.CustomerAddr))
r.request.AioCheckOutInvoiceOption.CustomerPhone = PtrNilString(option.CustomerPhone)
r.request.AioCheckOutInvoiceOption.CustomerEmail = PtrNilString(FormUrlEncode(option.CustomerEmail))
r.request.AioCheckOutInvoiceOption.ClearanceMark = (*ecpayBase.ClearanceMarkEnum)(PtrNilString(string(option.ClearanceMark)))
r.request.AioCheckOutInvoiceOption.CarruerType = (*ecpayBase.CarruerTypeEnum)(PtrNilString(string(option.CarruerType)))
r.request.AioCheckOutInvoiceOption.CarruerNum = PtrNilString(option.CarruerNum)
r.request.AioCheckOutInvoiceOption.LoveCode = PtrNilString(option.LoveCode)
//r.AioCheckOutInvoiceOption.InvoiceItemTaxType = PtrNilString(option.InvoiceItemTaxType)
r.request.AioCheckOutInvoiceOption.InvoiceRemark = PtrNilString(FormUrlEncode(option.InvoiceRemark))
return r
}
func (r *AioOrderRequestWithClient) GenerateUrlQuery() (string, error) {
if r.err != nil {
return "", r.err
}
params := StructToParamsMap(r.request)
checkMac := r.client.GenerateCheckMacValue(params)
r.request.CheckMacValue = checkMac
params = StructToParamsMap(r.request)
return NewECPayValuesFromMap(params).Encode(), nil
}
func (r *AioOrderRequestWithClient) GenerateCheckMac() (string, error) {
if r.err != nil {
return "", r.err
}
params := StructToParamsMap(r.request)
checkMac := r.client.GenerateCheckMacValue(params)
return checkMac, nil
}
func (r *AioOrderRequestWithClient) GenerateRequestHtml() (string, error) {
if r.err != nil {
return "", r.err
}
params := StructToParamsMap(r.request)
checkMac := r.client.GenerateCheckMacValue(params)
r.request.CheckMacValue = checkMac
params = StructToParamsMap(r.request)
return r.client.GenerateAutoSubmitHtmlForm(params, r.client.GetCurrentServer()+r.client.aioCheckOutPath), nil
}