# How to connect Stripe payments to your content

Pass one id from your page to Stripe, receive it in a webhook, and join it to the post that sent the buyer. Working code and a test you can run today.

By the Totems team. Published 2026-12-03. Updated 2026-12-03. https://usetotems.com/blog/stripe-payments-to-content

**How do I link Stripe sales to a post or channel?** Save a random visitor id when someone lands from your post. When they pay, send that id to Stripe as client_reference_id, and copy it into the payment's metadata. Listen for checkout.session.completed, look up the id, and you know the post. If the id is missing, match by email, or mark it Unknown.

Stripe is good at money and knows nothing about your posts. That is fine. You only need it to hold one small note for you and give it back.



## What has to happen

There are four moves: save an id, send it, get it back, look it up.



## Send the id

When your server makes the Checkout Session, set the id in two places. The first goes on the session. The second goes on the payment itself.



```ts title="Your server"
const session = await stripe.checkout.sessions.create({
  mode: "payment",
  line_items: [{ price: "price_123", quantity: 1 }],
  success_url: "https://studio.example/thanks",
  client_reference_id: visitorId,
  metadata: { visitor_id: visitorId },
  payment_intent_data: { metadata: { visitor_id: visitorId } },
});
```

For a subscription, use `subscription_data.metadata` instead. Stripe then also shows it on each invoice.

## Get it back

Check the signature first, then read the id. Use the payment id as the key, so one payment is one sale.

```ts title="Your webhook"
const event = stripe.webhooks.constructEvent(rawBody, signature, secret);
if (event.type === "checkout.session.completed") {
  const s = event.data.object;
  if (s.payment_status === "paid") {
    await saveSale({
      key: s.payment_intent,           // one payment, one sale
      visitorId: s.client_reference_id, // may be null
      email: s.customer_details?.email, // the fallback
      cents: s.amount_total,
      currency: s.currency,
    });
  }
}
```



## Join it to a post

Now the easy part. Look for proof in this order and stop at the first hit.

| Order | Proof | Trust it? |
|---|---|---|
| 1 | The visitor id on the payment | Yes. It came from the same browser |
| 2 | A Stripe customer you know | Yes. They have paid before |
| 3 | The same email | Yes, if the form used it too |
| 4 | Nothing | Mark it Unknown |

Here is a made-up day with four payments. Only one needs a guess, and we don't make one.





## Test it in fifteen minutes

1. Use Stripe test mode and a test card.
2. Click your own tagged link in a private window.
3. Pay with the test card.
4. Open the event in Stripe and find your id.
5. Pay again without clicking a link. It must land in Unknown.

The fifth step matters most. A setup that never says Unknown is guessing.



## A worked example with every field

A made-up studio sells a $490 course with a Payment Link and a $2,000 programme with Checkout. One buyer, Alex, does this.

| Day | What Alex does | What you save |
|---|---|---|
| 1 | Clicks a video link | Click, with link `/price` |
| 1 | Says yes to tracking | Visitor `v_51Xa` |
| 3 | Sends the form | Email, joined to `v_51Xa` |
| 9 | Opens Checkout | `client_reference_id: v_51Xa` |
| 9 | Pays $2,000 | Payment, with the id |
| 30 | Gets $500 back | Refund on the same payment |

On day 9 the webhook brings the id. You look up `v_51Xa`, find one click on day 1, and the video gets the sale. On day 30 the refund arrives with the payment's id. You find the sale and lower it. The video now shows $1,500.



Notice the last line. You keep the event ids so a repeat does nothing.

## Subscriptions need one more thing

A subscription pays again each month, with no visit and no browser. If you only save the id on the first session, month two looks like a new Unknown sale.

Set the id in `subscription_data.metadata`. Stripe's metadata page says a subscription's metadata is copied to its invoices as a snapshot. So each renewal carries the id.

```ts title="Subscription"
await stripe.checkout.sessions.create({
  mode: "subscription",
  line_items: [{ price: "price_monthly", quantity: 1 }],
  success_url: "https://studio.example/thanks",
  client_reference_id: visitorId,
  subscription_data: { metadata: { visitor_id: visitorId } },
});
```

Then decide what a renewal means for credit. Two fair choices:

- **All payments to the first source.** Good for asking what a video is worth over time.
- **Only the first payment.** Good for asking what brought new people this month.

Pick one and write it down. Changing halfway makes months impossible to compare.

## Edge cases worth a test

| Case | What happens | What to do |
|---|---|---|
| Buyer pays twice | Two payments, one id | Two sales, same source |
| Couple shares a laptop | Second buyer carries the first one's id | If the email belongs to someone else, flag it |
| Free trial | Session completes with no money | Don't count until an invoice is paid |
| 100% coupon | Amount is zero | Count the customer, not money |
| Other currency | Amount is in another unit | Save both, with that day's rate |
| You send an invoice by hand | No session at all | Match by customer or email |
| Dispute opens | Money leaves your balance | Take it out until it is decided |

The shared laptop is the sneaky one. The browser says Alex. The payment says Jo. The payment knows who paid, so Jo is the buyer. Don't give Jo's sale to Alex's clicks without other proof.

## A daily check that takes a minute

1. Count yesterday's paid Stripe payments.
2. Count yesterday's sales in your list.
3. If they differ, find the missing payment id.
4. Look at the share with no id. If it doubles, a page or button changed.

Do this for two weeks after you launch. Most mistakes show in the first three days.

## If you have no server

You can still do half of it. Use Payment Links with the id in the address. Once a week, export payments from Stripe and put them in a sheet. Add a second tab with your visitor ids and first clicks. Join them with a lookup. Payments without a match go in an Unknown tab, where you can see them. Don't delete that tab. It is the most honest part of the sheet.

## What can go wrong

- **You count two events as two sales.** Use the payment id, not the event id, as the key for the sale.
- **You count money that has not arrived.** Bank debits and vouchers can take 2 to 14 days to confirm. Wait for `paid`.
- **The field is taken.** If another tool uses `client_reference_id`, leave it and use metadata only.
- **The id is personal.** Never put an email in it. Stripe warns that links with parameters can turn up in unexpected places.

## When not to use Totems

If you have a developer and one checkout, the code above may be all you need. If your buyers pay inside a platform that hides its checkout, no script can add an id. And if you take fewer than ten payments a month, ask each buyer how they found you and write it down.

Totems does these steps for you, with a proof card on each sale. Its Stripe connection is written and passes our own tests. It is not yet tested with a live account.

## Questions

### Can I see the source of old Stripe payments?

Only if you saved it at the time. Stripe can't tell you which post a past buyer clicked. You can still bring the payments in and mark them Unknown.

### Should I put the video name in Stripe instead of an id?

You can, but it only holds one post. An id lets you look up every click that person made, so you can change the credit rule later.

### Which event should I listen for?

Use checkout.session.completed for Checkout and Payment Links. Some payment methods, such as bank debits, confirm days later, so check payment_status before you count the money.

### Can one sale arrive twice?

The same event can be sent again, because Stripe retries when your server does not answer. Save the event id and the payment id, and skip any you have seen.

## Sources

- [Stripe: The Checkout Session object](https://docs.stripe.com/api/checkout/sessions/object), checked 2026-12-03
- [Stripe: Metadata](https://docs.stripe.com/metadata), checked 2026-12-03
- [Stripe: Receive events with webhooks](https://docs.stripe.com/webhooks), checked 2026-12-03
- [Stripe: Track a payment link](https://docs.stripe.com/payment-links/url-parameters), checked 2026-12-03
