# Dodo Payments

> Connect Dodo Payments with an API key and a webhook signing secret, and pass the visitor id in checkout metadata.

Reveata connects to Dodo Payments with an API key, imports your payments, refunds, disputes and subscriptions, and receives new ones through a webhook in Dodo, which it adds for you when the key allows.

## Connect

1. In Dodo Payments, open **Developer → API keys** and create a key that can read payments, refunds, disputes and subscriptions (`payments:read`, `refunds:read`, `disputes:read`, `subscriptions:read`). A key without refunds or disputes stops the import with a message naming the missing permission.
2. In Reveata, open your website's **Settings → Payments**, choose **Dodo Payments** under **Pick your payment provider**, paste the key into **API key** and select **Connect Dodo Payments**. Test or live mode is detected from the key, so a test key only ever brings test payments.
3. If the key can also manage webhooks, Reveata adds the webhook in Dodo for you, subscribed to the events below, and saves its signing secret. The card shows the webhook as **Set up in Dodo Payments by Reveata** and you are done.
4. Otherwise the card shows the webhook URL of this connection. In Dodo, open **Developer → Webhooks**, add an endpoint with that URL and subscribe it to the events below. Copy the endpoint's signing secret (`whsec_…`), paste it into the card and choose **Save secret**. You can also paste it in **Webhook signing secret** while connecting, if you created the endpoint first; Reveata then adds nothing itself.

```text
payment.succeeded
refund.succeeded
subscription.active
subscription.renewed
subscription.on_hold
subscription.past_due
subscription.paused
subscription.unpaused
subscription.cancelled
subscription.failed
subscription.expired
subscription.plan_changed
subscription.updated
dispute.lost
dispute.accepted
```

Until the secret is saved, the card shows **Webhook missing** and Reveata reads Dodo every hour instead, so payments arrive late rather than never. **Disconnect** deletes the webhook endpoints in Dodo that point at this connection.

## What counts

- **Succeeded payments** are revenue, without tax. A subscription's first payment is new revenue; later ones are renewals that keep the first payment's source.
- **Refunds** are booked as negative revenue on the day they succeed.
- **Disputes** that are lost or accepted take their amount out of revenue like a refund. Won, cancelled or expired disputes change nothing.
- **Subscriptions** feed MRR, churn, upgrades and downgrades. Dodo can deliver events out of order; an older subscription state never overwrites a newer one.

On connect, Reveata imports the last 12 months.

## Pass the visitor id

Add the ids to the checkout session or payment `metadata` your server creates. Send them from the browser with `window.reveata.metadata()`, or read them from the `_dt_v` and `_dt_s` cookies (see [Stripe Checkout](https://reveata.com/docs/stripe-checkout#any-other-backend) for parsers):

```js
metadata: {
  reveata_visitor_id: body.reveata_visitor_id,
  reveata_session_id: body.reveata_session_id,
}
```

Reveata also reads `reveata_user_id` (the `userId` you pass to identify). Payments without ids are matched by the customer's email for visitors you [identify](https://reveata.com/docs/identify-visitors).

## Check it works

1. Connect a test key to a test website, with the webhook saved.
2. Open the page that starts your checkout in a private window, from a link ending in `?ref=test`, and pay in Dodo's test mode.
3. The payment appears under **Revenue** with the source `test`, and **Last delivery** on the card shows the webhook arrived.

A webhook delivered twice is processed once, and each payment is stored once under its Dodo id. See [Why payments are never counted twice](https://reveata.com/docs/no-double-counting).
