Forums

My stripe webhook keeps on timing out when trying to connect to remote host

This is my first time using stripe and I'm not really sure why it would be timing out.

We need more details to help you.

This is the code I have for the webhook. Python Flask, using sqlite DB

@app.route('/stripe-webhook', methods=['POST'])
def stripe_webhook():
    logging.info("Received a request.")

    payload = request.data
    sig_header = request.headers.get('Stripe-Signature')
    ENDPOINT_SECRET = "my_endpoint_secret"

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

    # Handle the event
    if event['type'] == 'checkout.session.completed':
        # Mark the user as subscribed
        user_id = session.get('client_reference_id')
        user = User.query.get(user_id)
        if user:
            user.is_subscribed = True
            db.session.commit()

    return 'Success', 200

[edit by admin: formatting]

I can't see any issues with that offhand. Perhaps try adding some logging in there, in between the different parts of the view, to see where it is spending its time?

I have this at the top function

logging.info("Received a request.")

should that be printing "Received a request" in my server.log? Sorry, I'm very new to this

I think it should yes

I have added other logging statements throughout, but not even the first one is being written in my server.logs file.

I can add stripe functionality without using the webhooks, but it is bad practice. :P

Any idea why it wouldn't be coming through? It does say I have the webhook configured properly from my stripe dash.

As logging can be pretty useful, it comes with its own overhead of setup, could you try print calls with flush=True argument (optionally with file=sys.stderr as well to redirect the output to the error log), just to be sure that it's not missing because of something unrelated?