mirror of
https://github.com/lnbits/lnbits.git
synced 2025-06-27 01:01:02 +02:00
fix: persist webhook call status
This commit is contained in:
parent
ead710e591
commit
1a9cd81ee9
@ -35,18 +35,18 @@ async def call_webhook(charge: Charges):
|
||||
json=public_charge(charge),
|
||||
timeout=40,
|
||||
)
|
||||
except AssertionError:
|
||||
charge.webhook = None
|
||||
return {"webhook_success": r.is_success, "webhook_message": r.reason_phrase}
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to call webhook for charge {charge.id}")
|
||||
logger.warning(e)
|
||||
return {"webhook_success": False, "webhook_message": str(e)}
|
||||
|
||||
|
||||
async def fetch_onchain_balance(charge: Charges):
|
||||
endpoint = (
|
||||
f"{charge.config['mempool_endpoint']}/testnet"
|
||||
if charge.config["network"] == "Testnet"
|
||||
else charge.config["mempool_endpoint"]
|
||||
if charge.config.network == "Testnet"
|
||||
else charge.config.mempool_endpoint
|
||||
)
|
||||
async with httpx.AsyncClient() as client:
|
||||
r = await client.get(endpoint + "/api/address/" + charge.onchainaddress)
|
||||
|
@ -19,6 +19,13 @@ class CreateCharge(BaseModel):
|
||||
extra: str = "{}"
|
||||
|
||||
|
||||
class ChargeConfig(BaseModel):
|
||||
mempool_endpoint: Optional[str]
|
||||
network: Optional[str]
|
||||
webhook_success: Optional[bool] = False
|
||||
webhook_message: Optional[str]
|
||||
|
||||
|
||||
class Charges(BaseModel):
|
||||
id: str
|
||||
description: Optional[str]
|
||||
@ -59,5 +66,9 @@ class Charges(BaseModel):
|
||||
return False
|
||||
|
||||
@property
|
||||
def config(self):
|
||||
return json.loads(self.extra)
|
||||
def config(self) -> ChargeConfig:
|
||||
charge_config = json.loads(self.extra)
|
||||
return ChargeConfig(**charge_config)
|
||||
|
||||
def must_call_webhook(self):
|
||||
return self.webhook and self.paid and self.config.webhook_success == False
|
||||
|
@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from loguru import logger
|
||||
|
||||
@ -7,6 +8,7 @@ from lnbits.extensions.satspay.crud import check_address_balance, get_charge
|
||||
from lnbits.helpers import get_current_extension_name
|
||||
from lnbits.tasks import register_invoice_listener
|
||||
|
||||
from .crud import update_charge
|
||||
from .helpers import call_webhook
|
||||
|
||||
|
||||
@ -32,5 +34,7 @@ async def on_invoice_paid(payment: Payment) -> None:
|
||||
await payment.set_pending(False)
|
||||
charge = await check_address_balance(charge_id=charge.id)
|
||||
|
||||
if charge.paid and charge.webhook:
|
||||
await call_webhook(charge)
|
||||
if charge.must_call_webhook():
|
||||
resp = await call_webhook(charge)
|
||||
extra = {**charge.config.dict(), **resp}
|
||||
await update_charge(charge_id=charge.id, extra=json.dumps(extra))
|
||||
|
@ -203,9 +203,14 @@
|
||||
:href="props.row.webhook"
|
||||
target="_blank"
|
||||
style="color: unset; text-decoration: none"
|
||||
>{{props.row.webhook || props.row.webhook}}</a
|
||||
>{{props.row.webhook}}</a
|
||||
>
|
||||
</div>
|
||||
<div class="col-4 q-pr-lg">
|
||||
<q-badge v-if="props.row.webhook_message" color="blue">
|
||||
{{props.row.webhook_message }}
|
||||
</q-badge>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row items-center q-mt-md q-mb-lg">
|
||||
<div class="col-2 q-pr-lg">ID:</div>
|
||||
|
@ -34,10 +34,9 @@ async def display(request: Request, charge_id: str):
|
||||
view_data = {
|
||||
"request": request,
|
||||
"charge_data": public_charge(charge),
|
||||
"mempool_endpoint": charge.config.mempool_endpoint,
|
||||
"network": charge.config.network
|
||||
}
|
||||
if "mempool_endpoint" in charge.config:
|
||||
view_data["mempool_endpoint"] = charge.config["mempool_endpoint"]
|
||||
view_data["network"] = charge.config["network"]
|
||||
|
||||
return satspay_renderer().TemplateResponse(
|
||||
"satspay/display.html",
|
||||
|
@ -1,3 +1,4 @@
|
||||
import json
|
||||
from http import HTTPStatus
|
||||
|
||||
from fastapi.params import Depends
|
||||
@ -19,7 +20,7 @@ from .crud import (
|
||||
get_charges,
|
||||
update_charge,
|
||||
)
|
||||
from .helpers import public_charge
|
||||
from .helpers import call_webhook, public_charge
|
||||
from .models import CreateCharge
|
||||
|
||||
#############################CHARGES##########################
|
||||
@ -57,6 +58,7 @@ async def api_charges_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
**{"time_elapsed": charge.time_elapsed},
|
||||
**{"time_left": charge.time_left},
|
||||
**{"paid": charge.paid},
|
||||
**{"webhook_message": charge.config.webhook_message},
|
||||
}
|
||||
for charge in await get_charges(wallet.wallet.user)
|
||||
]
|
||||
@ -118,4 +120,9 @@ async def api_charge_balance(charge_id):
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Charge does not exist."
|
||||
)
|
||||
|
||||
if charge.must_call_webhook():
|
||||
resp = await call_webhook(charge)
|
||||
extra = {**charge.config.dict(), **resp}
|
||||
await update_charge(charge_id=charge.id, extra=json.dumps(extra))
|
||||
|
||||
return {**public_charge(charge)}
|
||||
|
Loading…
x
Reference in New Issue
Block a user