/spree:spree-paymentsfrom spree
Guides working with Spree 6 payments: payment methods, gateways (Stripe, Adyen, PayPal, Razorpay), sessions, statuses, capture timing, refunds, webhooks, store credits, and custom payment methods.
Ships in spree. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
PaymentMethod (pm_, belongs_to :store) — a configured way to pay (STI: SpreeStripe::Gateway, Spree::PaymentMethod::Check, …)
├── PaymentSession (ps_) — gateway-side session (e.g. Stripe PaymentIntent); owned by a cart or order
├── PaymentSetupSession (pss_) — save a payment method for later (e.g. SetupIntent); belongs to a customer
└── GatewayCustomer — provider customer id per customer (internal, encrypted at rest)
Payment (py_) — one attempt against a cart/order (cart_id + order_id; #owner)
├── source: CreditCard (card_) | PaymentSource (wallets, BNPL, bank) | StoreCredit (credit_)
└── Refund (re_) × n — with a RefundReason (rr_)
A payment is linked to its session by payment.response_code == payment_session.external_id. A cart can carry several payments (e.g. store credit + card).
status is a plain string (Spree::HasStatus) written only by the payment workflows and Spree::Payment::Processing:
| Status | Meaning |
|---|---|
checkout | Created, not yet processed (default) |
processing | Claimed for a gateway call (atomic claim; blocks double submission) |
pending | Authorized, not captured |
requested | The provider accepted a capture or void and confirms it later by webhook (Adyen); capture, void and order cancel refuse it until the outcome arrives. payment.requested_action says which was asked |
completed | Captured — only these count toward what's paid |
failed | Gateway rejected it (retryable on the same row) |
void | Voided |
invalid | Superseded by a newer checkout payment on the same cart (has_invalid_status? — invalid? is ActiveModel's). Payments on a placed or draft order never supersede each other, and one created with no amount defaults to what is left to pay |
Capture events and refunds carry their own status: requested, completed or failed (failure_reason on a refund). Money sums count only completed captures and refunds; balance checks hold back every refund that has not failed. A gateway that confirms later answers requested? from capture, void or credit, then reports the outcome by webhook (capture_succeeded, capture_failed, void_succeeded, void_failed, refund_succeeded, refund_failed) through Spree::Payments::HandleModificationWebhook. A 6.0 store upgrading to 6.0.6 runs spree upgrade --step backfill_settlement_statuses once.
The order's payment_status (none, authorized, partially_paid, paid, partially_refunded, refunded, overcharged, voided) is derived by Spree::Orders::UpdateStatuses from payments and refunds (re-run on payment/refund/fulfillment events and by every placed-order money edit: fees, manual discounts, items) — never assign it. "Refunded" counts both ledgers: Spree::Refund rows and store credit issued as a refund (order.store_credit_refunds, StoreCredit#refunded_order_id), so an order refunded to store credit reads partially_refunded / refunded, not paid. Spree::Order.refunded / .partially_refunded scopes filter on this status.
capture_method is a store preference (preferred_capture_method, default checkout) that a payment method can override (capture_method, nil = inherit):
| Value | Behaviour |
|---|---|
checkout | Charged when the order is placed → completed |
on_dispatch | Authorized at checkout (pending), captured when goods are dispatched |
manual | Authorized at checkout (pending); staff capture when they choose |
await admin.store.update({ preferred_capture_method: 'on_dispatch' })
await admin.paymentMethods.update('pm_xxx', { capture_method: 'manual' }) // null → inherit store
await admin.orders.payments.capture('or_xxx', 'py_xxx')
await admin.orders.payments.void('or_xxx', 'py_xxx')
In Ruby: payment_method.resolved_capture_method, Spree.payment_capture_workflow.call(payment:, amount: nil) — amount is in cents; a smaller amount captures part and splits the remainder into a new pending payment. Spree.payment_void_workflow.call(payment:).
| Attribute | Notes |
|---|---|
type | API sends the shorthand (stripe, check, store_credit, …); the column holds the class |
store | Single owner — current_store.payment_methods |
active | Inactive methods are hidden everywhere |
storefront_visible | false = staff-only (e.g. "Invoice" for admin-created orders). Replaces display_on. The Store API payment and payment-session endpoints accept only active.storefront_visible methods — a staff-only method's ID is a 404 there, not a loophole |
capture_method | Override, see above |
preferences | Gateway credentials — YAML in a plain text column, not encrypted; treat DB dumps as secret |
Hooks to override on a subclass: session_required?, setup_session_supported?, source_required?, available_for_order?(order), available_for_store?(store), payment_icon_name. The Store API embeds payment_methods (with session_required) in the cart response.
spree add stripe # adds the gem, runs its installer and migrations
spree add adyen
spree add paypal
spree add --gem spree_razorpay_checkout # not in the catalog; also runs its installer
spree restart
Classic Rails app: bundle add … && bin/rails g …:install. Then create the payment method in the dashboard (Settings → Payments) and register its webhook URL (payment_method.webhook_url → /api/v3/webhooks/payments/pm_…) with the provider. spree_stripe is the reference implementation (sessions, setup sessions, webhooks, wallets, Stripe Connect payouts).
session_required? == trueconst opts = { spreeToken: cart.token }
// 1. After delivery is chosen (amount must include shipping); recreate/update if the total changes
const session = await client.carts.paymentSessions.create(cart.id, { payment_method_id: 'pm_xxx' }, opts)
// 2. Customer pays in the provider SDK with session.external_data.client_secret (3DS/redirects handled there)
// 3. Confirm — creates/settles the Payment, does NOT place the order
await client.carts.paymentSessions.complete(cart.id, session.id, { session_result: '…' }, opts)
// 4. Place the order (Order | OrderGroup on a multi-seller marketplace cart — narrow with isOrderGroup)
const result = await client.carts.complete(cart.id, opts)
Session statuses: pending, processing, completed, failed, canceled, expired (no cancel endpoint — cancel/expire come from the provider or expires_at).
Webhook-first completion. The provider's webhook (POST /api/v3/webhooks/payments/:payment_method_id) is the authoritative signal: Spree finds the payment method by its prefixed ID alone (providers send no API key or store header), sets Spree::Current.store to the payment method's store, verifies the signature against that method's own secret synchronously (401 on failure), enqueues Spree::Payments::HandleWebhookJob, returns 200, then settles the payment and completes the cart via Carts::Complete — the job also runs in the payment method's store, so a non-default store's webhooks work without any host/header routing. Whichever of browser or webhook arrives first wins; the other is a no-op. Replays are handled in two layers: freshness belongs to the provider's signature scheme inside parse_webhook_event (Stripe's Stripe::Webhook.construct_event rejects timestamps older than 5 minutes — a custom gateway must enforce its provider's equivalent), and Spree::Payments::HandleWebhook is idempotent on the payment (a redelivery for an already-completed payment is a no-op under the order lock), so a replayed event can't pay or complete twice. Never treat a redirect back from the provider as proof of payment.
session_required? == falseawait client.carts.payments.create(cart.id, {
payment_method_id: 'pm_check',
metadata: { purchase_order_number: 'PO-12345' }, // optional; amount defaults to total minus store credit
}, opts)
await client.carts.complete(cart.id, opts)
422 codes: payment_session_required (method needs a session), payment_method_unavailable (the method's available_for_order?(cart) is false — also checked when creating a payment session). On completion the payment lands pending (or completed when capture is checkout); staff capture it when the money arrives.
client.customer.paymentSetupSessions.create({ payment_method_id }, { token: jwt }) → confirm with the provider SDK using external_client_secret → .complete(id, { external_data: {} }) creates a CreditCard/PaymentSource. Requires a signed-in customer.
# app/models/my_gateway.rb
class MyGateway < Spree::PaymentMethod
preference :api_key, :string
preference :webhook_secret, :string
def session_required? = true
def source_required? = false
def payment_session_class = Spree::PaymentSessions::MyGateway # STI < Spree::PaymentSession
# `order:` receives the Cart during checkout
def create_payment_session(order:, amount: nil, external_data: {})
total = amount.presence || order.total_minus_store_credits
remote = client.create_session(amount_cents: Spree::Money.new(total, currency: order.currency).cents,
currency: order.currency, reference: order.prefixed_id)
payment_session_class.create!(owner: order, payment_method: self, amount: total, currency: order.currency,
status: 'pending', external_id: remote.id, customer: order.customer,
external_data: { client_secret: remote.client_secret })
end
def update_payment_session(payment_session:, amount: nil, external_data: {})
client.update_session(payment_session.external_id, amount: amount) if amount.present?
payment_session.update!(amount: amount) if amount.present?
payment_session
end
# Verify with the provider, settle the Payment, move the session. Must NOT complete the order.
def complete_payment_session(payment_session:, params: {})
remote = client.retrieve_session(payment_session.external_id)
case remote.status
when 'succeeded', 'authorized'
payment_session.process if payment_session.can_process?
payment_session.settle_payment!(captured: remote.status == 'succeeded') # false → payment stays pending
payment_session.complete unless payment_session.completed?
else
payment_session.fail if payment_session.can_fail?
end
payment_session
end
# Return { action: :captured|:authorized|:failed|:canceled, payment_session:, metadata: {} } or nil to ignore
def parse_webhook_event(raw_body, headers)
raise Spree::PaymentMethod::WebhookSignatureError unless client.valid_signature?(raw_body, headers['HTTP_X_SIGNATURE'], preferred_webhook_secret)
event = JSON.parse(raw_body)
session = payment_sessions.find_by(external_id: event.dig('data', 'session_id'))
return unless session
{ 'payment.succeeded' => { action: :captured, payment_session: session },
'payment.failed' => { action: :failed, payment_session: session } }[event['type']]
end
def payment_icon_name = 'my-gateway'
private
def client = MyProvider::Client.new(preferred_api_key)
end
# config/initializers/spree.rb — core reassigns this registry in its own after_initialize,
# so register inside after_initialize or the entry is wiped
Rails.application.config.after_initialize do
Spree.payment_methods << MyGateway
end
settle_payment!(captured:) is the single settlement path shared by the confirm call and the webhook — it locks the owner, finds or creates the payment, and confirms it; it's safe to race.process, complete, fail, cancel, expire) are non-raising compare-and-swap writes returning false when they lose a race; guards are can_process?, can_complete?, can_fail?…setup_session_supported?, payment_setup_session_class, create_payment_setup_session(customer:, external_data:), complete_payment_setup_session(setup_session:, params:).Spree::Gateway (ActiveMerchant-style authorize/purchase/capture/void/credit on a provider).Extend behaviour without subclassing via workflow hooks: payments.process.validate, payments.capture.before_capture, payments.void.after_void, payment_sessions.complete.after_complete, payments.handle_webhook.after_handle (Spree.hooks.register('payments.capture.validate', ->(wf) { wf.reject!('…') if … })). See spree-workflows.
Refunds are Admin-only (no Store API route) and run through Spree::Refunds::Create (Spree.refund_create_workflow): the refund row commits first, then the gateway is credited; a clean gateway failure removes the uncredited row.
await admin.orders.refunds.create('or_xxx', { payment_id: 'py_xxx', amount: '25.00', refund_reason_id: 'rr_xxx' })
The param is refund_reason_id (older @spree/admin-sdk releases also accepted reason_id, which the API silently drops — the refund then falls back to the store's first refund reason).
Spree.refund_create_workflow.call(payment: payment, amount: 25, reason: reason, refunder: admin_user)
# amount nil → full creditable balance (payment.credit_allowed)
Hooks: refunds.create.validate, .before_refund, .after_refund. Publishes payment.refunded; the order moves to partially_refunded / refunded. Returns-driven refunds (to the original payment or store credit) go through Spree::Returns::Refund — see spree-returns.
Store credit (credit_) | Gift card (gc_) | |
|---|---|---|
| Belongs to | A customer (and store) | A store; optional customer |
| Issued by | Admin (admin.customers.storeCredits.create('cust_xxx', { amount, currency, memo })) or return/exchange workflows (originator) | Admin (admin.giftCards.create, bulk via admin.giftCardBatches.create) |
| At checkout | client.carts.storeCredits.apply(cartId, amount?) / .remove — signed-in customers | client.carts.giftCards.apply(cartId, code) / .remove — guests too; separate from discount_codes |
| Expiry | Never; spent oldest-first (Spree::StoreCredit.oldest_first) | Per card expires_at |
| Statuses | — | active, partially_redeemed, redeemed, canceled (+ expired by date) |
Both pay via Spree::PaymentMethod::StoreCredit and reduce the amount due, not the order total. Both are applied before the customer picks a payment method, never chosen as one: the store-credit method is excluded from cart.payment_methods, and POST /carts/:id/payments with it answers payment_method_unavailable. Collect whatever amount_due is left with a real method. A cart uses a gift card or store credit, never both (each apply refuses while the other is on the cart). A gift card's payment covers min(spendable balance, total) and is resized on every totals recalculation as delivery, discounts or tax move; it's released when nothing is left to pay. Item changes still unapply both (see spree-checkout). Workflows: Spree.gift_card_apply_workflow / gift_card_remove_workflow / gift_card_redeem_workflow / gift_card_cancel_workflow; Spree.store_credit_apply_service / store_credit_remove_service. Cancelling a gift card is refused once it has been spent against. A gift card's code is a bearer credential: Admin API responses show it in full only to callers holding read_gift_cards (masked to the last four characters elsewhere, e.g. on orders), the webhook delivery log stores it as [REDACTED], and company members see it masked on colleagues' orders. Setting customer_id on a gift card through the Admin API needs read_customers too (403 required_permission otherwise). Spree::StoreCreditCategory / StoreCreditType are deprecated shells.
payment.completed, payment.paid, payment.captured, payment.voided, payment.refunded, payment.capture_requested, payment.capture_failed, payment.void_requested, payment.void_failed, refund.requested, refund.failed, order.paid (order fully settled — also per child order on split marketplace checkouts), payment_session.{processing,completed,failed,canceled,expired}, payment_setup_session.*, gift_card.*. There is no gateway log table — for transaction forensics use payment.gateway_dashboard_payment_url or the PaymentSession's external_data. See spree-events-webhooks.
processing: the claim happened but the gateway call/crash didn't finish. failed and processing are claimable, so re-running the workflow (Spree.payment_process_workflow.call(payment:)) retries on the same row — check the provider dashboard first./jobs). Check payment_session.status and cart.completed_at.parse_webhook_event raised WebhookSignatureError — wrong secret, or you parsed a re-serialized body instead of raw_body.payment_session_required: that method needs the session flow.node_modules/@spree/docs/dist/developer/core-concepts/payments.mdnode_modules/@spree/docs/dist/developer/how-to/custom-payment-method.mdnode_modules/@spree/docs/dist/developer/core-concepts/store-credits-gift-cards.mdnode_modules/@spree/docs/dist/integrations/payments/stripe.md (also adyen.md, paypal.md, razorpay.md)Spree::PaymentMethod, Spree::Payment, Spree::PaymentSession, Spree::Payments::*, Spree::Refunds::Create in spree_core; spree/providers/stripe for a full gatewayspree-checkout, spree-workflows, spree-returns, spree-events-webhooks, spree-securityWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub spree/agent-skills --plugin spree39 skills in this plugin
/spree-api-v3Use when the user is calling or debugging Spree 6's REST API v3 — the Store API (storefronts, mobile apps, customers), the Admin API (integrations, back-office tools) or the Seller API (marketplace sellers). Covers auth (publishable/secret keys, JWT audiences), scopes and permissions, prefixed IDs, the cart → order split, checkout `requirements[]`, order statuses, the `{data, meta}` envelope, expand/fields, Ransack filters, errors, idempotency, localization headers and rate limits. Common phrasings include "Spree API", "Store API", "Admin API", "publishable key", "secret key", "X-Spree-Api-Key", "X-Spree-Token", "API scopes", "403 required_scope", "cart_ vs or_", "complete cart", "cart 404 after checkout", "prefixed IDs", "expand", "Idempotency-Key", "429 rate limit", "v3 endpoint", "Admin API OpenAPI spec". For ADDING or documenting (OpenAPI) a new endpoint use spree-resource; for AI agents driving the store over MCP use spree-mcp; for roles/permissions internals use spree-auth-permissions./spree-auth-permissionsUse when the user is working on who can sign in to Spree 6 and what they may do — staff roles and permission keys, secret-key scopes, registering permission scopes for an extension, 403 errors with `required_permission` / `required_scope`, storefront ownership rules (`Spree::Storefront::AccessPolicy`), custom login strategies (SSO, OIDC, Auth0/Okta/Entra/Firebase), JWT audiences, or swapping the customer/admin user class. Common phrasings include "Spree roles", "staff permissions", "add a role", "read_orders permission", "Spree.permissions.register_scope", "API key scopes", "403 Missing permission", "who can see this order", "SSO for the dashboard", "OIDC login", "custom authentication strategy", "Spree.customer_class", "admin_user_class", "CanCanCan in Spree 6". Spree 6./spree-b2bUse when building B2B or wholesale commerce on Spree 6 — companies and buyer organizations (company trees, divisions, members, invitations, buying on a company's behalf), catalogs and negotiated pricing (owned price lists, assignment to companies or customer groups), customer groups, gated storefronts (`storefront_access` login_required / prices_hidden), the Next.js wholesale portal, minimum order quantity / order multiples / order minimums, PO numbers, orders on account (pay by invoice, staff-keyed draft orders, `payment_pending`) and freight shipping (cartons, pallets, unpriced freight rates). Common phrasings include "B2B", "wholesale", "build a B2B store", "pay by invoice", "net terms", "draft order for a company", "company account", "Spree::Company", "division", "negotiated prices", "customer-specific pricing", "price list per company", "MOQ", "order multiple", "case pack", "order minimum", "PO number", "login to see prices", "wholesale portal", "X-Spree-Channel", "freight", "pallet"./spree-catalogUse when the user is working with the Spree 6 product catalog — products, variants, option types/values, the default variant, product types (templates), categories vs collections, custom fields, media (images, video, media library), channel publications, search providers (database, spree_meilisearch, custom), digital assets, or catalog imports/exports. Common phrasings include "create a product", "add variants", "size and color options", "product type", "category tree", "collection", "brand", "custom field on products", "product images", "product video", "primary_media", "product not showing in store", "publish product on channel", "Meilisearch", "reindex", "custom search provider", "digital download", "master variant". Covers the catalog graph and how to read/write it from Ruby, the Admin SDK and `spree api`./spree-checkoutUse when the user is working on Spree 6 checkout — the Cart → Order split, checkout requirements and advisory steps, `Spree::Checkout::Registry` (add_requirement / register_step / base_steps), cart completion (`Spree::Carts::Complete`), `carts.complete` / `carts.add_item` workflow hooks, addresses, payment sessions, guest checkout, cart association/merge on login, or the Store API / `@spree/sdk` checkout sequence. Common phrasings include "checkout broken", "cart won't complete", "why can't this cart complete", "add a checkout step", "require a field before checkout", "require terms acceptance", "skip the delivery step", "order stuck", "double charge", "guest checkout", "cart not found after checkout", "merge carts on login", "custom checkout validation", "block checkout if…". Covers where each customization belongs and how completion guarantees idempotency./spree-cliUse when calling the Spree 6 Admin API from the command line or driving it programmatically as an agent — exploring endpoints, reading or mutating store data, and especially DEBUGGING (inspecting an order/product/customer, checking why a request failed, reproducing a 403/422). The `spree api` command group in `@spree/cli` is a `gh api`-style generic HTTP client: `spree api get|post|patch|delete <path>` plus offline discovery (`spree api endpoints`, `spree api schema`). Common phrasings include "spree api", "spree CLI", "call the admin API from the terminal", "spree api get", "inspect this order", "why is this Spree request failing", "list admin endpoints", "spree auth". Also covers the project-side dev commands agents reach for while debugging (`spree shell`, `spree test`, `spree console`, `spree add`, `spree plugin new`). For SDK/TypeScript integration use spree-typescript-sdk; for raw API protocol details use spree-api-v3.Other plugins' skills for the same job