-
Notifications
You must be signed in to change notification settings - Fork 0
/
qiwi.go
204 lines (175 loc) · 5.29 KB
/
qiwi.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
/*****************************************************************************
*
* PROJECT: qiwi-bill-payments-go-sdk
* LICENSE: See LICENSE in the top level directory
* FILE: qiwi.go
* DESCRIPTION: Main file
* COPYRIGHT: (c) 2021 RINWARES <rinwares.com>
* AUTHOR: Rinat Namazov <rinat.namazov@rinwares.com>
*
*****************************************************************************/
package qiwi
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
const (
clientName = "go_sdk"
clientVersion = "1.1.0"
)
// The API base URL.
const (
billsURL = "https://api.qiwi.com/partner/bill/v1/bills/"
paymentURL = "https://oplata.qiwi.com/create"
)
// The API dateTime format.
const dateTimeFormat = "2006-01-02T15:04:05.999999-07:00"
// Invoice Payment Statuses
const (
WAITING = "WAITING" // Invoice issued awaiting for payment.
PAID = "PAID" // Invoice paid.
REJECTED = "REJECTED" // Invoice rejected by customer.
EXPIRED = "EXPIRED" // Invoice expired. Invoice not paid.
)
// The refund status enum.
const (
PARTIAL = "PARTIAL" // The partial refund of the invoice amount.
FULL = "FULL" // The full refund of the invoice amount.
)
// HTTPClient is HTTP client.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
// QiwiBillPaymentsAPI for rest v3.
type QiwiBillPaymentsAPI struct {
httpClient HTTPClient
secretKey string
}
// NewQiwiBillPaymentsAPI creates a new QiwiBillPaymentsAPI instance.
func NewQiwiBillPaymentsAPI(secretKey string) *QiwiBillPaymentsAPI {
return NewQiwiBillPaymentsAPIWithClient(secretKey, &http.Client{})
}
// NewQiwiBillPaymentsAPIWithClient creates a new QiwiBillPaymentsAPI instance
// and allows you to pass a http.Client.
func NewQiwiBillPaymentsAPIWithClient(secretKey string, httpClient HTTPClient) *QiwiBillPaymentsAPI {
return &QiwiBillPaymentsAPI{
httpClient: httpClient,
secretKey: "Bearer " + secretKey,
}
}
// makeRequest makes a request.
func (m *QiwiBillPaymentsAPI) makeRequest(url, method string, body interface{}, resp interface{}) error {
var data []byte
if body != nil {
var err error
data, err = json.Marshal(body)
if err != nil {
return err
}
}
req, err := http.NewRequest(method, billsURL+url, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", m.secretKey)
r, err := m.httpClient.Do(req)
if err != nil {
return err
}
defer r.Body.Close()
respData, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if r.StatusCode != 200 {
var errRes ErrorResponse
err = json.Unmarshal(respData, &errRes)
if err != nil {
return err
}
return errRes
} else if resp != nil {
err = json.Unmarshal(respData, &resp)
if err != nil {
return err
}
}
return nil
}
// SetSecretKey sets new secret key.
func (m *QiwiBillPaymentsAPI) SetSecretKey(secretKey string) {
m.secretKey = "Bearer " + secretKey
}
// CreatePaymentForm creating checkout link.
func (m *QiwiBillPaymentsAPI) CreatePaymentForm(paymentInfo PaymentInfo) string {
var v url.Values
v.Add("amount", paymentInfo.Amount.Value)
v.Add("publicKey", paymentInfo.PublicKey)
v.Add("billId", paymentInfo.BillId)
v.Add("successUrl", paymentInfo.SuccessUrl)
v.Add("customFields[apiClient]", clientName)
v.Add("customFields[apiClientVersion]", clientVersion)
if paymentInfo.ThemeCode != "" {
v.Add("customFields[themeCode]", paymentInfo.ThemeCode)
}
return paymentURL + "?" + v.Encode()
}
// GetBillInfo getting bill info.
func (m *QiwiBillPaymentsAPI) GetBillInfo(billId string) (*BillResponse, error) {
var resp BillResponse
err := m.makeRequest(billId, "GET", nil, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// CreateBill creating bill.
func (m *QiwiBillPaymentsAPI) CreateBill(billInfo CreateBillInfo) (*BillResponse, error) {
billRequest := billInfo.GetCreateBillRequest()
billRequest.CustomFields.ApiClient = clientName
billRequest.CustomFields.ApiClientVersion = clientVersion
var resp BillResponse
err := m.makeRequest(billInfo.BillId, "PUT", billRequest, &resp)
if err != nil {
return nil, err
}
if billInfo.SuccessUrl != "" {
resp.PayUrl = resp.PayUrl + "&successUrl=" + url.QueryEscape(billInfo.SuccessUrl)
}
return &resp, nil
}
// CancelBill cancelling unpaid bill.
func (m *QiwiBillPaymentsAPI) CancelBill(billId string) (*BillResponse, error) {
var resp BillResponse
err := m.makeRequest(billId+"/reject", "POST", nil, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// GetRefundInfo getting refund info.
// Method is not available for individuals.
func (m *QiwiBillPaymentsAPI) GetRefundInfo(billId, refundId string) (*RefundResponse, error) {
var resp RefundResponse
err := m.makeRequest(billId+"/refunds/"+refundId, "GET", nil, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// Refund paid bill.
// Method is not available for individuals.
func (m *QiwiBillPaymentsAPI) Refund(billId, refundId string, amount MoneyAmount) (*RefundResponse, error) {
req := RefundBillRequest{Amount: amount}
var resp RefundResponse
err := m.makeRequest(billId+"/refunds/"+refundId, "PUT", req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}