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).
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).
1. Card registration
startRecurringPayment works like startPayment, with two extra fields: customer and recurring.
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: '[email protected]',
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; themethodfield is ignored. - The second (
config) argument is the same as forstartPayment:redirectUrlorurls.startTokenPaymentalso accepts a{ redirectUrl }config. - The request sends
threeDSReqAuthMethod: '02'(registered user) by default. Set thethreeDSReqAuthMethodfield to send'01'(guest) or'05'(registered with a third-party identifier) instead. - The other
startPaymentorder fields (items,delivery,timeout,maySelect*and so on) work here too. - If a
recurringfield 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.
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 and IPN. The card registration IPN has two extra fields:
cardMask: the masked card number, for examplexxxx-xxxx-xxxx-1234expiry: the card's expiry, for example2028-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:
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: '[email protected]',
language: 'EN',
})
// response.transactionId identifies the charge
await markTokenUsed(nextUnusedToken)- The call goes to SimplePay's
/dorecurringendpoint withtype: 'MIT'andthreeDSReqAuthMethod(default'02', overridable with thethreeDSReqAuthMethodfield). - No redirect is needed; the response (
SimplePayTokenResponse) has nopaymentUrl. - An IPN reports the result, just like a one-time payment.
- Use each token once. When the tokens run out or
untilhas 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).
import { cancelCard } from 'simplepay-js-sdk'
const response = await cancelCard(registrationTransactionId)
if (response.status === 'DISABLED') {
await deleteTokensAndCard(customerId) // the remaining tokens can go too
}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.
