# Connect Stripe

This guide connects your Stripe account to Seatify. At the end, a paid checkout creates a license under your policy, and the buyer gets the license key by email.

## Before you start

- A [policy](../../../concepts/policies/) for the licenses that you sell. Turn on [Email the license key](../../../concepts/policies/#emails) to send the key to the buyer.
- A Stripe account with a payment link for a one-time purchase. See the [Stripe documentation](https://docs.stripe.com/payment-links).
- Your application claims and validates licenses. See [Add your application](../../licensing/add-your-application/).

Start in a Stripe sandbox or in test mode. Test mode and live mode have different signing secrets, so each mode needs its own [connection](../../../concepts/payment-providers/). Repeat the steps for live mode.

## Steps

{{% steps %}}

### Create the connection

Connect Stripe on the Payment providers page of the dashboard. Give the connection a name, such as `Stripe test mode`. Leave the signing secret empty. Stripe shows the secret after the webhook endpoint exists.

Choose the rule that finds the customer of a purchase. Use Client reference ID when your backend creates the checkout session and sets `client_reference_id` to your own user ID. Use Email address when you sell through payment links. See [Customer matching](../../../concepts/payment-providers/#customer-matching).

### Add the webhook endpoint in Stripe

Copy the webhook URL from the connection in the dashboard. It looks like this:

```
https://hooks.seatify.dev/v1/stripe/<connection id>
```

Add a webhook endpoint in Stripe with this URL. Choose the Snapshot payload style. Seatify refuses a thin event, because a thin event does not hold the checkout session or the invoice, and Stripe shows that delivery as failed. Select these events:

| Event | Use |
| --- | --- |
| `checkout.session.completed` | A paid one-time purchase creates the license. |
| `checkout.session.async_payment_succeeded` | A payment that completes later, such as a bank debit, creates the license. |
| `invoice.paid` | A subscription creates and renews its license. |
| `charge.refunded` | A full refund suspends the license. |
| `charge.dispute.created` | A dispute suspends the license. |

### Save the signing secret

Copy the signing secret of the endpoint in Stripe. It starts with `whsec_`. Edit the connection in the dashboard and save the signing secret.

Until the secret is saved, the connection refuses every webhook and creates no license. Stripe shows these deliveries as failed.

### Map the payment link

Create a mapping on the connection. Choose Payment link, enter the ID of the payment link, and select the policy. The ID starts with `plink_`.

A purchase through this payment link now creates one license under the policy.

### Test a purchase

Open the payment link and pay with a [Stripe test card](https://docs.stripe.com/testing).

- The Events tab of the connection shows the event with the status Processed.
- The license is on the Licenses page, and a customer owns it.
- The buyer gets the license key by email.

If the event is Ignored or Failed, its reason says why. See [Events](../../../concepts/payment-providers/#events). Fix the cause, and then resend the event in Stripe.

{{% /steps %}}

## Metadata instead of a mapping

If your backend creates the checkout session, it can name the policy in the session. Set the metadata value `seatify_policy` to the policy ID. The dashboard shows the policy ID on the policy page. This purchase needs no mapping.

```bash
curl https://api.stripe.com/v1/checkout/sessions \
  -u "$STRIPE_SECRET_KEY:" \
  -d mode=payment \
  -d "line_items[0][price]=$STRIPE_PRICE_ID" \
  -d "line_items[0][quantity]=1" \
  -d "metadata[seatify_policy]=$POLICY_ID" \
  -d client_reference_id=user-42 \
  --data-urlencode "success_url=https://example.com/thanks"
```

- The metadata value is read before the payment link mapping.
- A value that names no policy of your account creates no license. The payment link mapping is not used instead.
- One checkout session creates one license.
- A subscription does not read this value. Map its price or its product.

## Subscriptions

A subscription is mapped by its price or by its product. Create a mapping on the connection, choose Price or Product, and enter the ID. A price ID starts with `price_`, and a product ID starts with `prod_`. A price mapping is read before a product mapping.

Choose the quantity rule:

| Rule | Use |
| --- | --- |
| One license | One buyer, one license. |
| One license for each unit | The buyer gets a separate key for each unit. |
| One license, quantity as seats | A team shares one key, and the quantity is the number of [seats](../../../concepts/seats-and-devices/#seats). The policy must bind customers. |
| One license, quantity as devices | The buyer gets one key and pays for each device. The quantity is the number of [devices](../../../concepts/seats-and-devices/#devices). The policy must bind devices. |

The first paid invoice creates the license. The license expires at the end of the paid period, and each paid renewal moves the expiry date forward. See [Subscriptions](../../../concepts/payment-providers/#subscriptions).

Set a grace period on the policy. Stripe collects a renewal shortly after the period ends, so a license without a grace period expires for a short time at each renewal. A grace period of a few days also gives time for a payment that Stripe tries again. See [Subscription](../../licensing/licensing-models/#subscription).

When a subscription is canceled, no invoice is paid for the next period, and the license expires.

## Refunds and disputes

A full refund and a new dispute each suspend the license of a one-time purchase. You can turn each one off on the connection. See [Refunds and disputes](../../../concepts/payment-providers/#refunds-and-disputes).

A refund of a subscription payment does not change the license. Cancel the subscription in Stripe too.

## Next steps

- [Payment providers](../../../concepts/payment-providers/)
- [Licensing models](../../licensing/licensing-models/)
- [Add your application](../../licensing/add-your-application/)
- [Licenses](../../../concepts/licenses/)

