-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpayment_checkout.go
88 lines (81 loc) · 2.07 KB
/
payment_checkout.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
package rm
import (
"context"
"errors"
)
// CreatePaymentCheckoutRequest :
type CreatePaymentCheckoutRequest struct {
Order struct {
ID string `json:"id"`
Title string `json:"title"`
Detail string `json:"detail"`
AdditionalData string `json:"additionalData"`
Amount uint `json:"amount"`
Currency string `json:"currencyType"`
} `json:"order"`
Customer struct {
UserID string `json:"userId"`
Email string `json:"email,omitempty"`
CountryCode string `json:"countryCode,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
} `json:"customer"`
Type PaymentType `json:"type"`
Method []PaymentMethod `json:"method"`
StoreID string `json:"storeId"`
RedirectURL string `json:"redirectUrl"`
NotifyURL string `json:"notifyUrl"`
LayoutVersion layout `json:"layoutVersion"`
}
// CreatePaymentCheckoutResponse :
type CreatePaymentCheckoutResponse struct {
Item struct {
CheckoutID string `json:"checkoutId"`
URL string `json:"url"`
} `json:"item"`
Code string `json:"code"`
}
// CreatePaymentCheckout :
func (c *Client) CreatePaymentCheckout(
ctx context.Context,
req CreatePaymentCheckoutRequest,
) (*CreatePaymentCheckoutResponse, error) {
req.LayoutVersion = LayoutV3
if req.Method == nil {
req.Method = make([]PaymentMethod, 0)
}
if req.Type == "" {
req.Type = PaymentTypeWeb
}
if req.Order.Currency == "" {
req.Order.Currency = "MYR"
}
if req.StoreID == "" {
if c.storeID != "" {
req.StoreID = c.storeID
} else {
// prevent concurrency race
c.mu.Lock()
defer c.mu.Lock()
res, err := c.GetStores(ctx)
if err != nil {
return nil, err
}
if len(res.Items) == 0 {
return nil, errors.New("rm: no available store to init payment")
}
req.StoreID = res.Items[0].ID
}
}
resp := new(CreatePaymentCheckoutResponse)
if err := c.do(
ctx,
"create_payment_checkout",
"post",
c.openEndpoint+"/v3/payment/online",
req,
resp,
); err != nil {
return nil, err
}
return resp, nil
}