Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
import type { PaymentMethodCreateParams, PaymentMethodUpdateParams } from '@spree/admin-sdk'
import { CAPTURE_METHODS } from '@spree/core'
import { requiredMessage } from '@spree/dashboard-ui'
import { z } from 'zod/v4'
// Empty means "inherit from the store" — the API models that as null, so the
// form's empty option maps back to null on submit.
export const CAPTURE_METHOD_OVERRIDES = ['', ...CAPTURE_METHODS] as const
export const paymentMethodBaseFormSchema = z.object({
name: z.string().min(1, { error: requiredMessage('name') }),
description: z.string().optional(),
storefront_visible: z.boolean(),
active: z.boolean(),
capture_method: z.enum(CAPTURE_METHOD_OVERRIDES),
// Provider STI shorthand (e.g. `stripe`). Only set in create mode; the
// top-level type is optional on the shared shape and required on the
// create-only schema below.
type: z.string().optional(),
})
export const paymentMethodCreateFormSchema = paymentMethodBaseFormSchema.extend({
type: z.string().min(1, { error: requiredMessage('payment_method.type') }),
})
export type PaymentMethodFormValues = z.infer<typeof paymentMethodBaseFormSchema>
export const PAYMENT_METHOD_BASE_DEFAULTS: PaymentMethodFormValues = {
name: '',
description: '',
storefront_visible: true,
active: true,
capture_method: '',
}
export const PAYMENT_METHOD_CREATE_DEFAULTS: PaymentMethodFormValues = {
...PAYMENT_METHOD_BASE_DEFAULTS,
type: '',
}
/** Wire `type` values that must not appear in the add-payment-method provider picker. */
export const PAYMENT_METHOD_TYPES_HIDDEN_FROM_PICKER = ['custom_payment_source_method'] as const
/** The providers a merchant may add: not hidden, and not already installed on this store. */
export function filterPaymentMethodProviderTypes<T extends { type: string; installed?: boolean }>(
types: T[],
): T[] {
const hidden = new Set<string>(PAYMENT_METHOD_TYPES_HIDDEN_FROM_PICKER)
return types.filter((entry) => !hidden.has(entry.type) && !entry.installed)
}
export function paymentMethodValuesToCreateParams(
v: PaymentMethodFormValues,
preferences: Record<string, unknown>,
): PaymentMethodCreateParams {
// Schema should have already rejected this — guard against a misuse path
// that would otherwise send an empty `type` and let the server 422.
if (!v.type) throw new Error('payment method type is required')
return {
type: v.type,
name: v.name,
description: v.description?.length ? v.description : null,
active: v.active,
capture_method: v.capture_method || null,
storefront_visible: v.storefront_visible,
...(Object.keys(preferences).length > 0 ? { preferences } : {}),
}
}
export function paymentMethodValuesToUpdateParams(
v: PaymentMethodFormValues,
): PaymentMethodUpdateParams {
return {
name: v.name,
description: v.description?.length ? v.description : null,
active: v.active,
capture_method: v.capture_method || null,
storefront_visible: v.storefront_visible,
}
}