forked from go-pay/gopay
/
payment.go
189 lines (180 loc) · 6.61 KB
/
payment.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
package paypal
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/SuperH-0630/gopay"
)
// 支付授权详情(Show details for authorized payment)
// Code = 0 is success
// 文档:https://developer.paypal.com/docs/api/payments/v2/#authorizations_get
func (c *Client) PaymentAuthorizeDetail(ctx context.Context, authorizationId string) (ppRsp *PaymentAuthorizeDetailRsp, err error) {
if authorizationId == gopay.NULL {
return nil, errors.New("authorization_id is empty")
}
url := fmt.Sprintf(paymentAuthorizeDetail, authorizationId)
res, bs, err := c.doPayPalGet(ctx, url)
if err != nil {
return nil, err
}
ppRsp = &PaymentAuthorizeDetailRsp{Code: Success}
ppRsp.Response = new(PaymentAuthorizeDetail)
if err = json.Unmarshal(bs, ppRsp.Response); err != nil {
return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs))
}
if res.StatusCode != http.StatusOK {
ppRsp.Code = res.StatusCode
ppRsp.Error = string(bs)
ppRsp.ErrorResponse = new(ErrorResponse)
_ = json.Unmarshal(bs, ppRsp.ErrorResponse)
}
return ppRsp, nil
}
// 重新授权支付授权(Reauthorize authorized payment)
// Note:This request is currently not supported for Partner use cases.
// 文档:https://developer.paypal.com/docs/api/payments/v2/#authorizations_reauthorize
func (c *Client) PaymentReauthorize(ctx context.Context, authorizationId string, bm gopay.BodyMap) (ppRsp *PaymentReauthorizeRsp, err error) {
if authorizationId == gopay.NULL {
return nil, errors.New("authorization_id is empty")
}
url := fmt.Sprintf(paymentReauthorize, authorizationId)
res, bs, err := c.doPayPalPost(ctx, bm, url)
if err != nil {
return nil, err
}
ppRsp = &PaymentReauthorizeRsp{Code: Success}
ppRsp.Response = new(PaymentAuthorizeDetail)
if err = json.Unmarshal(bs, ppRsp.Response); err != nil {
return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs))
}
if res.StatusCode != http.StatusCreated {
ppRsp.Code = res.StatusCode
ppRsp.Error = string(bs)
ppRsp.ErrorResponse = new(ErrorResponse)
_ = json.Unmarshal(bs, ppRsp.ErrorResponse)
}
return ppRsp, nil
}
// 作废支付授权(Void authorized payment)
// Code = 0 is success
// 文档:https://developer.paypal.com/docs/api/payments/v2/#authorizations_void
func (c *Client) PaymentAuthorizeVoid(ctx context.Context, authorizationId string) (ppRsp *EmptyRsp, err error) {
if authorizationId == gopay.NULL {
return nil, errors.New("authorization_id is empty")
}
url := fmt.Sprintf(paymentAuthorizeVoid, authorizationId)
res, bs, err := c.doPayPalPost(ctx, nil, url)
if err != nil {
return nil, err
}
ppRsp = &EmptyRsp{Code: Success}
if res.StatusCode != http.StatusNoContent {
ppRsp.Code = res.StatusCode
ppRsp.Error = string(bs)
ppRsp.ErrorResponse = new(ErrorResponse)
_ = json.Unmarshal(bs, ppRsp.ErrorResponse)
}
return ppRsp, nil
}
// 支付授权捕获(Capture authorized payment)
// Code = 0 is success
// 文档:https://developer.paypal.com/docs/api/payments/v2/#authorizations_capture
func (c *Client) PaymentAuthorizeCapture(ctx context.Context, authorizationId string, bm gopay.BodyMap) (ppRsp *PaymentAuthorizeCaptureRsp, err error) {
if authorizationId == gopay.NULL {
return nil, errors.New("authorization_id is empty")
}
url := fmt.Sprintf(paymentAuthorizeCapture, authorizationId)
res, bs, err := c.doPayPalPost(ctx, bm, url)
if err != nil {
return nil, err
}
ppRsp = &PaymentAuthorizeCaptureRsp{Code: Success}
ppRsp.Response = new(PaymentAuthorizeCapture)
if err = json.Unmarshal(bs, ppRsp.Response); err != nil {
return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs))
}
if res.StatusCode != http.StatusCreated {
ppRsp.Code = res.StatusCode
ppRsp.Error = string(bs)
ppRsp.ErrorResponse = new(ErrorResponse)
_ = json.Unmarshal(bs, ppRsp.ErrorResponse)
}
return ppRsp, nil
}
// 支付捕获详情(Show captured payment details)
// Code = 0 is success
// 文档:https://developer.paypal.com/docs/api/payments/v2/#captures_get
func (c *Client) PaymentCaptureDetail(ctx context.Context, captureId string) (ppRsp *PaymentCaptureDetailRsp, err error) {
if captureId == gopay.NULL {
return nil, errors.New("capture_id is empty")
}
url := fmt.Sprintf(paymentCaptureDetail, captureId)
res, bs, err := c.doPayPalGet(ctx, url)
if err != nil {
return nil, err
}
ppRsp = &PaymentCaptureDetailRsp{Code: Success}
ppRsp.Response = new(PaymentAuthorizeCapture)
if err = json.Unmarshal(bs, ppRsp.Response); err != nil {
return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs))
}
if res.StatusCode != http.StatusOK {
ppRsp.Code = res.StatusCode
ppRsp.Error = string(bs)
ppRsp.ErrorResponse = new(ErrorResponse)
_ = json.Unmarshal(bs, ppRsp.ErrorResponse)
}
return ppRsp, nil
}
// 支付捕获退款(Refund captured payment)
// Code = 0 is success
// 文档:https://developer.paypal.com/docs/api/payments/v2/#captures_refund
func (c *Client) PaymentCaptureRefund(ctx context.Context, captureId string, bm gopay.BodyMap) (ppRsp *PaymentCaptureRefundRsp, err error) {
if captureId == gopay.NULL {
return nil, errors.New("capture_id is empty")
}
url := fmt.Sprintf(paymentCaptureRefund, captureId)
res, bs, err := c.doPayPalPost(ctx, bm, url)
if err != nil {
return nil, err
}
ppRsp = &PaymentCaptureRefundRsp{Code: Success}
ppRsp.Response = new(PaymentCaptureRefund)
if err = json.Unmarshal(bs, ppRsp.Response); err != nil {
return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs))
}
if res.StatusCode != http.StatusCreated {
ppRsp.Code = res.StatusCode
ppRsp.Error = string(bs)
ppRsp.ErrorResponse = new(ErrorResponse)
_ = json.Unmarshal(bs, ppRsp.ErrorResponse)
}
return ppRsp, nil
}
// 支付退款详情(Show refund details)
// Code = 0 is success
// 文档:https://developer.paypal.com/docs/api/payments/v2/#refunds_get
func (c *Client) PaymentRefundDetail(ctx context.Context, refundId string) (ppRsp *PaymentRefundDetailRsp, err error) {
if refundId == gopay.NULL {
return nil, errors.New("refund_id is empty")
}
url := fmt.Sprintf(paymentRefundDetail, refundId)
res, bs, err := c.doPayPalGet(ctx, url)
if err != nil {
return nil, err
}
ppRsp = &PaymentRefundDetailRsp{Code: Success}
ppRsp.Response = new(PaymentCaptureRefund)
if err = json.Unmarshal(bs, ppRsp.Response); err != nil {
return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs))
}
if res.StatusCode != http.StatusOK {
ppRsp.Code = res.StatusCode
ppRsp.Error = string(bs)
ppRsp.ErrorResponse = new(ErrorResponse)
_ = json.Unmarshal(bs, ppRsp.ErrorResponse)
}
return ppRsp, nil
}