From 680d5274c48eb69588f63b592ee8b6660ee49cc5 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Wed, 28 Jun 2023 13:25:28 +0100 Subject: [PATCH] Fix create ticket endpoint (#7) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * create ticket when creating invoice not on check payment --------- Co-authored-by: dni ⚡ (cherry picked from commit 1d57e1ae1a1c2faadd30f358b2fc906ea713b624) --- helpers.py | 31 +++++++++---------------------- views_api.py | 11 ++++++++++- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/helpers.py b/helpers.py index 0c046bd..9b647b7 100644 --- a/helpers.py +++ b/helpers.py @@ -155,7 +155,14 @@ async def send_ticket(competition_id, ticket_id): status_code=HTTPStatus.NOT_FOUND, detail="Competition could not be fetched.", ) - + ticket = await get_ticket(ticket_id) + if not ticket or ticket.competition_id != competition_id: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="Ticket could not be fetched.", + ) + # TODO: Save payment-hash on ticket, so we could use get_standalone_payment() + # instead of get_payments() all_payments = await get_payments( wallet_id=competition.wallet, incoming=True, @@ -177,24 +184,4 @@ async def send_ticket(competition_id, ticket_id): payment, = all_payments if payment.pending: await payment.check_status() - if payment.pending: - return {"paid": False} - exists = await get_ticket(ticket_id) - if exists: - return {"paid": True} - # Danger: Not safe against parallel requests - if competition.state != "INITIAL" or competition.amount_tickets <= 0 or datetime.utcnow() > datetime.strptime(competition.closing_datetime, "%Y-%m-%dT%H:%M:%S.%fZ"): - raise HTTPException( - status_code=HTTPStatus.FORBIDDEN, - detail="Competition is close for new tickets. If you think you should get a refund, please contact the admin." - ) - - await create_ticket( - ticket_id=ticket_id, - wallet=competition.wallet, - competition=competition_id, - amount=payment.sat, - reward_target=str(payment.extra.get("reward_target")), - choice=int(payment.extra.get("choice")) - ) - return {"paid": True} + return {"paid": not payment.pending} diff --git a/views_api.py b/views_api.py index 8bf9179..10590d6 100644 --- a/views_api.py +++ b/views_api.py @@ -8,7 +8,7 @@ import shortuuid from starlette.exceptions import HTTPException -from lnbits.core.crud import get_user +from lnbits.core.crud import get_user, create_ticket from lnbits.core.services import create_invoice from lnbits.decorators import WalletTypeInfo, get_key_type @@ -197,6 +197,15 @@ async def api_ticket_make_ticket(competition_id, data: CreateInvoiceForTicket): memo=f"Bets4SatsTicketId:{competition_id}.{ticket_id}", extra={"tag": "bets4sats", "reward_target": data.reward_target, "choice": data.choice}, ) + await create_ticket( + ticket_id=ticket_id, + wallet=competition.wallet, + competition=competition_id, + amount=data.amount, + reward_target=str(data.reward_target), + choice=int(data.choice) + ) + # TODO: Delete ticket if invoice expires. except Exception as e: raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e)) return {"ticket_id": ticket_id, "payment_request": payment_request}