public api for checking an invoice status with longpolling.

This commit is contained in:
fiatjaf
2021-06-02 17:05:07 -03:00
parent 7a25c8ce24
commit fc792f874b
4 changed files with 45 additions and 7 deletions
+2 -2
View File
@@ -20,7 +20,7 @@ from ..services import (
pay_invoice,
perform_lnurlauth,
)
from ..tasks import sse_listeners
from ..tasks import api_invoice_listeners
@core_app.route("/api/v1/wallet", methods=["GET"])
@@ -295,7 +295,7 @@ async def api_payments_sse():
send_payment, receive_payment = trio.open_memory_channel(0)
print("adding sse listener", send_payment)
sse_listeners.append(send_payment)
api_invoice_listeners.append(send_payment)
send_event, event_to_send = trio.open_memory_channel(0)
+37
View File
@@ -0,0 +1,37 @@
import trio # type: ignore
import datetime
from http import HTTPStatus
from quart import jsonify
from lnbits import bolt11
from .. import core_app
from ..crud import get_standalone_payment
from ..tasks import api_invoice_listeners
@core_app.route("/public/v1/payment/<payment_hash>", methods=["GET"])
async def api_public_payment_longpolling(payment_hash):
payment = await get_standalone_payment(payment_hash)
if not payment:
return jsonify({"message": "Payment does not exist."}), HTTPStatus.NOT_FOUND
elif not payment.pending:
return jsonify({"status": "paid"}), HTTPStatus.OK
try:
invoice = bolt11.decode(payment.bolt11)
expiration = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry)
if expiration < datetime.datetime.now():
return jsonify({"status": "expired"}), HTTPStatus.OK
except:
return jsonify({"message": "Invalid bolt11 invoice."}), HTTPStatus.BAD_REQUEST
send_payment, receive_payment = trio.open_memory_channel(0)
print("adding standalone invoice listener", payment_hash, send_payment)
api_invoice_listeners.append(send_payment)
async for payment in receive_payment:
if payment.payment_hash == payment_hash:
return jsonify({"status": "paid"}), HTTPStatus.OK