-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added stripe webhook handler example
- Loading branch information
1 parent
2e5e91f
commit 9467274
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
from stripe import StripeClient | ||
from stripe.events import V1BillingMeterErrorReportTriggeredEvent | ||
|
||
from flask import Flask, request, jsonify | ||
|
||
app = Flask(__name__) | ||
api_key = os.environ.get('STRIPE_API_KEY') | ||
webhook_secret = os.environ.get('WEBHOOK_SECRET') | ||
|
||
client = StripeClient(api_key) | ||
|
||
@app.route('/webhook', methods=['POST']) | ||
def webhook(): | ||
webhook_body = request.data | ||
sig_header = request.headers.get('Stripe-Signature') | ||
|
||
try: | ||
thin_event = client.parse_thin_event(webhook_body, sig_header, webhook_secret) | ||
|
||
# Fetch the event data to understand the failure | ||
event = client.v2.core.events.retrieve(thin_event.id) | ||
if isinstance(event, V1BillingMeterErrorReportTriggeredEvent): | ||
# CHECK: fetch_object is present and callable, returning a strongly-typed object (without casting) | ||
meter = event.fetch_related_object() | ||
meter_id = meter.id | ||
|
||
# Record the failures and alert your team | ||
# Add your logic here | ||
|
||
return jsonify(success=True), 200 | ||
except Exception as e: | ||
return jsonify(error=str(e)), 400 | ||
|
||
if __name__ == '__main__': | ||
app.run(port=4242) |