Overview
This guide walks you through building a custom payment method integration for Spree. By the end, you’ll have a fully functional payment gateway that:- Appears as a payment option during checkout
- Uses Payment Sessions for PCI-compliant payment collection
- Supports 3D Secure and alternative payment methods
- Handles webhooks for reliable payment confirmation
- Optionally supports saving payment methods for future use (Payment Setup Sessions)
Step 1: Create the Payment Method Model
Create a new model inheriting fromSpree::PaymentMethod. This is the central class that defines how your gateway processes payments.
app/models/my_gateway.rb
Step 2: Register the Payment Method
Add your gateway to the list of available payment methods so it appears in the admin panel:config/initializers/spree.rb
Show a logo and setup guide (optional)
Payment methods backed by an external provider also appear in the admin dashboard under Settings > Integrations, next to other connected services. A method is listed there when it inherits fromSpree::Gateway, or when its class defines self.third_party? to return true. Declare a logo and a link to your setup guide so merchants recognise the provider and know how to connect it:
app/models/my_gateway.rb
logo_url accepts anything an image tag accepts, including a data: URI if you don’t want to host the file. Both are optional: without a logo the card shows the provider’s first letter, and without a guide the link is hidden. Integrations declare the same two methods — see custom delivery rate providers.
Step 3: Add Payment Session Support
Payment Sessions are the modern, PCI-compliant way to handle payments. Your gateway creates a session with the provider, the frontend collects payment details using the provider’s SDK, and Spree records the result.Define the STI Subclass
Create a Payment Session subclass for your gateway. This uses Single Table Inheritance (STI) on thespree_payment_sessions table:
app/models/spree/payment_sessions/my_gateway.rb
Implement Session Methods on the Gateway
Override the key methods on yourPaymentMethod subclass:
app/models/my_gateway.rb
remove_payment_source(source). Spree calls it from a background job after a customer’s saved card is removed, and retries it if it raises. Delete the card at your provider, and treat a card the provider no longer has as removed. The default does nothing, which suits providers whose saved cards the storefront lists from Spree, as with Stripe.
How the Frontend Uses It
The frontend creates a session, then uses the provider’s SDK to collect payment:Important: Always create the payment session after shipping is selected. If the order total changes (shipping rate change, coupon applied), update the session with the new amount (
client.carts.paymentSessions.update) rather than creating another one. The complete call in step 5 only handles payment — step 6 finalizes the order.Step 4: Handle Webhooks (Recommended)
Webhooks ensure payments are captured even if the customer closes their browser after paying. Spree provides a generic webhook endpoint atPOST /api/v3/webhooks/payments/:payment_method_id — you don’t need to create your own controller or routes.
Your gateway just needs to implement parse_webhook_event to normalize the provider-specific payload:
app/models/my_gateway.rb
- Verifies the signature synchronously (returns
401ifWebhookSignatureErroris raised) - Enqueues a background job (
Spree::Payments::HandleWebhookJob) for async processing - Returns
200 OKimmediately to prevent provider retries - The job creates/updates the
Paymentrecord, marks the session completed, and completes the order
payment_method.webhook_url — register this with your provider during setup.
Supported Webhook Actions
Return
metadata: beside the action to hand provider data to the session’s
apply_settlement_metadata. It crosses a background job, so keep it JSON-safe
with string keys.
Providers that answer later
If your provider accepts a capture, void or refund and reports the outcome later by webhook, answer those verbs withrequested: true:
app/models/my_gateway.rb
requested? only when the provider has accepted the request and will
report its outcome by webhook. The payment or refund then waits as requested,
and parse_webhook_event must return the outcome with the reference of the
record it settles. Don’t look the record up there: the outcome can arrive before
Spree has written it, so Spree’s job looks it up and waits for it.
app/models/my_gateway.rb
:capture_succeeded, :capture_failed, :void_succeeded,
:void_failed, :refund_succeeded and :refund_failed. The default
modification_record(action, reference) finds a capture event or refund by
transaction_id, and a void’s payment by its response code; override it if
your provider names them otherwise. If your sessions settle as captured, override the session’s
capture_reference_for_settlement(payment) to return the provider’s capture
reference, so a capture failure reported later finds its capture event. See
Payments for
what each outcome does.
Step 5: Control Payment Method Visibility (Optional)
Override visibility methods to control where and when your payment method appears:app/models/my_gateway.rb
Step 6: Add Payment Setup Session Support (Optional)
If your provider supports saving payment methods for future use (like Stripe’s SetupIntent), you can add Payment Setup Session support:app/models/my_gateway.rb
app/models/spree/payment_setup_sessions/my_gateway.rb
Gateway Options Reference
For every gateway action (authorize, purchase, capture, void, credit), Spree passes a hash of gateway options:Related Documentation
- Payments - Payment architecture and core concepts
- Checkout Customization - Customizing the checkout flow
- Events - Subscribe to payment events
- Stripe Integration - Reference implementation using Stripe
- Adyen Integration - Reference implementation using Adyen

