-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclient.go
executable file
·321 lines (255 loc) · 10.2 KB
/
client.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
package amazonpay
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// GetProfile get user profile
func (amazonPay *AmazonPay) GetProfile(token string) (profile Profile, err error) {
fmt.Println(amazonPay.OAuthEndpoint + "/user/profile")
req, _ := http.NewRequest("GET", amazonPay.OAuthEndpoint+"/user/profile", nil)
req.Header.Add("Authorization", "bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err == nil {
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
respErr := OAuthResponseError{}
if err = json.Unmarshal(contents, &respErr); err == nil {
if respErr.Error != "" {
return profile, errors.New(respErr.ErrorDescription)
}
}
if err == nil {
err = json.Unmarshal(contents, &profile)
}
return profile, err
}
return profile, err
}
// SetOrderReferenceDetails set order details such as order amount and explanation in Order Reference
func (amazonPay *AmazonPay) SetOrderReferenceDetails(orderReferenceID string, attrs OrderReferenceAttributes) (result SetOrderReferenceDetailsResult, err error) {
var params = Params{
"Action": "SetOrderReferenceDetails",
"AmazonOrderReferenceId": orderReferenceID,
}
err = updateParams(¶ms, "OrderReferenceAttributes", attrs)
if err == nil {
err = amazonPay.Post(params, &result)
}
return result, err
}
// ConfirmOrderReference confirm order details
func (amazonPay *AmazonPay) ConfirmOrderReference(orderReferenceID string) error {
var params = Params{
"Action": "ConfirmOrderReference",
"AmazonOrderReferenceId": orderReferenceID,
}
return amazonPay.Post(params, nil)
}
// GetOrderReferenceDetails Returns the details and current state of the Order Reference object.
func (amazonPay *AmazonPay) GetOrderReferenceDetails(orderReferenceID string, addressToken string) (result GetOrderReferenceDetailsResponse, err error) {
var params = Params{
"Action": "GetOrderReferenceDetails",
"AmazonOrderReferenceId": orderReferenceID,
"AccessToken": addressToken,
}
err = amazonPay.Post(params, &result)
return result, err
}
// AuthorizeInput authorize input struct
type AuthorizeInput struct {
SellerAuthorizationNote string
TransactionTimeout uint
CaptureNow bool
SoftDescriptor string
}
// Authorize process secures the funds specified for the payment method stored in the Order Reference.
func (amazonPay *AmazonPay) Authorize(orderReferenceID string, authorizationReferenceID string, amount Price, input AuthorizeInput) (result AuthorizeResponse, err error) {
var params = Params{
"Action": "Authorize",
"AmazonOrderReferenceId": orderReferenceID,
"AuthorizationReferenceId": authorizationReferenceID,
}
updateParams(¶ms, "AuthorizationAmount", amount)
updateParams(¶ms, "", input)
err = amazonPay.Post(params, &result)
return result, err
}
// GetAuthorizationDetails returns the total authorized amount for authorization status and authorization.
func (amazonPay *AmazonPay) GetAuthorizationDetails(authorizationID string) (result GetAuthorizationDetailsResponse, err error) {
var params = Params{
"Action": "GetAuthorizationDetails",
"AmazonAuthorizationId": authorizationID,
}
err = amazonPay.Post(params, &result)
return result, err
}
// CloseAuthorization Close authorization
func (amazonPay *AmazonPay) CloseAuthorization(authorizationID string, closureReason string) error {
var params = Params{
"Action": "CloseAuthorization",
"AmazonAuthorizationId": authorizationID,
"ClosureReason": closureReason,
}
return amazonPay.Post(params, nil)
}
// CaptureInput capture input struct
type CaptureInput struct {
SellerCaptureNote string
SoftDecriptor string
}
// Capture request funds from the authorized payment method.
func (amazonPay *AmazonPay) Capture(authorizationID string, captureReferenceID string, captureAmount Price, input CaptureInput) (result CaptureResponse, err error) {
var params = Params{
"Action": "Capture",
"AmazonAuthorizationId": authorizationID,
"CaptureReferenceId": captureReferenceID,
}
updateParams(¶ms, "CaptureAmount", captureAmount)
updateParams(¶ms, "", input)
err = amazonPay.Post(params, &result)
return result, err
}
// GetCaptureDetails returns the detailed sales request status and the total amount refunded by sales request.
func (amazonPay *AmazonPay) GetCaptureDetails(captureID string) (result GetCaptureDetailsResponse, err error) {
var params = Params{
"Action": "GetCaptureDetails",
"AmazonCaptureId": captureID,
}
err = amazonPay.Post(params, &result)
return result, err
}
// CloseOrderReference complete order reference and will not be able to generate a new authorization from this Order Reference.
func (amazonPay *AmazonPay) CloseOrderReference(orderReferenceID string, closureReason string) error {
var params = Params{
"Action": "CloseOrderReference",
"AmazonOrderReferenceId": orderReferenceID,
"ClosureReason": closureReason,
}
return amazonPay.Post(params, nil)
}
// CancelOrderReference Cancels a previously confirmed order reference.
func (amazonPay *AmazonPay) CancelOrderReference(orderReferenceID string, reason string) error {
var params = Params{
"Action": "CancelOrderReference",
"AmazonOrderReferenceId": orderReferenceID,
"CancelationReason": reason,
}
return amazonPay.Post(params, nil)
}
// RefundInput refund input struct
type RefundInput struct {
SellerRefundNote string
SoftDescriptor string
}
// Refund refund the funds requested
func (amazonPay *AmazonPay) Refund(captureID string, refundReferenceID string, refundAmount Price, input RefundInput) (result RefundResponse, err error) {
var params = Params{
"Action": "Refund",
"AmazonCaptureId": captureID,
"RefundReferenceId": refundReferenceID,
}
updateParams(¶ms, "RefundAmount", refundAmount)
updateParams(¶ms, "", input)
err = amazonPay.Post(params, &result)
return result, err
}
// GetRefundDetails get refund details
func (amazonPay *AmazonPay) GetRefundDetails(refundID string) (result GetRefundDetailsResponse, err error) {
var params = Params{
"Action": "GetRefundDetails",
"AmazonRefundId": refundID,
}
err = amazonPay.Post(params, &result)
return result, err
}
// AuthorizeOnBillingAgreementInput authorize on billing agreement input struct
type AuthorizeOnBillingAgreementInput struct {
SellerAuthorizationNote string
TransactionTimeout uint
CaptureNow bool
SoftDecriptor string
SellerNote string
PlatformID string `json:"PlatformId"`
SellerOrderAttributes SellerOrderAttributes
InheritShippingAddress bool
}
// AuthorizeOnBillingAgreement process secures the funds specified for the payment method stored in the Billing Agreement.
func (amazonPay *AmazonPay) AuthorizeOnBillingAgreement(billingAgreementID string, authorizationReferenceID string, amount Price, input AuthorizeOnBillingAgreementInput) (result AuthorizeOnBillingAgreementResponse, err error) {
var params = Params{
"Action": "AuthorizeOnBillingAgreement",
"AmazonBillingAgreementId": billingAgreementID,
"AuthorizationReferenceId": authorizationReferenceID,
}
updateParams(¶ms, "AuthorizationAmount", amount)
updateParams(¶ms, "", input)
err = amazonPay.Post(params, &result)
return result, err
}
// CloseBillingAgreement Close billing agreement
func (amazonPay *AmazonPay) CloseBillingAgreement(billingAgreementID string, closureReason string) error {
var params = Params{
"Action": "CloseBillingAgreement",
"AmazonBillingAgreementId": billingAgreementID,
"ClosureReason": closureReason,
}
return amazonPay.Post(params, nil)
}
// ConfirmBillingAgreement confirm billing agreement
func (amazonPay *AmazonPay) ConfirmBillingAgreement(billingAgreementID string) error {
var params = Params{
"Action": "ConfirmBillingAgreement",
"AmazonBillingAgreementId": billingAgreementID,
}
return amazonPay.Post(params, nil)
}
// CreateOrderReferenceForIdInput create order reference for id input struct
type CreateOrderReferenceForIdInput struct {
InheritShippingAddress bool
ConfirmNow bool
OrderReferenceAttributes OrderReferenceAttributes
}
// CreateOrderReferenceForId creates an order reference.
func (amazonPay *AmazonPay) CreateOrderReferenceForId(id string, idType string, input CreateOrderReferenceForIdInput) (result CreateOrderReferenceForIdResponse, err error) {
var params = Params{
"Action": "CreateOrderReferenceForId",
"Id": id,
"IdType": idType,
}
updateParams(¶ms, "", input)
err = amazonPay.Post(params, &result)
return result, err
}
// GetBillingAgreementDetails returns the details and current state of the Billing Agreement object.
func (amazonPay *AmazonPay) GetBillingAgreementDetails(billingAgreementID string, addressConsentToken string) (result GetBillingAgreementDetailsResponse, err error) {
var params = Params{
"Action": "GetBillingAgreementDetails",
"AmazonBillingAgreementId": billingAgreementID,
"AddressConsentToken": addressConsentToken,
}
err = amazonPay.Post(params, &result)
return result, err
}
// SetBillingAgreementDetails set billing agreement such as a description of the agreement and other information about the merchant.
func (amazonPay *AmazonPay) SetBillingAgreementDetails(billingAgreementID string, attrs BillingAgreementAttributes) (result SetBillingAgreementDetailsResponse, err error) {
var params = Params{
"Action": "SetBillingAgreementDetails",
"AmazonBillingAgreementId": billingAgreementID,
}
err = updateParams(¶ms, "BillingAgreementAttributes", attrs)
if err == nil {
err = amazonPay.Post(params, &result)
}
return result, err
}
// ValidateBillingAgreement validates the status of the BillingAgreement object and the payment method associated with it.
func (amazonPay *AmazonPay) ValidateBillingAgreement(billingAgreementID string) (result ValidateBillingAgreementResponse, err error) {
var params = Params{
"Action": "ValidateBillingAgreement",
"AmazonBillingAgreementId": billingAgreementID,
}
err = amazonPay.Post(params, &result)
return result, err
}