# Razorpay

Attribute captured Razorpay payments created through the Orders API.

## 1. Connect Razorpay

1. In VisitTrack, open **Settings → Revenue** and pick **Razorpay**.
2. Copy the webhook URL shown there. It looks like `https://visitrack.app/api/razorpay-webhook/YOUR_SITE_ID`.
3. In Razorpay: Account & Settings → Webhooks → **Add new webhook**, with a secret of your choice. Paste the URL and subscribe to `payment.captured` and `refund.processed`.
4. Copy the same webhook secret Razorpay gives you back into VisitTrack and press **Connect**. It's stored encrypted and only used to verify signatures.

## 2. Pass the visitor id

Put it in the order's `notes`. Razorpay copies order notes onto the payment. You can also set them in the Checkout options.

Orders API:
```
const order = await razorpay.orders.create({
  amount: 49900,
  currency: "INR",
  notes: { visitrack_visitor_id: visitorId ?? "" },
});
```

Checkout.js:
```
const rzp = new Razorpay({
  key: "rzp_live_...",
  order_id: order.id,
  notes: { visitrack_visitor_id: localStorage.getItem("_ana_vid") ?? "" },
});
rzp.open();
```

## Events

| Event | What VisitTrack does |
| --- | --- |
| `payment.captured` | Records the payment (`amount`, `currency`, `email`) against the visitor in `notes.visitrack_visitor_id`. |
| `refund.processed` | Subtracts the refund from the original payment. `refund.created` is ignored, because a refund can still fail at that point. |
| anything else | Acknowledged with `200` and ignored. |

## Test it

1. Open your site in a normal browser tab so the tracker records a visit (localhost is ignored unless the script tag has `data-allow-local`).
2. Complete a checkout in test mode.
3. Open **Revenue** in the dashboard. The payment shows up within a few seconds, with the referrer, campaign and landing page that brought that visitor in.

> **Nothing showed up?** — See [Missing or unattributed payments](https://visitrack.app/docs/revenue-troubleshooting). The webhook response body always says why a payment was skipped.
