Skip to main content

Checkout Integration Guide

This guide covers everything you need to know about integrating SwiftPay checkout into your application.

How It Works

1

Create Session

Your server creates a checkout session via the API
2

Redirect Customer

Redirect the customer to the SwiftPay hosted checkout page
3

Customer Pays

Customer enters payment details and completes payment
4

Handle Result

Customer is redirected back to your site; webhook confirms payment

Creating a Checkout Session

Basic Example

const session = await fetch("https://api.swiftpay.cx/api/checkout/sessions", {
   method: "POST",
   headers: {
      Authorization: "Bearer mp_live_your_api_key",
      "Content-Type": "application/json",
   },
   body: JSON.stringify({
      items: [
         {
            name: "Product Name",
            price: 29.99,
            quantity: 1,
            description: "Product description",
            imageUrl: "https://example.com/product.jpg",
         },
      ],
      customer: {
         email: "[email protected]",
      },
      urls: {
         success: "https://yoursite.com/success?session_id={SESSION_ID}",
         cancel: "https://yoursite.com/cancel",
         failure: "https://yoursite.com/failure",
      },
   }),
});

Request Parameters

items
array
required
List of items in the checkout.
customer
object
required
Customer information
urls
object
Redirect URLs after checkout
description
string
Order description for your records
askShipping
boolean
default:"false"
Whether to collect shipping address
metadata
array
Custom metadata for your records

Checkout URL

After creating a session, redirect the customer to:
https://app.swiftpay.cx/checkout/{session_id}
// Using Next.js redirect
import { redirect } from 'next/navigation';

export async function createCheckout(formData) {
  const session = await createCheckoutSession(formData);
  redirect(`https://app.swiftpay.cx/checkout/${session.id}`);
}

Handling the Result

Success Redirect

When payment succeeds, the customer is redirected to your success URL:
https://yoursite.com/success?session_id=sess_abc123
Never rely solely on the redirect to confirm payment. Always verify via webhook or API.

Verify Payment Status

// Verify the payment was successful
const response = await fetch(
   `https://api.swiftpay.cx/api/checkout/sessions/${sessionId}`,
   {
      headers: { Authorization: "Bearer mp_live_your_api_key" },
   }
);

const session = await response.json();

if (session.data.status === "completed") {
   // Payment confirmed - fulfill the order
}

Session Statuses

StatusDescription
pendingSession created, awaiting payment
processingPayment is being processed
completedPayment successful
failedPayment failed
expiredSession expired (after 24 hours)

Collecting Shipping Address

Set askShipping: true to collect the customer’s shipping address:
const session = await createSession({
  items: [...],
  customer: { email: '[email protected]' },
  askShipping: true,
  urls: { ... }
});
The shipping address will be available in the session data:
{
   "shippingAddressLine1": "123 Main St",
   "shippingCity": "San Francisco",
   "shippingState": "CA",
   "shippingPostalCode": "94102",
   "shippingCountry": "US"
}

Using Metadata

Store custom data with the session using metadata:
const session = await createSession({
  items: [...],
  customer: { email: '[email protected]' },
  metadata: [
    { key: 'order_id', value: 'ORD-12345' },
    { key: 'customer_id', value: 'cust_abc123' },
    { key: 'subscription', value: true }
  ],
  urls: { ... }
});
Metadata is returned in webhooks and when fetching the session.

Webhook Integration

For production, always use webhooks to confirm payments:
// Your webhook endpoint
app.post("/webhooks/swiftpay", async (req, res) => {
   const signature = req.headers["x-webhook-signature"];

   // Verify signature
   if (!verifySignature(req.body, signature, webhookSecret)) {
      return res.status(401).send("Invalid signature");
   }

   const event = req.body;

   if (event.eventType === "checkout.succeeded") {
      const sessionId = event.payload.sessionId;
      // Fulfill the order
   }

   res.status(200).send("OK");
});
See the Webhooks Guide for complete webhook setup instructions.

Best Practices

Always Use Webhooks

Don’t rely on redirects alone. Webhooks ensure you never miss a payment.

Store Session IDs

Save the session ID with your order for easy reference and debugging.

Use Metadata

Link sessions to your internal orders using metadata.

Handle All Statuses

Build UI for pending, completed, and failed states.