Skip to content

fix: stripe weebhook#1345

Merged
MODSetter merged 1 commit intomainfrom
fix/stripe-routes
May 5, 2026
Merged

fix: stripe weebhook#1345
MODSetter merged 1 commit intomainfrom
fix/stripe-routes

Conversation

@MODSetter
Copy link
Copy Markdown
Owner

@MODSetter MODSetter commented May 5, 2026

Description

Motivation and Context

FIX #

Screenshots

API Changes

  • This PR includes API changes

Change Type

  • Bug fix
  • New feature
  • Performance improvement
  • Refactoring
  • Documentation
  • Dependency/Build system
  • Breaking change
  • Other (specify):

Testing Performed

  • Tested locally
  • Manual/QA verification

Checklist

  • Follows project coding standards and conventions
  • Documentation updated as needed
  • Dependencies updated as needed
  • No lint/build errors or new warnings
  • All relevant tests are passing

High-level PR Summary

This PR fixes a critical bug in the Stripe webhook handler related to metadata extraction from checkout sessions. The main fix addresses a KeyError: 0 that occurred when recent Stripe SDK versions changed their StripeObject implementation to no longer subclass dict. The solution uses the .items() method from the Mapping protocol which works for both plain dictionaries and Stripe objects. Additionally, the PR wraps the main webhook event handlers in a try-except block to ensure proper error logging and 500 responses so Stripe will automatically retry failed webhook deliveries.

⏱️ Estimated Review Time: 5-15 minutes

💡 Review Order Suggestion
Order File Path
1 surfsense_backend/app/routes/stripe_routes.py

Need help? Join our Discord

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced Stripe webhook handler robustness for more reliable payment processing and checkout session handling.
    • Improved error logging and recovery for unexpected payment events to ensure proper retry behavior.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
surf-sense-frontend Building Building Preview, Comment May 5, 2026 7:18am

Request Review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dfa08fb9-1b96-40a2-820e-c7c271c02687

📥 Commits

Reviewing files that changed from the base of the PR and between 2a63a0a and 5ff6bae.

📒 Files selected for processing (1)
  • surfsense_backend/app/routes/stripe_routes.py

📝 Walkthrough

Walkthrough

The Stripe webhook handler is improved with more robust metadata extraction that safely handles non-dict Stripe objects, and the event routing control flow is refactored to use set-based event type grouping with comprehensive exception logging.

Changes

Stripe Webhook Robustness & Control Flow

Layer / File(s) Summary
Metadata Extraction
surfsense_backend/app/routes/stripe_routes.py (lines 91–106)
_get_metadata now safely iterates metadata.items() for all Stripe metadata objects instead of conditionally calling dict(metadata), preventing incorrect behavior with non-dict SDK objects.
Webhook Handler Control Flow & Error Handling
surfsense_backend/app/routes/stripe_routes.py (lines 454–502)
Webhook handler is wrapped in a try/except that logs event id and type on any exception and re-raises for FastAPI 500 response. Event routing uses set membership (event.type in {...}) to group success events (checkout.session.completed, charge.succeeded, payment_intent.succeeded) and failure events (charge.failed, payment_intent.payment_failed), preserving unpaid-session early returns and purchase-type fulfillment logic.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Metadata hops along .items() so fair,
Exceptions now logged with details to spare,
Event types sorted with sets in a row,
Stripe webhooks now perform a finest show! 🎪

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/stripe-routes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@MODSetter MODSetter merged commit 3edefb9 into main May 5, 2026
4 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant