I have a mongodb database tracking if a user has paid through stripe. Once a user pays, I would like to change the database from False to True. However, even though my webhook seems to be working on the Stripe dashboard, the database isn’t updating. Additionally, if I add a print statement it doesn’t print anything.
I am using the test mode on stripe and using Heroku domain for my endpoint. My stripe dashboard indicates that the invoice.payment_succeeded is working normally and is marked as – ‘succeeded’
Here’s my webhook code:
@main_bp.route("/hooks", methods=["POST"])
def stripe_webhook():
try:
payload = request.get_data(as_text=True)
sig_header = request.headers.get("Stripe-Signature")
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
data = event['data']
except ValueError as e:
# Invalid payload
return "Invalid payload", 400
except stripe.error.SignatureVerificationError as e:
# Invalid signature
return "Invalid signature", 400
# Handle the checkout.session.completed event
if event['type'] == 'invoice.paid':
# THIS PART IS NOT WORKING ↓
print("Payment was successful.")
mycol_users_consumer.find_one_and_update({"consumer_email": current_user.email},
{"$set": {"user_paid": "True"}})
elif event['type'] == 'invoice.payment_failed':
print("Payment not successful.")
print(data)
return "Success", 200
except Exception as e:
print(e)
Heroku Logs indicate a 200 on the webook:
2020-10-12T21:37:47.350062+00:00 heroku[router]: at=info method=POST path="/create-checkout-session" host=.herokuapp.com request_id=2d251ee6-3a4b-42f4-aad0-5afef2d5cf69 fwd="99.243.145.14" dyno=web.1 connect=1ms service=324ms status=200 bytes=240 protocol=http
2020-10-12T21:37:47.349260+00:00 app[web.1]: 10.155.219.74 - - [12/Oct/2020:21:37:47 +0000] "POST /create-checkout-session HTTP/1.1" 200 74 "http://.herokuapp.com/checkout" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
2020-10-12T21:38:12.840418+00:00 heroku[router]: at=info method=POST path="/hooks" host=.herokuapp.com request_id=6e2c93a1-1543-464a-960e-f455c48e0b88 fwd="54.187.205.235" dyno=web.1 connect=1ms service=3ms status=200 bytes=166 protocol=http
2020-10-12T21:38:12.842340+00:00 app[web.1]: 10.16.250.243 - - [12/Oct/2020:21:38:12 +0000] "POST /hooks HTTP/1.1" 200 7 "-" "Stripe/1.0 (+https://stripe.com/docs/webhooks)"
Advertisement
Answer
Your question talks about seeing invoice.payment_succeeded
events in the dashboard, but your code is handling the invoice.paid
event type. If the mismatch was unintentional, your code needs to be updated to handle the invoice.payment_succeeded
event type.