PHP SDK
The official PHP SDK tappay-mn/tappay-php calls the TapPay API for you. It signs requests with HMAC, sends an Idempotency-Key, retries safely, pages through lists and verifies webhooks, so you do not write HTTP by hand. Use it on your server with your secret key.
Install
Add the package to your project.
composer require tappay-mn/tappay-php
Supported versions
The SDK supports these runtime versions. A runtime leaves the list one minor version after its end of life.
| SDK | Package | Version | Install | Supported runtimes | Registry | Source |
|---|---|---|---|---|---|---|
| PHP | tappay-mn/tappay-php | 3.1.0 | composer require tappay-mn/tappay-php | PHP 8.2, 8.3, 8.4, 8.5 | Packagist | gitlab.com/node-platform/nodepay-php |
Quickstart
Take a first test payment in five minutes with a sandbox key. Each step is a full script that you can run on its own.
- Create an invoice for the order. The response has the
invoiceIdand theintentId.
<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$invoice = $tappay->invoices->create([
'body' => [
'terminalId' => 'YOUR_TERMINAL_ID',
'amount' => 1000,
'description' => 'Order #1001',
'orderId' => 'order-' . vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)) . '',
],
]);
echo json_encode($invoice);- Create a checkout session for the invoice and send the customer to its
url.
<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$checkoutSession = $tappay->checkoutSessions->create([
'body' => [
'invoiceId' => 'INVOICE_ID',
'terminalId' => 'YOUR_TERMINAL_ID',
'returnUrl' => 'https://example.com/return',
'cancelUrl' => 'https://example.com/cancel',
],
]);
echo json_encode($checkoutSession);- In the sandbox, let the simulator pay the intent. In production the customer pays.
<?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);- Verify the webhook over the raw body with
constructEvent.
<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
use TapPay\Exception\SignatureVerificationError;
$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
// Your web framework gives you these: the raw body, the headers and the path of your endpoint.
$in = json_decode(file_get_contents('php://stdin'), true);
try {
$event = $tappay->webhooks->constructEvent($in['rawBody'], $in['headers'], ['secrets' => [getenv('TAPPAY_WEBHOOK_SECRET')], 'path' => $in['path']]);
echo $event->id ? 'valid' : 'invalid';
} catch (SignatureVerificationError $e) {
echo 'invalid';
}- The event carries only ids. Read the object itself with
events.retrieveObjectbefore you hand over goods.
<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
// In your webhook handler pass the event from constructEvent. Here it is built from the payment of step 3.
$event = ['id' => 'evt_example', 'type' => 'payment.succeeded', 'objectId' => 'PAYMENT_ID'];
$payment = $tappay->events->retrieveObject($event);
echo json_encode($payment);- Refund the payment if the customer returns the order.
<?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 the item',
],
]);
echo json_encode($refund);Configuration
Pass options to the constructor. An option you leave out is read from the environment. The environment, sandbox or production, follows the key prefix: sk_test_ is the sandbox and sk_live_ is production, so you never set the base URL in code.
| Option | Environment variable | Default | Meaning |
|---|---|---|---|
apiKey | TAPPAY_SECRET_KEY | required | Secret key. A publishable key pk_… is rejected. |
baseUrl | TAPPAY_BASE_URL | from the key | Only for tests against your own stack. Never in production. |
signingSecret | TAPPAY_SIGNING_SECRET | none | Signs each request with HMAC when your system uses the API key + HMAC signature method. |
timeoutMs | — | 30000 | Time limit of one call in milliseconds. |
maxRetries | — | 2 | Retries after a network error, 429 or 5xx. |
maxRetryAfterMs | — | 60000 | Upper limit for Retry-After. Above it the SDK does not wait. |
nodeVersion | — | 2026-09-27 | The TapPay-Version header. Each SDK version pins one API version, and this option overrides it. |
<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
// Options you leave out are read from TAPPAY_SECRET_KEY, TAPPAY_BASE_URL and TAPPAY_SIGNING_SECRET.
$tappay = new TapPay([
'apiKey' => getenv('TAPPAY_SECRET_KEY'),
'signingSecret' => getenv('TAPPAY_SIGNING_SECRET'),
'timeoutMs' => 15000,
'maxRetries' => 3,
'maxRetryAfterMs' => 30000,
'nodeVersion' => '2026-09-27',
]);
// Per call: the second argument.
$invoice = $tappay->invoices->retrieve(['id' => 'YOUR_INVOICE_ID'], ['timeoutMs' => 5000, 'nodeVersion' => '2026-09-27']);
Idempotency and retries
Every POST that creates something is sent with an Idempotency-Key. The SDK makes a new key for each call and sends the same key on each retry of that call, so a retry never creates a second invoice or refund. You can pass your own key, as shown in Idempotency. The SDK retries a network error, 429 and 5xx with exponential backoff and respects Retry-After.
Errors
Each API error is a subclass of TapPayError with status, code, message, requestId and docsUrl. The code is a XXX-NNN value from the error catalog.
| Class | When |
|---|---|
InvalidRequestError | 400, 422 |
AuthenticationError | 401 |
PermissionError | 403 |
NotFoundError | 404 |
ConflictError | 409 |
IdempotencyError | 409 VAL-010, same key with a different body |
RateLimitError | 429 |
ApiError | 5xx and other answers |
ConnectionError | network error |
TimeoutError | the call took longer than the time limit |
<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
use TapPay\Exception\NotFoundError;
use TapPay\Exception\TapPayError;
$tappay = new TapPay(['apiKey' => getenv('TAPPAY_SECRET_KEY')]);
try {
$invoice = $tappay->invoices->retrieve(['id' => 'YOUR_INVOICE_ID']);
} catch (NotFoundError $e) {
error_log('not found ' . $e->code . ' ' . $e->requestId);
} catch (TapPayError $e) {
error_log($e->status . ' ' . $e->code . ' ' . $e->docsUrl);
}
Pagination
A list call returns a page with data, hasMore and nextCursor. The iterator fetches the next pages as you go.
<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
$tappay = new TapPay(['apiKey' => getenv('TAPPAY_SECRET_KEY')]);
// Every page, fetched as you go.
foreach ($tappay->payments->list(['query' => ['limit' => 50]])->autoPaging() as $payment) {
echo $payment['id'], ' ', $payment['status'], PHP_EOL;
}
// Only the first page.
$page = $tappay->payments->list(['query' => ['limit' => 10]])->firstPage();
var_dump(count($page->data), $page->hasMore, $page->nextCursor);
Webhooks
Verify each webhook over the raw body, before any JSON parsing. During a secret rotation pass the new and the previous secret: either one may match. The event is thin, so read the object with events->retrieveObject. Read the webhook guide for the whole flow.
<?php
require 'vendor/autoload.php';
use TapPay\TapPay;
use TapPay\Exception\SignatureVerificationError;
$tappay = new TapPay(['apiKey' => getenv('TAPPAY_SECRET_KEY')]);
try {
// php://input is the raw body. A json_decode result fails with RAW_BODY_REQUIRED.
$event = $tappay->webhooks->constructEvent(file_get_contents('php://input'), getallheaders(), [
// During a rotation both secrets are valid: the new one and the previous one.
'secrets' => array_values(array_filter([getenv('TAPPAY_WEBHOOK_SECRET'), getenv('TAPPAY_WEBHOOK_SECRET_PREVIOUS')])),
'path' => $_SERVER['REQUEST_URI'],
]);
} catch (SignatureVerificationError $e) {
http_response_code(400);
exit;
}
// The event is thin: read the object itself with GET before you act on it.
$object = $tappay->events->retrieveObject($event);
http_response_code(200);
POS client
PosClient calls the POS API of a card terminal with a partner credential. It gets and renews the token and signs each request. Read the POS API reference and the POS terminal guide.
<?php
require 'vendor/autoload.php';
use TapPay\Pos\PosClient;
$pos = new PosClient([
'clientId' => getenv('POS_CLIENT_ID'),
'clientSecret' => getenv('POS_CLIENT_SECRET'),
'apiKey' => getenv('POS_API_KEY'),
'signingSecret' => getenv('POS_SIGNING_SECRET'),
'environment' => 'sandbox',
]);
$qr = $pos->payments->createQr(['body' => [
'pos_reference' => 'pos-1001',
'narrative' => 'Order #1001',
'amount' => 1000,
'currency' => 'MNT',
'card_terminal' => ['terminal_code' => 'T-SANDBOX-POS', 'merchant_code' => 'M-SANDBOX-POS'],
]]);
// Do not trust the callback: read the payment from the API.
$payment = $pos->payments->retrieve(['paymentId' => $qr['payment_id']]);
echo $payment['status'];
Versioning
The SDK follows semantic versioning. A major version may break your code, and minor and patch versions do not. Each SDK version pins one TapPay-Version. The release notes are on the Packagist page and in the CHANGELOG.md of the package. The Changelog lists the SDK versions for each API version.