Search code examples
python-3.xgoogle-cloud-firestorestripe-paymentsfastapi

stripe.error.SignatureVerificationError: No signatures found matching the expected signature for payload" - Python FastAPI


I am integrating Stripe API with Firebase using Fastapi and I am facing the issue - "stripe.error.SignatureVerificationError: No signatures found matching the expected signature for payload"

The code is working in local testing with Stripe CLI but doesn't work in deployment. The error is on the last line.

async def feathers_buy_update(request: Request):
    """Endpoint to handle the Stripe session status updates like checkout completed or checkout expired

    Args:

    """
    event = None
    payload = await request.body()
    sig_header = request.headers.get("stripe-signature")
    endpoint_secret = "<endpoint_secret>"

    event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret)

I printed the type of all three inputs. Payload - bytes, sig_header - string, endpoint_secret - string.


Solution

  • There's a lot of different reasons you could be hitting verification issues, but usually when you hit these issues after already confirming things work locally during testing it's one of the following:

    • You're using the wrong webhook endpoint secret. When testing with the Stripe CLI you get back a webhook secret (whsec_123) from the stripe listen command, but a lot of people forget to replace it once they deploy their real webhook endpoint. Make sure the webhook secret you have in your code matches what is displayed in the dashboard (when you open the webhook endpoint in the Stripe dashboard you'll see a section where you can "Reveal" the Signing Secret).
    • Your production server has some kind of configuration or setting in place that's manipulating the raw body/payload which wasn't set while testing locally. This will really depend on your framework - you'll probably want to log the payload as a string to confirm whether it looks correct first.