Next.js
npm install @swft-checkout/nextjs @swft-checkout/jsApp Router (Server Action)
Section titled “App Router (Server Action)”'use server'import { swftCheckout } from '@swft-checkout/nextjs'
export async function checkout(cartItems: CartItem[]) { await swftCheckout({ merchantApiKey: process.env.SWFT_MERCHANT_API_KEY!, currency: 'GBP', lineItems: cartItems.map(item => ({ name: item.name, quantity: item.quantity, price: item.price, product_id: item.id, })), shippingMethods: [ { id: 'standard', label: 'Standard Shipping (3-5 days)', cost: 499 }, { id: 'express', label: 'Express Shipping (next day)', cost: 999 }, ], subtotal: cartItems.reduce((s, i) => s + i.price * i.quantity, 0), }) // swftCheckout calls redirect() — never returns}import { checkout } from './actions'
export default function CartPage({ items }: { items: CartItem[] }) { return ( <form action={checkout.bind(null, items)}> <button type="submit">Checkout with Swft →</button> </form> )}Webhook handler (App Router)
Section titled “Webhook handler (App Router)”import { createSwftWebhookHandler } from '@swft-checkout/nextjs'
export const POST = createSwftWebhookHandler( process.env.SWFT_WEBHOOK_SECRET!, async (payload) => { if (payload.event === 'payment_succeeded') { await createOrder({ ...payload }) } })