Conversation
…s handling - Added a new endpoint `/stripe/finalize-checkout` to synchronously fulfill a checkout session, addressing the webhook-vs-redirect race condition. - Updated the `PurchaseSuccessPage` component to handle various states of the checkout process, including loading, completed, pending, and failed states. - Introduced a new response model `FinalizeCheckoutResponse` to provide immediate feedback on the purchase status. - Enhanced the Stripe API service to include the new finalize checkout functionality.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
📝 WalkthroughWalkthroughThis PR adds a new ChangesCheckout Finalization Flow
Sequence DiagramsequenceDiagram
participant Client as Frontend Client
participant API as Backend API
participant Stripe as Stripe API
participant DB as Database
Client->>Client: Extract session_id from redirect URL
Client->>Client: Initialize polling state machine
loop Poll until success or max attempts
Client->>API: GET /stripe/finalize-checkout?session_id={id}
activate API
API->>Stripe: Retrieve checkout session
activate Stripe
Stripe-->>API: Session + payment_status
deactivate Stripe
API->>API: Verify client_reference_id matches user.id
alt Payment succeeded
API->>DB: Fulfill purchase (pages or tokens)
API->>DB: Refresh user state
else Payment failed/incomplete
API->>DB: Mark purchase as failed
API->>DB: Refresh user state
end
API-->>Client: FinalizeCheckoutResponse (status, limits, usage)
deactivate API
Client->>Client: Update polling state (completed/failed/pending)
end
Client->>Client: Render completion UI with balance/error
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly Related PRs
Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Description
Motivation and Context
FIX #
Screenshots
API Changes
Change Type
Testing Performed
Checklist
High-level PR Summary
This PR fixes a race condition in the Stripe checkout flow where users would land on the purchase-success page before the
checkout.session.completedwebhook was delivered. The solution introduces a new/stripe/finalize-checkoutendpoint that allows the frontend to synchronously fulfill purchases by retrieving the session from Stripe's API on page load. The backend now appendssession_id={{CHECKOUT_SESSION_ID}}to success URLs so the frontend can call this endpoint immediately. The implementation includes idempotency guarantees, authorization checks (verifyingclient_reference_idmatches the authenticated user), and proper handling of concurrent webhook and finalize-checkout calls. The frontend now polls the finalize endpoint until the purchase is completed or displays appropriate fallback messages for async payment methods. Additionally, the code standardizes purchase type metadata frompremium_credittopremium_tokenswhile maintaining backward compatibility for in-flight sessions.⏱️ Estimated Review Time: 30-90 minutes
💡 Review Order Suggestion
surfsense_backend/app/schemas/stripe.pysurfsense_web/contracts/types/stripe.types.tssurfsense_backend/app/routes/stripe_routes.pysurfsense_web/lib/apis/stripe-api.service.tssurfsense_web/app/dashboard/[search_space_id]/purchase-success/page.tsxSummary by CodeRabbit
Release Notes