Skip to main content

Medusa

To accept payments through MONEI in your Medusa v2 store, install and configure the official medusa-payment-monei plugin. It adds one Medusa payment provider for each MONEI payment method and store routes for saved cards and the live payment status.

Live demo

This page describes plugin version 1.0.0 and later

Version 1.0.0 is a full rewrite with breaking changes. If you use 0.1.x, read Upgrade from 0.1.x before you update.

Requirements​

ComponentMinimumTested up to
Medusa2.17.02.22.3
Node.js22.22.024

You also need:

  • A MONEI account and an API key: a scoped API key (plugin 1.3.0 or later) or the default API key.
  • A public URL for your Medusa backend. MONEI sends the payment callbacks to it.
  • The Redis event bus in production. See Callbacks.
Use Medusa 2.17.1 or later with the Redis event bus

Medusa 2.17.0 did not start in our tests with the Redis event bus in the default worker mode. The plugin works on 2.17.0 with the local event bus.

Before you begin​

To test your integration:

Install​

Install the package in your Medusa backend:

npm install medusa-payment-monei

The example storefront is the Medusa Next.js starter with every MONEI flow on this page added. Use it as a reference for your own storefront.

Configure​

Add the plugin to plugins and the payment provider to the Payment Module in medusa-config.ts:

import {defineConfig} from '@medusajs/framework/utils';

module.exports = defineConfig({
// ...
plugins: ['medusa-payment-monei'],
modules: [
{
resolve: '@medusajs/medusa/payment',
options: {
providers: [
{
resolve: 'medusa-payment-monei/providers/monei',
id: 'monei',
options: {
apiKey: process.env.MONEI_API_KEY
}
}
]
}
}
]
});

Set the environment variables:

# .env
MONEI_API_KEY=pk_test_xxxxxxxxxxxxxxxxxxxxx
MEDUSA_BACKEND_URL=https://medusa.example.com

MEDUSA_BACKEND_URL is the public URL of your Medusa backend. MONEI sends the payment callbacks to it, so it must be reachable from the internet. Medusa also uses this variable as the default for admin.backendUrl. Set it in the backend's environment: your storefront can use a variable with the same name for another URL.

Use a scoped API key​

From plugin 1.3.0, the plugin can use a scoped API key (mk_…) instead of the default API key (pk_…). A scoped key reaches only the scopes that the plugin needs.

  1. Create a scoped API key with these scopes:

    • payments:read, payments:create, payments:manage and payments:refund, for payments.
    • customers:read and customers:write, for saved cards.
    • applePayDomains:write, for the Apple Pay domain registration.
  2. Set the secret key and its signing secret in the backend environment:

    # .env
    MONEI_API_KEY=mk_test_xxxxxxxxxxxxxxxxxxxxx
    MONEI_SIGNING_SECRET=mksec_xxxxxxxxxxxxxxxxxxxxx
  3. Add signingSecret to the plugin options in medusa-config.ts. Do not set accountId.

    options: {
    apiKey: process.env.MONEI_API_KEY,
    signingSecret: process.env.MONEI_SIGNING_SECRET
    }

The plugin does not start with a scoped key and no mksec_… signing secret.

Before you change from a pk_… key

Wait until no card, Bizum, PayPal, Multibanco or MB WAY payment is pending and Medusa has processed every callback that MONEI sent, including retries. The callbacks of payments created with the pk_… key have no v2 signature, so the plugin refuses them after the change.

To change to a new scoped key:

  1. Set signingSecret to [newSecret, oldSecret] on every backend instance and worker.
  2. Then set MONEI_API_KEY to the new key.
  3. Keep the old key until its payments are complete and Medusa has processed their callbacks, including retries. Then revoke the old key and remove its secret. After a revoke, the callbacks of its payments have no v2.
The provider id must be monei

The callback URLs and the store routes use this id. Another id breaks them.

Options​

OptionTypeDefaultWhat it does
apiKeystringrequiredYour MONEI API key: the default API key (pk_…) or a scoped API key (mk_…). A test mode key creates test payments.
signingSecretstring or string[]—Required with a scoped API key: its signing secret (mksec_…). During a key change, an array with the new secret first. Do not set it with a pk_… key.
backendUrlstringMEDUSA_BACKEND_URLThe public base URL of your Medusa backend. Set it here only to use another value than MEDUSA_BACKEND_URL. It must be an absolute http or https URL.
accountIdstring—Partner accounts only: the merchant account to act for. Do not set it with a scoped API key.
capturebooleantruetrue charges at once (SALE). false only authorizes card and PayPal payments, and you capture them later. See Capture mode.
releaseUnorderedHoldsAfterMinutesnumber30Minutes after an authorization after which the plugin cancels it when its cart has no order. 0 turns this off. A whole number from 0 to 10080 (7 days).

capture must be a boolean and releaseUnorderedHoldsAfterMinutes a number. The plugin refuses a string such as "false" or "30", so do not pass an environment variable to them without converting it.

Enable the payment methods on your regions​

The plugin registers one Medusa payment provider for each payment method. Go to Medusa Admin → Settings → Regions, edit each region and select the MONEI providers you want to offer in Payment Providers.

Payment methodMedusa provider idLabel in Medusa Admin
Card, Apple Pay and Google Paypp_monei_moneiMonei (MONEI)
Bizumpp_monei-bizum_moneiMonei Bizum (MONEI)
PayPalpp_monei-paypal_moneiMonei Paypal (MONEI)
MB WAYpp_monei-mbway_moneiMonei Mbway (MONEI)
Multibancopp_monei-multibanco_moneiMonei Multibanco (MONEI)

Each provider offers only its own payment method. The method must also be active in MONEI Dashboard → Settings → Payment methods.

Supported payment methods​

  • Card (Visa, Mastercard, and more), with for saved cards
  • Apple Pay and Google Pay, through the card provider. The Hosted Payment Page shows them next to the card form when the buyer's device supports them. In the embedded and express checkout, the monei.js PaymentRequest button shows them.
  • Bizum (Spain)
  • PayPal
  • MB WAY (Portugal)
  • Multibanco (Portugal)

MONEI uses the customer's billing country to decide which payment methods it can offer. Send the cart's billing country as billing_country when you create the payment session. See Session data.

Payment flows​

The customer picks a MONEI payment method in the payment step of your storefront, and your storefront creates a Medusa payment session for that provider. The plugin creates a MONEI payment for the session. It does not charge yet.

There are two ways to let the customer pay: send them to the MONEI , or embed monei.js components in your checkout. In both, MONEI sends a callback to your backend, and the callback completes the cart when the customer does not come back to your storefront.

Hosted Payment Page​

  1. Create the session with return_url, the absolute URL of your return page:

    await sdk.store.payment.initiatePaymentSession(cart, {
    provider_id: 'pp_monei_monei',
    data: {return_url: 'https://shop.example.com/checkout/monei-return'}
    });
  2. On Place order, send the customer to session.data.redirect_url.

  3. The customer pays on the MONEI page and comes back to return_url. The plugin adds a session_id query parameter to it, and also monei_canceled=1 when the customer cancels.

  4. On the return page, call cart.complete. When the cart is not complete yet, poll the cart and GET /store/monei/payment-status until the order exists or the payment fails.

Without return_url, the customer stays on the MONEI page after the payment. The callback still completes the cart.

Detect a cancel with monei_canceled=1

A payment that the customer cancels on the Hosted Payment Page stays PENDING, and Medusa records no failure. Use monei_canceled=1 to detect a cancel, and GET /store/monei/payment-status to detect a failed payment.

The example storefront creates a new session each time the customer submits the payment step. When Medusa deletes the old session, the plugin cancels its MONEI payment.

Embedded components (monei.js)​

Use monei.js to show the payment form in your own checkout.

  1. Create the session with monei_session_id, a value your storefront keeps for the whole cart (the example storefront uses the cart id).
  2. Mount the components:
    • Card fields and the Apple Pay and Google Pay button (PaymentRequest) mount with your MONEI account ID and the same monei_session_id. MONEI accepts their token only for a payment created with that value.
    • Bizum and PayPal mount with session.data.id as paymentId.
  3. On Place order, get the one-time token and confirm the payment. For a card token, also send the cardholder name: MONEI refuses a card payment without it.
  4. When the payment is SUCCEEDED or AUTHORIZED, call cart.complete. Otherwise go to your return page and let the callback complete the cart.
import {CardInput, confirmPayment} from '@monei-js/react-components';

// Mount the card fields with your account ID and the monei_session_id of the session
<CardInput ref={card} accountId={MONEI_ACCOUNT_ID} sessionId={cart.id} />;

// On "Place order"
const {token, error} = await card.current.submit();
if (error) {
setError(error);
return;
}
const {status} = await confirmPayment({
paymentId: session.data.id,
paymentToken: token,
paymentMethod: {card: {cardholderName}}
});
if (status === 'SUCCEEDED' || status === 'AUTHORIZED') {
await sdk.store.cart.complete(cart.id);
}

3D Secure and the Bizum approval run in the monei.js modal. When confirmPayment throws, read GET /store/monei/payment-status before you show an error: a lost response can hide a payment that went through.

PayPal with capture: false

Mount every PayPal component, embedded and express, with transactionType: "AUTH" when capture is false. Otherwise PayPal cannot pay the payment. Read the mode from GET /store/monei/config.

Do not give a container element the id monei-paypal: the PayPal SDK reads window["monei-paypal"] as its namespace.

Session data​

Your storefront can send these keys in data when it creates a MONEI payment session:

KeyTypeWhat it does
return_urlstringThe absolute URL where MONEI sends the customer back from the Hosted Payment Page. Embedded flows do not need it.
monei_session_idstringThe monei.js sessionId that the card, wallet and express components mount with.
billing_countrystringThe ISO 3166-1 alpha-2 code of the cart's billing address, for example ES.
save_cardbooleanCard provider and signed-in customers only: true saves the card. See Saved cards.
expected_amountnumber or stringExpress checkout: the amount the wallet showed, in major units. The plugin refuses the session when it differs from the cart amount.

The plugin reads the billing country when it creates the session. When the customer changes the billing country after that, create a new session.

session.data then holds the MONEI payment: id, session_id, amount (major units), currency, status, redirect_url and monei_customer_missing. Do not send session.data back when you create a session: the plugin refuses (400) a request whose data holds a key that the plugin sets.

Express checkout​

Express checkout puts Apple Pay, Google Pay and PayPal buttons on your storefront. The customer pays from the wallet sheet. Apple Pay and Google Pay give you the customer's email, addresses and shipping choice, so nobody fills in the checkout form. PayPal gives only the payer's name, email and country, not a delivery address: the example storefront keeps the delivery address that the cart already has.

  1. Mount the buttons with your MONEI account ID and a monei_session_id.
  2. When the customer approves, the wallet returns a one-time token and the customer details. Put the details (email, addresses, shipping option) on the cart.
  3. Create the session with expected_amount, the amount the wallet showed, and the same monei_session_id.
  4. Confirm the payment with the wallet token, as in the embedded flow. When the wallet returns a card token, send the cardholder name from the wallet billing details.

The example storefront refuses a wallet result without an email address, because express checkout has no form to collect it.

Apple Pay in the embedded and express checkout runs on your storefront domain, and Apple needs that domain registered. The plugin registers it for you about one minute after the backend starts: every https origin in STORE_CORS (Medusa's storeCors), except localhost and IP addresses. For this to work:

  1. Serve the domain association file on your storefront at /.well-known/apple-developer-merchantid-domain-association, with HTTP 200 and no redirect. The example storefront ships it.
  2. Put your storefront origin in STORE_CORS, for example STORE_CORS=https://shop.example.com.
  3. Check the backend log for MONEI Apple Pay: registered shop.example.com. If MONEI refuses the domain, the log shows a warning with the reason. You can also register the domain in MONEI Dashboard → Settings → Payment methods → Apple Pay.

The Apple Pay or Google Pay button only appears for a customer whose device and browser can pay with that wallet.

For the equivalent in a custom integration, see Express checkout.

Saved cards​

A signed-in customer can save a card and pay with it later. The plugin creates a MONEI customer for each signed-in Medusa customer. Card details are stored on MONEI servers, never in your Medusa database.

  1. Create the card session with save_card: true. After the payment, MONEI saves the card to the customer. For a guest, the card is not saved.
  2. List the saved cards with GET /store/monei/payment-methods, and delete one with DELETE /store/monei/payment-methods/:id.
  3. Pay with a saved card through POST /store/monei/confirm. Your storefront sends only the id from the list: the card token never reaches the browser. The route pays only a card session that the customer created while signed in.

Callbacks​

The plugin registers a callback URL on every payment it creates, so you do not need a webhook in the MONEI Dashboard. MONEI sends each payment update to:

<backendUrl>/hooks/payment/<provider>_monei

<backendUrl> is the backendUrl option, or MEDUSA_BACKEND_URL when the option is not set.

For example, https://medusa.example.com/hooks/payment/monei_monei for card payments and https://medusa.example.com/hooks/payment/monei-bizum_monei for Bizum. Each callback has a signature in the MONEI-Signature header. With a pk_… key, the plugin verifies v1 with that key. With a scoped key, it verifies v2 with signingSecret. Then the plugin gets the payment status from MONEI before it acts on it.

With a pk_… key, the plugin also accepts the events of a webhook in the MONEI Dashboard, but then each payment reaches Medusa twice. If you added one for Medusa, delete it. With a scoped key, the plugin refuses these events, so capture and cancel payments in Medusa Admin, not in the MONEI Dashboard.

If MONEI cannot read the signing secret of a scoped key, the callback has no v2 and the plugin refuses it. The checkout can still complete when the customer comes back to the storefront. Check the MONEI Dashboard for payments whose cart stays open.

Use the Redis event bus in production

Medusa delivers MONEI callbacks through its event bus. When the plugin cannot get a payment from MONEI, the Redis event bus retries the event. The local event bus drops it, so the cart of a paid payment can stay open.

Capture mode​

capture decides when the money moves.

capture: true (default) — the customer is charged as they pay (SALE).

capture: false — card and PayPal payments are only : the money is held on the customer's card or PayPal account. You it from the order in Medusa Admin → Orders. Bizum, MB WAY and Multibanco always charge at once, whatever this option says.

Capture in full, within 7 days

The plugin captures a payment in full only, and refuses a partial capture in Medusa Admin. A card authorization expires after about 7 days: capture before then.

Use capture: false when stock can run out while the customer pays. When that happens, Medusa creates no order, and with capture: false no money is taken. releaseUnorderedHoldsAfterMinutes (default 30) after the authorization, the plugin cancels it when the cart still has no order. An order that exists keeps its authorization. The plugin schedules this check when the authorization callback reaches your backend. With the local event bus, a restart loses the scheduled check.

Set a longer delay, or 0, if your store creates orders long after the payment, for example from a back office. When the plugin cannot cancel an authorization, it logs an error and emits the monei.hold_release_failed event with payment_id, session_id, cart_id, amount, currency and reason. Subscribe to it to alert your team, and cancel the payment in the MONEI Dashboard if the cart has no order.

With capture: true, when the MONEI callback completes a paid cart and the completion fails, Medusa refunds the payment.

Refunds​

Refund from the order in Medusa Admin → Orders, in full or in part. The refund reaches MONEI straight away.

A refund made in the MONEI Dashboard does not update the Medusa order. If Medusa says that a refund may have been applied, check the payment in the MONEI Dashboard before you try again.

Store API routes​

The plugin adds these routes. They are under /store, so they need the publishable API key header like every Medusa store route.

RouteAuthBody or queryResponse
GET /store/monei/payment-methodsCustomer{ payment_methods: [{ id, brand, last4, expires_at }] }
DELETE /store/monei/payment-methods/:idCustomer{ id, deleted: true }
POST /store/monei/confirmCustomer{ payment_session_id, payment_method_id }{ status, redirect_url }
GET /store/monei/payment-statusNone?cart_id=...&session_id=...{ status }
GET /store/monei/configNone{ capture }
  • id of a saved card is an opaque id, not the card token.
  • expires_at is in seconds since the Unix epoch.
  • status is a Medusa session status: pending, authorized, captured or error.
  • POST /store/monei/confirm pays only a card session of the signed-in customer's cart.

Orphan payments​

An orphan payment is a paid or authorized MONEI payment whose Medusa session no longer exists or points to another payment. For example, the customer pays a Multibanco reference or approves a Bizum request after a cart change: MONEI cannot cancel those once they are sent.

The plugin logs an error and emits the monei.orphan_payment event with payment_id, session_id, amount, currency and status (AUTHORIZED or SUCCEEDED). The event can fire more than once for one payment. Subscribe to it to alert your team. If no order was created, cancel an AUTHORIZED payment or refund a SUCCEEDED one in the MONEI Dashboard.

Limits​

  • Full capture only. The plugin refuses a partial capture in Medusa Admin (POST /admin/payments/:id/capture). A custom workflow that captures less still captures the full amount at MONEI. See Capture mode.
  • Currencies with 2 decimals only. The plugin refuses other currencies.
  • MONEI Dashboard refunds are not synced to the Medusa order.
  • A Multibanco order is created only when the customer pays. The cart stays open until then, and the callback completes it.

Upgrade from 0.1.x​

Version 1.0.0 is a full rewrite. Before you deploy it:

  • Set capture: false to keep authorizing. 0.1.x authorized by default. 1.0.0 charges at once by default.
  • Move the package to plugins. Add medusa-payment-monei to plugins and change the provider resolve to medusa-payment-monei/providers/monei, as in Configure.
  • Set MEDUSA_BACKEND_URL (plugin 1.1.0 or later), or the backendUrl option (any 1.x version), to the public URL of your backend. webhookSecret is removed: the plugin checks the signature with your API key.
  • Delete the 0.1.x webhook in the MONEI Dashboard (Settings → Webhooks), if you added one. See Callbacks.
  • Enable the new providers on your regions. There is one provider per payment method, and 1.0.0 offers only the supported payment methods. 0.1.x offered every method active on your MONEI account. See Enable the payment methods on your regions.
  • Send return_url when you create a session for the Hosted Payment Page. client_secret is removed from the session data.
  • Check the amounts of your 0.1.x orders. 0.1.x as published sent the Medusa amount as cents, so a 19.99 EUR cart was charged 0.20 EUR. Compare the MONEI amount of one 0.1.x payment with its Medusa order total. When the MONEI amount is 1/100 of the total, refund those orders in the MONEI Dashboard, not from Medusa.

Capture, cancel and refund of orders placed with 0.1.x still work from Medusa Admin. A checkout that a customer started on 0.1.x and did not finish completes only when the customer comes back to the storefront, and 1.0.0 refuses it when its MONEI payment amount is not the cart amount. If the customer paid such a payment and the cart stayed open, cancel (AUTHORIZED) or refund (SUCCEEDED) it in the MONEI Dashboard. The full list of changes is in the plugin README.

Troubleshoot​

A payment method does not appear at checkout​

  • Add its provider to the region in Medusa Admin, and activate the method in MONEI Dashboard → Settings → Payment methods.
  • MONEI uses the customer's billing country to decide which methods it can offer. Send billing_country when you create the session.
  • Apple Pay and Google Pay only appear on a device and browser that can pay with them. Apple Pay also needs your storefront domain registered: check the backend log for MONEI Apple Pay.

The cart stays open after the customer paid​

  • Check that <backendUrl>/hooks/payment/ is reachable from the internet: no IP allowlist and no bot protection in front of it.
  • Use the Redis event bus. The local event bus drops a callback that fails once.
  • A Multibanco cart stays open until the customer pays the reference.

Before you go live​

  • Make sure that you are using live (production) mode API Key.
  • Make sure that you have at least one active payment method, and that its provider is on your regions in Medusa Admin.
  • Set MEDUSA_BACKEND_URL to the public URL of your live backend, and use the Redis event bus.
  • For Apple Pay, put your live storefront origin in STORE_CORS, serve the domain association file, and check that the backend log says MONEI Apple Pay: registered. See Express checkout.