diff --git a/lnbits/extensions/boltcards/__init__.py b/lnbits/extensions/boltcards/__init__.py index 11b8dd2d..bfdc7492 100644 --- a/lnbits/extensions/boltcards/__init__.py +++ b/lnbits/extensions/boltcards/__init__.py @@ -1,8 +1,11 @@ +import asyncio + from fastapi import APIRouter from starlette.staticfiles import StaticFiles from lnbits.db import Database from lnbits.helpers import template_renderer +from lnbits.tasks import catch_everything_and_restart db = Database("ext_boltcards") @@ -23,5 +26,12 @@ def boltcards_renderer(): from .lnurl import * # noqa from .tasks import * # noqa + + +def boltcards_start(): + loop = asyncio.get_event_loop() + loop.create_task(catch_everything_and_restart(wait_for_paid_invoices)) + + from .views import * # noqa from .views_api import * # noqa diff --git a/lnbits/extensions/boltcards/crud.py b/lnbits/extensions/boltcards/crud.py index 1c48500d..be9b2961 100644 --- a/lnbits/extensions/boltcards/crud.py +++ b/lnbits/extensions/boltcards/crud.py @@ -214,19 +214,17 @@ async def create_refund(hit_id, refund_amount) -> Refund: refund_id = urlsafe_short_hash() await db.execute( """ - INSERT INTO boltcards.hits ( + INSERT INTO boltcards.refunds ( id, hit_id, - refund_amount, - payment_hash + refund_amount ) - VALUES (?, ?, ?, ?) + VALUES (?, ?, ?) """, ( refund_id, hit_id, refund_amount, - payment_hash, ), ) refund = await get_refund(refund_id) diff --git a/lnbits/extensions/boltcards/static/js/index.js b/lnbits/extensions/boltcards/static/js/index.js index 254f9d88..4d6134f9 100644 --- a/lnbits/extensions/boltcards/static/js/index.js +++ b/lnbits/extensions/boltcards/static/js/index.js @@ -84,10 +84,10 @@ new Vue({ field: 'refund_amount' }, { - name: 'time', + name: 'date', align: 'left', label: 'Time', - field: 'time' + field: 'date' } ], pagination: { diff --git a/lnbits/extensions/boltcards/tasks.py b/lnbits/extensions/boltcards/tasks.py index a7eea026..1b51c98b 100644 --- a/lnbits/extensions/boltcards/tasks.py +++ b/lnbits/extensions/boltcards/tasks.py @@ -20,16 +20,28 @@ async def wait_for_paid_invoices(): async def on_invoice_paid(payment: Payment) -> None: - if payment.extra.get("tag")[0:6] != "Refund": - # not an lnurlp invoice + if not payment.extra.get("refund"): return if payment.extra.get("wh_status"): # this webhook has already been sent return - hit = await get_hit(payment.extra.get("tag")[7 : len(payment.extra.get("tag"))]) + hit = await get_hit(payment.extra.get("refund")) + if hit: refund = await create_refund( - hit_id=hit.id, refund_amount=payment.extra.get("amount") + hit_id=hit.id, refund_amount=(payment.amount / 1000) ) await mark_webhook_sent(payment, 1) + + +async def mark_webhook_sent(payment: Payment, status: int) -> None: + payment.extra["wh_status"] = status + + await core_db.execute( + """ + UPDATE apipayments SET extra = ? + WHERE hash = ? + """, + (json.dumps(payment.extra), payment.payment_hash), + )