[Python] Is it a best practice to send confirmation emails inside a Stripe...

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a Flask web application where users can purchase a subscription via Stripe Checkout. I've implemented a webhook endpoint to handle the checkout.session.completed event.

    Here’s what currently happens inside the webhook handler:


    1. Stripe sends the checkout.session.completed event.


    2. I verify the event signature.


    3. I update the user's subscription status in the database.


    4. I send a confirmation email (e.g., "Your subscription is now active").

    The relevant logic is synchronous (the email is sent before responding 200 OK to Stripe).

    Here is a simple code snippet:

    try:
    event = stripe.Webhook.construct_event(payload, sig_header, STRIPE_WEBHOOK_SECRET)
    except stripe.error.SignatureVerificationError:
    return 'Invalid signature', 400

    if event['type'] == 'checkout.session.completed':
    session = event['data']['object']
    user = User.query.filter_by(email=session['customer_email']).first()
    user.is_subscribed = True
    db.session.commit()

    # Send confirmation email
    send_email(
    to=user.email,
    subject='Subscription Confirmed',
    body='Your subscription is now active.'
    )

    return '', 200


    My questions: Is it considered efficient and best practice to send transactional emails (like confirmations) inside the webhook handler?

    Should I offload email sending to a background job (e.g., Celery or RQ) and just respond to Stripe immediately to avoid webhook timeouts or retry issues?

    Continue reading...

Compartilhe esta Página