-
Notifications
You must be signed in to change notification settings - Fork 1
/
pay_request.go
170 lines (147 loc) · 6.45 KB
/
pay_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
package linepay
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// `Amount` required, valid amount `form.amount != sum(packages[].amount) + sum(packages[].userFee) + shippingFee`
// `Currency` required, is ISO 4217, supported: USD, JPY, TWD, THB
// `OrderId` required
// if `Capture` true: only need to call `Confirm API` to process payments. false: call `Confirm API` and then `Capture API`
type PaymentsRequest struct {
Amount int `json:"amount"`
Currency string `json:"currency"`
OrderID string `json:"orderId"`
Packages []PaymentsPackageRequest `json:"packages"`
RedirectUrls PaymentsRedirectUrlsRequest `json:"redirectUrls"`
Options PaymentsOptionsRequest `json:"options"`
}
// `Id` required
// `Amount` required, valid amount `packages[].amount != sum(packages[].products[].quantity * packages[].products[].price)`
// `Name` required
type PaymentsPackageRequest struct {
ID string `json:"id"`
Amount int `json:"amount"`
UserFee int `json:"userFee,omitempty"`
Name string `json:"name"`
Products []PaymentsPackageProductRequest `json:"products"`
}
// `Name` required
// `Quantity` required
// `Price` required
type PaymentsPackageProductRequest struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
ImageURL string `json:"imageUrl,omitempty"`
Quantity int `json:"quantity"`
Price int `json:"price"`
OriginalPrice int `json:"originalPrice,omitempty"`
}
const (
PaymentsConfirmUrlTypeClient string = "CLIENT"
PaymentsConfirmUrlTypeServer string = "SERVER"
PaymentsConfirmUrlTypeNone string = "NONE"
)
// NOTE: for the behavior of `ConfirmUrl`, when `ConfirmUrlType` set to `PaymentsConfirmUrlTypeClient`,
// LINE server will send user to the `ConfirmUrl` with `transactionId`. `orderID` won't send for this case.
// the exception is, when user use *QR scanner* at the `waitPreLogin` page (login by LINE account or QR Code scan page)
// server will send user to `ConfirmUrl` with `orderID`
// `ConfirmUrl` required
// `CancelUrl` required
type PaymentsRedirectUrlsRequest struct {
AppPackageName string `json:"appPackageName,omitempty"`
ConfirmURL string `json:"confirmUrl"`
ConfirmURLType string `json:"confirmUrlType,omitempty"`
CancelURL string `json:"cancelUrl"`
}
type PaymentsOptionsRequest struct {
Payment PaymentsOptionsPaymentRequest `json:"payment"`
Display PaymentsOptionsDisplayRequest `json:"display"`
Shipping PaymentsOptionsShippingRequest `json:"shipping"`
FamilyService PaymentsOptionsFamilyServiceRequest `json:"familyService"`
Extra PaymentsOptionsExtraRequest `json:"extra"`
}
type PaymentsOptionsPaymentRequest struct {
Capture bool `json:"capture,omitempty"`
PayType string `json:"payType,omitempty"` // NORMAL, PREAPPROVED
}
type PaymentsOptionsDisplayRequest struct {
Locale string `json:"locale,omitempty"` // en, ja, ko, th, zh_TW, zh_CN
CheckConfirmURLBrowser bool `json:"checkConfirmUrlBrowser,omitempty"`
}
type PaymentsOptionsShippingRequest struct {
ShippintType string `json:"type,omitempty"` // NO_SHIPPING, FIXED_ADDRESS, SHIPPING
FeeAmount string `json:"feeAmount,omitempty"` //why string?
FeeInquiryURL string `json:"feeInquiryUrl,omitempty"`
FeeInquiryType string `json:"feeInquiryType,omitempty"` // CONDITION, FIXED
Address PaymentsOptionsShippingAddressRequest `json:"address"`
}
type PaymentsOptionsShippingAddressRequest struct {
Country string `json:"country,omitempty"`
PostalCode string `json:"postalCode,omitempty"`
State string `json:"state,omitempty"`
City string `json:"city,omitempty"`
Detail string `json:"detail,omitempty"`
Optional string `json:"optional,omitempty"`
Recipient PaymentsOptionsShippingAddressRecipientRequest `json:"recipient,omitempty"`
}
type PaymentsOptionsShippingAddressRecipientRequest struct {
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
FirstNameOptional string `json:"firstNameOptional,omitempty"`
LastNameOptional string `json:"lastNameOptional,omitempty"`
Email string `json:"email,omitempty"`
PhoneNo string `json:"phoneNo,omitempty"`
}
type PaymentsOptionsFamilyServiceRequest struct {
AddFriends []PaymentsOptionsFamilyServiceAddFriendsRequest `json:"addFriends"`
}
type PaymentsOptionsFamilyServiceAddFriendsRequest struct {
AddType string `json:"type,omitempty"` // line@
IDs []string `json:"ids,omitempty"`
}
type PaymentsOptionsExtraRequest struct {
BranchName string `json:"branchName,omitempty"`
BranchID string `json:"branchId,omitempty"`
}
// response
type PaymentsResponse struct {
ReturnCode string `json:"returnCode"`
ReturnMessage string `json:"returnMessage"`
Info PaymentsInfoResponse `json:"info"`
}
type PaymentsInfoResponse struct {
TransactionID int64 `json:"transactionId"`
PaymentAccessToken string `json:"paymentAccessToken"`
PaymentURL PaymentsInfoPaymentURLResponse `json:"paymentUrl"`
}
type PaymentsInfoPaymentURLResponse struct {
Web string `json:"web"`
App string `json:"app"`
}
func (client *Client) PaymentsRequest(ctx context.Context, request *PaymentsRequest) (response *PaymentsResponse, err error) {
body, err := json.Marshal(request)
res, err := client.post(ctx, endpointV3PaymentsRequest, body)
if err != nil {
err = fmt.Errorf("PaymentsRequest post error = %v", err.Error())
return
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
bodyBytes, ioerr := ioutil.ReadAll(res.Body)
if ioerr != nil {
err = fmt.Errorf("ReadAll read body failed: %s", ioerr.Error())
return
}
response = &PaymentsResponse{}
if err = json.Unmarshal(bodyBytes, response); err != nil {
return
}
} else {
err = fmt.Errorf("failed response, StatusCode: %d", res.StatusCode)
return
}
return
}