> ## Documentation Index
> Fetch the complete documentation index at: https://docs.postmetric.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Stripe

> Connect Stripe Checkout, PaymentIntent, and Payment Links

Connect Stripe to automatically track revenue and link it to your marketing campaigns.

## Supported Stripe methods

* **Stripe Checkout** - Hosted checkout pages
* **PaymentIntent API** - Custom payment flows
* **Payment Links** - Shareable payment links
* **Other Stripe methods** - Any Stripe payment method

## Setup

### Step 1: Get your webhook URL

Your webhook URL is:

```
https://your-domain.com/api/webhooks/stripe
```

### Step 2: Configure Stripe webhook

1. Go to your [Stripe Dashboard](https://dashboard.stripe.com/webhooks)
2. Click **"Add endpoint"**
3. Enter your webhook URL: `https://your-domain.com/api/webhooks/stripe`
4. Select events to listen for:
   * `checkout.session.completed`
   * `payment_intent.succeeded`
   * `charge.refunded`
5. Copy the **Signing secret** (you'll need this for verification)

### Step 3: Add webhook secret to environment

Add your Stripe webhook secret to your environment variables:

```env theme={null}
STRIPE_WEBHOOK_SECRET=whsec_...
```

## Stripe Checkout

### Include visitor/session IDs in metadata

When creating a checkout session, include visitor and session IDs in the metadata:

```javascript theme={null}
// Client-side: Get visitor/session IDs
const visitorId = window.postMetric?.getVisitorId();
const sessionId = window.postMetric?.getSessionId();

// Server-side: Create checkout session
const session = await stripe.checkout.sessions.create({
  payment_method_types: ["card"],
  line_items: [
    /* your items */
  ],
  mode: "payment",
  success_url: "https://your-site.com/success",
  cancel_url: "https://your-site.com/cancel",
  metadata: {
    websiteId: "your-website-id",
    visitorId: visitorId, // From client
    sessionId: sessionId, // From client
  },
});
```

### Redirect to checkout

```javascript theme={null}
window.location.href = session.url;
```

## PaymentIntent API

### Include visitor/session IDs in metadata

```javascript theme={null}
const paymentIntent = await stripe.paymentIntents.create({
  amount: 1000, // $10.00
  currency: "usd",
  metadata: {
    websiteId: "your-website-id",
    visitorId: visitorId,
    sessionId: sessionId,
  },
});
```

## Payment Links

For Payment Links, you can't add metadata directly. Instead, use the customer email matching method:

1. Enable [user identification](/advanced/user-identification) in PostMetric
2. Identify users before they purchase
3. Payments will be linked via email matching

## View revenue data

Once payments are linked, you can view:

* Revenue by source/channel
* Revenue by campaign
* Conversion rates
* Average order value

## Refunds

Refunds are automatically tracked when a `charge.refunded` event is received. The payment status will be updated to "refunded" in your dashboard.

## Troubleshooting

### Payments not appearing

1. Check that the webhook is configured correctly
2. Verify the `websiteId` is included in payment metadata
3. Check webhook logs in Stripe Dashboard
4. Verify the webhook secret is set correctly

### Payments not linked to visitors

1. Ensure visitor/session IDs are included in payment metadata
2. Enable user identification for email matching fallback
3. Check that the visitor was tracked before the payment

## Next steps

<Card title="Revenue attribution guide" icon="dollar-sign" href="/revenue-attribution/get-started">
  Learn more about revenue attribution
</Card>
