You are helping integrate LinkJolt affiliate tracking for a no-code shop that uses hosted payment link providers (Stripe Payment Links, Gumroad, and/or Lemon Squeezy).

## Context
- Merchant ID: YOUR_MERCHANT_ID
- Platforms: Stripe Payment Links + Gumroad + Lemon Squeezy (all auto-detected)
- Tracking Script: https://www.linkjolt.io/api/tracking.js?id=YOUR_MERCHANT_ID

## How It Works

The LinkJolt tracking script detects and auto-enhances outbound payment links on every page:

| Link hostname | Injected query parameter |
|---|---|
| `buy.stripe.com/*` | `client_reference_id=tc_X_aff_Y_cid_Z` |
| `<stripe-pricing-table>`, `<stripe-buy-button>` (Stripe's embeds) | `client-reference-id="tc_X_aff_Y_cid_Z"` attribute |
| `*.gumroad.com`, `gum.co` | `tc`, `aff`, `cid` |
| `*.lemonsqueezy.com` | `checkout[custom][tc]`, `checkout[custom][aff]`, `checkout[custom][cid]` |

LinkJolt must ALSO receive each provider's sales, so conversions are attributed server-side when the purchase completes: connect Stripe in LinkJolt, or configure a webhook on each provider they use (below).

---

## Code Integration

### Add Tracking Script

Add this to your website's `<head>` tag:

**HTML:**
```html
<script src="https://www.linkjolt.io/api/tracking.js?id=YOUR_MERCHANT_ID" async></script>
```

**Next.js / React:**
```tsx
import Script from 'next/script'

// Add this in your layout.tsx or _app.tsx
<Script
  src="https://www.linkjolt.io/api/tracking.js?id=YOUR_MERCHANT_ID"
  strategy="afterInteractive"
/>
```

That single `<script>` tag enhances links for all three providers automatically. No further code changes needed in the site.

---

## Provider webhook setup (pick whichever the merchant uses)

### Stripe Payment Links
Quickest: LinkJolt → Integration Hub → Stripe → Step 1, **Set up with Stripe**, and approve once on Stripe's site. No webhook and no secret are needed after that. Or by hand:
1. Stripe Dashboard → Developers → Webhooks → Add endpoint
2. URL: `https://www.linkjolt.io/api/webhooks/stripe/merchant/YOUR_MERCHANT_ID`
3. Required event: `checkout.session.completed`
4. Optional (for subscriptions): `invoice.payment_succeeded`, `customer.subscription.updated`, `customer.subscription.deleted`
5. Save the Signing secret (`whsec_*`) in LinkJolt → Settings → Payment Providers (provider: stripe).

### Gumroad
1. REQUIRED FIRST: in LinkJolt → Settings → Payment Providers, add a Gumroad webhook secret (any long random value, the dialog has a generator). This is not optional: LinkJolt rejects Gumroad pings it cannot verify.
2. Copy the full Ping URL shown on that page. It looks like `https://www.linkjolt.io/api/webhooks/gumroad?merchant=YOUR_MERCHANT_ID&token=...` where the token is derived from the saved secret. The URL below is NOT usable without the token.
3. Gumroad Dashboard → Settings → Advanced → Ping: paste the full URL (including the token) in the "Ping endpoint" field and save.
4. No event selection needed, Gumroad's Ping fires on every sale automatically.
5. If the merchant's pings arrive HMAC-signed (X-Gumroad-Signature), signatures computed with the same saved secret are also accepted.

### Lemon Squeezy
1. Lemon Squeezy Dashboard → Settings → Webhooks → "+"
2. Callback URL: `https://www.linkjolt.io/api/webhooks/lemonsqueezy?merchant=YOUR_MERCHANT_ID`
3. Required event: `order_created`
4. Save Webhook. Copy the signing secret and save in LinkJolt → Settings → Payment Providers (provider: lemonsqueezy).

---

## Tracking Script API Reference

```javascript
// Format: "tc_CAMPAIGN123_aff_AFFILIATE456_cid_CLICK789"
window.linkjolt.referral

// Returns: { trackingCode, affiliateId, clickId }
window.linkjolt.getTrackingInfo()

// Returns boolean
window.linkjolt.isAffiliateVisit()

// Browser-side conversion reporting (used by Squarespace / Teachable / BigCommerce paths)
window.linkjolt.trackPurchase(amount, { orderId: 'ORDER_ID' })
```

---

## Testing

1. Visit your site with test parameters: `https://your-site.com/?tc=TEST123&aff=AFF456`
2. Open browser console and verify:
   ```javascript
   console.log(window.linkjolt.referral);
   // Should output: "tc_TEST123_aff_AFF456"
   ```
3. Complete a real or test purchase in your platform.
4. Check LinkJolt dashboard for the conversion.

## Common issues

- "Payment links not being enhanced" → make sure links are rendered as `<a href="buy.stripe.com/...">` tags (not behind JS that opens a new tab programmatically).
- "Pricing table or buy button sale has no affiliate" → the page must load the tracking script, and the element must not carry a `client-reference-id` of your own (the script never replaces one).
- "Webhook received but no conversion" → the visitor didn't go through an affiliate link first, or the cookie expired (30-day window).
- "Gumroad ping returns 401" → save the Gumroad application secret in LinkJolt → Settings → Payment Providers so HMAC verification passes.
- "Lemon Squeezy webhook signature fails" → save the LS webhook signing secret in LinkJolt → Settings → Payment Providers (provider: lemonsqueezy).
