Open MercatoDocsSearch docsAPI reference

Framework

Shipping & Payment Providers

When a sales document needs a delivery price or a payment option, a sales provider can supply it. Register the provider under a stable identifier, then check the method available to an operator on a quote or order.

The sales module now discovers shipping and payment providers so modules can plug in custom pricing and checkout flows without forking core logic.

Register a provider

Providers are registered at module load time and exposed on both the backend (for calculations) and the admin UI (for configuration forms).

// src/modules/your-module/index.ts
import { registerPaymentProvider } from '@open-mercato/core/modules/sales/lib/providers'

registerPaymentProvider({
  key: 'your-gateway',
  label: 'Your Gateway',
  description: 'Redirects buyers to your hosted checkout.',
  settings: {
    // Optional UI schema rendered in admin forms
    fields: [
      { key: 'checkoutUrl', label: 'Checkout URL', type: 'url', required: true },
      { key: 'apiKey', label: 'API key', type: 'secret' },
    ],
    // Optional validation for providerSettings
    schema: z.object({
      checkoutUrl: z.string().url(),
      apiKey: z.string().min(1).optional(),
    }),
  },
  // Optional hook: return surcharge adjustments (kind defaults to "surcharge")
  async calculate({ document, context, settings }) {
    const fee = Number(settings.applicationFee ?? 0)
    if (!fee) return { adjustments: [] }
    return {
      adjustments: [
        {
          amountNet: fee,
          amountGross: fee,
          currencyCode: context.currencyCode,
          metadata: { applied: true },
        },
      ],
    }
  },
})

Shipping providers follow the same shape via registerShippingProvider and can emit shipping adjustments.

// src/modules/your-module/index.ts
import { registerShippingProvider } from '@open-mercato/core/modules/sales/lib/providers'

registerShippingProvider({
  key: 'priority-courier',
  label: 'Priority courier',
  description: 'Weight-based express shipping.',
  settings: {
    fields: [
      { key: 'base', label: 'Base fee', type: 'number', required: true },
      { key: 'perKg', label: 'Per-kg rate', type: 'number', required: true },
    ],
  },
  async calculate({ metrics, settings, context, method }) {
    const base = Number(settings.base ?? method.baseRateNet ?? 0)
    const perKg = Number(settings.perKg ?? 0)
    const amount = base + metrics.totalWeight * perKg
    if (!amount) return { adjustments: [] }
    return {
      adjustments: [
        {
          amountNet: amount,
          amountGross: amount,
          currencyCode: context.currencyCode,
          metadata: { applied: true, weight: metrics.totalWeight },
        },
      ],
    }
  },
})

Runtime behaviour

Default providers

Out of the box the sales module registers:

Stripe is available as an opt-in helper:

import { registerStripeProvider } from '@open-mercato/core/modules/sales/lib/providers'

registerStripeProvider()

You can extend or replace these by registering providers in your own modules; registry calls are idempotent, so re-registering a key overwrites the previous handler.

Admin configuration

The sales settings page (/backend/config/sales) now includes Shipping Methods and Payment Methods sections built with the provider registry:

These settings drive the provider calculations during quote/order creation and updates, ensuring shipping costs and payment fees stay in sync with the configured provider rules.

Previous: Routes and pages · Next: ACL / Features