Skip to content

Commit

Permalink
Fix create ticket endpoint (#7)
Browse files Browse the repository at this point in the history
* create ticket when creating invoice not on check payment
---------

Co-authored-by: dni ⚡ <[email protected]>
(cherry picked from commit 1d57e1a)
  • Loading branch information
talvasconcelos authored and oren-z0 committed Dec 16, 2023
1 parent 5f21dd2 commit 00704f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
31 changes: 9 additions & 22 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}
11 changes: 10 additions & 1 deletion views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 00704f3

Please sign in to comment.