Skip to content

fix: metadata extraction in Stripe checkout session#1347

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

fix: metadata extraction in Stripe checkout session#1347
MODSetter merged 1 commit intomainfrom
fix/stripe-routes

Conversation

@MODSetter
Copy link
Copy Markdown
Owner

@MODSetter MODSetter commented May 5, 2026

  • Updated the _get_metadata function to handle changes in the Stripe SDK, specifically for StripeObject which is no longer a subclass of dict in stripe>=15.0.
  • Implemented a fallback mechanism in finalize_checkout to recover purchase type from the database if metadata extraction fails, ensuring robust handling of checkout sessions.

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 metadata extraction issues in Stripe checkout sessions caused by breaking changes in the Stripe SDK version 15.0 and above. The _get_metadata function has been rewritten to handle three different scenarios: plain dictionaries (older SDKs), modern StripeObject instances using the to_dict() method, and a fallback to the private _data attribute. Additionally, a defensive fallback mechanism was added to the finalize_checkout function that queries the database to recover the purchase type when metadata extraction fails, preventing purchases from being incorrectly routed or getting stuck.

⏱️ 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 checkout payment processing reliability with improved error handling and fallback mechanisms for edge cases.

- Updated the `_get_metadata` function to handle changes in the Stripe SDK, specifically for `StripeObject` which is no longer a subclass of `dict` in `stripe>=15.0`.
- Implemented a fallback mechanism in `finalize_checkout` to recover purchase type from the database if metadata extraction fails, ensuring robust handling of checkout sessions.
@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 8:27am

Request Review

@MODSetter MODSetter merged commit 9576d1f into main May 5, 2026
4 of 10 checks passed
@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: 388dc3cc-9507-4902-973e-aafc31e67d21

📥 Commits

Reviewing files that changed from the base of the PR and between 3c0f318 and dd8c503.

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

📝 Walkthrough

Walkthrough

The pull request enhances the robustness of Stripe metadata extraction in stripe_routes.py by rewriting the _get_metadata function to handle various Stripe SDK object formats and adding a defensive database lookup fallback in finalize_checkout when metadata extraction fails.

Changes

Stripe Metadata Extraction & Fallback

Layer / File(s) Summary
Metadata Extraction Logic
surfsense_backend/app/routes/stripe_routes.py (lines 99–144)
_get_metadata now handles multiple Stripe SDK metadata formats: returns empty dict for missing metadata, directly uses dict objects, calls to_dict(recursive=False) when available, falls back to private _data attribute, and logs warnings/exceptions when extraction fails.
Checkout Finalization Fallback
surfsense_backend/app/routes/stripe_routes.py (lines 616–657)
finalize_checkout adds defensive logic when metadata is empty: queries PremiumTokenPurchase and PagePurchase tables by stripe_checkout_session_id to infer purchase type, logs error if neither table contains a matching row, and logs info when purchase type is recovered via DB lookup.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • MODSetter/SurfSense#1345: Also modifies metadata extraction in stripe_routes.py to handle diverse Stripe SDK object formats, suggesting coordinated robustness improvements.

Poem

🐰 A rabbit hops through metadata's maze,
Where Stripe SDK formats set a blaze,
Now gracefully we handle each strange form,
With fallback queries to keep us warm! 🌿

✨ 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.

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