Skip to content

Create checkout

All variants share provider, customer, redirect URLs, optional client IP, and an idempotency key.

const common = {
provider: "midtrans",
customer: {
externalId: "customer-42",
email: "buyer@example.com",
name: "Buyer",
},
successUrl: "https://merchant.example/checkout/success",
cancelUrl: "https://merchant.example/checkout/cancel",
clientIp: null,
} as const;
import { createCheckout } from "@byfungsi/sapa";
const checkout = createCheckout({
...common,
type: "single",
idempotencyKey: "order-42",
item: {
name: "Consulting session",
amount: 500_000n,
currency: "IDR",
fulfillment: { type: "billing_only" },
},
});
const checkout = createCheckout({
...common,
type: "catalog",
priceId: "price-5e761e68-ab6e-4e67-81d4-36e1811b6272",
idempotencyKey: "catalog-order-42",
});

Seed the complete catalog with seedCatalog before using a catalog price.

const checkout = createCheckout({
...common,
type: "offer",
offerKey: "workspace.pro",
offerRevision: 1,
quantity: null,
idempotencyKey: "offer-order-42",
});

For per-seat offers, set quantity to the buyer-selected count.

const checkout = createCheckout({
...common,
type: "subscription_renewal",
subscriptionId: "sub-00000000-0000-4000-8000-000000000001",
idempotencyKey: "renewal-2026-10-customer-42",
});
const checkout = createCheckout({
...common,
type: "invoice",
invoiceId: "inv-00000000-0000-4000-8000-000000000001",
idempotencyKey: "invoice-checkout-42",
});

Only an invoice whose collection status is payment_due can create a checkout. Persist the returned checkoutId before redirecting the customer.