Coupons
Apply coupon codes at checkout with billingData.couponCodes, let shoppers enter their own, and see what each billing platform expects.
Coupons live on your billing platform. At checkout you pass their codes in billingData.couponCodes, and Altruon uses them for the price estimate and when it creates the subscription. This page shows how to apply coupons on each checkout and how the codes map to each billing platform.
- Your serverPass the codes
billingData.couponCodesin the Create Session request. - AltruonEstimate and subscribeSends the codes with the estimate and with the subscription.
- Billing platformApply the discountValidates each code and prices the subscription.
Before you start
- The coupons exist on your billing platformAltruon passes the values through. See What each billing platform expects for the identifier to use.
- You know which checkout you useThe way a coupon reaches the session differs per checkout. See Choose your checkout.
Where coupons come from
| Checkout | Coupons you apply | Coupons the shopper enters |
|---|---|---|
| Hosted pages | Coupon codes on the hosted page, pre-applied and built into the link | The Coupon code toggle under Settings > Checkout > Fields |
| Dynamic hosted checkout | billingData.couponCodes in the Create Session request, shown as pre-applied in the order summary | The same Coupon code toggle |
| Altruon.js | billingData.couponCodes in the Create Session request | Your own coupon field. You re-create the session with the updated list |
Pass coupons when you create the session
Send couponCodes inside billingData on POST /api/session/v1/create. It must be an array of strings.
{
"paymentData": {
"currency": "EUR",
"paymentMethod": "card"
},
"redirectUrl": "https://yourdomain.com/success",
"billingData": {
"billingPlatformId": "3287b8c7-ce43-41fd-9d58-f510e610b8f3",
"lineItems": [
{
"type": "plan",
"id": "price_123",
"quantity": 1
}
],
"couponCodes": ["SPRING25", "WELCOME10"]
}
}The Create Session reference lists every other field of the request.
Let shoppers enter a coupon on hosted checkout
Go to Settings > Checkout, open the Fields tab and enable Coupon code. Shoppers can then type their own codes on hosted pages and on dynamic hosted checkout. This toggle is independent from the coupons you pre-apply.
Let shoppers enter a coupon with Altruon.js
Coupons are part of the session's billingData, which is set from your server. To let shoppers add or remove a coupon on your own checkout page, re-create the session with the updated list. Creating a session is one API call and every session is single use, so this is the recommended pattern.
Keep the coupon list in your page state
Whenever the shopper applies or removes a coupon, ask your server for a new session that includes the updated list.
const [couponCodes, setCouponCodes] = useState([]);
useEffect(() => {
// Create a new session whenever the pricing inputs change.
initializeCheckout(); // POST /api/session with { couponCodes, ... }
}, [couponCodes]);Forward the list when you create the session
On your server, add the coupon list to billingData. Leave the field out when the list is empty.
const data = await createSession({
paymentData: { currency: cfg.currency },
redirectUrl,
billingData: {
billingPlatformId: cfg.billingConnectionId,
lineItems: buildLineItems(cfg),
...(couponCodes?.length ? { couponCodes } : {}),
},
});Render the new session
Bind the new session ID and mount the component again. Altruon.js appends an iframe to the container, so clear the previous mount first.
altruon.setSession(sessionId);
document.querySelector('#payment-container').innerHTML = '';
await altruon.renderComponent('#payment-container');Both snippets come from the Altruon merchant demo, which works this way: see client/src/pages/CheckoutPage.jsx and server/src/routes.js.
Show the discount before the shopper pays
Pair the pattern above with the estimate endpoint:
POST /api/checkout/v1/{billingConnectionId}/estimate-subscriptionIt prices the subscription, including the coupon discount and the tax, without creating anything. The demo's order summary (client/src/components/OrderSummary.jsx) runs the estimate again every time the coupon list or the billing country changes.
What each billing platform expects
You always send couponCodes as an array of strings. Altruon maps the values for each billing platform:
| Billing platform | Altruon sends them as | The values must be |
|---|---|---|
| Chargebee | coupon_ids | Valid Chargebee coupon IDs |
| Recurly | coupon_codes | Valid Recurly coupon codes |
| Stripe Billing | discounts[].coupon | Stripe coupon IDs, not promotion code IDs |
| Frisbii | coupon_codes | Valid Frisbii coupon codes |
With Altruon Billing, send the coupon's Code from Settings > Altruon Billing > Coupons. See Altruon Billing coupons.
Behavior
- Several coupons.
couponCodesis an array, so you can send more than one code. - Empty array. An empty array means no coupon is sent.
- Invalid codes. Invalid or unknown coupons are rejected by the billing platform.
Test it
Test every coupon in sandbox before you use it in production.
- Create a sandbox session with the code in
billingData.couponCodes, or enter it in the coupon field of a hosted checkout. - Check that the order summary shows the discount.
- Complete a test payment. See Testing for sandbox test cards.
A code your billing platform does not know is rejected, so also try one invalid code.