basic invoice listeners.

This commit is contained in:
fiatjaf
2020-10-04 12:04:47 -03:00
parent e2f6c20e3b
commit 04222f1f01
12 changed files with 193 additions and 26 deletions
+5
View File
@@ -6,3 +6,8 @@ lnurlp_ext: Blueprint = Blueprint("lnurlp", __name__, static_folder="static", te
from .views_api import * # noqa
from .views import * # noqa
from .tasks import on_invoice_paid
from lnbits.core.tasks import register_invoice_listener
register_invoice_listener("lnurlp", on_invoice_paid)
+18
View File
@@ -14,3 +14,21 @@ def m001_initial(db):
);
"""
)
# def m002_webhooks_and_success_actions(db):
# """
# Webhooks and success actions.
# """
# db.execute("ALTER TABLE pay_links ADD COLUMN webhook_url TEXT;")
# db.execute("ALTER TABLE pay_links ADD COLUMN success_text TEXT;")
# db.execute("ALTER TABLE pay_links ADD COLUMN success_url TEXT;")
# db.execute(
# """
# CREATE TABLE invoices (
# payment_hash PRIMARY KEY,
# link_id INTEGER NOT NULL REFERENCES pay_links (id),
# webhook_sent BOOLEAN NOT NULL DEFAULT false
# );
# """
# )
+10 -1
View File
@@ -7,12 +7,15 @@ from typing import NamedTuple
class PayLink(NamedTuple):
id: str
id: int
wallet: str
description: str
amount: int
served_meta: int
served_pr: int
webhook_url: str
success_text: str
success_url: str
@classmethod
def from_row(cls, row: Row) -> "PayLink":
@@ -27,3 +30,9 @@ class PayLink(NamedTuple):
@property
def lnurlpay_metadata(self) -> LnurlPayMetadata:
return LnurlPayMetadata(json.dumps([["text/plain", self.description]]))
class Invoice(NamedTuple):
payment_hash: str
link_id: int
webhook_sent: bool
+12
View File
@@ -0,0 +1,12 @@
import aiohttp
from lnbits.core.models import Payment
async def on_invoice_paid(payment: Payment) -> None:
islnurlp = "lnurlp" in payment.extra.get("tags", {})
print("invoice paid on lnurlp?", islnurlp)
if islnurlp:
print("dispatching webhook")
async with aiohttp.ClientSession() as session:
await session.post("https://fiatjaf.free.beeceptor.com", json=payment)
+5
View File
@@ -4,6 +4,7 @@ from http import HTTPStatus
from lnurl import LnurlPayResponse, LnurlPayActionResponse
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
from lnbits import bolt11
from lnbits.core.crud import get_user
from lnbits.core.services import create_invoice
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
@@ -126,6 +127,10 @@ async def api_lnurl_callback(link_id):
description_hash=hashlib.sha256(link.lnurlpay_metadata.encode("utf-8")).digest(),
extra={"tag": "lnurlp"},
)
inv = bolt11.decode(payment_request)
inv.payment_hash
resp = LnurlPayActionResponse(pr=payment_request, success_action=None, routes=[])
return jsonify(resp.dict()), HTTPStatus.OK