make refunds work properly

This commit is contained in:
Lee Salminen 2022-08-29 09:32:13 -06:00
parent 7baec1de15
commit 88210129b7
4 changed files with 31 additions and 11 deletions

View file

@ -1,8 +1,11 @@
import asyncio
from fastapi import APIRouter from fastapi import APIRouter
from starlette.staticfiles import StaticFiles from starlette.staticfiles import StaticFiles
from lnbits.db import Database from lnbits.db import Database
from lnbits.helpers import template_renderer from lnbits.helpers import template_renderer
from lnbits.tasks import catch_everything_and_restart
db = Database("ext_boltcards") db = Database("ext_boltcards")
@ -23,5 +26,12 @@ def boltcards_renderer():
from .lnurl import * # noqa from .lnurl import * # noqa
from .tasks 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 import * # noqa
from .views_api import * # noqa from .views_api import * # noqa

View file

@ -214,19 +214,17 @@ async def create_refund(hit_id, refund_amount) -> Refund:
refund_id = urlsafe_short_hash() refund_id = urlsafe_short_hash()
await db.execute( await db.execute(
""" """
INSERT INTO boltcards.hits ( INSERT INTO boltcards.refunds (
id, id,
hit_id, hit_id,
refund_amount, refund_amount
payment_hash
) )
VALUES (?, ?, ?, ?) VALUES (?, ?, ?)
""", """,
( (
refund_id, refund_id,
hit_id, hit_id,
refund_amount, refund_amount,
payment_hash,
), ),
) )
refund = await get_refund(refund_id) refund = await get_refund(refund_id)

View file

@ -84,10 +84,10 @@ new Vue({
field: 'refund_amount' field: 'refund_amount'
}, },
{ {
name: 'time', name: 'date',
align: 'left', align: 'left',
label: 'Time', label: 'Time',
field: 'time' field: 'date'
} }
], ],
pagination: { pagination: {

View file

@ -20,16 +20,28 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None: async def on_invoice_paid(payment: Payment) -> None:
if payment.extra.get("tag")[0:6] != "Refund": if not payment.extra.get("refund"):
# not an lnurlp invoice
return return
if payment.extra.get("wh_status"): if payment.extra.get("wh_status"):
# this webhook has already been sent # this webhook has already been sent
return 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: if hit:
refund = await create_refund( 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) 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),
)