Stripe
Track sales from Stripe Checkout, Payment Links and Stripe Billing with a webhook – one-off purchases, subscriptions, refunds and disputes.
- Setup
- about 15 minneeds a developer
- Tracking via
- A webhook from your Stripe
- You need
- The Stripe dashboard (Workbench) and the code that creates Checkout
What the integration reports
- SalesYes – subscriptions too
- RefundsYes – partial and disputes too
- Personal discount codesYes – subscriptions too
If your customers pay through Stripe Checkout, Payment Links or subscriptions created directly in Stripe Billing on your own site, Stripe reports each sale straight to us through a webhook. That covers one-off purchases, subscriptions and renewals. You don't need a tracking script on the page to report the sale – but the click ID has to reach Stripe. If customers sign up in your own app (a SaaS product with a subscription, for example), the tracking code handles it without any code on your server: see step 3.
Before you start
- Access to your Stripe dashboard (Workbench).
- Access to add a line of HTML to your site and your app (the easy path) – or to the code that creates Checkout Sessions or subscriptions (the developer path). Setup can send the guide straight to your developer.
- Your store ID. The address under Program → Setup already has it filled in; on this page it appears as the placeholder
DIN_BUTIK.
In Setup, Stripe has three steps: Choose platform, Stripe setup (webhook and signing secret) and Confirm (the test).
We never ask for your Stripe API key. All you give us is the webhook's signing secret, which can only confirm that a message really comes from you.
Step 1: Create the webhook
In Stripe, go to Workbench → Webhooks → Create an event destination.
- Choose Your account and the Snapshot payload format – not Thin, which Stripe suggests. Thin leaves out the order data, and without it the sale can't be recorded.
- Select the events (listed below).
- Choose Webhook endpoint and use this URL:
https://app.affilyflow.com/api/stripe/webhook/DIN_BUTIKAlways:
checkout.session.completedcheckout.session.async_payment_succeededcharge.refundedcharge.dispute.createdcharge.dispute.closedcharge.succeededradar.early_fraud_warning.created
Only for subscriptions:
invoice.payment_succeededinvoice_payment.paidcustomer.subscription.deleted
invoice_payment.paid gives us the payment reference on subscription payments, so a refund or dispute on a renewal can be matched to the sale – without it, the commission stays in place.
charge.dispute.closed tells us how a dispute ended. If you win it, we restore the sale – without the event, the sale stays rejected even though you kept the money.
If you have an older webhook, add charge.dispute.closed under Workbench → Webhooks → your endpoint → Edit destination. Everything else works as before.
Step 2: Add the signing secret
Copy the endpoint's Signing secret (it starts with whsec_) and paste it under Setup → Stripe in Affilyflow. Without it, we reject every event. Don't send it by email – the store owner pastes it in themselves.
If you also want to test in Stripe's test mode, create a matching event destination in test mode and paste its signing secret into the same field, separated by a space. We try both.
Step 3: Carry the click to the payment
There are two paths. If you have a site and an app where the customer signs up and pays by card – perhaps days later – the easy path is the right one. It needs no code on your server.
The easy path: subscriptions in your own app (SaaS)
a) Add the script line to your site and your app. The same line in both places, on every page (e.g. in <head>). It keeps the click when a customer arrives from an affiliate.
<script src="https://app.affilyflow.com/t/YOUR_STORE.js" async></script>b) Enter your app's domain under Setup → Stripe. If the app lives on a different domain than the site (say the site on company.com and the app on app.company.io), the script adds the click ID to links from the site into the app – including links rendered later. The script in the app stores it again. If everything is on the same domain, skip this step.
c) Mark the form where the customer signs up. One attribute on the form. The script finds the email field itself:
<form data-affilyflow-signup>
<input type="email" name="email">
…
</form>If the field has a special name, give it: data-affilyflow-signup="work_email". If your app creates the account with JavaScript rather than a regular form, call:
window.affilyflow && window.affilyflow.identify(email);d) Connect Stripe with the webhook from steps 1 and 2.
When the customer signs up, the script sends only the click ID, your store and a fingerprint (sha256) of the customer's email – never the email itself. When Stripe later reports the payment, we find the click through the customer's email. That covers Checkout, subscriptions without Checkout, a trial and an upgrade from a free plan – as long as the click is within the program's cookie window.
- A click ID on the payment itself (
client_reference_idor metadata) always wins. If you already use the developer path, nothing changes. - If the customer pays with a different email than the one the account was created with, the click cannot be found.
- With Wait for consent, nothing is sent until the customer has accepted marketing – exactly like the rest of the tracking.
- If another affiliate network has the customer's latest click, the customer is not linked (last click wins).
- The sale shows in your sale details as Linked via the customer's email at sign-up.
For developers: pass the click ID directly
Affiliates send customers to your site with ?afid=… in the address (and the same click ID in the alias ?afy=…, which survives ad blockers that strip afid). Store the click ID in a cookie on your main domain when the customer lands, so it survives the move from www to, say, an app subdomain:
// On every page: keep the click ID from ?afid= (or its alias ?afy=) in a cookie on your main domain (90 days)
const q = new URLSearchParams(location.search);
const afid = [q.get('afid'), q.get('afy')].find((v) => v && /^c_[A-Za-z0-9_-]{4,64}$/.test(v));
if (afid) {
const domain = location.hostname.split('.').slice(-2).join('.');
document.cookie = 'af_click=' + afid + '; domain=.' + domain + '; path=/; max-age=7776000; SameSite=Lax; Secure';
}
// On your server, read the af_click cookie from the request when creating the Checkout Session.Then pass it as client_reference_id when you create the Checkout Session:
// stripe.checkout.sessions.create({ ...,
client_reference_id: req.cookies.af_click || undefined,
// })- The domain is taken as the last two parts of the hostname. If your domain has more parts (such as
.co.uk), change it to your own. - If you create the session on the server, the browser has to send the cookie along with the request.
- If you already use
client_reference_idfor your own ID (such as a customer or order number), put the click ID in the session's metadata instead:metadata: { af_click: req.cookies.af_click }. We use the first ofclient_reference_id,metadata.af_clickandmetadata.afidthat is a click ID (c_…). - If you use Payment Links, add the click ID to the link as
?client_reference_id=…. The easiest way: put the tracking code on every page – it stores the click itself and adds it automatically to every link tobuy.stripe.com, including links rendered later.
Afterwards, check in Stripe that client_reference_id (or metadata.af_click) on the checkout.session.completed event is "c_…" and not null. If it is null, the click ID never arrived – check the cookie domain.
Subscriptions without Checkout
If you create subscriptions directly with Stripe Billing (for example with Stripe Elements or your own payment page) instead of Checkout, there is no checkout.session.completed. Put the click ID in the subscription's metadata when you create it:
// stripe.subscriptions.create({ customer, items, ...,
metadata: { af_click: req.cookies.af_click },
// })- Stripe copies the subscription's metadata to every invoice. The first paid invoice (
invoice.payment_succeeded) creates the sale, and renewals earn commission just as with Checkout. - We read
metadata.af_clickormetadata.afidon the subscription. We can't see the customer's metadata – we never fetch anything from your Stripe account – so the click ID has to be on the subscription. - If the subscription starts with a trial, the sale waits for the first paid invoice, exactly as with Checkout.
- If you use Checkout and put the click in
subscription_data.metadata, nothing is counted twice: the same invoice only counts once.
Test
The test is in the Confirm step in Setup. You can test in Stripe's test mode without moving any money:
- Press Open test link in Setup.
- Complete a one-off purchase in Stripe Checkout with a test card (e.g. 4242 4242 4242 4242) in the same browser. The webhook also has to receive test events – create it in test mode, or create a matching one for testing (see step 2).
- The live test in Setup shows the test click and the purchase as a test sale: no commission, no fee, no emails.
In test mode, only a Checkout purchase made after a click on the test link is recorded – everything else in test mode is ignored. If you would rather test in live mode, buy something cheap with a real card after opening the test link, and refund it in Stripe afterwards.
Without a test, tracking is confirmed on its own by the first affiliate purchase. Your program becomes visible to affiliates once we have received the first event from your webhook.
What Stripe reports
- The amount. Commission is calculated on the payment amount excluding VAT, taken directly from Stripe. For one-off purchases it is also calculated without shipping (
shipping_costin Checkout), as on the other platforms – the sale amount still includes shipping, and the sale details show what the commission was calculated on. - Currency. Sales in other currencies are converted to Danish kroner at the ECB rate at the time of the sale, and the rate is locked on the sale.
- Subscriptions. Turn on Commission on renewals under Program, and every renewal automatically earns commission for as long as the program says. The same invoice can never count twice. Subscriptions created without Checkout are covered when the click ID is in the subscription's metadata, or when the customer was linked at sign-up (see step 3).
- Trial periods. If the subscription starts with a free trial, the sale is only created at the first paid invoice. A renewal of DKK 0 earns no commission.
- Refunds and disputes. A full refund or a dispute rejects the sale, and the affiliate is notified. If you win the dispute (or an inquiry is closed without becoming a dispute), we restore the sale as it was – with the same status and approval date. If the sale has been refunded or changed since, it is not restored. A partial refund reduces commission and fee in the same proportion. If the program pays a fixed amount per sale, a partial refund doesn't change it. For subscriptions, this requires
invoice_payment.paidon the webhook – without it, a refund of a renewal can't be matched to the sale. - Your orders in total. The webhook also counts paid orders without an affiliate – only the number per day, never amounts, customers or IDs. That makes it possible to measure tracking coverage: how many orders carried an affiliate click, and how many of those became a sale.
Capture rate
Setup shows your capture rate: how many of the paid orders Stripe reports to us came through an affiliate, and how many of the orders with an affiliate click or code were recorded. We only keep daily counts – never orders, amounts or customers. The figures apply from the day counting started.
Personal discount codes
A personal discount code earns the affiliate commission, even without a click. On Stripe, this works for both one-off payments and subscriptions.
Stripe only sends us the coupon's ID, not the promotion code the customer typed. So:
- Create the code under Program → Discount codes in Affilyflow, and choose the affiliate under Who is the code for?.
- Create the coupon in Stripe with the same ID as the code – e.g.
ANNA10. The promotion code the customer types can have the same name.
If the customer has a valid click, the click always wins. If the click has expired, or there is no click, the sale is credited to the affiliate the coupon ID belongs to. The webhook address contains your store ID (as above), so we know which store the code belongs to.
Subscriptions with a personal code
- With Checkout there is nothing more to do: we read the coupon on the payment (
checkout.session.completed). The first payment becomes the sale, and renewals earn commission, exactly as with a click. - Without Checkout we can't see which code the customer used – a discount on the invoice only carries Stripe's internal ID. Put the code in the subscription's metadata when you create it:
metadata: { af_coupon: 'ANNA10' }. Onlyaf_couponcounts; a discount on the invoice alone never credits a sale. Don't useaf_coupontogether with Checkout – there we read the code on the payment, and otherwise the code could arrive before the customer's click. - If the subscription starts with a trial or a free first month, we remember the code and wait for the first paid invoice.
- Without a click, no terms are locked on the click. The first payment and the renewals are therefore calculated with the program's terms when they are recorded – including how many months renewals earn commission.
- Refunds and disputes work as with a click: a refunded first payment rejects the sale and stops the renewals.
Missing something?
Write to us – we answer quickly, and the page gets fixed.