Skip to content

Idempotency

View as Markdown

Overview

A network call can fail after TapPay has already done the work. If you send it again, you could charge twice. The Idempotency key prevents this. TapPay does the work once and returns the same result on every repeat.

The Idempotency-Key header

Send an Idempotency-Key header with every POST request. Use a new random value, such as a UUID, for each new operation. Keep the same value when you retry that operation.

TapPay remembers each key for the idempotency_ttl, which is 24 hours. A repeat inside that time returns the first result instead of doing the work again.

Same key, different body

A key belongs to one request body. If you send the same key with a different body, TapPay returns 409 with the code VAL-010. Use a new key for a new operation.

Request still running

If you repeat a request while the first one is still running, TapPay returns 409 with the code VAL-013. Wait a moment and send the same request again with the same key.

Duplicate orders

An Invoice has a second safety check. The pair of terminalId and orderId must be unique. Creating a second invoice with the same pair is a duplicate, even with a new key.

Send a stable orderId from your own system for each order. Then the same order can never be collected twice.

The right way to retry

Create the key once, before the first attempt, and store it with your order. Use that same key for every retry. After a timeout or a network error, repeat the call with the same key and the same body.

Do not make a new key for each retry. A new key looks like a new operation, so TapPay does the work again. Read the object with GET when you only need to check its state.

See Create an invoice for the request, and the checkout guide for a full flow.

In the SDKs

The SDKs send an Idempotency-Key on every POST and reuse it on their own retries. To repeat an operation later, for example after your server restarts, pass your own key saved with the order.

import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: process.env.TAPPAY_SECRET_KEY });
const body = { terminalId: 'YOUR_TERMINAL_ID', amount: 1000, description: 'Order #1001', orderId: 'order-1001' };
// Save the key with the order and pass it again when you repeat the same operation.
const key = 'order-1001-invoice';
const first = await tappay.invoices.create({ body }, { idempotencyKey: key });
const again = await tappay.invoices.create({ body }, { idempotencyKey: key });
console.log(first.invoiceId === again.invoiceId, tappay.lastResponse?.idempotentReplayed);
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => getenv('TAPPAY_SECRET_KEY')]);
$body = ['terminalId' => 'YOUR_TERMINAL_ID', 'amount' => 1000, 'description' => 'Order #1001', 'orderId' => 'order-1001'];
// Save the key with the order and pass it again when you repeat the same operation.
$key = 'order-1001-invoice';
$first = $tappay->invoices->create(['body' => $body], ['idempotencyKey' => $key]);
$again = $tappay->invoices->create(['body' => $body], ['idempotencyKey' => $key]);
var_dump($first['invoiceId'] === $again['invoiceId'], $tappay->lastResponse->idempotentReplayed);
import os
import tappay

client = tappay.TapPay(os.environ["TAPPAY_SECRET_KEY"])
body = {"terminalId": "YOUR_TERMINAL_ID", "amount": 1000, "description": "Order #1001", "orderId": "order-1001"}
# Save the key with the order and pass it again when you repeat the same operation.
key = "order-1001-invoice"
first = client.invoices.create({"body": body}, idempotency_key=key)
again = client.invoices.create({"body": body}, idempotency_key=key)
print(first["invoiceId"] == again["invoiceId"], client.last_response.idempotent_replayed)