Skip to content

Migrate from Stripe Charges API to Stripe Checkout Sessions API#403

Closed
li-xinwei wants to merge 4 commits intosnap-cloud:mainfrom
cs169:stripe-checkout-migration
Closed

Migrate from Stripe Charges API to Stripe Checkout Sessions API#403
li-xinwei wants to merge 4 commits intosnap-cloud:mainfrom
cs169:stripe-checkout-migration

Conversation

@li-xinwei
Copy link
Contributor

Summary

  • Migrate payment processing from the legacy Stripe Charges API (Stripe::Charge.create) and embedded Checkout.js to the modern Stripe Checkout Sessions API (Stripe::Checkout::Session.create)
  • Redirect users to Stripe's hosted checkout page for a more secure payment experience with built-in support for SCA/3D Secure, Apple Pay, Google Pay, and other payment methods
  • Send itemized ticket breakdown as line_items to Stripe instead of only the total cost, improving payment tracking and reconciliation on both the Stripe Dashboard and customer receipts
  • Add stripe_session_id to the payments table for better payment tracking and session-based verification

Changes

Payment Model (app/models/payment.rb)

  • Replaced purchase method (which used Stripe::Charge.create) with:
    • create_checkout_session — creates a Stripe Checkout Session with itemized line items and redirects to Stripe
    • complete_checkout — retrieves the session after redirect, verifies payment status, and records charge details
  • Added build_line_items private method that maps each unpaid TicketPurchase to a Stripe line item with proper currency conversion
  • Added unpaid_ticket_purchases helper method

Payments Controller (app/controllers/payments_controller.rb)

  • create action now creates a Payment record and Stripe Checkout Session, then redirects to Stripe's hosted page
  • Added success action — handles the return from Stripe after successful payment, verifies the session, marks tickets as paid, and handles registration
  • Added cancel action — handles payment cancellation, redirects back to payment page
  • Removed dependency on stripeToken/stripeEmail params (no longer needed with hosted checkout)

Routes (config/routes.rb)

  • Added success and cancel collection routes under payments

Views

  • Replaced legacy Stripe Checkout.js <script> tag with a standard form submit button that initiates the checkout flow
  • Updated payment summary page messaging to indicate redirect to Stripe

Database

  • Migration to add stripe_session_id (string, unique index) to payments table
  • Updated db/schema.rb

Tests

  • Updated spec/models/payment_spec.rb to test create_checkout_session and complete_checkout methods using RSpec mocks
  • Updated spec/features/ticket_purchases_spec.rb to work with the new checkout flow

Test plan

  • Verify that selecting tickets and clicking "Pay" creates a Stripe Checkout Session and redirects to Stripe
  • Verify successful payment redirects back with confirmation and marks tickets as paid
  • Verify cancelled payment redirects back to payment page with appropriate message
  • Verify itemized ticket details appear correctly on the Stripe Checkout page
  • Verify registration ticket flow still works (auto-registration after payment)
  • Verify free ticket purchases are unaffected
  • Verify currency conversion works correctly in checkout line items
  • Run bundle exec rspec spec/models/payment_spec.rb
  • Run bundle exec rspec spec/features/ticket_purchases_spec.rb

Made with Cursor

zhouyijun111 and others added 4 commits February 22, 2026 18:35
add new feature for counting the total sales for each currency
Replace the legacy Stripe Charges API and embedded Checkout.js with
the modern Stripe Checkout Sessions API. This redirects users to
Stripe's hosted checkout page for a more secure and flexible payment
experience.

Key changes:
- Payment model: replace Stripe::Charge.create with
  Stripe::Checkout::Session.create and session verification
- Send itemized ticket breakdown as line_items to Stripe instead of
  only the total cost, improving payment tracking and reconciliation
- Add stripe_session_id column to payments table for session tracking
- Add success/cancel callback routes for Stripe redirect flow
- Update payment views to use standard form submission instead of
  embedded Stripe Checkout.js
- Update tests to use RSpec mocks for Checkout Session API
@li-xinwei li-xinwei closed this Mar 5, 2026
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.

3 participants