Background
⌘K

Auto-Apply Affiliate Coupons at Checkout

Learn how to automatically apply affiliate coupon codes during checkout

Silvestro
Written by Silvestro
Updated more than a month ago

When a customer arrives on your site through an affiliate referral link, Affonso automatically stores the affiliate's coupon data in the browser. This guide shows you how to retrieve that data and pass it to your payment provider so the discount is applied at checkout — without the customer having to manually enter a code.

This works with both program-wide coupons and unique affiliate coupons. For an overview of both coupon types, see Double-Sided Incentives with Coupon Codes.

Step 1: Retrieve Coupon Data on the Frontend

The Affonso tracking pixel provides JavaScript methods to access coupon data. Add this logic to your checkout page or checkout button handler:

function getCouponForCheckout() {
  // Check if Affonso is loaded
  if (typeof window.Affonso === 'undefined') return null;

  // Prefer unique affiliate coupon over program-wide coupon
  const affiliateCoupons = window.Affonso.getAffiliateCoupons();
  if (affiliateCoupons.length > 0) {
    return affiliateCoupons[0]; // Most recently created coupon
  }

  // Fall back to program-wide coupon
  return window.Affonso.getDiscount();
}

Each coupon object contains:

FieldDescriptionExample
couponIdPayment provider's coupon ID"promo_abc123"
promoCodeHuman-readable coupon code"JOHN20"
amountDiscount value20
type"percentage" or "fixed""percentage"
maxDuration"once", "forever", or "repeating""once"

Step 2: Send to Your Backend

Pass the couponId to your backend when creating the checkout session:

const coupon = getCouponForCheckout();

const response = await fetch('/api/checkout', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    priceId: 'price_xxx',
    couponId: coupon?.couponId || null,
    // ... your other checkout data
  })
});

const { url } = await response.json();
window.location.href = url;

Step 3: Apply in Your Payment Provider

Stripe

Pass the couponId as a discount when creating a Checkout Session:

const session = await stripe.checkout.sessions.create({
  mode: 'subscription', // or 'payment'
  line_items: [{ price: priceId, quantity: 1 }],
  // Apply the affiliate coupon
  discounts: couponId
    ? [{ coupon: couponId }]
    : undefined,
  success_url: 'https://yoursite.com/success',
  cancel_url: 'https://yoursite.com/cancel',
});

If you use Stripe Promotion Codes instead of Coupons, you can pass allow_promotion_codes: true to let customers enter the code manually, or use the promoCode field to pre-fill it.

Stripe Checkout Session API Docs

DODO

Pass the couponId as discount_id:

const session = await fetch('https://api.dodopayments.com/checkout-sessions', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${DODO_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    product_id: productId,
    discount_id: couponId || undefined,
    // ... other fields
  }),
});

DODO Checkout Session API Docs

Creem

Pass the promoCode as discount_code:

const session = await fetch('https://api.creem.io/v1/checkouts', {
  method: 'POST',
  headers: {
    'x-api-key': CREEM_API_KEY,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    product_id: productId,
    discount_code: promoCode || undefined,
    // ... other fields
  }),
});

Note: Creem uses the promoCode (the human-readable code) instead of couponId.

Creem Checkout API Docs

Polar

Pass the couponId as discount_id:

const checkout = await polar.checkouts.create({
  productPriceId: priceId,
  discountId: couponId || undefined,
  successUrl: 'https://yoursite.com/success',
});

Polar Checkout API Docs

Paddle

Pass the couponId as discount_id in the checkout items:

Paddle.Checkout.open({
  items: [{ priceId: priceId, quantity: 1 }],
  discountId: couponId || undefined,
});

Paddle Checkout API Docs

Optional: Show the Discount Before Checkout

You can display the discount to the customer before they reach checkout to increase conversion:

const coupon = getCouponForCheckout();

if (coupon) {
  const discountText = coupon.type === 'percentage'
    ? `${coupon.amount}% off`
    : `$${coupon.amount} off`;

  document.getElementById('discount-badge').textContent =
    `Your discount: ${discountText} with code ${coupon.promoCode}`;
}

Troubleshooting

Coupon data is null:

  • Make sure the Affonso tracking pixel is installed and loaded before your checkout code runs
  • Verify the customer arrived via an affiliate referral link (?via=... parameter)
  • Check that the affiliate has an active coupon (program-wide or unique)

Coupon is not applied at checkout:

  • Verify the couponId matches a valid coupon in your payment provider
  • Ensure the coupon hasn't expired or exceeded its usage limit
  • Check that the coupon is valid for the product/price being purchased

Was this article helpful?

If you still need help, our support team is here for you.

Contact Support
bg

Ready to Scale Your SaaS?

Affonso is the easiest way to launch your own affiliate program. We take care of the technical stuff, so you can focus on growing your business.