---
url: https://simplepay.webmania.cc/en/guide/recurring-payments.md
description: >-
  SimplePay recurring payments in Node.js: card registration with
  startRecurringPayment, storing tokens, charging a token with
  startTokenPayment, removing a card with cancelCard.
---

# Recurring payments (card storage and tokens)

With recurring payments the customer registers their card during the first payment. SimplePay then issues **tokens**: each token can be used for one later charge without the customer (MIT – merchant initiated transaction).

::: info Prerequisite
Recurring payments must be enabled for your merchant account by SimplePay. Otherwise SimplePay answers with an error code (for example `5044`, see [Error codes](../reference/errors)).
:::

## 1. Card registration

`startRecurringPayment` works like [`startPayment`](./one-time-payment), with two extra fields: `customer` and `recurring`.

```ts
import { startRecurringPayment, toISO8601DateString } from 'simplepay-js-sdk'

const sixMonths = new Date(Date.now() + 182 * 24 * 60 * 60 * 1000)

const response = await startRecurringPayment({
  orderRef: 'sub-2026-0001',
  total: 4990,
  currency: 'HUF',
  customerEmail: 'customer@example.com',
  customer: 'Test Customer',         // customer's name
  recurring: {
    times: 6,                        // number of tokens = number of later charges
    until: toISO8601DateString(sixMonths), // tokens are valid until this date
    maxAmount: 10000,                // maximum amount of one charge
  },
  invoice: { name: 'Test Customer', country: 'HU', state: 'Budapest', city: 'Budapest', zip: '1111', address: 'Fő utca 1.' },
})

// response.paymentUrl – redirect the customer here
// response.tokens     – save these for the customer
await saveTokens(customerId, response.tokens, response.transactionId)
```

Differences from `startPayment`:

* Card (`CARD`) only; the `method` field is ignored.
* The second (`config`) argument is the same as for `startPayment`: `redirectUrl` or `urls`. `startTokenPayment` also accepts a `{ redirectUrl }` config.
* The request sends `threeDSReqAuthMethod: '02'` (registered user) by default. Set the `threeDSReqAuthMethod` field to send `'01'` (guest) or `'05'` (registered with a third-party identifier) instead.
* The other `startPayment` order fields (`items`, `delivery`, `timeout`, `maySelect*` and so on) work here too.
* If a `recurring` field is falsy (`0`, `''`), the SDK uses a default: `times: 3`, `until`: about 180 days from now, `maxAmount: 12000`. The type marks all three fields as required.

The response (`SimplePayRecurringResponse`) contains the usual fields plus a `tokens: string[]` array.

::: warning Tokens are your responsibility
You must store the tokens securely (per customer, together with the registration `transactionId`). The SDK stores nothing.
:::

## 2. Back URL and IPN

Same endpoints as one-time payments: [back URL](./back-url) and [IPN](./ipn). The card registration IPN has two extra fields:

* `cardMask`: the masked card number, for example `xxxx-xxxx-xxxx-1234`
* `expiry`: the card's expiry, for example `2028-12-31T00:00:00+01:00`

Save them so the customer can see which card is registered.

## 3. Charging a token

When the next charge is due (for example in a daily `cron` job), call `startTokenPayment`:

```ts
import { startTokenPayment } from 'simplepay-js-sdk'

const response = await startTokenPayment({
  orderRef: `sub-2026-0001-${Date.now()}`, // new orderRef for every charge
  token: nextUnusedToken,                   // a token not used yet
  total: 4990,                              // at most maxAmount
  currency: 'HUF',
  method: 'CARD',                           // required, must be CARD
  customer: 'Test Customer',
  customerEmail: 'customer@example.com',
  language: 'EN',
})

// response.transactionId identifies the charge
await markTokenUsed(nextUnusedToken)
```

* The call goes to SimplePay's `/dorecurring` endpoint with `type: 'MIT'` and `threeDSReqAuthMethod` (default `'02'`, overridable with the `threeDSReqAuthMethod` field).
* No redirect is needed; the response (`SimplePayTokenResponse`) has no `paymentUrl`.
* An IPN reports the result, just like a one-time payment.
* Use each token once. When the tokens run out or `until` has passed, the customer must register the card again.

## 4. Removing a card

Let customers remove their registered card. `cardId` is the **SimplePay transaction ID of the card registration** (the `transactionId` of the `startRecurringPayment` response).

```ts
import { cancelCard } from 'simplepay-js-sdk'

const response = await cancelCard(registrationTransactionId)

if (response.status === 'DISABLED') {
  await deleteTokensAndCard(customerId) // the remaining tokens can go too
}
```

::: warning HUF account only
`cancelCard` always uses `SIMPLEPAY_MERCHANT_ID_HUF` / `SIMPLEPAY_MERCHANT_KEY_HUF`. Cards registered on other currency accounts cannot be removed with it yet.
:::

Response (`SimplePayCancelCardResponse`): `salt`, `merchant`, `cardId`, `status: 'DISABLED'`, `expiry`.

## Sandbox test cards for recurring payments

| Card number | Result |
|---|---|
| `4908 3660 9990 0425` | Successful payment |
| `5437 5884 9903 4193` | Registration succeeds, payment fails |
| `6757 6192 9705 7001` | Registration fails, payment succeeds |

Details: [Testing](./testing).
