Developers · Examples

Code examples

Copy-paste snippets for the workflows every integration eventually needs. Every example is runnable against test mode with a fresh account.

Charge a card

Create and confirm a PaymentIntent in one call for saved payment methods.

Request
import Paytab from "paytab";
const paytab = new Paytab(process.env.PAYTAB_SECRET_KEY);

const payment = await paytab.payments.create({
  amount: 4999,
  currency: "gbp",
  customer: "cus_NffrFeUf...",
  payment_method: "pm_1P4k...",
  confirm: true,
  description: "Pro plan · monthly",
  metadata: { order_id: "4102" },
});
Response
{
  "id": "pay_1P4kJ9L2c...",
  "amount": 4999,
  "currency": "gbp",
  "status": "succeeded",
  "amount_received": 4999,
  "customer": "cus_NffrFeUf...",
  "metadata": { "order_id": "4102" },
  "created": 1751824512
}

Refund a payment

Partial refund with a reason. Fees are refunded proportionally.

Request
const refund = await paytab.payments.refund("pay_1P4kJ9L2c...", {
  amount: 1500,
  reason: "requested_by_customer",
});
Response
{
  "id": "re_1P4kL8L2c...",
  "payment": "pay_1P4kJ9L2c...",
  "amount": 1500,
  "currency": "gbp",
  "status": "succeeded",
  "reason": "requested_by_customer"
}

Verify a webhook

Every webhook is signed with HMAC-SHA256. Verify the signature before trusting the payload.

Request
import express from "express";
import Paytab from "paytab";

const paytab = new Paytab(process.env.PAYTAB_SECRET_KEY);
const app = express();

app.post("/webhooks/paytab",
  express.raw({ type: "application/json" }),
  (req, res) => {
    const sig = req.headers["paytab-signature"];
    let event;
    try {
      event = paytab.webhooks.constructEvent(
        req.body, sig, process.env.PAYTAB_WEBHOOK_SECRET,
      );
    } catch {
      return res.status(400).send("Invalid signature");
    }

    if (event.type === "payment.succeeded") {
      // fulfil order
    }
    res.json({ received: true });
  },
);
Response
{
  "id": "evt_1P4k...",
  "type": "payment.succeeded",
  "livemode": false,
  "created": 1751824512,
  "data": {
    "object": {
      "id": "pay_1P4kJ9L2c...",
      "amount": 4999,
      "currency": "gbp",
      "status": "succeeded"
    }
  }
}

Create a payment link

Shareable hosted checkout URL — no frontend code required.

Request
const link = await paytab.paymentLinks.create({
  line_items: [{ price: "price_1P4k...", quantity: 1 }],
  after_completion: {
    type: "redirect",
    redirect: { url: "https://example.com/thanks" },
  },
});

console.log(link.url);
Response
{
  "id": "plink_1P4k...",
  "url": "https://buy.paytab.co.uk/test_aEU...",
  "active": true,
  "line_items": { "data": [{ "quantity": 1, "price": "price_1P4k..." }] }
}

Create a customer

Store contact and billing details to reuse across payments and subscriptions.

Request
const customer = await paytab.customers.create({
  email: "ada@example.com",
  name: "Ada Lovelace",
  address: {
    line1: "10 Downing Street",
    city: "London",
    postal_code: "SW1A 2AA",
    country: "GB",
  },
});
Response
{
  "id": "cus_NffrFeUf...",
  "email": "ada@example.com",
  "name": "Ada Lovelace",
  "created": 1751824512
}

Start a subscription

Bill a customer on a recurring schedule with a proration on upgrades.

Request
const subscription = await paytab.subscriptions.create({
  customer: "cus_NffrFeUf...",
  items: [{ price: "price_1P4k..." }],
  trial_period_days: 14,
});
Response
{
  "id": "sub_1P4k...",
  "customer": "cus_NffrFeUf...",
  "status": "trialing",
  "current_period_end": 1753034112,
  "items": { "data": [{ "price": { "id": "price_1P4k..." } }] }
}