# Save customers and charge them again

> Save a customer's card, charge it later with a merchant-initiated payment, and start a subscription.

Source: https://docs.tappay.mn/guides/customers-recurring/

## Overview {#overview}

A [customer](https://docs.tappay.mn/glossary.md#customer) is a payer that you keep on your side, with an id that you choose. When the customer saves a card, you can charge it later without the customer being present. This is a merchant-initiated charge, or [MIT](https://docs.tappay.mn/glossary.md#mit), and a [subscription](https://docs.tappay.mn/glossary.md#subscription) repeats it on a schedule.

## Before you begin {#before-you-begin}

Your key needs `customers:write`, `checkout:write` and `payments:write` in its [scope](https://docs.tappay.mn/glossary.md#scope), and you use your [secret key](https://docs.tappay.mn/glossary.md#secret-key), shown below as `sk_test_YOUR_SECRET_KEY`. Use a [sandbox](https://docs.tappay.mn/glossary.md#sandbox) key while you build. TapPay must also enable MIT for your merchant and set its limits, `mit_max_amount` and `mit_frequency`. Until then every MIT charge fails with [RCR-055](https://docs.tappay.mn/errors.md#RCR-055), so ask your partner or TapPay to set them. A [billing plan](https://docs.tappay.mn/glossary.md#billing-plan) describes what a subscription charges. Create it once, as below, and reuse it for every subscription.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/billing-plans" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"name":"Monthly plan","amount":1000,"interval":"MONTH","intervalCount":1}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const billingPlan = await tappay.billingPlans.create({ body: { name: 'Monthly plan', amount: 1000, interval: 'MONTH', intervalCount: 1 } });
console.log(JSON.stringify(billingPlan));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$billingPlan = $tappay->billingPlans->create([
    'body' => [
        'name' => 'Monthly plan',
        'amount' => 1000,
        'interval' => 'MONTH',
        'intervalCount' => 1,
    ],
]);
echo json_encode($billingPlan);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
billing_plan = client.billing_plans.create({"body": {"name": "Monthly plan", "amount": 1000, "interval": "MONTH", "intervalCount": 1}})
print(json.dumps(billing_plan))
```

## Steps {#steps}

### Create the customer {#create-the-customer}

Send your own `customerId` and an optional `name`. The `customerId` is unique inside your merchant, so a repeated id returns a conflict. You use the same id in every later step.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/customers" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"customerId":"cust-'"$(uuidgen)"'","name":"Test Customer"}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const customer = await tappay.customers.create({ body: { customerId: `cust-${crypto.randomUUID()}`, name: 'Test Customer' } });
console.log(JSON.stringify(customer));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$customer = $tappay->customers->create(['body' => ['customerId' => 'cust-' . vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)) . '', 'name' => 'Test Customer']]);
echo json_encode($customer);
```

```python title="Python"
import json
import uuid
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
customer = client.customers.create({"body": {"customerId": "cust-" + str(uuid.uuid4()) + "", "name": "Test Customer"}})
print(json.dumps(customer))
```

### Save a card {#save-a-card}

The first [payment](https://docs.tappay.mn/glossary.md#payment) of a customer is a [CIT](https://docs.tappay.mn/glossary.md#cit), because the customer starts it. Create a [checkout session](https://docs.tappay.mn/glossary.md#checkout-session) with `mode` `SETUP` and the `customer`. The `amount` is required for `SETUP`. Set `uiMode` to `EMBEDDED` to get a `clientSecret`, or leave it out for the hosted `url`. The customer must agree that you may charge the card later, and TapPay records this as `agreedAt`. Choose the kind of consent with `setupFutureUsage`: `UNSCHEDULED` (default) or `RECURRING`. To create a [subscription](https://docs.tappay.mn/glossary.md#subscription) with the saved card, set `setupFutureUsage` to `RECURRING` on `SETUP`, otherwise you get `RCR-112`. To save a card during a normal payment, use `PAYMENT` mode and also set `setupFutureUsage` to `UNSCHEDULED` or `RECURRING`.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/checkout/sessions" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"mode":"SETUP","uiMode":"EMBEDDED","amount":"1000","customer":"CUSTOMER_ID","terminalId":"YOUR_TERMINAL_ID","returnUrl":"https://example.com/return","cancelUrl":"https://example.com/cancel"}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const checkoutSession = await tappay.checkoutSessions.create({
  body: {
    mode: 'SETUP',
    uiMode: 'EMBEDDED',
    amount: '1000',
    customer: 'CUSTOMER_ID',
    terminalId: 'YOUR_TERMINAL_ID',
    returnUrl: 'https://example.com/return',
    cancelUrl: 'https://example.com/cancel',
  },
});
console.log(JSON.stringify(checkoutSession));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$checkoutSession = $tappay->checkoutSessions->create([
    'body' => [
        'mode' => 'SETUP',
        'uiMode' => 'EMBEDDED',
        'amount' => '1000',
        'customer' => 'CUSTOMER_ID',
        'terminalId' => 'YOUR_TERMINAL_ID',
        'returnUrl' => 'https://example.com/return',
        'cancelUrl' => 'https://example.com/cancel',
    ],
]);
echo json_encode($checkoutSession);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
checkout_session = client.checkout_sessions.create({
    "body": {
        "mode": "SETUP",
        "uiMode": "EMBEDDED",
        "amount": "1000",
        "customer": "CUSTOMER_ID",
        "terminalId": "YOUR_TERMINAL_ID",
        "returnUrl": "https://example.com/return",
        "cancelUrl": "https://example.com/cancel",
    },
})
print(json.dumps(checkout_session))
```

### Complete the setup in the sandbox {#complete-the-setup-in-the-sandbox}

The [simulator](https://docs.tappay.mn/glossary.md#simulator) acts as the customer. It sends a [test card](https://docs.tappay.mn/glossary.md#test-card) and the consent. The customer's page makes this call, so it sends your [publishable key](https://docs.tappay.mn/glossary.md#publishable-key) and the session `clientSecret`, and not your secret key.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/checkout/embedded/sessions/SESSION_ID/sandbox/simulate" \
  -H "Authorization: Bearer pk_test_YOUR_PUBLISHABLE_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "X-Client-Secret: CLIENT_SECRET" \
  -H "Content-Type: application/json" \
  -d '{"channel":"CARD","card":{"pan":"4242424242424242","expiry":"12/30","cvv":"123"},"consent":true}'
```

```js title="Node.js"
const res = await fetch('https://api.tappay.mn/api/checkout/embedded/sessions/SESSION_ID/sandbox/simulate', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer pk_test_YOUR_PUBLISHABLE_KEY',
    'TapPay-Version': '2026-09-27',
    'Idempotency-Key': crypto.randomUUID(),
    'X-Client-Secret': 'CLIENT_SECRET',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    channel: 'CARD',
    card: {
      pan: '4242424242424242',
      expiry: '12/30',
      cvv: '123',
    },
    consent: true,
  }),
});
console.log(await res.text());
```

```php title="PHP"
<?php
$body = json_encode([
    'channel' => 'CARD',
    'card' => [
        'pan' => '4242424242424242',
        'expiry' => '12/30',
        'cvv' => '123',
    ],
    'consent' => true,
]);
$ch = curl_init('https://api.tappay.mn/api/checkout/embedded/sessions/SESSION_ID/sandbox/simulate');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer pk_test_YOUR_PUBLISHABLE_KEY',
        'TapPay-Version: 2026-09-27',
        'Idempotency-Key: ' . bin2hex(random_bytes(16)),
        'X-Client-Secret: CLIENT_SECRET',
        'Content-Type: application/json',
    ],
]);
echo curl_exec($ch);
```

```python title="Python"
import json, urllib.request, uuid

req = urllib.request.Request(
    'https://api.tappay.mn/api/checkout/embedded/sessions/SESSION_ID/sandbox/simulate',
    data=json.dumps({
        'channel': 'CARD',
        'card': {
            'pan': '4242424242424242',
            'expiry': '12/30',
            'cvv': '123',
        },
        'consent': True,
    }).encode(),
    method='POST',
    headers={
    'Authorization': 'Bearer pk_test_YOUR_PUBLISHABLE_KEY',
    'TapPay-Version': '2026-09-27',
    'Idempotency-Key': str(uuid.uuid4()),
    'X-Client-Secret': 'CLIENT_SECRET',
    'Content-Type': 'application/json',
    },
)
print(urllib.request.urlopen(req).read().decode())
```

### Find the saved card {#find-the-saved-card}

List the customer's [payment methods](https://docs.tappay.mn/glossary.md#payment-method). The new card has `kind` `CARD`, `status` `ACTIVE` and `usage` `UNSCHEDULED`. Keep its `id`, because every later charge refers to it. TapPay saves the card just after the session completes, so if the list is still empty, wait a moment and list again.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/customers/CUSTOMER_ID/payment-methods" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27"
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const paymentMethod = await tappay.paymentMethods.list({ customerId: 'CUSTOMER_ID' });
console.log(JSON.stringify(paymentMethod));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$paymentMethod = $tappay->paymentMethods->list(['customerId' => 'CUSTOMER_ID']);
echo json_encode($paymentMethod);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
payment_method = client.payment_methods.list({"customerId": "CUSTOMER_ID"})
print(json.dumps(payment_method))
```

### Charge the saved card {#charge-the-saved-card}

Create a payment with the `terminalId`, the `customer`, the `paymentMethod` and an `amount`. With a merchant-level key the `terminalId` is required; with a [terminal](https://docs.tappay.mn/glossary.md#terminal)-level key it is not. No [invoice](https://docs.tappay.mn/glossary.md#invoice) and no action from the customer are needed. TapPay records the payment with `source` `MIT` and charges the card at once. The `amount` must stay within your `mit_max_amount`.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/payments" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"terminalId":"YOUR_TERMINAL_ID","customer":"CUSTOMER_ID","paymentMethod":"PAYMENT_METHOD_ID","amount":500,"description":"Monthly fee"}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const payment = await tappay.payments.create({
  body: {
    terminalId: 'YOUR_TERMINAL_ID',
    customer: 'CUSTOMER_ID',
    paymentMethod: 'PAYMENT_METHOD_ID',
    amount: 500,
    description: 'Monthly fee',
  },
});
console.log(JSON.stringify(payment));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$payment = $tappay->payments->create([
    'body' => [
        'terminalId' => 'YOUR_TERMINAL_ID',
        'customer' => 'CUSTOMER_ID',
        'paymentMethod' => 'PAYMENT_METHOD_ID',
        'amount' => 500,
        'description' => 'Monthly fee',
    ],
]);
echo json_encode($payment);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
payment = client.payments.create({
    "body": {
        "terminalId": "YOUR_TERMINAL_ID",
        "customer": "CUSTOMER_ID",
        "paymentMethod": "PAYMENT_METHOD_ID",
        "amount": 500,
        "description": "Monthly fee",
    },
})
print(json.dumps(payment))
```

### Start a subscription {#start-a-subscription}

Create a checkout session with `mode` `SUBSCRIPTION` and the `billingPlanId` of your plan. The customer chooses a card and agrees to the plan, and then TapPay starts the subscription. A card that is saved with `usage` `UNSCHEDULED`, as in step 2, cannot pay a subscription. To start a subscription for a saved card, save it with `usage` `RECURRING`, then call `integration.subscriptions.create` with `customer`, `billingPlan` and `paymentMethod`.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/checkout/sessions" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"mode":"SUBSCRIPTION","uiMode":"EMBEDDED","customer":"CUSTOMER_ID","subscription":{"billingPlanId":"PLAN_ID"},"terminalId":"YOUR_TERMINAL_ID","returnUrl":"https://example.com/return","cancelUrl":"https://example.com/cancel"}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const checkoutSession = await tappay.checkoutSessions.create({
  body: {
    mode: 'SUBSCRIPTION',
    uiMode: 'EMBEDDED',
    customer: 'CUSTOMER_ID',
    subscription: {
      billingPlanId: 'PLAN_ID',
    },
    terminalId: 'YOUR_TERMINAL_ID',
    returnUrl: 'https://example.com/return',
    cancelUrl: 'https://example.com/cancel',
  },
});
console.log(JSON.stringify(checkoutSession));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$checkoutSession = $tappay->checkoutSessions->create([
    'body' => [
        'mode' => 'SUBSCRIPTION',
        'uiMode' => 'EMBEDDED',
        'customer' => 'CUSTOMER_ID',
        'subscription' => [
            'billingPlanId' => 'PLAN_ID',
        ],
        'terminalId' => 'YOUR_TERMINAL_ID',
        'returnUrl' => 'https://example.com/return',
        'cancelUrl' => 'https://example.com/cancel',
    ],
]);
echo json_encode($checkoutSession);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
checkout_session = client.checkout_sessions.create({
    "body": {
        "mode": "SUBSCRIPTION",
        "uiMode": "EMBEDDED",
        "customer": "CUSTOMER_ID",
        "subscription": {
            "billingPlanId": "PLAN_ID",
        },
        "terminalId": "YOUR_TERMINAL_ID",
        "returnUrl": "https://example.com/return",
        "cancelUrl": "https://example.com/cancel",
    },
})
print(json.dumps(checkout_session))
```

## Verify {#verify}

Check the card and the charge with their `GET` calls and with [events](https://docs.tappay.mn/glossary.md#event). The charge in step 5 sends `payment.succeeded`, and a subscription sends `subscription.*` events when it changes. A [payer link](https://docs.tappay.mn/glossary.md#payer-link) sends `link.created` and `link.revoked`. A [request to pay](https://docs.tappay.mn/glossary.md#r2p) that the payer declines sends `r2p.declined`, and a [mandate](https://docs.tappay.mn/glossary.md#mandate) change sends `mandate.*`.

A subscription has `status` `TRIALING`, `ACTIVE`, `PAST_DUE`, `PAUSED` or `CANCELLED`. `PAST_DUE` means that a charge failed and TapPay retries it by your `subscription_retry_schedule`. The subscription then shows a `cardUpdateUrl`, a link where the payer replaces the card. Manage a subscription with [pause](https://docs.tappay.mn/reference/subscriptions/pause.md), [resume](https://docs.tappay.mn/reference/subscriptions/resume.md) and [cancel](https://docs.tappay.mn/reference/subscriptions/cancel.md), and read its charges with [list cycles](https://docs.tappay.mn/reference/subscriptions/list-cycles.md).

A payer link joins a payer's wallet to your customer. A customer that TapPay creates from a payer link has `source` `PAYER_LINK`. Read the links of a customer as below, and use [revoke](https://docs.tappay.mn/reference/payer-links/revoke.md) to end one. A payment method has `kind` `CARD` or `WALLET_MANDATE`. The payer's wallet creates a mandate, so you can only read it.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/customers/CUSTOMER_ID/payer-links" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27"
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const page = await tappay.payerLinks.list({ customerId: 'CUSTOMER_ID' }).firstPage();
console.log(JSON.stringify(page));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$page = $tappay->payerLinks->list(['customerId' => 'CUSTOMER_ID'])->firstPage();
echo json_encode(['data' => $page->data, 'hasMore' => $page->hasMore, 'nextCursor' => $page->nextCursor]);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
page = client.payer_links.list({"customerId": "CUSTOMER_ID"}).first_page()
print(json.dumps({"data": page.data, "has_more": page.has_more, "next_cursor": page.next_cursor}))
```

> **Note:** **Request to pay in the sandbox**
>
> The sandbox has no merchant-side simulator for a request to pay. Read a request with [Retrieve a request to pay](https://docs.tappay.mn/reference/r2p-requests/get.md) or [List requests of an intent](https://docs.tappay.mn/reference/r2p-requests/list-by-intent.md) after a wallet has sent it.

## Common mistakes {#common-mistakes}

- Charging a card before MIT is enabled and its limits are set. A charge above `mit_max_amount`, or one that breaks `mit_frequency`, fails with [RCR-055](https://docs.tappay.mn/errors.md#RCR-055) `MANDATE_LIMIT`. So does a charge when you have not set the limits. A mandate has its own `maxAmount` and `frequency` with the same error.
- Charging a `CARD` before one successful first payment with `agreedAt` set. An MIT charge works only after that payment, and otherwise TapPay returns `MANDATE_LIMIT` again.
- Using a card token that came from another merchant, from a wallet, or not through TapPay. TapPay returns [RCR-059](https://docs.tappay.mn/errors.md#RCR-059) `MIT_TOKEN_SCOPE_INVALID`.
- Charging a `CARD` whose payment route is no longer active. The method becomes `REQUIRES_ACTION`, and the payer must save the card again.
- Charging a `WALLET_MANDATE` while the wallet partner has no `ACTIVE` route. TapPay returns `422` `RAIL_NOT_ACTIVE`, and the mandate stays as it was.
- Starting a subscription with a card of `usage` `UNSCHEDULED`. TapPay returns [RCR-112](https://docs.tappay.mn/errors.md#RCR-112), so save the card with `usage` `RECURRING`.

## Next steps {#next-steps}

Handle `subscription.*` events with the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md), and [refund](https://docs.tappay.mn/glossary.md#refund) a charge with the [refunds guide](https://docs.tappay.mn/guides/refunds.md). See [Create a customer](https://docs.tappay.mn/reference/customers/create.md), [Create a billing plan](https://docs.tappay.mn/reference/billing-plans/create.md), [Create a subscription](https://docs.tappay.mn/reference/subscriptions/create.md) and [Create a payment](https://docs.tappay.mn/reference/payments/create.md).
