# Stripe

> Connect Stripe so your customers and their payments appear against the campaigns that brought them.

Available on the Attribute plan and above.

Connect Stripe to see which campaigns and ads brought the people who paid
you. Each payment, less any refund, is credited to the visits that led to it,
by your attribution settings, and a buyer's page in Visitors shows when they became a customer and
what they paid.

## Before you begin

- The Attribute plan.
- A Numbat role that can edit site settings. A Workspace Member cannot.
- Access to the Stripe account's API keys. In Stripe that is usually an
  Administrator or a Developer.
- One Stripe connection per site.

## Steps

You connect Stripe with a restricted key, a key that can only do what you
allow. Numbat needs it to read six things.

1. [Open the Stripe integration in Numbat](https://app.numbat.io/go/integrations/stripe)
   Choose **Open Stripe API keys**. Stripe opens in a new tab. You can
   also open [API keys in Stripe](https://dashboard.stripe.com/apikeys)
   from here.

   To try it out first, switch Stripe to test mode before you make the
   key. A test key reads only test data, and you can connect a live key
   later.

   ![The Stripe card in Numbat, marked Not connected, with five numbered steps to make a restricted key, a table of the six permissions set to Read, and an Open Stripe API keys button](../../../assets/docs/numbat/stripe-key-steps.png)

2. In Stripe, choose **Create restricted key**. When Stripe asks how the key
   will be used, choose **Providing this key to a third-party application**.
   Name it Numbat, with the URL numbat.io.

   ![Stripe's Create a restricted key dialog, asking how the key will be used, with Providing this key to a third-party application as the second choice](../../../assets/docs/stripe/key-usage-dialog.png)

3. Tick **Customize permissions for this key**. Without the tick, Stripe
   gives the key its own default permissions.

   ![Stripe's Enter website details step, with the name Numbat, the URL https://numbat.io, and Customize permissions for this key ticked](../../../assets/docs/stripe/key-details.png)

   Stripe turns some permissions on by default, so set every permission to
   **None** first. Each row has a None, Read and Write control.

   ![A permission row in Stripe, with a None, Read and Write control and None selected](../../../assets/docs/stripe/permission-control.png)

   Then set these six to **Read**, and leave everything else at **None**:

   - Events
   - Checkout Sessions
   - Customers
   - Subscriptions
   - Invoices
   - Charges and Refunds

   The list is long, so use the search box to find each one by name, then set
   it to **Read**.

   ![Stripe's permission editor with event typed in the search box, showing the Events permission set to Read](../../../assets/docs/stripe/permission-search.png)

   A few permissions sit inside a group, such as Billing for Subscriptions
   and Invoices. When the row itself is grayed out, set the group to **Read**
   and the ones inside it follow.

   ![Stripe's permission editor with the Billing group set to Read and the Subscriptions row inside it reading Read](../../../assets/docs/stripe/permission-group.png)

4. Choose **Create key**, then copy it. A live key starts with `rk_live_`
   and a test key with `rk_test_`.

   ![Stripe's API keys page, listing the new Numbat restricted key with its token and an Access policy of None](../../../assets/docs/stripe/restricted-key-created.png)

5. Back in Numbat, paste the key and choose **Connect Stripe to
   <your site>**. Numbat checks that the key can read all six. If one is
   missing, the page names it, and nothing is saved.

   ![Step 5 on the Stripe card, with a key pasted into the field and the Connect Stripe to Demo button](../../../assets/docs/numbat/stripe-paste-key.png)

The card reads **Importing** with a running count of customers, then
**Syncing**. Numbat imports the last 90 days of purchases first, then checks
Stripe for new ones every few minutes.

![The Stripe card in Numbat, marked Syncing, with the account in Live mode, who connected it, what the key can read, and Checkout links on](../../../assets/docs/numbat/stripe-connected.png)

## What appears where

- Ad Campaigns shows Stripe revenue in the Revenue column, against each
  campaign, ad group, ad and keyword. Each currency shows on its own.
- Visitors gains a Revenue column, and a buyer's page has a Stripe tab and
  Stripe rows on its timeline: started a trial, became a customer, paid and
  refunded.

## How purchases are matched to visitors

A purchase only helps your reports once Numbat knows which visitor made it.
Numbat tries two ways, in this order.

The first is the checkout link. When a visitor who allowed identification
clicks a Stripe Payment Link on your site, or opens a Stripe pricing table,
Numbat adds a reference to it. Stripe keeps that reference with the
purchase, and Numbat reads it back. This needs nothing from you.

The second is email. If the buyer's email is one Numbat has seen on your
site, for example in a form they filled in, the purchase is matched to them.

### If your own server creates the checkout

If your site sends visitors to a Stripe Checkout that your server creates,
pass Numbat's reference to it. In the page, ask Numbat for the reference and
send it with the request that starts the checkout.

```js
numbat("checkoutReference", function (reference) {
  // reference is null when the visitor has not allowed identification
  startCheckout({ numbatReference: reference });
});
```

On your server, set it as the Checkout Session's `client_reference_id`. If
you already use `client_reference_id` for something else, keep your own
value there. Numbat leaves it alone and matches those purchases by email.

## Check that it works

Once purchases have synced, the Stripe page shows how many of your Stripe
customers were matched to a visitor, and how many by each route. Below it,
the customers who did not match are counted by reason.

| Reason | What it means |
| --- | --- |
| Email not seen on your site | The buyer never gave Numbat that email. |
| No checkout link and no email | The checkout carried no reference, and Stripe has no email for the buyer. |
| Checkout opened over a day after the visit | The reference was too old by the time they paid. |
| Visitor not seen yet | The reference is valid, but Numbat had not recorded that visit when the purchase arrived. |
| Your own reference on the checkout | The checkout carried your own `client_reference_id`, so only email could match it. |

A low match rate is often normal. A buyer who paid without ever filling in a
form, and without a checkout link from your site, can't be matched.

## If you also connect HubSpot

A sale can be both a won deal in [HubSpot](/docs/integrations/hubspot) and a payment in Stripe, so Numbat
never adds the two together. Ad Campaigns and Visitors show one of them.
By default it is the one you connected first.

To change it, choose **HubSpot won deals** or **Stripe payments** under
**Revenue column** on the Stripe page or the HubSpot page. Both pages show
the same choice. You can change it at any time: nothing imported is lost,
and the revenue shown changes the next time a page loads.

![The Revenue column row on the Stripe card, with a choice between HubSpot won deals and Stripe payments](../../../assets/docs/numbat/stripe-revenue-switch.png)

## Disconnect Stripe

Disconnecting removes the key and everything Numbat imported from Stripe,
including Stripe revenue in Ad Campaigns and Visitors. Nothing changes in
Stripe. If you connect again, the import starts over.

1. [Open the Stripe integration in Numbat](https://app.numbat.io/go/integrations/stripe)
2. Choose **Disconnect**, then **Disconnect Stripe** to confirm.
3. To revoke the key too, open **Developers > API keys** in Stripe and
   delete the Numbat key.

## Troubleshooting

### "This is a secret key"

You pasted a key that starts with `sk_`. A secret key can change your Stripe
account, so Numbat refuses it before it leaves the page. Make a restricted
key as in the steps above.

### "This key cannot read …"

This key is missing one of the six permissions. Edit the key in Stripe, set
the ones named to **Read**, and paste it again.

### "This key reads the other Stripe mode"

The site is connected with a test key and you pasted a live one, or the
reverse. Test and live purchases are never mixed. Disconnect first, then
connect the new key.

### The card says "New key needed"

Stripe no longer accepts the key, usually because it was deleted or expired.
Syncing has paused, and everything already imported stays, including the
revenue in Ad Campaigns. Paste a new restricted key on the Stripe page to
resume.
