# Connect Stripe revenue

Send your Stripe sales and refunds to SourceTrack. Each sale is credited to the source that brought the buyer, when the checkout carries the buyer’s visitor ID.

This is a webhook connection you set up yourself, not a Stripe app. Stripe test-mode events are ignored, so test with a small live purchase and then refund it.

**You need:** admin access to your Stripe account and a SourceTrack plan that includes revenue (the Free plan does not). For Checkout Sessions, you also need access to the code that creates them.

## Which Stripe Method Are You Using?

Pick the entry that matches how your site takes payment. You can use more than one.

- **Payment Links (`buy.stripe.com`):** add the SourceTrack tracking code to the page that holds the link. On the standard install, it adds the visitor ID to the link when a visitor clicks it, if tracking is allowed for that visitor. A link that already has its own `client_reference_id` is left as it is, and the cookieless install does not add it. Then do Step 2.
- **Checkout Sessions created by your backend:** do Step 1 and Step 2.
- **Payment Intents without Checkout:** this connection does not read `payment_intent.succeeded`, so it does not record these sales. Send them with the Offline Conversions API instead (see Next Step).

## Steps: Stripe Integration

### Step 1: Send the visitor ID with each checkout

1. In the browser, read the visitor ID with `sourcetrack.getToken()` and send it with your checkout request.
2. On your server, pass it to Stripe as `client_reference_id`, or as `anonymous_id` in the session’s `metadata`.
3. If the visitor has not allowed tracking, `getToken()` returns nothing. Create the checkout anyway: the sale is still recorded, without a source.

```
// Browser: read the visitor ID and send it with your checkout request
const visitorId = window.sourcetrack ? window.sourcetrack.getToken() : null

// Server (Node.js): pass it to Stripe
const session = await stripe.checkout.sessions.create({
  mode: 'payment',
  line_items: [{ price: 'price_123', quantity: 1 }],
  success_url: 'https://yoursite.com/success',
  cancel_url: 'https://yoursite.com/cancel',
  client_reference_id: req.body.visitorId
  // or: metadata: { anonymous_id: req.body.visitorId }
})
```

### Step 2, option A: Connect with a restricted key

SourceTrack checks the key, creates the webhook for you when the key allows it, and imports your payments from the last 12 months.

1. In Stripe, open **Developers → API keys** and create a restricted key.
2. Give it **Read** access to Checkout Sessions, Invoices, Charges, Refunds and Disputes. To let SourceTrack create the webhook, also give **Write** access to Webhook Endpoints. Leave everything else at None.
3. In SourceTrack, open [Integrations](https://app.sourcetrack.ai/app/integrations) → **Stripe webhook recipe**.
4. Paste the key (it starts with `rk_live_`) and click **Connect Stripe**.
5. If the card asks you to set up the webhook manually or paste a signing secret, follow option B below.

Imported payments appear on their original dates. Payments that carry no visitor ID are imported without a source, and the Stripe card shows how many.

### Step 2, option B: Add the webhook by hand

1. In SourceTrack, open [Integrations](https://app.sourcetrack.ai/app/integrations) → **Stripe webhook recipe** and copy the **Stripe Webhook Listener URL**. It looks like this:
   https://api.srctk.com/api/webhooks/stripe/YOUR_SITE_KEY
2. In Stripe, open **Developers → Webhooks** and add an endpoint with that URL.
3. Select these events:
    - Every site: `checkout.session.completed` and `refund.created`
    - Delayed payments (ACH, SEPA debit, Klarna): `checkout.session.async_payment_succeeded`
    - Subscriptions: `invoice.paid`, `invoice.payment_failed`, `customer.subscription.created`, `customer.subscription.updated` and `customer.subscription.deleted`
    - Refund changes and lost disputes: `charge.refunded`, `refund.updated`, `refund.failed` and `charge.dispute.closed`
4. Save the endpoint and copy its signing secret (it starts with `whsec_`).
5. Back in the Stripe card in SourceTrack, paste the signing secret and click **Save signing secret**.

### What you get

- Refunds are subtracted from your revenue. When SourceTrack finds the original sale, the refund comes off that sale’s source.
- Subscription renewals are credited to the source of that customer’s first checkout.
- With subscription events connected, the Dashboard can show MRR and churn by channel.

## Check That It Works

1. Visit your site through a tagged link, for example with `?utm_source=test`.
2. Make a small **live** purchase, then refund it.
3. In SourceTrack, open the Stripe card on **Integrations**. Under **Recent webhook activity**, the purchase should show as `success`.
4. In Stripe, the webhook’s delivery log should show a `200` response.

Test-mode purchases, including Stripe’s “Send test webhook”, get a `200` response but are not recorded as revenue.

Not seeing data? See [Troubleshooting](https://www.sourcetrack.ai/docs/troubleshooting).

## Common Mistakes

- **Stripe shows 400 errors:** the signing secret or the endpoint URL is wrong. Copy the URL from the Stripe card again and paste the endpoint’s `whsec_` secret.
- **Sales have no source:** the checkout did not carry the visitor ID. Check Step 1.
- **A custom key such as `metadata.st_aid`:** SourceTrack does not read it. Use `client_reference_id` or `metadata.anonymous_id`.
- **Bank and Klarna payments are missing:** add `checkout.session.async_payment_succeeded` to the webhook.

## Next Step

To record sales made outside Stripe Checkout, use the [Offline Conversions API](https://www.sourcetrack.ai/developers/offline-conversions).
