Frameworksbeta
Two route files and one component: take payments and handle webhooks in your full-stack app.
Beta
@cubis/cambill ships route handlers for each framework. You write three small pieces:
- A checkout route. Your server decides the amount and creates the payment.
- A webhook route. CamBill tells your server when the payment settles.
- The checkout component. It calls your checkout route and shows the payment in your page.
The API key and the webhook secret stay on your server. The browser only ever sees one payment's id and client secret.
Before you start
npm install @cubis/cambillPut your keys in the environment. The handlers read these names, so you don't pass them in code:
CAMBILL_API_KEY=khpay_test_… # Dashboard → API & webhooks
CAMBILL_WEBHOOK_SECRET=whsec_… # the same page, after you add your endpointIn the dashboard:
- Turn on a connector (Connectors → Bakong KHQR, test mode).
- Under Checkout → Embed on your site, list your site's origin (
http://localhost:3000while developing). - Under API & webhooks, set your webhook URL to
https://your-site/api/cambill/webhook.
Add it to your app
Checkout route. Read what the customer is buying, look up the price yourself, and return the payment to create.
import { checkout } from "@cubis/cambill/next"
export const POST = checkout(async (request) => {
const { orderId } = await request.json()
const order = await db.orders.find(orderId) // your data, not the browser's
return { amount: order.total, currency: "USD", external_id: order.id }
})Webhook route. Name the events you care about. Each handler gets the event, typed.
import { webhook } from "@cubis/cambill/next"
export const POST = webhook({
"payment_intent.succeeded": async (event) => {
await db.orders.markPaid(event.data.external_id!)
},
})The page. You can render the component from a server component. To pass callbacks such as onSuccess, render it from a "use client" component instead.
import { CamBillCheckout } from "@cubis/cambill/react"
export default async function Pay({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params
return <CamBillCheckout endpoint="/api/cambill/checkout" body={{ orderId: id }} />
}That's the whole integration. Open the page and press Simulate payment on the test checkout: your webhook handler runs.
What the handlers do for you
checkout(create)
- Answers
POSTonly, with{ id, client_secret }and nothing else from the payment. - Return
nullfromcreateto answer404(an unknown order, say), or throw aResponseto answer with it. - An API refusal keeps its status and title, like
400 Return URL Not Allowed. Any other failure is a plain500with no detail, and is logged on your server. - Set
external_id, and opening the checkout twice for one order returns the same payment, not a second one.
webhook(handlers)
- Checks the signature on the raw body, before anything is parsed.
- Answers
400to a bad or old signature (more than 5 minutes),500when your handler throws (so CamBill retries the delivery), and204otherwise, including for event types you don't handle. - A
"*"handler runs for every event, after its own. - Deliveries can repeat and arrive out of order, so make handlers idempotent:
markPaidon an order that's already paid should do nothing.
See Webhooks for every event and its data.
The component
CamBillCheckout with endpoint POSTs body as JSON to your route (with the page's cookies), then shows the payment it gets back. onSuccess, onExpired and onError work as on the widget. onError reports endpoint_failed when your route doesn't answer { id, client_secret }.
Ship only on the webhook
onSuccess runs in the browser, so anyone can fake it. Use it to show a thank-you message. Fulfil the order in your webhook handler.
Using Vue or Svelte? The same endpoint and body options work on their components. See Widget.
Other runtimes
@cubis/cambill/server exports the same checkout and webhook as plain (request: Request) => Promise<Response>, for Hono, Bun, Deno, Cloudflare Workers or any server built on Web requests:
import { checkout, webhook } from "@cubis/cambill/server"
const handleWebhook = webhook({ "invoice.paid": (e) => markInvoicePaid(e.data.id) })
app.post("/api/cambill/webhook", (c) => handleWebhook(c.req.raw)) // HonoWhere there is no process.env (Workers), pass the keys yourself: checkout(create, { apiKey }) and webhook(handlers, { secret }). Both also take a CamBill client you already have: checkout(create, { cambill }).