71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
import requests, os, json, qrcode
|
|
from configparser import ConfigParser
|
|
from requests.structures import CaseInsensitiveDict
|
|
|
|
invoice = ConfigParser(allow_no_value=True)
|
|
|
|
config = ConfigParser(allow_no_value=True)
|
|
config.read('configs/config.beer')
|
|
LNBITS_URL = config.get('LNBITS', 'URL')
|
|
LNBITS_APIKEY= config.get('LNBITS', 'API_KEY')
|
|
INTERVAL=config.get('LNBITS', 'CHECK_INTERVAL') #Check for Payment every n Seconds
|
|
SATS_PER_BEER=config.get('BEER', 'SATS_PER_BEER')#how many sats to charge fo one beer
|
|
|
|
# API Call
|
|
url = LNBITS_URL + "/api/v1/payments/"
|
|
headers = CaseInsensitiveDict()
|
|
headers["Content-Type"] = "application/json"
|
|
headers["X-Api-Key"] = LNBITS_APIKEY
|
|
|
|
def GetStoredPaymenInfos():
|
|
invoice.read('configs/invoice.beer')
|
|
return str(invoice.get('PAYMENTINFOS', 'PAYMENT_HASH')), str(invoice.get('PAYMENTINFOS', 'INVOICE'))
|
|
|
|
def SetLiveData(phash,inv):
|
|
invoice.read('configs/invoice.beer')
|
|
#print("set live data: \n" + str(invoice) + "\n"+ str(phash))
|
|
invoice.set('PAYMENTINFOS', 'INVOICE', str(inv))
|
|
invoice.set('PAYMENTINFOS', 'PAYMENT_HASH', str(phash))
|
|
invoice.write
|
|
with open('configs/invoice.beer', 'w') as configfile:
|
|
invoice.write(configfile)
|
|
|
|
def CreateQrCode(INVOICE):
|
|
qr = qrcode.QRCode(box_size=10,border=5)
|
|
qr.add_data(INVOICE)
|
|
qr.make(fit=True)
|
|
img = qr.make_image(fill_color="white", back_color="black")
|
|
img.save('media/qrcode.png')
|
|
|
|
def CreateInvoice():
|
|
data = '{"out": false, "amount": \"%s\", "memo": "Bier", "webhook": "payment", "unit": "sat"}' % SATS_PER_BEER
|
|
response = requests.post(url, headers=headers, data=data)
|
|
|
|
PAYMENT_HASH=json.loads(response.content)["payment_hash"]
|
|
INVOICE=json.loads(response.content)["payment_request"]
|
|
|
|
SetLiveData(PAYMENT_HASH, INVOICE)
|
|
CreateQrCode(INVOICE)
|
|
print("Invoice: "+INVOICE)
|
|
print("Payment Hash: "+PAYMENT_HASH)
|
|
|
|
def ResetInvoice():
|
|
SetLiveData(None, None)
|
|
|
|
def CheckPaymentStatus():
|
|
if GetStoredPaymenInfos()[0] != "None":
|
|
check_url = url + GetStoredPaymenInfos()[0]
|
|
content = requests.get(check_url, headers=headers).content
|
|
return json.loads(content)["paid"]
|
|
|
|
def Generate():
|
|
print (GetStoredPaymenInfos()[0])
|
|
if GetStoredPaymenInfos()[0] == "None" or GetStoredPaymenInfos()[0] == None or GetStoredPaymenInfos()[0] == "":
|
|
if os.path.exists("media/qrcode.png"):
|
|
os.remove("media/qrcode.png")
|
|
CreateInvoice()
|
|
else:
|
|
if not os.path.exists("media/qrcode.png"):
|
|
CreateQrCode(INVOICE)
|
|
|