LN-Beertap_pi/NFC.py

73 lines
2.7 KiB
Python

import nfc, ndef, time, requests,json, sys, os
from nfc.tag import tt1
from nfc.tag import tt2
from nfc.tag import tt3
from nfc.tag import tt4
from configparser import ConfigParser
from requests.structures import CaseInsensitiveDict
config = ConfigParser(allow_no_value=True)
config.read('configs/config.beer')
LNBITS_URL = config.get('LNBITS', 'URL')
invoice = ConfigParser(allow_no_value=True)
headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/json"
def DecodeLNURL(lnurl):
decodeurl = LNBITS_URL + "/api/v1/payments/decode"
data = '{"data": \"%s\"}' % lnurl
response = requests.post(decodeurl, headers=headers, data=data)
decodedurl = json.loads(response.content)["domain"]
return decodedurl
def Pay(lnurl):
invoice.read('configs/invoice.beer')
INVOICE=invoice.get('PAYMENTINFOS', 'INVOICE')
paymentinfo = requests.get(DecodeLNURL(lnurl), headers=headers).content
callback = json.loads(paymentinfo)["callback"]
k1 = json.loads(paymentinfo)["k1"]
payurl = '%s?k1=%s&pr=%s' % (callback, k1, INVOICE)
pay = requests.get(payurl, headers=headers).content
print(json.loads(pay)["status"])
while not config.getboolean('LIVE', 'BEERTAP_ACTIVE'):
time.sleep(1)
config.read('configs/config.beer')
with nfc.ContactlessFrontend('tty:S0:pn532') as clf:
print('waiting for connection')
print(clf)
tag = clf.connect(rdwr={'on-connect': lambda tag: False})
if tag.ndef.length > 0:
print("Found NDEF Data:")
for i, record in enumerate(tag.ndef.records):
print("record", i + 1)
print(" type =", record.type)
print(" name =", record.name)
if hasattr(record, "text"):
text = record.text
print(" text =", record.text)
if hasattr(record, "data"):
print(" data =", record.data.decode('utf-8'))
text = record.data.decode('utf-8')
if hasattr(record, "domain"):
print(" data =", record.data.decode('utf-8'))
text = record.data.decode('utf-8')
uri = text.split(":")[0].lower()
print(uri)
config.read('configs/config.beer')
#print(str(config.getboolean('LIVE', 'BEERTAP_ACTIVE')))
#if config.getboolean('LIVE', 'BEERTAP_ACTIVE'):
# print("beertap active")
if config.getboolean('LIVE', 'BEERTAP_ACTIVE'):
LNURLW = text.split(":")[1]
print(" LNURLW = " + LNURLW)
print("Paying Invoice with NFC LNURLW: %s" % LNURLW)
Pay(LNURLW)
LNURLW = None