Stripe Checkout Sessions
This guide shows you how to integrate referral codes from Referral Rocket into your Stripe checkout sessions, allowing you to track referrals and attribute conversions properly.
Step 1: Install Referral Rocket SDK
First, install the Referral Rocket library by adding the script to the <head> section of your website. Replace YOUR-CAMPAIGN-ID with your actual campaign ID.
<!-- Add this script in the HEAD tag -->
<script type="text/javascript" campaign-id="YOUR-CAMPAIGN-ID" defer src="https://app.referralrocket.io/widget/widgetIndex.js"></script>Step 2: Retrieve Referral Code
Use the getReferralCode() function to retrieve the referral code stored in cookies. This function returns the referral code if available, or null if no referral code is found.
const referralCode = window.Rocket.getReferralCode();
console.log('Referral code:', referralCode); // Will log the code or nullStep 4: Create Stripe Checkout Session with Referral Code
Stripe Checkout Sessions must be created server-side using your Stripe secret key. The referral code captured client-side needs to be sent to your server first, then included when creating the session.
Important: Never call
stripe.checkout.sessions.createfrom the browser. It requires your Stripe secret key, which must never be exposed client-side.
Client-side — send the referral code to your server when initiating checkout:
// On your checkout page (browser)
const referralCode = window.Rocket.getReferralCode();
fetch('/create-checkout-session', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ referralCode })
});Server-side — include the referral code as client_reference_id (Node.js example):
Last updated