Stripe Webhooks

I’m trying to implement Stripe webhooks into my app to confirm subscription payment (payment_succeeded, payment failed, session completed).
Stripe is successfully sending the webhooks and I’m recieving them, but stripe sig verification is consistently failing:

Stripe webhook received!
Received Signature Header: t=1695558609,v1=c36992d950cebe0b9e35b921e7cc2eaf0ce05cc7f1aea54187ac21bbd4a997fc,v0=f297193f4723de1f69cfdc064de67425d8f05f57ddda0060ee63d1e9320d55d6
First 100 bytes of payload: b’{\n “id”: “evt_3NtrRqKx9mFp39qk1uFmceNP”,\n “object”: “event”,\n “api_version”: “2023-08-16”,\n "cre’
Error: Stripe signature verification failed

API secret key and Webhook signing secret are correct. The webhook URL is correct as I’m receiving the webhook.

Setting up the CLI to listen locally I’m seeing:
2023-09-24 13:30:11 ← [200] POST https://bqs3i7bzcsyzuawc.anvil.app/VHYXNWDFOTKW4JQ4ZEOI53SZ/_/api/webhook [evt_1NtrRsKx9mFp39qkuujklfWO]

Which suggests success.

I’m wondering if it’s something to do with how Anvil handles incoming headers for webhooks that is causing the problem.
I’ve looked at the Anvil documentation, but with regards to webhooks it mostly relates to Trello and I cant find anything relevant in the forum. I don’t know where to go from here to get to the bottom of it.

Here’s the relevant part of the function in my server module:

@anvil.server.http_endpoint(“/webhook”)
def handle_stripe_webhook(**kwargs):
print(“Stripe webhook received!”)

# Retrieve the Stripe signature header
sig_header = anvil.server.request.headers.get('stripe-signature', anvil.server.request.headers.get('STRIPE-SIGNATURE'))

print(f"Received Signature Header: {sig_header}") 
# Extract the payload as StreamingMedia
payload = anvil.server.request.body
# Convert the StreamingMedia payload to bytes
payload_bytes = payload.get_bytes()
print(f"First 100 bytes of payload: {payload_bytes[:100]}")

Please bear in mind I’m neither a coder or a developer when replying.

I should add I’m using a stripe payment link for taking payments (not sure if that’s relevant or not).

Thanks in adavance
Adam

Hello @mail,

I think this is the code you’re looking for.

import stripe
import json

stripe_secret_key = anvil.secrets.get_secret('stripe_secret_key')

@anvil.server.http_endpoint("/webhook_number_1")
def webhook_number_1():
  signing_secret = anvil.secrets.get_secret('webhook_number_1_signing_secret')
  payload = anvil.server.request.body.get_bytes()
  
  if stripe_secret_key:
    # Only verify the event if there is an endpoint secret defined
    # Otherwise use the basic event deserialized with json
    sig_header = anvil.server.request.headers.get("stripe-signature")
    try:
      event = stripe.Webhook.construct_event(
          payload, sig_header, signing_secret
      )
      print('Webhook signature verification succeeded.')
      return anvil.server.HttpResponse(status=200, body=json.dumps({'success':True}), headers=None)
    except stripe.error.SignatureVerificationError as e:
      print('⚠️  Webhook signature verification failed.' + str(e))
      return anvil.server.HttpResponse(status=400, body=json.dumps({'success':False}), headers=None)`
3 Likes

Hello
I’ve been here for just a few days and I don’t know much about Python and HTTP and I want to create an app that receives text messages from my Roblox game and I’ve read a lot of posts and examples and I’ve tried several attempts but I can’t receive the text message from my game and this is the third post I’ve made.
I think it’s very simple I just need a few lines to print the text that my game sends I can do anything in Roblox I’m good at Lua but I don’t know how to arrange the data so that Anvil allows my text message to pass
Most of what I found receives texts from Trello, and I tried to receive the same way from Roblox, but to no avail. I used a code that someone used on YouTube to send text messages from Roblox to Trello, but it did not work with Anvil.

Text message code to Trello
What I want to know is how do I make Anvil receive my text message? What do I write to him in Roblox so that he allows my text message to pass through?

Rather than try to resurrect years old posts that aren’t about your subject, please make a new post with your question with an appropriate subject line. You’ll get more attention to your question that way. Also note what tutorials you’ve gone through in your attempt to write your code, so that people don’t suggest ones you’ve already gone through.

1 Like

Hello
I’m sorry, and I apologize if what I’m doing violates the rules. I’ll keep quiet from now on. I’ll search silently and won’t write in old threads.
I don’t want to upset you or give you a bad impression, but I did everything you wrote. I created a new topic, searched all the documentation related to my topic, and tried all the examples similar to what I wanted to do. A forum moderator separated my reply from an old thread and created a new one in it. I’m not lazy, and I want to learn. I like your site and your forum.
I was wrong to think it was similar to what we do on the Roblox Developer Forum. Digging through old threads is better than creating new ones. Learning from others’ experiences allows you to progress, rather than repeating what others have done and failed at.

That’s perfectly understandable, the way to apply that idea to the Anvil Forum is to link to the old post
image
where you are trying to do something similar, but still different.

2 Likes