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]>
  • Loading branch information
talvasconcelos and dni authored Jun 28, 2023
1 parent 13658ce commit 1d57e1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
4 changes: 0 additions & 4 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,5 @@ async def on_invoice_paid(payment: Payment) -> None:
await api_ticket_send_ticket(
payment.memo,
payment.payment_hash,
CreateTicket(
name=str(payment.extra.get("name")),
email=str(payment.extra.get("email")),
),
)
return
33 changes: 12 additions & 21 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from fastapi import Depends, Query
from starlette.exceptions import HTTPException

from lnbits.core.crud import get_user
from lnbits.core.crud import get_user, get_standalone_payment
from lnbits.core.services import create_invoice
from lnbits.core.views.api import api_payment
from lnbits.decorators import WalletTypeInfo, get_key_type

from . import events_ext
Expand Down Expand Up @@ -110,40 +109,32 @@ async def api_ticket_make_ticket(event_id, name, email):
memo=f"{event_id}",
extra={"tag": "events", "name": name, "email": email},
)
await create_ticket(payment_hash=payment_hash, wallet=event.wallet, event=event.id, name=name, email=email)
except Exception as e:
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
return {"payment_hash": payment_hash, "payment_request": payment_request}


@events_ext.post("/api/v1/tickets/{event_id}/{payment_hash}")
async def api_ticket_send_ticket(event_id, payment_hash, data: CreateTicket):
async def api_ticket_send_ticket(event_id, payment_hash):
event = await get_event(event_id)
if not event:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Event could not be fetched.",
)

status = await api_payment(payment_hash)
if status["paid"]:

exists = await get_ticket(payment_hash)
if exists:
return {"paid": True, "ticket_id": exists.id}

ticket = await create_ticket(
payment_hash=payment_hash,
wallet=event.wallet,
event=event_id,
name=data.name,
email=data.email,
ticket = await get_ticket(payment_hash)
if not ticket:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Ticket could not be fetched.",
)
if not ticket:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Event could not be fetched.",
)

payment = await get_standalone_payment(payment_hash)
if not payment.pending and event.price_per_ticket * 1000 == payment.amount:
return {"paid": True, "ticket_id": ticket.id}

return {"paid": False}


Expand Down

0 comments on commit 1d57e1a

Please sign in to comment.