Save customers and charge them again
Overview
A 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, and a subscription repeats it on a schedule.
Before you begin
Your key needs customers:write, checkout:write and payments:write in its scope, and you use your secret key, shown below as sk_test_YOUR_SECRET_KEY. Use a 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, so ask your partner or TapPay to set them. A billing plan describes what a subscription charges. Create it once, as below, and reuse it for every subscription.
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}'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
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);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
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.
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"}'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
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);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
The first payment of a customer is a CIT, because the customer starts it. Create a 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 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.
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"}'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
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);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
The simulator acts as the customer. It sends a test card and the consent. The customer’s page makes this call, so it sends your publishable key and the session clientSecret, and not your secret key.
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}'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
$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);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
List the customer’s payment methods. 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.
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"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
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);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
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-level key it is not. No 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.
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"}'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
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);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
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.
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"}'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
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);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
Check the card and the charge with their GET calls and with events. The charge in step 5 sends payment.succeeded, and a subscription sends subscription.* events when it changes. A payer link sends link.created and link.revoked. A request to pay that the payer declines sends r2p.declined, and a 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, resume and cancel, and read its charges with list cycles.
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 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.
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"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
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]);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}))Common mistakes
- Charging a card before MIT is enabled and its limits are set. A charge above
mit_max_amount, or one that breaksmit_frequency, fails with RCR-055MANDATE_LIMIT. So does a charge when you have not set the limits. A mandate has its ownmaxAmountandfrequencywith the same error. - Charging a
CARDbefore one successful first payment withagreedAtset. An MIT charge works only after that payment, and otherwise TapPay returnsMANDATE_LIMITagain. - Using a card token that came from another merchant, from a wallet, or not through TapPay. TapPay returns RCR-059
MIT_TOKEN_SCOPE_INVALID. - Charging a
CARDwhose payment route is no longer active. The method becomesREQUIRES_ACTION, and the payer must save the card again. - Charging a
WALLET_MANDATEwhile the wallet partner has noACTIVEroute. TapPay returns422RAIL_NOT_ACTIVE, and the mandate stays as it was. - Starting a subscription with a card of
usageUNSCHEDULED. TapPay returns RCR-112, so save the card withusageRECURRING.
Next steps
Handle subscription.* events with the webhooks guide, and refund a charge with the refunds guide. See Create a customer, Create a billing plan, Create a subscription and Create a payment.