Integration Module 2.0 for Medusa is outRead the announcement
C

Cpay

cPay payment provider for MedusaJS v2 — card payments via cpay.com.mk

npm install medusa-payment-cpay
Category
Payments
Built by
Farukshaban
Version
2.0.1
Monthly downloads
Loading downloads
Last updated

medusa-payment-cpay

cPay payment plugin for MedusaJS v2. Integrates the cPay card payment gateway used in North Macedonia.

The plugin ships three parts, and Medusa loads all of them automatically:

  • a Payment Module Provider () that builds the signed cPay payment form,
  • the webhook route that validates cPay's response, authorizes the payment and places the order,
  • a subscriber that captures the payment in Medusa when the order is placed.

Built against the cPay Merchant Integration Specification v2.6.8 (MD5 checksum, default) with opt-in support for v3.5 (SHA256 checksum).

Supported cards: MasterCard, Maestro, Visa, Diners Club, Domestic cards.

Upgrading from 1.x? Read Upgrade from 1.x first. 2.0 is a breaking release.

How It Works

cPay is a redirect-based payment gateway. Card details are never entered on your site:

The order is placed by the backend when cPay's notification arrives. It does not depend on the customer's browser returning to your storefront.

Requirements

  • Medusa (built and unit-tested with 2.13.0 and 2.23.1)
  • The Node.js version that your Medusa version requires (Medusa 2.23 needs Node 22.22 or later). To build the plugin from source you need Node 22.
  • A cPay merchant account (merchant ID, merchant name, checksum key from your bank)

Installation

Register the plugin and the provider in . The plugin entry loads the webhook route and the subscriber. The provider entry is still necessary: Medusa does not register module providers of a plugin automatically.

Environment variables

The webhook route cannot read provider options. It reads these variables directly from the environment:

VariableUsed for
Validation of the ReturnCheckSum. Must be the provider's .
Must be the provider's . A mismatch is reported in the log as a configuration error.
Browser redirect after payment. Falls back to the first entry of .

Use the same environment variables for the provider options, as in the example above, so that the two cannot differ.

cPay posts data. Medusa parses it by default, so no middleware is necessary.

Enable in Medusa Admin

Go to Settings > Regions > [Your Region] > Payment Providers and enable cpay.

cPay specification versions

v2.6.x (default)v3.x (opt-in)
ChecksumMD5, HMAC-SHA256,
parameternot sent
Payment URLthe URL from your bank (also , , )

The SHA256 implementation matches the example in Appendix A of specification v3.5, but it was not yet run against the cPay system. Ask your bank which version your merchant account uses, and test with before you go live.

Storefront Integration

See the directory for reference implementations:

  • — hidden form + redirect button
  • — callback handler after the cPay redirect

Payment Button

When cPay is selected, the payment button must submit a hidden HTML form via POST. The form parameters come from :

Initiate the session in the usual way. The provider sets the amount itself; the storefront sends no cPay data:

Callback Page

Create a route at . The webhook redirects the browser here with query params:

ParamValue
or
Details2 reference
ID of the order that the webhook placed (success only)
Error message (on failure)
  • with : the order exists. Clear the cart cookie and go to the confirmation page. Do not complete the cart again.
  • without : the payment is authorized, but the webhook could not place the order. Complete the cart from the storefront as a fallback.
  • : show the error with a retry option.

Treat these query params as display hints only. They are not signed. Read the order from Medusa before you show a confirmation.

Payment Info Map

Refunds

only records the refund in Medusa. It does not move money.

Make the refund in the cPay merchant portal (or through your bank) first, then record it in Medusa Admin. An automatic refund needs cPay's credit transaction (), which requires a separate contract with the bank and is not implemented.

Capture

cPay's default transaction is a one-step purchase: the card is charged at the gateway. Medusa only marks the payment as when the cart is completed. The plugin's subscriber listens to and captures every payment of the order (a local state change, no call to cPay). Other providers are not touched.

Webhook Behaviour

A cPay response has no field that says if the payment succeeded. The only signal is which URL cPay calls ( or ). The webhook is strict for this reason. Do not remove these checks:

  1. ReturnCheckSum validation. Every security-critical field must be in the signed header, every declared field must be in the body, and the lengths must match.
  2. must be or . Any other value does nothing.
  3. The session comes from signed data. The session ID is read from in the signed body, not from the request query.
  4. must be present and equal to .
  5. is required for success. cPay generates it only when card data was submitted.
  6. Success and failure cannot contradict each other for the same .
  7. Repeated notifications are no-ops. cPay repeats the PUSH (+15 s, +5 min, +1 h).
  8. A failed session update gives a fail redirect, never a success page.
  9. POST only. A GET handler would put checksum parameters into access logs.

When the customer may have been charged but the webhook cannot continue, it writes one log line that starts with . Set an alert on this text and reconcile these payments by hand with , and the amount.

The order gets , , and in . The full cPay response stays on the payment session ().

Checksum Algorithm

  • Request checksum: merchant sends with payment request
  • Return checksum: cPay sends back (first two params swapped + cPayPaymentRef added)

The utilities are exported for custom integrations:

Upgrade from 1.x

2.0 is a Medusa plugin. 1.x was a provider package with files that you copied by hand.

  1. Delete the copied files from your project: and . If they stay, the route conflicts with the plugin's route and the subscriber runs two times. If you copied the provider (Option B of 1.x), delete too.
  2. Remove the middleware for from .
  3. Change : add the entry, and change the provider from (or ) to . Keep , so the provider ID stays and existing payments still resolve.
  4. Update the storefront callback page: when is in the URL, go to the confirmation page and do not complete the cart again.
  5. Stop sending when you initiate the session. The provider sets it.
  6. Imports changed: is now . The package has no root export.

Behaviour changes that you can notice:

  • is sent as an ISO 3166-1 numeric code (), as digits only. A , or value that does not fit the cPay format is not sent.
  • MKD and ALL amounts are rounded to a whole unit (cPay rejects other amounts). The original amount is kept in .
  • Medusa's generic route answers . Point cPay only at (the provider builds these URLs).
  • Payment sessions that were started on 1.x and return after the upgrade are still found.

Testing

  1. Set for the testing period
  2. cPay provides a 1-month testing window from merchant definition
  3. Test transactions can be submitted from any domain during this period
  4. After going live, switch to the production key from the bank
  5. After going live, payments only work from your registered domain

Important Notes (from cPay spec)

RuleDetail
No iframes/popupscPay form must open as full page redirect
CurrencyMKD for banks in North Macedonia; ALL or EUR for banks in Albania
Amount formatMultiplied by 100. For MKD and ALL the last two digits are always
Details2Unique per payment, max 10 chars alphanumeric
PUSH notificationsOnly on ports 80/443, valid SSL, static IP
Domain restrictionAfter test period, only registered domain works
Forbidden chars and trigger IP block by cPay
ReturnCheckSumMust validate — prevents fake success callbacks

File Structure

Security

To report a vulnerability, see SECURITY.md.

License

MIT