-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcoinpayment.py
275 lines (254 loc) · 10.4 KB
/
coinpayment.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import urllib.request
import urllib.parse
import urllib.error
import hmac
import hashlib
import json
from django.conf import settings, ImproperlyConfigured
import requests
import os
class CoinPayments():
def __init__(self, public_key, private_key, ipn_url=None):
self.url = 'https://www.coinpayments.net/api.php'
self.public_key = public_key
self.private_key = private_key
self.ipn_url = ipn_url
self.format = 'json'
self.version = 1
@classmethod
def get_instance(cls):
"""
Checks Django settings for api keys & IPN url and
returns and initialized instance of `CoinPayments`
"""
if not getattr(settings, 'COINPAYMENTS_API_KEY', None) or \
not getattr(settings, 'COINPAYMENTS_API_SECRET', None):
raise ImproperlyConfigured(
'COINPAYMENTS_API_KEY and COINPAYMENTS_API_SECRET are required!')
ipn_url = getattr(settings, 'COINPAYMENTS_IPN_URL', None)
if ipn_url:
if not getattr(settings, 'COINPAYMENTS_IPN_SECRET', None) or \
not getattr(settings, 'COINPAYMENTS_MERCHANT_ID', None):
raise ImproperlyConfigured('COINPAYMENTS_IPN_SECRET and '
'COINPAYMENTS_MERCHANT_ID are required if IPN is turned on!')
return CoinPayments(settings.COINPAYMENTS_API_KEY, settings.COINPAYMENTS_API_SECRET, ipn_url=ipn_url)
def create_hmac(self, **params):
"""
Generate an HMAC based upon the url arguments/parameters
We generate the encoded url here and return it to request because
the hmac on both sides depends upon the order of the parameters, any
change in the order and the hmacs wouldn't match
"""
encoded = urllib.parse.urlencode(params).encode('utf-8')
return encoded, hmac.new(bytearray(self.private_key, 'utf-8'), encoded, hashlib.sha512).hexdigest()
def request(self, request_method, **params):
"""
The basic request that all API calls use
the parameters are joined in the actual api methods so the parameter
strings can be passed and merged inside those methods instead of the
request method
"""
encoded, sig = self.create_hmac(**params)
headers = {'hmac': sig}
proxy_dict = {
"https": os.environ.get('FIXIE_URL', 'http://fixie:[email protected]:1080')
}
if request_method == 'get':
req = urllib.request.Request(self.url, headers=headers)
elif request_method == 'post':
headers['Content-Type'] = 'application/x-www-form-urlencoded'
req = urllib.request.Request(
self.url, data=encoded, headers=headers)
try:
response = urllib.request.urlopen(req)
# status_code = response.getcode()
response_body = response.read()
except urllib.error.HTTPError as error:
status_code = error.getcode()
response_body = error.read()
return json.loads(response_body)
def irequest(self, request_method, **params):
"""
The basic request that all API calls
"""
encoded, sig = self.create_hmac(**params)
headers = {'hmac': sig}
proxy_dict = {
'https': 'http://fixie:[email protected]:1080'
}
try:
if request_method == 'get':
reqs = requests.get(
self.url, headers=headers, proxies=proxy_dict, timeout=1000)
elif request_method == 'post':
headers['Content-Type'] = 'application/x-www-form-urlencoded'
reqs = requests.post(self.url, data=encoded,
headers=headers, proxies=proxy_dict, timeout=1000)
response_body = reqs.text
except (requests.exceptions.ConnectionError, requests.exceptions.ProxyError) as error:
# status_code = error.getcode()
response_body = error.read()
return json.loads(response_body)
def create_transaction(self, params=None):
"""
Creates a transaction to give to the purchaser
https://www.coinpayments.net/apidoc-create-transaction
"""
if params is None:
params = {}
if self.ipn_url:
params.update({'ipn_url': self.ipn_url})
params.update({'cmd': 'create_transaction',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def get_basic_info(self, params=None):
"""
Gets merchant info based on API key (callee)
https://www.coinpayments.net/apidoc-get-basic-info
"""
if params is None:
params = {}
params.update({'cmd': 'get_basic_info',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def rates(self, params=None):
"""
Gets current rates for currencies
https://www.coinpayments.net/apidoc-rates
"""
if params is None:
params = {}
params.update({'cmd': 'rates',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def balances(self, params=None):
"""
Get current wallet balances
https://www.coinpayments.net/apidoc-balances
"""
if params is None:
params = {}
params.update({'cmd': 'balances',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def get_deposit_address(self, params=None):
"""
Get address for personal deposit use
https://www.coinpayments.net/apidoc-get-deposit-address
"""
if params is None:
params = {}
params.update({'cmd': 'get_deposit_address',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def get_callback_address(self, params=None):
"""
Get a callback address to recieve info about address status
https://www.coinpayments.net/apidoc-get-callback-address
"""
if params is None:
params = {}
if self.ipn_url:
params.update({'ipn_url': self.ipn_url})
params.update({'cmd': 'get_callback_address',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def create_transfer(self, params=None):
"""
Not really sure why this function exists to be honest, it transfers
coins from your addresses to another account on coinpayments using
merchant ID
https://www.coinpayments.net/apidoc-create-transfer
"""
if params is None:
params = {}
params.update({'cmd': 'create_transfer',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def create_withdrawal(self, params=None):
"""
Withdraw or masswithdraw(NOT RECOMMENDED) coins to a specified address,
optionally set a IPN when complete.
https://www.coinpayments.net/apidoc-create-withdrawal
"""
if params is None:
params = {}
params.update({'cmd': 'create_withdrawal',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def convert_coins(self, params=None):
"""
Convert your balances from one currency to another
https://www.coinpayments.net/apidoc-convert
"""
if params is None:
params = {}
params.update({'cmd': 'convert',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def get_withdrawal_history(self, params=None):
"""
Get list of recent withdrawals (1-100max)
https://www.coinpayments.net/apidoc-get-withdrawal-history
"""
if params is None:
params = {}
params.update({'cmd': 'get_withdrawal_history',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def get_withdrawal_info(self, params=None):
"""
Get information about a specific withdrawal based on withdrawal ID
https://www.coinpayments.net/apidoc-get-withdrawal-info
"""
if params is None:
params = {}
params.update({'cmd': 'get_withdrawal_info',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def get_conversion_info(self, params=None):
"""
Get information about a specific withdrawal based on withdrawal ID
https://www.coinpayments.net/apidoc-get-conversion-info
"""
if params is None:
params = {}
params.update({'cmd': 'get_conversion_info',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)
def get_tx_info_multi(self, params=None):
"""
Get tx info (up to 25 ids separated by | )
https://www.coinpayments.net/apidoc-get-tx-info
"""
if params is None:
params = {}
params.update({'cmd': 'get_tx_info_multi',
'key': self.public_key,
'version': self.version,
'format': self.format})
return self.irequest('post', **params)