-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqiwi.go
170 lines (144 loc) · 4.36 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
package qiwi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
)
const (
MaxIdleConnections int = 20000
RequestTimeout int = 25
ApiUrl string = "https://edge.qiwi.com"
)
type QiwiPersonalApi struct {
httpClient *http.Client
apiKey string
}
func NewQiwiPersonalApiWithHttpClient(apiKey string, client *http.Client) *QiwiPersonalApi {
qiwi := new(QiwiPersonalApi)
qiwi.httpClient = client
qiwi.apiKey = apiKey
return qiwi
}
func NewQiwiPersonalApi(apiKey string) *QiwiPersonalApi {
client := &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: MaxIdleConnections,
},
Timeout: time.Duration(RequestTimeout) * time.Second,
}
return NewQiwiPersonalApiWithHttpClient(apiKey, client)
}
func (qiwi *QiwiPersonalApi) newRequest(apiKey, method, spath string, data map[string]interface{}) (req *http.Request, err error) {
var path = ApiUrl + spath
var body io.Reader
if len(data) > 0 {
s, err := json.Marshal(data)
if err != nil {
return nil, err
}
body = bytes.NewBuffer(s)
}
req, err = http.NewRequest(method, path, body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
return req, err
}
type QiwiTransError struct {
Code string
Message string
}
func (qiwi *QiwiPersonalApi) sendRequest(apiKey, method, spath string, data map[string]interface{}) (body []byte, err error) {
req, err := qiwi.newRequest(apiKey, method, spath, data)
response, err := qiwi.httpClient.Do(req)
if err != nil && response == nil {
return nil, err
} else {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
if response.Body != nil {
response.Body.Close()
}
return nil, err
}
if response.StatusCode == 400 {
var res QiwiTransError
err = json.Unmarshal(body, &res)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s : %s", res.Code, res.Message)
}
if response.StatusCode != 200 {
return nil, errors.New(response.Status)
}
if response.Body != nil {
response.Body.Close()
}
return body, nil
}
}
type Currency int
const (
CURRENCY_RUB Currency = 643
CURRENCY_USD Currency = 840
CURRENCY_EUR Currency = 978
)
type IdentificationLevel string
const (
ID_LEVEL_SIMPLE IdentificationLevel = "SIMPLE"
ID_LEVEL_VERIFIED IdentificationLevel = "VERIFIED"
ID_LEVEL_FULL IdentificationLevel = "FULL"
ID_LEVEL_ANONYMOUS IdentificationLevel = "ANONYMOUS"
)
type PaymentSource string
const (
PAY_SOURCE_QW_RUB PaymentSource = "QW_RUB"
PAY_SOURCE_QW_USD PaymentSource = "QW_USD"
PAY_SOURCE_QW_EUR PaymentSource = "QW_EUR"
PAY_SOURCE_CARD PaymentSource = "CARD"
PAY_SOURCE_MK PaymentSource = "MK"
)
type PaymentHistoryOperation string
const (
PH_OPERATION_ALL PaymentHistoryOperation = "ALL"
PH_OPERATION_IN PaymentHistoryOperation = "IN"
PH_OPERATION_OUT PaymentHistoryOperation = "OUT"
PH_OPERATION_QIWI_CARD PaymentHistoryOperation = "QIWI_CARD"
)
type PaymentStatus string
const (
PAY_STATUS_WAITING PaymentStatus = "WAITING"
PAY_STATUS_SUCCESS PaymentStatus = "SUCCESS"
PAY_STATUS_ERROR PaymentStatus = "ERROR"
)
/*
99 - Перевод на QIWI Wallet
1963 - Перевод на карту Visa (карты российских банков)
21013 - Перевод на карту MasterCard (карты российских банков)
Для карт, выпущенных банками стран Азербайджан, Армения, Белоруссия, Грузия, Казахстан, Киргизия, Молдавия, Таджикистан, Туркменистан, Украина, Узбекистан:
1960 – Перевод на карту Visa
21012 – Перевод на карту MasterCard
31652 - национальная платежная система МИР
466 - Тинькофф Банк
464 - Альфа-Банк
*/
type PaymentProvider string
const (
PROVIDER_QIWI_WALLET PaymentProvider = "99"
PROVIDER_VISA PaymentProvider = "1963"
PROVIDER_MASTERCARD PaymentProvider = "21013"
PROVIDER_VISA_CIS PaymentProvider = "1960"
PROVIDER_MASTERCARD_CIS PaymentProvider = "21012"
PROVIDER_MIR PaymentProvider = "31652"
PROVIDER_TINKOFF PaymentProvider = "466"
PROVIDER_ALFABANK PaymentProvider = "464"
)