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 null

Step 4: Create Stripe Checkout Session with Referral Code

When creating your Stripe checkout session, include the referral code in the client_reference_id field. This allows you to track which purchases came from referrals.

const createCheckoutSession = async () => {
  try {
    // Get the referral code from Referral Rocket
    const referralCode = window.Rocket.getReferralCode();
    
    // Create the Stripe checkout session
    const stripeSession = await stripe.checkout.sessions.create({
      success_url: "https://your-website.com/success",
      cancel_url: "https://your-website.com/cancel",
      client_reference_id: referralCode, // Pass the referral code here
      mode: "subscription", // or "payment" for one-time purchases
      line_items: [
        {
          price: "price_1JZ2e2LzKb4YjC8Cw", // Your Stripe price ID
          quantity: 1,
        }
      ]
    });
    
    console.log('Checkout session created:', stripeSession.id);
    return stripeSession;
    
  } catch (error) {
    console.error('Error creating checkout session:', error);
    throw error;
  }
};

Last updated