generated from mythrnr/template-lib-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_payment.go
165 lines (138 loc) · 4.2 KB
/
api_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
package paypayopa
import (
"context"
"encoding/json"
"net/http"
"strconv"
"time"
)
type CreatePaymentPayload struct {
MerchantPaymentID string `json:"merchantPaymentId"`
UserAuthorizationID string `json:"userAuthorizationId"`
Amount *MoneyAmount `json:"amount"`
RequestedAt int64 `json:"requestedAt"`
StoreID string `json:"storeId"`
TerminalID string `json:"terminalId"`
OrderReceiptNumber string `json:"orderReceiptNumber"`
OrderDescription string `json:"orderDescription"`
OrderItems []*MerchantOrderItem `json:"orderItems"`
Metadata *json.RawMessage `json:"metadata"`
ProductType string `json:"productType"`
AgreeSimilarTransaction bool `json:"-"`
}
func createPayment(
ctx context.Context,
client *opaClient,
req *CreatePaymentPayload,
) (*Payment, *ResultInfo, error) {
const timeout = 30 * time.Second
res := &Payment{}
info, err := client.POST(
ctxWithTimeout(ctx, timeout),
"/v2/payments?agreeSimilarTransaction="+
strconv.FormatBool(req.AgreeSimilarTransaction),
res,
req,
)
if err != nil || !info.Success() {
return nil, info, err
}
return res, info, nil
}
func cancelPayment(
ctx context.Context,
client *opaClient,
merchantPaymentID string,
) (*ResultInfo, error) {
const timeout = 15 * time.Second
return client.DELETE(
ctxWithTimeout(ctx, timeout),
"/v2/payments/"+merchantPaymentID,
)
}
func getPaymentDetails(
ctx context.Context,
client *opaClient,
merchantPaymentID string,
) (*Payment, *ResultInfo, error) {
const timeout = 15 * time.Second
res := &Payment{}
info, err := client.GET(
ctxWithTimeout(ctx, timeout),
"/v2/payments/"+merchantPaymentID,
res,
)
if err != nil || !info.Success() {
return nil, info, err
}
return res, info, nil
}
type ConsultExpectedCashbackInfoPayload struct {
RequestID string `json:"requestId"`
MerchantPaymentID string `json:"merchantPaymentId"`
UserAuthorizationID string `json:"userAuthorizationId"`
Amount *MoneyAmount `json:"amount"`
RequestedAt int64 `json:"requestedAt"`
StoreID string `json:"storeId"`
OrderItems []*MerchantOrderItem `json:"orderItems"`
ProductType string `json:"productType"`
Lang Lang `json:"-"`
}
type CreateContinuousPaymentPayload struct {
MerchantPaymentID string `json:"merchantPaymentId"`
UserAuthorizationID string `json:"userAuthorizationId"`
Amount *MoneyAmount `json:"amount"`
RequestedAt int64 `json:"requestedAt"`
StoreID string `json:"storeId"`
TerminalID string `json:"terminalId"`
OrderReceiptNumber string `json:"orderReceiptNumber"`
OrderDescription string `json:"orderDescription"`
OrderItems []*MerchantOrderItem `json:"orderItems"`
Metadata *json.RawMessage `json:"metadata"`
}
func createContinuousPayment(
ctx context.Context,
client *opaClient,
req *CreateContinuousPaymentPayload,
) (*Payment, *ResultInfo, error) {
const timeout = 30 * time.Second
res := &Payment{}
info, err := client.POST(
ctxWithTimeout(ctx, timeout),
"/v1/subscription/payments",
res,
req,
)
if err != nil || !info.Success() {
return nil, info, err
}
return res, info, nil
}
type CashbackInfoResponse struct {
Campaignmessage string `json:"campaignMessage"`
}
func consultExpectedCashbackInfo(
ctx context.Context,
client *opaClient,
req *ConsultExpectedCashbackInfoPayload,
) (*CashbackInfoResponse, *ResultInfo, error) {
const timeout = 15 * time.Second
rq, err := client.Request(
ctxWithTimeout(ctx, timeout),
http.MethodPost,
"/v1/payments/cashback/expected",
req,
)
if err != nil {
return nil, nil, err
}
if req.Lang != "" {
rq.Header.Set(headerNameLang, string(req.Lang))
}
res := &CashbackInfoResponse{}
info, err := client.Do(rq, res)
if err != nil || !info.Success() {
return nil, info, err
}
return res, info, nil
}