-
Notifications
You must be signed in to change notification settings - Fork 3
/
charge.py
97 lines (72 loc) · 2.45 KB
/
charge.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
import requests
import datetime
import configparser
charge_config = configparser.ConfigParser()
charge_config.read('config.ini')
charge_url = charge_config['charge']['url']
charge_token = charge_config['charge']['token']
def getinfo():
node_info_request = requests.get(charge_url + '/info', auth=('api-token', charge_token))
if node_info_request.status_code == 200:
node_info = node_info_request.json()
try:
#invoice_id = invoice_info['payment_hash']
# print('sms id: ' + invoice_id)
# print('info: \n' + str(invoice_info))
pass
except KeyError as e:
print('key error')
return node_info
else:
return False
def invoice(msat=None, amount=0, cur='EUR', desc=False):
dtime = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
if not desc:
desc=dtime+'-invoice-without-description'
crypto = {
'msatoshi': msat,
'description': desc,
'expiry': 600
}
fiat = {
'amount': amount,
'currency': cur,
'description': desc,
'expiry': 600
}
if amount == 0:
payload = crypto
else:
payload = fiat
invoice_info_request = requests.post(charge_url + '/invoice', auth=('api-token', charge_token), data=payload)
if invoice_info_request.status_code == 201:
invoice_info = invoice_info_request.json()
return invoice_info
else:
print('error: ' + str(invoice_info_request.status_code))
return False
def get_invoice(id=None):
if id:
data = requests.get(charge_url + '/invoice/'+id, auth=('api-token', charge_token))
else:
data = requests.get(charge_url + '/invoices', auth=('api-token', charge_token))
if data.status_code == 200:
invoice_info = data.json()
return invoice_info
else:
return False
def register_webhook(invoice_id, callback_url):
payload = {
'url': callback_url
}
webhook_info_request = requests.post(charge_url + '/invoice/' + invoice_id + '/webhook',
auth=('api-token', charge_token), data=payload)
if webhook_info_request.status_code == 201:
return True
elif webhook_info_request.status_code == 405:
print('already paid')
return False
elif webhook_info_request.status_code == 410:
print('invoice expired')
return False
return False