pay gem + stripe gem (https://github.com/pay-rails/pay)
Personmodel:pay_customer default_payment_processor: :stripe- Stripe config via
.envenv vars (not Rails credentials) - No custom webhook controller; pay gem auto-mounts
POST /pay/webhooks/stripe
Fields: amount_cents, amount_cents_remaining, currency, payer_type, person_id/organization_id, pay_charge_id, type, metadata (JSON), check_number, memo.
Subclasses:
- CashPayment: manual cash, created by admins
- CheckPayment: manual check, requires
check_number - ExternalProcessorPayment: auto-created from Stripe charges (linked to
Pay::Charge)
Why duplicate Stripe data?
ExternalProcessorPaymentintentionally mirrors what's in the pay gem'spay_chargestable. We own thePaymentmodel and its STI hierarchy, so we keep our own records rather than coupling business logic to the pay gem's schema.
Polymorphic join: source (Payment / Scholarship / Refund / Discount) → allocatable (EventRegistration).
amountin cents- Tracks how much of a payment has been applied to each registration
- Supports overpayment (one payment → multiple registrations) and partial payments (multiple payments → one registration)
- Reverted allocations use
reverted_id(points back to the original allocation)
method: stripe | check | cash- Stripe refunds: auto-synced from the charge via
PayChargeExtensions - Creates an allocation with negative amount to free up
amount_cents_remainingon the payment
- Simple
amount_cents, creates allocations like a payment - No payer: admin-applied credit
- Belongs to a
Person(recipient), optionally linked to aGrant - On task completion, auto-creates / updates its
Allocationto the event registration
Events::RegistrationsController#create → if cost > 0 and not paid in full:
person.set_payment_processor :stripe- Stripe Checkout Session created with
metadata: { event_registration_id: registration.id } - User completes payment on Stripe
- Stripe sends webhook → pay gem creates
Pay::Charge PayChargeExtensions#create_external_processor_paymentfires (after_save_commit):- Reads
metadata["event_registration_id"] - Creates
ExternalProcessorPayment+Allocationfor remaining cost
- Reads
Events::PublicRegistrationsController#create → same Stripe Checkout flow when payment method is "Credit Card Now".
Events::BulkPaymentsController#create → creates FormSubmission (role: "bulk_payment") → Stripe Checkout with metadata: { form_submission_id: submission.id } → webhook creates ExternalProcessorPayment (no auto-allocation; admin manually allocates).
PaymentsController#create directly creates CashPayment or CheckPayment, no Stripe involved. Admin allocates manually.
Concern mixed into Pay::Charge in config/initializers/pay.rb.
after_save_commit :create_external_processor_payment: dispatches onmetadatakeysafter_save_commit :sync_refunds: creates localRefundrecords from Stripe refund data- Checks
ExternalProcessorPayment.exists?(pay_charge_id:)to avoid duplicates (idempotent)
Stripe is authoritative. Reconciliation reads Stripe and repairs the portal's local records; it never creates a card charge. Cancel payment attempt is the one explicit Stripe write.
- Data health → Membership invoices with payments to reconcile lists invoices with
an unresolved autopay attempt (
pending,unknown,needs_action), a checkout session whose 31-minute window has passed, or a portal-originated membership payment that is unallocated while the invoice still owes. - The invoice admin panel summarizes unresolved attempts and links to Reconcile payments, which lists the attempts, the stored checkout session, and any unallocated Stripe payment. The page performs a read-only lookup of the member's recent PaymentIntents for that invoice.
- A succeeded PaymentIntent is normally recorded by
PayChargeExtensionsthroughPay::Stripe::Charge.sync_payment_intent. If no local payment exists, Record this charge records one throughPaymentServices::RecordStripeChargeafter the admin confirms it; the reconciliation service then applies it withMembership::Reconcile::Payment. - A charge that succeeded but could not be applied marks the attempt
succeededwith anunallocated_paymentnote. The invoice stays in Data health while it owes. - Membership credits and overpayments lists portal-originated membership payments with money left after their invoice was paid, or with no linked invoice. External Stripe payments with no membership link stay out of both checks.
- A credit can be acknowledged on the payment page. Acknowledgement hides the payment from both Data health checks without changing the payment or its remaining amount. Remove the acknowledgement to show it again.
- Cancel payment attempt handles any unresolved attempt with a PaymentIntent. It cancels the PaymentIntent (no money has moved) and then runs the normal state machine so the attempt becomes failed and the invoice can be retried. If Stripe says the payment already succeeded, it reconciles instead of canceling. It is a business action like expiring an abandoned Checkout Session, not reconciliation rewriting Stripe data.
- Mark failed: no charge found is only available when no PaymentIntent was ever stored. A known PaymentIntent is always decided by Stripe.
STRIPE_PUBLIC_KEY=pk_test_... # Publishable key
STRIPE_PRIVATE_KEY=sk_test_... # Secret key
STRIPE_WEBHOOK_RECEIVE_TEST_EVENTS=true # Allow test-mode events without webhook cycle
STRIPE_SIGNING_SECRET=whsec_... # From stripe listen CLI
https://docs.stripe.com/stripe-cli
stripe listen --forward-to localhost:3000/pay/webhooks/stripeThe webhook endpoint POST /pay/webhooks/stripe is auto-mounted by Pay::Engine (Rails engine mounted at /pay). No route configuration needed.
Copy the whsec_... secret from the stripe listen output into STRIPE_SIGNING_SECRET in .env.
stripe trigger payment_intent.succeededThis simulates a complete payment. The charge won't have meaningful metadata (event_registration_id, etc.), so PayChargeExtensions won't create a local payment, but it confirms the webhook pipeline works.
To test end-to-end, go through registration in the browser. Stripe test mode accepts 4242 4242 4242 4242 (and other test card numbers) without real money.
For production, configure in Stripe Dashboard → Developers → Webhooks:
https://YOUR_DOMAIN/pay/webhooks/stripe
payment_intent.succeeded/charge.succeeded→ triggersPayChargeExtensionscharge.refunded→ triggers refund sync
All handled by pay gem; no custom webhook controller.