Platform
Integration Guides
Step-by-step workflows with sequence diagrams for the most common payment integration patterns.
Create a checkout session server-side, redirect your customer to the PaymentHooks-hosted page, and receive the outcome on your registered webhook endpoints and, if you set one, the session's webhookUrl. Card details are entered on the hosted page, never on your servers.
- 1Call POST /v1/checkout-sessions with lineItems (description, quantity, unitPrice in the smallest unit of itemLocalCurrency), itemLocalCurrency, successUrl, cancelUrl and a webhookUrl for this session.
- 2Redirect the customer's browser to data.url from the response.
- 3The customer pays on the hosted page; PaymentHooks routes the charge to one of your connected gateways.
- 4PaymentHooks POSTs checkout.session.completed (or checkout.session.payment_failed) to your registered endpoints, signed with each endpoint's secret, and to the session's webhookUrl, signed with your session webhook secret (GET /v1/webhooks/session-secret). Registered endpoints also receive payment.succeeded / payment.failed. A failed delivery is retried automatically with the same event id.
- 5Verify the signature, branch on the event type, and fulfil the order using data.object.id (the session), data.object.payment_intent (the payment id) and the metadata you set on the session (data.object.metadata). On checkout.session.completed, data.object.status is complete and payment_status is paid.
- 6On success the customer is redirected to successUrl with session_id and payment_id query parameters. Treat the redirect as a UI signal only — confirm with the webhook or GET /v1/checkout-sessions/:id.
// Server: create a hosted checkout session
const res = await fetch('https://paymenthooks.com/api/v1/checkout-sessions', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.PAYMENTHOOKS_SECRET_KEY}`, // phk_live_...
'Content-Type': 'application/json',
'Idempotency-Key': 'order_8812',
},
body: JSON.stringify({
lineItems: [{ description: 'Running shoes', quantity: 1, unitPrice: 5000 }], // smallest unit of itemLocalCurrency
itemLocalCurrency: 'USD',
successUrl: 'https://example.com/checkout/success',
cancelUrl: 'https://example.com/cart',
webhookUrl: 'https://example.com/webhooks/checkout',
metadata: { order_id: 'ord_123' },
}),
});
const { data: session } = await res.json();
res.redirect(303, session.url);
// Webhook handler — the same code works for the session webhookUrl
// (verify with your session webhook secret) and for a registered endpoint
// (verify with that endpoint's signing secret). Skip event ids you have
// already processed: retries reuse the same id.
switch (event.type) {
case 'checkout.session.completed':
// status is 'complete', payment_status is 'paid'
await fulfilOrder(event.data.object.metadata.order_id, event.data.object.payment_intent);
break;
case 'checkout.session.payment_failed':
await markOrderFailed(event.data.object.metadata.order_id);
break;
case 'checkout.session.expired':
await releaseReservedStock(event.data.object.metadata.order_id);
break;
}A subscription links a customer to a recurring price. Every subscription gets a renewal schedule, whether it was started through a subscription-mode checkout (which charges the first period) or created directly with POST /v1/subscriptions (which takes no payment up front). How each renewal is collected depends on whether PaymentHooks can charge the customer off-session.
- 1Create a Product (POST /v1/products) and a recurring Price (POST /v1/prices) with productId, itemLocalCurrencyAmount (smallest unit of itemLocalCurrency), itemLocalCurrency and interval (day | week | month | year).
- 2Create a Customer (POST /v1/customers) and store the returned id. Creation is create-only: an email that already exists for your account returns 409 DUPLICATE_EMAIL — reuse that customer instead.
- 3To charge the first period, create a checkout session with mode: 'subscription', the customerId and metadata.priceId. When it is paid, PaymentHooks creates the subscription, emits subscription.created, and schedules the next renewal at the end of the period. checkout.session.completed is sent once the subscription exists and carries its id in data.object.subscription.
- 4Alternatively, create a Subscription (POST /v1/subscriptions) with customerId and priceId (optional trialEndAt, gatewayConnectionId, metadata). It starts active (or trialing when a trial applies) and emits subscription.created. No payment is taken now; the first renewal is scheduled for the end of the trial or first period. If that schedule cannot be created the request fails with 502 SCHEDULING_FAILED and no subscription is kept.
- 5On each scheduled renewal, PaymentHooks debits the customer off-session only when the subscription has a CCAvenue standing-instruction (SI) mandate. Success emits subscription.renewed plus invoice.created for an auto-generated, already-paid invoice. Failure emits subscription.renewal_failed.
- 6Without a mandate, PaymentHooks emits subscription.renewal_due instead of charging. Collect payment yourself, for example with POST /v1/subscriptions/:id/retry, which returns a checkoutUrl for the customer.
- 7After the first failed renewal the subscription is past_due and a retry is scheduled. When the retries are used up it is cancelled and subscription.cancelled is emitted — revoke access on that event.
- 8To cancel, call POST /v1/subscriptions/:id/cancel. Without a body it ends immediately and subscription.cancelled has status cancelled. With { "cancelAtPeriodEnd": true } the subscription keeps running: the first subscription.cancelled shows status active, cancel_at_period_end true and canceled_at null, and a second subscription.cancelled with status cancelled is sent when the period ends instead of renewing.
const api = (path: string, body: unknown) =>
fetch(`https://paymenthooks.com/api/v1${path}`, {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.PAYMENTHOOKS_SECRET_KEY}`, 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}).then((r) => r.json());
// 1. Catalog (run once)
const { data: product } = await api('/products', { name: 'Pro Plan' });
const { data: price } = await api('/prices', {
productId: product.id,
itemLocalCurrencyAmount: 2900, // smallest unit of itemLocalCurrency
itemLocalCurrency: 'USD',
interval: 'month',
});
// 2a. Customer pays the first period through hosted checkout
const { data: session } = await api('/checkout-sessions', {
mode: 'subscription',
customerId: 'ph_cus_8f14e45fceea167a5a36dedd4bea2543',
lineItems: [{ description: 'Pro Plan', quantity: 1, unitPrice: price.itemLocalCurrencyAmount }],
itemLocalCurrency: price.itemLocalCurrency,
metadata: { priceId: price.id },
successUrl: 'https://example.com/billing/success',
cancelUrl: 'https://example.com/billing',
});
// 2b. Or create a subscription directly (no charge now; first renewal scheduled)
const { data: subscription } = await api('/subscriptions', {
customerId: 'ph_cus_8f14e45fceea167a5a36dedd4bea2543',
priceId: price.id,
trialEndAt: '2026-07-22T00:00:00.000Z',
});Some payments cannot complete in a single server call: the card issuer requires a 3D Secure challenge, or the gateway needs the customer on its own page. The simplest way to support this is hosted checkout, which runs the challenge for you. With the direct Payments API you redirect the customer yourself.
- 1Hosted checkout: nothing to build. When a 3D Secure challenge is needed the customer is taken through it from the hosted page and your session webhookUrl (not your registered endpoints) receives payment.requires_action. Fulfil on the checkout.session.completed that follows, or confirm the final result with GET /v1/payments/:id (the payment id is data.object.payment_intent).
- 2Payments API: call POST /v1/payments with transactionLocalCurrencyAmount, transactionLocalCurrency, customerId and optionally paymentMethod, description, metadata and idempotencyKey. PaymentHooks routes the charge to a connected gateway and returns it with transactionAmountUsdCents (USD cents) alongside.
- 3If the gateway completes the charge immediately, data.status is success and payment.succeeded is sent to your endpoints.
- 4If the gateway returns a redirect, the response includes data.redirectUrl and the payment is not yet successful (typically pending; payment.created is emitted). Redirect the customer's browser to data.redirectUrl.
- 5When the gateway reports the final result, PaymentHooks updates the payment and emits payment.succeeded or payment.failed. You can also check GET /v1/payments/:id.
- 6Never treat the customer's return to your site as proof of payment — confirm via the webhook or the API.
const res = await fetch('https://paymenthooks.com/api/v1/payments', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.PAYMENTHOOKS_SECRET_KEY}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
transactionLocalCurrencyAmount: 8500,
transactionLocalCurrency: 'EUR',
customerId: 'ph_cus_8f14e45fceea167a5a36dedd4bea2543',
paymentMethod: 'card',
idempotencyKey: 'order_9921',
}),
});
const { data: payment } = await res.json();
if (payment.redirectUrl) {
// The gateway needs the customer to finish the payment on its page
return res.redirect(303, payment.redirectUrl);
}
if (payment.status === 'success') {
await fulfilOrder(payment.id);
}
// Otherwise wait for payment.succeeded / payment.failed on your endpoint,
// or check GET /api/v1/payments/:id