# 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.

```html
<!-- 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.

```javascript
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

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.create` from 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:

```javascript
// 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):

```javascript
// On your server — never run this in the browser
const session = await stripe.checkout.sessions.create({
  success_url: "https://your-website.com/success",
  cancel_url: "https://your-website.com/cancel",
  client_reference_id: req.body.referralCode, // passed from client
  mode: "subscription", // or "payment" for one-time purchases
  line_items: [
    {
      price: "price_1JZ2e2LzKb4YjC8Cw", // your Stripe price ID
      quantity: 1,
    }
  ]
});
```
