I have an issue with a stripe webhook, which I am running locally for the test.
async def my_webhook_view(request: Request, db: Session = Depends(get_db)):
payload = await request.body()
sig_header = request.headers.get('stripe-signature')
event = None
try:
event = stripe.Webhook.construct_event(
payload, sig_header, ENDPOINT_SECRET
)
except ValueError as e:
# Invalid payload
raise HTTPException(status_code=400, detail="Invalid payload")
except stripe.error.SignatureVerificationError as e:
# Invalid signature
raise HTTPException(status_code=400, detail="Invalid signature")
if event['type'] == 'checkout.session.completed':
#email = event['data']['object']['customer_details']['email']
# Check if the event has already been processed
event_id = event['id']
if db.query(ProcessedEvent).filter_by(id=event_id).first():
return {"message": "Event already processed"}
# Process the event
session = event['data']['object']
db_event = ProcessedEvent(id=event_id)
db.add(db_event)
db.commit()
msg = fulfill_checkout(event['data']['object']['id'])
if msg == "Event received":
print("Event received")
return JSONResponse(status_code=200, content={"message": "Success"})
else:
print("Event unprocessed")
return JSONResponse(status_code=200, content={"message": "Cancel"})
There is no delay (1s), the event is immediately received, and the debugger enters well into return JSONResponse(status_code=200, content={“message”: “Success”})
but nothing happens on the stripe payment page.
I get a timeout after some time with the success url but everything has been correctly processed in the background tasks.
The checkout properly sets the success and cancel urls.
#Stripe Payment
@app.post('/create-checkout-session')
async def create_checkout_session(price_id: str = Form(...)):
try:
checkout_session = stripe.checkout.Session.create(
line_items=[
{
# Provide the exact Price ID (for example, pr_1234) of the product you want to sell
'price': price_id,
'quantity': 1,
},
],
mode='payment',
success_url= f'{DOMAIN}/success',
cancel_url= f'{DOMAIN}/cancel',
billing_address_collection = 'required'
)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
return RedirectResponse(checkout_session.url,303)