Skip to main content

Stripe Integration

Integrate Endorsely with your Stripe checkout to automatically track affiliate conversions and calculate commissions.

Integration Steps

Step 1: Connect with your Stripe account

This takes 30 seconds. Just click the "Connect with Stripe" button in your Endorsely dashboard and you'll be taken to your Stripe page to connect it with Endorsely, so we can track conversions that come from an affiliate link.

Step 2: Add our script to your website

Add this script to every page on your site that you want affiliates to be able to link to with their referral ID:

<script
async
src="https://assets.endorsely.com/endorsely.js"
data-endorsely="<your-organization-id>"
></script>
caution

If you use a cookie tool on your website that automatically removes tracking cookies without consent, you will need to whitelist Endorsely's cookie to allow affiliate clicks to be tracked.

Step 3: Send the referral ID to Stripe

There are multiple ways to send the referral ID to Stripe depending on how you've implemented your checkout:

Option A: Server-side Checkout Session

When creating a Stripe checkout session on your server, include the Endorsely referral ID in the metadata:

const session = await stripe.checkout.sessions.create({
success_url: "https://example-website.com/success",
cancel_url: "https://example-website.com/cancel",
// add referral id received from client to metadata
metadata: {
endorsely_referral: req.body.referral,
},
line_items: [{ price: "price_xyz", quantity: 1 }],
mode: "subscription",
});

Option B: Server-side Subscription Creation

When creating a subscription directly:

const subscription = await stripe.subscriptions.create({
customer: "cus_xyz",
// add referral id received from client to metadata
metadata: {
endorsely_referral: req.body.referral,
},
items: [{ price: "price_xyz" }],
});

For embedded Stripe links, use this script to automatically append the referral ID:

<script
async
src="https://assets.endorsely.com/endorsely.js"
data-endorsely="<your-organization-id>"
></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
setTimeout(function () {
$('a[href^="https://buy.stripe.com/"]').each(function () {
const oldBuyUrl = $(this).attr("href");
const referralId = window.endorsely_referral;
if (!oldBuyUrl.includes("client_reference_id")) {
const newBuyUrl = oldBuyUrl + "?client_reference_id=" + referralId;
$(this).attr("href", newBuyUrl);
}
});
$("[pricing-table-id]").each(function () {
$(this).attr("client-reference-id", window.endorsely_referral);
});
$("[buy-button-id]").each(function () {
$(this).attr("client-reference-id", window.endorsely_referral);
});
}, 2000);
});
</script>

Option D: Stripe Pricing Table

For Stripe Pricing Tables:

<script
src="https://assets.endorsely.com/endorsely.js"
data-endorsely="<your-organization-id>"
></script>

<div id="pricing-table-container"></div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const pricingTable = document.createElement("stripe-pricing-table");
pricingTable.setAttribute(
"pricing-table-id",
"<your-pricing-table-id>" // <-------- Add your pricing table id here
);
pricingTable.setAttribute(
"publishable-key",
"<your-publishable-key>" // <-------- Add your publishable key here
);
if (window.endorsely_referral) {
pricingTable.setAttribute(
"client-reference-id",
window.endorsely_referral
);
}
document
.getElementById("pricing-table-container")
.appendChild(pricingTable);
});
</script>

Accessing the Referral ID

You can access the unique referral ID in the frontend using the window object:

window.endorsely_referral;
  • If you're calling Stripe client-side, include the endorsely_referral metadata using window.endorsely_referral
  • If you're calling Stripe server-side, pass the Endorsely referral ID from your client to your server using the window.endorsely_referral variable