Refund a payment
Overview
A refund returns all or part of a successful payment to the payer. You create it from your server, and TapPay sends the money back the way it came. You can refund one payment several times, until the refunds add up to the paid amount.
Before you begin
Your key needs refunds:write and checkout: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. The refund amount comes out of your merchant balance, so keep enough balance for it.
Steps
Create a payment to refund
You need a payment that has succeeded. Create a checkout session as in the checkout guide.
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 '{"terminalId":"YOUR_TERMINAL_ID","amount":"1000","description":"Order 1","orderId":"order-'"$(uuidgen)"'","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: {
terminalId: 'YOUR_TERMINAL_ID',
amount: '1000',
description: 'Order 1',
orderId: `order-${crypto.randomUUID()}`,
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' => [
'terminalId' => 'YOUR_TERMINAL_ID',
'amount' => '1000',
'description' => 'Order 1',
'orderId' => 'order-' . vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)) . '',
'returnUrl' => 'https://example.com/return',
'cancelUrl' => 'https://example.com/cancel',
],
]);
echo json_encode($checkoutSession);import json
import uuid
import tappay
client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
checkout_session = client.checkout_sessions.create({
"body": {
"terminalId": "YOUR_TERMINAL_ID",
"amount": "1000",
"description": "Order 1",
"orderId": "order-" + str(uuid.uuid4()) + "",
"returnUrl": "https://example.com/return",
"cancelUrl": "https://example.com/cancel",
},
})
print(json.dumps(checkout_session))Pay it in the sandbox
Let the simulator pay for the customer. The response has the id of the payment, and you use it in the refund.
curl -sS -X POST "https://api.tappay.mn/api/v1/sandbox/intents/INTENT_ID/simulate" \
-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 '{"channel":"QR","outcome":"SUCCEEDED"}'import { TapPay } from '@tappay-mn/node';
const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const result = await tappay.sandbox.simulateIntent({ id: 'INTENT_ID', body: { channel: 'QR', outcome: 'SUCCEEDED' } });
console.log(JSON.stringify(result));<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$result = $tappay->sandbox->simulateIntent(['id' => 'INTENT_ID', 'body' => ['channel' => 'QR', 'outcome' => 'SUCCEEDED']]);
echo json_encode($result);import json
import tappay
client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
result = client.sandbox.simulate_intent({"id": "INTENT_ID", "body": {"channel": "QR", "outcome": "SUCCEEDED"}})
print(json.dumps(result))Refund the payment
Send the amount as a string and an optional reason. This example refunds the full paid amount of 1000. A partial amount works only when the payment’s rail supports partial refunds; otherwise TapPay returns RFD-032. To refund chosen order lines, send lines with the invoiceLineId and quantity of each line. Send an Idempotency-Key, so that a retry does not refund twice. The response is 202, because a refund finishes later, and its status starts as PENDING.
curl -sS -X POST "https://api.tappay.mn/api/v1/payments/PAYMENT_ID/refunds" \
-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 '{"amount":"1000","reason":"Customer returned an item"}'import { TapPay } from '@tappay-mn/node';
const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const refund = await tappay.refunds.create({ paymentId: 'PAYMENT_ID', body: { amount: '1000', reason: 'Customer returned an item' } });
console.log(JSON.stringify(refund));<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$refund = $tappay->refunds->create([
'paymentId' => 'PAYMENT_ID',
'body' => [
'amount' => '1000',
'reason' => 'Customer returned an item',
],
]);
echo json_encode($refund);import json
import tappay
client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
refund = client.refunds.create({"paymentId": "PAYMENT_ID", "body": {"amount": "1000", "reason": "Customer returned an item"}})
print(json.dumps(refund))Read the refund
Read the refund until its status is SUCCEEDED or FAILED. A FAILED refund has a failureCode.
curl -sS "https://api.tappay.mn/api/v1/refunds/REFUND_ID" \
-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 refund = await tappay.refunds.retrieve({ id: 'REFUND_ID' });
console.log(JSON.stringify(refund));<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$refund = $tappay->refunds->retrieve(['id' => 'REFUND_ID']);
echo json_encode($refund);import json
import tappay
client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
refund = client.refunds.retrieve({"id": "REFUND_ID"})
print(json.dumps(refund))List the refunds of a payment
List every refund of the payment. Add up their amounts to see how much of the payment is still left to refund.
curl -sS "https://api.tappay.mn/api/v1/payments/PAYMENT_ID/refunds" \
-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.refunds.list({ paymentId: 'PAYMENT_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->refunds->list(['paymentId' => 'PAYMENT_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.refunds.list({"paymentId": "PAYMENT_ID"}).first_page()
print(json.dumps({"data": page.data, "has_more": page.has_more, "next_cursor": page.next_cursor}))Verify
TapPay sends refund.* events when the refund changes, so you can wait for them instead of polling. The status goes from PENDING to PROCESSING, and then to SUCCEEDED or FAILED. Your own refunds have reasonCode MERCHANT_REQUEST. TapPay uses PREAUTH_ADJUSTMENT for the difference of a pre-auth payment, and OVERPAID for an overpaid payment, which only TapPay refunds.
requestedVia tells you where the refund was requested. It is API for your keys, PORTAL for the merchant portal, and BACKOFFICE for TapPay staff. It is POS for a bank POS terminal, and SYSTEM for refunds that TapPay makes automatically.
If a same-day cancellation (kind is VOID) is rejected, that refund becomes FAILED with failureCode NOT_VOIDABLE. TapPay then automatically creates a regular refund for the same amount with a new id. The new refund has replacesRefundId set to the rejected refund’s id. Link the two and do not refund again.
The refunds of one payment add up to at most the paid amount. The money goes back through the same channel that took it, and the fee is not returned. If your balance is too small, it goes negative and TapPay settles it from your next income. If the payment was split, each beneficiary returns its share in the same ratio as the split.
TapPay also corrects the tax receipt. A full refund returns the receipt. A partial refund returns it and issues a new receipt for the remaining amount, and lines chooses which lines return. Read more in the receipts guide.
Common mistakes
- Refunding more than is left. TapPay returns RFD-030 when the amount is above what can still be refunded.
- Treating the
202response as a finished refund. The money moves later, so read thestatusor wait for therefund.*event. - Expecting the fee back. TapPay keeps the fee of the original payment.
- Refunding after the refund window of the payment. TapPay returns RFD-034.
- Refunding a payment that is not eligible, or one that has no refund route. TapPay returns RFD-022 or RFD-031.
- Sending a partial amount for a payment that supports only a full refund. TapPay returns RFD-032.
- Reusing an
Idempotency-Keywith different parameters. TapPay returns RFD-036, so use a new key for each new refund.
Next steps
Return the tax receipt with the receipts guide, and follow refund events with the webhooks guide. See Create a refund, Retrieve a refund and List refunds of a payment.