forked from ssantos21/cln-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswiss_pay_invoice.py
112 lines (81 loc) · 2.86 KB
/
swiss_pay_invoice.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
import json
from datetime import datetime
from collections import defaultdict
import requests
import random
import string
import utils
import total_fee_income
def random_string(length):
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
# amount_in_satoshis, description, email, redirect_url, webhook_url
def create_invoice(apiKey, title, description, amount_in_satoshis, email):
url = "https://api.swiss-bitcoin-pay.ch/checkout"
payload = json.dumps({
"title": title,
"description": description,
"amount": amount_in_satoshis,
"unit": "sat",
"onChain": False,
"delay": 10, # default value
"email": email,
"emailLanguage": "en",
# "redirect": True,
# "redirectAfterPaid": "https://example.com/order/3",
# "webhook": "https://example.com/webhook/3",
# "extra": {
# "customNote": "Custom note 3",
# "customDevice": "Device 3",
# "AnyValue": None
# }
"redirect": False,
"redirectAfterPaid": None,
"webhook": None,
"extra": None
})
headers = {
'Content-Type': 'application/json',
'api-key': apiKey
}
response = requests.request("POST", url, headers=headers, data=payload)
# print(response)
# print(response.text)
# print(response.json())
return response.json()
def pay_invoice(nodeRestUrl, rune, invoice):
rpc_method = "v1/pay"
url = "%s/%s" % (nodeRestUrl, rpc_method)
payload = { "bolt11": invoice }
headers = {
"content-type": "application/json",
"Rune": rune
}
response = requests.post(url, headers=headers, json=payload)
return response.json()
def execute(hours):
settings_data = utils.get_settings_data()
apiKey = settings_data['apiKey']
rune = settings_data['rune']
nodeRestUrl = settings_data['nodeRestUrl']
result = total_fee_income.execute(hours=hours)
title = "Order #3"
description = "Sample order description #3"
amount_in_satoshis = result['channel']['total']
email = settings_data['email']
print("Amount in satoshis: ", amount_in_satoshis)
result = create_invoice(apiKey, title, description, amount_in_satoshis, email)
invoice = result['pr']
return pay_invoice(nodeRestUrl, rune, invoice)
if __name__ == "__main__":
settings_data = utils.get_settings_data()
apiKey = settings_data['apiKey']
rune = settings_data['rune']
nodeRestUrl = settings_data['nodeRestUrl']
title = "Order #3"
description = "Sample order description #3"
amount_in_satoshis = 500000
email = settings_data['email']
# For testing purpose, if you run this file directly
result = create_invoice(apiKey, title, description, amount_in_satoshis, email)
invoice = result['pr']
pay_invoice(nodeRestUrl, rune, invoice)