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 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

View File

@ -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)

View File

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

View File

@ -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),
)