Skip to content

Accept a payment in your chat

The in-chat checkout is a card that appears in the conversation. Your customer enters their card or taps Apple Pay, and the chat carries on. They never leave your product, and card details never touch your page.

Try it yourself in the live demo.

Building this takes three things: a payment link, one script tag, and one function call.

On your server, create a payment link:

const response = await fetch("https://api.cyan.cash/v1/payment-links", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CYAN_API_KEY}`,
"Idempotency-Key": order.id,
"Content-Type": "application/json",
},
body: JSON.stringify({
amount: 3600,
description: "2 × House Blend, 340 g",
}),
});
const link = await response.json();

amount is in cents: 3600 is $36. The response includes the link’s url. Send it to your chat page along with your agent’s reply, exactly as you received it.

Load widget.js once, then mount the checkout wherever the message should appear:

<div id="payment"></div>
<script src="https://pay.cyan.cash/v1/widget.js"></script>
<script>
const checkout = Cyan.mount("#payment", {
url: link.url,
onPaid({ link }) {
// Show "Paid" in the chat and let your agent confirm the order.
},
});
</script>

That’s the whole integration. The widget draws the card, loads the payment form, and sizes itself to fit.

Load the script once, mount in an effect, and unmount when the message goes away.

import { useEffect, useRef } from "react";
let widget: Promise<any> | undefined;
function loadWidget() {
widget ??= new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = "https://pay.cyan.cash/v1/widget.js";
script.onload = () => resolve((window as any).Cyan);
script.onerror = () => {
widget = undefined; // let a later mount try again
reject(new Error("Couldn't load the checkout."));
};
document.head.appendChild(script);
});
return widget;
}
export function Checkout({
url,
onPaid,
}: {
url: string;
onPaid: (linkId: string) => void;
}) {
const slot = useRef<HTMLDivElement>(null);
useEffect(() => {
let checkout: { unmount(): void } | undefined;
let cancelled = false;
loadWidget()
.then((Cyan) => {
if (cancelled || !slot.current) return;
checkout = Cyan.mount(slot.current, {
url,
onPaid: ({ link }: { link: string }) => onPaid(link),
});
})
.catch(console.error);
return () => {
cancelled = true;
checkout?.unmount();
};
}, [url]);
return <div ref={slot} style={{ maxWidth: 428 }} />;
}

Pass callbacks to Cyan.mount to keep your chat in step with the checkout:

Callback Runs when
onReady() The payment form is ready.
onPaid({ link }) The payment succeeded. link is the payment link’s ID.
onProcessing({ link }) The payment was accepted and the bank is confirming it.
onError({ message }) An attempt failed, for example a declined card.

When a card is declined, the checkout shows the reason and your customer can try again right there. You don’t have to handle it.

If your customer reloads the chat after paying, mount the same url again. The checkout shows the paid state and calls onPaid again, so the conversation picks up where it left off.

  • Width: the checkout fills the element you mount it in. Around 428px wide suits most chats.
  • Height: leave it to the widget. It sizes itself, so the messages around it never jump.
  • Look: set a theme, an accent color and a corner style to match your product. See Customize the checkout.

When a message is removed, call checkout.unmount().

If your site sets a Content Security Policy, allow Cyan in two directives:

Content-Security-Policy: script-src https://pay.cyan.cash; frame-src https://pay.cyan.cash