-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpayment.go
224 lines (187 loc) · 6.97 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
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
package cryptomus
import (
"encoding/json"
"errors"
"time"
)
const (
createInvoiceEndpoit = "/payment"
generateInvoiceQRCodeEndpoint = "/payment/qr"
paymentInfoEndpoint = "/payment/info"
paymentHistoryEndpoint = "/payment/list"
paymentServicesListEndpoint = "/payment/services"
)
type InvoiceRequest struct {
Amount string `json:"amount"`
Currency string `json:"currency"`
OrderId string `json:"order_id"`
*InvoiceRequestOptions
}
type InvoiceRequestOptions struct {
Network string `json:"network,omitempty"`
UrlReturn string `json:"url_return,omitempty"`
UrlSuccess string `json:"url_success,omitempty"`
UrlCallback string `json:"url_callback,omitempty"`
IsPaymentMultiple bool `json:"is_payment_multiple,omitempty"`
Lifetime uint16 `json:"lifetime,omitempty"`
ToCurrency string `json:"to_currency,omitempty"`
Subtract uint8 `json:"subtract,omitempty"`
AccuarcyPaymentPercent uint8 `json:"accuarcy_payment_percent,omitempty"`
AdditionalData string `json:"additional_data,omitempty"`
Currencies []Currency `json:"currencies,omitempty"`
ExceptCurrencies []Currency `json:"except_currencies,omitempty"`
CourseSource string `json:"course_source,omitempty"`
FromReferralCode string `json:"from_referral_code,omitempty"`
DiscountPercent int8 `json:"discount_percent,omitempty"`
IsRefresh bool `json:"is_refresh,omitempty"`
}
type Currency struct {
Currency string `json:"currency"`
Network string `json:"network,omitempty"`
}
type Payment struct {
UUID string `json:"uuid"`
OrderId string `json:"order_id"`
Amount string `json:"amount"`
PaymentAmount string `json:"payment_amount,omitempty"`
PaymentAmountUSD string `json:"payment_amount_usd,omitempty"`
PayerAmount string `json:"payer_amount,omitempty"`
PayerAmountExchangeRate string `json:"payer_amount_exchange_rate,omitempty"`
DiscountPercent int8 `json:"discount_percent,omitempty"`
Discount string `json:"discount,omitempty"`
PayerCurrency string `json:"payer_currency,omitempty"`
Currency string `json:"currency"`
MerchantAmount string `json:"merchant_amount,omitempty"`
Network string `json:"network,omitempty"`
Address string `json:"address,omitempty"`
From string `json:"from,omitempty"`
TxId string `json:"txid,omitempty"`
PaymentStatus string `json:"payment_status"`
Status string `json:"status,omitempty"`
Url string `json:"url"`
ExpiredAt float64 `json:"expired_at"`
IsFinal bool `json:"is_final"`
AdditionalData string `json:"additional_data,omitempty"`
Comments string `json:"comments,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type invoiceRawResponse struct {
Result *Payment
State int8
}
type paymentQRCodeRawResponse struct {
Result struct {
Image string `json:"image"`
} `json:"result"`
State int8 `json:"state"`
}
type PaymentInfoRequest struct {
PaymentUUID string `json:"uuid,omitempty"`
OrderId string `json:"order_id,omitempty"`
}
type PaymentHistoryResponse struct {
Payments []*Payment
Paginate *PaymentHistoryPaginate
}
type PaymentHistoryPaginate struct {
Count int16 `json:"count"`
HasPages bool `json:"hasPages"`
NextCursor string `json:"nextCursor,omitempty"`
PreviousCursor string `json:"previousCursor,omitempty"`
PerPage int16 `json:"perPage"`
}
type paymentHistoryRawResponse struct {
State int8 `json:"state"`
Result []*Payment `json:"result"`
Paginate *PaymentHistoryPaginate `json:"paginate"`
}
type PaymentService struct {
Network string `json:"network"`
Currency string `json:"currency"`
IsAvailable bool `json:"isAvailable"`
Limit *PaymentServiceLimit `json:"limit"`
Commision *PaymentServiceCommision `json:"commision"`
}
type PaymentServiceLimit struct {
MinAmount string `json:"minAmount"`
MaxAmount string `json:"maxAmount"`
}
type PaymentServiceCommision struct {
FeeAmount string `json:"feeAmount"`
Percent string `json:"percent"`
}
type paymentServiceListRawResponse struct {
Result []*PaymentService `json:"result"`
State int8 `json:"state"`
}
func (c *Cryptomus) CreateInvoice(invoiceReq *InvoiceRequest) (*Payment, error) {
res, err := c.fetch("POST", createInvoiceEndpoit, invoiceReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
response := &invoiceRawResponse{}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return nil, err
}
return response.Result, nil
}
func (c *Cryptomus) GeneratePaymentQRCode(paymentUUID string) (string, error) {
payload := map[string]any{"merchant_payment_uuid": paymentUUID}
res, err := c.fetch("POST", generateInvoiceQRCodeEndpoint, payload)
if err != nil {
return "", err
}
defer res.Body.Close()
response := &paymentQRCodeRawResponse{}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return "", err
}
return response.Result.Image, nil
}
func (c *Cryptomus) GetPaymentInfo(paymentInfoReq *PaymentInfoRequest) (*Payment, error) {
if paymentInfoReq.PaymentUUID == "" || paymentInfoReq.OrderId == "" {
return nil, errors.New("you should pass one of required values [PaymentUUID, OrderId]")
}
res, err := c.fetch("POST", paymentInfoEndpoint, paymentInfoReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
response := &invoiceRawResponse{}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return nil, err
}
return response.Result, nil
}
func (c *Cryptomus) GetPaymentHistory(dateFrom, dateTo time.Time) (*PaymentHistoryResponse, error) {
payload := map[string]any{"date_from": dateFrom, "date_to": dateTo}
res, err := c.fetch("POST", paymentHistoryEndpoint, payload)
if err != nil {
return nil, err
}
defer res.Body.Close()
response := &paymentHistoryRawResponse{}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return nil, err
}
paymentHistory := &PaymentHistoryResponse{
Payments: response.Result,
Paginate: response.Paginate,
}
return paymentHistory, nil
}
func (c *Cryptomus) GetPaymentServicesList() ([]*PaymentService, error) {
payload := make(map[string]any)
res, err := c.fetch("POST", paymentServicesListEndpoint, payload)
if err != nil {
return nil, err
}
defer res.Body.Close()
response := &paymentServiceListRawResponse{}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return nil, err
}
return response.Result, nil
}