zahls.ch
TWINT, cards and PostFinance (Switzerland)
@zahls/medusa-plugin
zahls.ch payment provider for Medusa v2.
This plugin lets a Medusa application create and manage zahls.ch Gateway checkouts from the backend. It supports:
- Hosted checkout, where the customer is redirected to the zahls.ch payment page (Copy to clipboard
session.data.link) - Swiss payment methods such as TWINT, cards, and PostFinance
- Captures for authorized / reserved transactions
- Refunds through zahls.ch transactions
- Medusa's built-in payment webhook route for asynchronous status updates, with optional HMAC signature verification
The plugin never handles raw card data directly. zahls.ch credentials remain on the Medusa backend.
Compatibility
- Medusa v2.18.x
- zahls.ch Gateway API
Install
1npm install @zahls/medusa-plugin
Configure Medusa
Register the plugin and payment provider in Copy to clipboardmedusa-config.ts:
1234567891011121314151617181920import { defineConfig } from "@medusajs/framework/utils"export default defineConfig({plugins: [{resolve: "@zahls/medusa-plugin",options: {},},],modules: [{resolve: "@medusajs/medusa/payment",options: {providers: [{resolve: "@zahls/medusa-plugin/providers/zahls",id: "zahls",options: {apiKey: process.env.ZAHLS_API_KEY,instance: process.env.ZAHLS_INSTANCE,
After the application starts, enable zahls.ch for the relevant region in Medusa Admin → Settings → Regions. Per Medusa's payment-provider model, the resulting provider identifier is Copy to clipboardpp_zahls_zahls when the service identifier is Copy to clipboardzahls and the configured provider Copy to clipboardid is Copy to clipboardzahls.
Configuration Options
Option Required Description Copy to clipboardapiKey Yes Instance API secret from zahls.ch → API & Integrations. Keep it server-side. Copy to clipboardinstance Yes Instance name (Copy to clipboardexample for Copy to clipboardexample.zahls.ch). Copy to clipboardwebhookSecret Yes Signing key for Copy to clipboardX-Webhook-Signature verification. Webhooks are rejected without it. Copy to clipboardsuccessRedirectUrl No Storefront URL after a successful payment. Copy to clipboardfailedRedirectUrl No Storefront URL after a failed payment. Copy to clipboardcancelRedirectUrl No Storefront URL after the customer cancels. Copy to clipboardskipResultPage No Skip the zahls.ch result page (default Copy to clipboardtrue).
Auth uses the Copy to clipboardX-API-KEY header (recommended by the zahls.ch / Payrexx REST API).
Hosted Checkout
The plugin creates a zahls.ch Gateway and stores the returned checkout Copy to clipboardlink in the payment-session data. The storefront should redirect the customer to that URL:
1234const link = paymentSession.data?.linkif (typeof link === "string") {window.location.href = link}
Use backend / webhook state as the source of truth. The storefront should not treat the redirect alone as proof of payment success.
When available, customer name, email, company, and billing address from the Medusa payment context are prefilled on the Gateway.
Webhooks
Medusa provides a built-in webhook listener route for payment providers at:
1/hooks/payment/[identifier]_[provider]
For this plugin, with service identifier Copy to clipboardzahls and provider Copy to clipboardid: "zahls", add this URL in the zahls.ch merchant backend (Webhooks), with JSON content type:
1https://your-medusa-backend.com/hooks/payment/zahls_zahls
The plugin verifies Copy to clipboardX-Webhook-Signature when Copy to clipboardwebhookSecret is set, loads the Gateway from zahls.ch, maps the status to a Medusa payment action, and returns the payment session reference (Copy to clipboardreferenceId) back to Medusa.
zahls.ch status Medusa webhook action Copy to clipboardconfirmed Copy to clipboardcaptured Copy to clipboardauthorized / Copy to clipboardreserved Copy to clipboardauthorized Copy to clipboardwaiting Copy to clipboardpending_authorization Copy to clipboardcancelled Copy to clipboardcanceled Copy to clipboardfailed / Copy to clipboarddeclined Copy to clipboardfailed
Copy to clipboardreferenceId on the Gateway is set to the Medusa payment session id so webhooks can resolve the session.
What the Plugin Stores
The payment-session data returned by the provider includes:
- Copy to clipboard
id— zahls.ch Gateway id - Copy to clipboard
hash - Copy to clipboard
link— hosted checkout URL - Copy to clipboard
referenceId— Medusa payment session id - Copy to clipboard
status - Copy to clipboard
amountand Copy to clipboardcurrency - Copy to clipboard
transactionIdwhen available - Copy to clipboard
lastRefundIdafter a refund
Current Behavior and Limitations
- Checkout is hosted-redirect only. There is no embedded card widget mode.
- Copy to clipboard
authorizePaymentchecks the remote zahls.ch Gateway status rather than performing a separate authorization step. - Copy to clipboard
capturePaymentsucceeds immediately when the Gateway is already Copy to clipboardconfirmed. For Copy to clipboardauthorized/ Copy to clipboardreserved/ Copy to clipboarduncaptured, it calls zahls.ch capture (and falls back to charge if needed). - Copy to clipboard
updatePaymentrecreates the Gateway when amount or currency changes before payment. - Refunds require a successful zahls.ch transaction id on the session.
- Copy to clipboard
webhookSecretis required; unsigned webhooks are rejected.
Sandbox Checklist
- Create a Gateway and complete one successful hosted checkout (e.g. TWINT or card).
- Confirm the storefront redirect to Copy to clipboard
session.data.linkworks. - Verify at least one webhook-driven status update to Medusa.
- Verify Copy to clipboard
X-Webhook-Signaturerejection when the secret is wrong. - Capture an authorized / reserved payment if your zahls.ch flow supports it.
- Verify one full refund and one partial refund.
- Verify canceled and failed checkouts map cleanly back into Medusa session state.
Local Development
12npm run buildnpm run dev
12npm run test:unitnpm run test:integration:modules
Module integration tests need PostgreSQL (Copy to clipboardDB_HOST, Copy to clipboardDB_USERNAME, Copy to clipboardDB_PASSWORD, Copy to clipboardDB_PORT). Defaults: Copy to clipboardlocalhost:5432, user Copy to clipboardpostgres.
Publish locally with Copy to clipboardnpx medusa plugin:publish, then in a Medusa app:
1npx medusa plugin:add @zahls/medusa-plugin
License
MIT
