-
Notifications
You must be signed in to change notification settings - Fork 475
Add EventNotificationHandler example #1701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+56
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,55 @@ | ||
| """ | ||
| event_notification_handler_endpoint.py - receive and process event notifications (AKA thin events) like "v1.billing.meter.error_report_triggered" using EventNotificationHandler. | ||
|
|
||
| In this example, we: | ||
| - write a fallback callback to handle unrecognized event notifications | ||
| - create a StripeClient called client | ||
| - Initialize an EventNotificationHandler with the client, webhook secret, and fallback callback | ||
| - register a specific handler for the "v1.billing.meter.error_report_triggered" event notification type | ||
| - use handler.handle() to process the received notification webhook body | ||
| """ | ||
|
|
||
| import os | ||
| from flask import Flask, request, jsonify | ||
|
|
||
| from stripe import StripeClient, UnhandledNotificationDetails | ||
| from stripe.v2.core import EventNotification | ||
| from stripe.events import V1BillingMeterErrorReportTriggeredEventNotification | ||
|
|
||
| app = Flask(__name__) | ||
| api_key = os.environ.get("STRIPE_API_KEY", "") | ||
| webhook_secret = os.environ.get("WEBHOOK_SECRET", "") | ||
|
|
||
|
|
||
| def fallback_callback( | ||
| notif: EventNotification, | ||
| client: StripeClient, | ||
| details: UnhandledNotificationDetails, | ||
| ): | ||
| print(f"Got an unhandled event of type {notif.type}!") | ||
|
|
||
|
|
||
| client = StripeClient(api_key) | ||
| handler = client.notification_handler(webhook_secret, fallback_callback) | ||
|
|
||
|
|
||
| # can be anywhere in your codebase | ||
| @handler.on_v1_billing_meter_error_report_triggered | ||
| def handle_meter_error( | ||
| notif: V1BillingMeterErrorReportTriggeredEventNotification, | ||
| client: StripeClient, | ||
| ): | ||
| event = notif.fetch_event() | ||
| print(f"Err! No meter found: {event.data.developer_message_summary}") | ||
|
|
||
|
|
||
| @app.route("/webhook", methods=["POST"]) | ||
| def webhook(): | ||
| webhook_body = request.data | ||
| sig_header = request.headers.get("Stripe-Signature") | ||
|
|
||
| try: | ||
| handler.handle(webhook_body, sig_header) | ||
| return jsonify(success=True), 200 | ||
| except Exception as e: | ||
| return jsonify(error=str(e)), 500 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Copilot Autofix
AI 13 days ago
To fix the information exposure, we should avoid returning internal error messages or exception details (like
str(e)) to API clients. Instead, log the actual exception—including the traceback if desired—using server-side logging (for example, using Python's standardloggingmodule), and return a generic error message to the client.The fix requires:
loggingat the top of the file if not already present.logging.exception()(or similar)."An internal error has occurred.", rather than user-facing exception details.The code to change is in the exception handler in the
webhook()function, specifically on lines 54–55. Logging should be done inside the except block, before returning the generic message.