> ## Documentation Index
> Fetch the complete documentation index at: https://docs.corrily.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom integration

> Detailed guidance on how to integrate Corrily with a payment gateway.

## (Recommended) Provide automated access of subscription and charge events

The easiest and recommended way to notify Corrily of subscription and charge events is to grant Corrily automated access to your payment gateway's webhook events. Email [api@corrily.com](mailto:api@corrily.com) to get started.

If you're using Stripe and would like to get your Corrily integration started now, click the **Connect with Stripe** button to grant Corrily read-only access to your Stripe subscription and charge events.

<a target="_blank" href="https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_IvVOYSONFblYt8sgK4ZTee6JwJSwdNGD&scope=read_only">
  <button
    style={{
backgroundColor: "#230e7d",
color: "#fff",
padding: "12px 15px",
fontSize: "18px",
border: "none",
borderRadius: "4px",
}}
  >
    Connect with Stripe
  </button>
</a>

If for whatever reason you cannot or do not want to grant Corrily automated access, you can manually notify Corrily instead.

## Manually notify Corrily of subscription and charge events

<Warning>
  <h3>Only required when automated access is not an option</h3>

  If you have granted Corrily automated access to your payment gateway's webhook events, as explained in the previous section, you can stop here.
</Warning>

<Note>
  <h3>"Charge" events is a generic term in this guide</h3>

  In this guide we use the word "charge" to refer to a non-recurring payment. Different payment gateways use different words for this concept. When reading your payment gateway's documentation, "charge" may not be the word you're looking for.
</Note>

### The basic workflow

* You set up your own webhook server
* Your webhook server receives subscription and charge events from your payment gateway
* Your webhook server extracts only the data that Corrily needs and sends it to Corrily via the subscription and charge REST API endpoints

<Warning>
  <h3>Corrily needs to be notified of subscription AND charge events</h3>

  Corrily needs to know about charge events because charge events are much closer to realized revenue than subscription events. In other words, charges are more like a realized commitment to pay, whereas subscription events are more like an intent to pay. Getting both signals helps Corrily's AI models provide more optimal prices for you.
</Warning>

### Build a webhook server

See the following docs for more guidance on building a webhook server for your particular payment gateway:

#### Stripe

* [Receive event notifications with webhooks](https://stripe.com/docs/webhooks)
* [Using webhooks with subscriptions](https://stripe.com/docs/billing/subscriptions/webhooks)

#### Chargebee

* [Events and Webhooks](https://www.chargebee.com/docs/2.0/events_and_webhooks.html)
* [Webhook Settings](https://www.chargebee.com/docs/2.0/webhook_settings.html)

#### Recurly

* [Webhooks](https://developers.recurly.com/reference/webhooks/)

#### Braintree

* [Webhooks](https://developer.paypal.com/braintree/docs/guides/webhooks/overview)

### Handle subscription events

Payment gateways emit webhook events over the entire subscription lifecycle. Keeping Corrily up-to-date on important events helps Corrily determine optimal prices for your customers. Examples of important events include:

* Signing up for a trial
* Converting to a paying customer
* Switching to a higher or lower price
* Canceling a subscription

The following sections detail which events you need to handle, depending on your payment gateway.

#### Stripe

* [`customer.subscription.created`](https://stripe.com/docs/api/events/types#event_types-customer.subscription.created)
* [`customer.subscription.updated`](https://stripe.com/docs/api/events/types#event_types-customer.subscription.updated)

#### Chargebee

* [`subscription_created `](https://apidocs.chargebee.com/docs/api/events?prod_cat_ver=2#subscription_created)
* [`subscription_activated`](https://apidocs.chargebee.com/docs/api/events?prod_cat_ver=2#subscription_activated)
* [`subscription_cancelled`](https://apidocs.chargebee.com/docs/api/events?prod_cat_ver=2#subscription_cancelled)
* [`subscription_changed`](https://apidocs.chargebee.com/docs/api/events?prod_cat_ver=2#subscription_changed)

#### Recurly

* [`new_subscription_notification`](https://developers.recurly.com/reference/webhooks/#new-subscription)
* [`updated_subscription_notification`](https://developers.recurly.com/reference/webhooks/#canceled-subscription)
* [`canceled_subscription_notification`](https://developers.recurly.com/reference/webhooks/#canceled-subscription)

#### Braintree

* [`Braintree::WebhookNotification::Kind::SubscriptionCanceled`](https://developer.paypal.com/braintree/docs/reference/general/webhooks/subscription)
* [`Braintree::WebhookNotification::Kind::SubscriptionExpired `](https://developer.paypal.com/braintree/docs/reference/general/webhooks/subscription)
* [`Braintree::WebhookNotification::Kind::SubscriptionTrialEnded`](https://developer.paypal.com/braintree/docs/reference/general/webhooks/subscription)
* [`Braintree::WebhookNotification::Kind::SubscriptionWentActive`](https://developer.paypal.com/braintree/docs/reference/general/webhooks/subscription)
* [`Braintree::WebhookNotification::Kind::SubscriptionWentPastDue`](https://developer.paypal.com/braintree/docs/reference/general/webhooks/subscription)

### Send subscription data to Corrily

Use Corrily's [Create subscription](../api-reference/subscriptions/create-subscription) and [Update subscription](../api-refernce/calculate-price/update-subscription) REST API endpoints to send the data from your webhook server to Corrily.

#### Handle subscription discounts

If you are providing a discount to a user, you should send Corrily the price that the user paid. You should not try to represent the discount with multiple calls to the Corrily API.

#### Map your payment gateway's subscription statuses to Corrily's values

The following tables explain how to map your payment gateways status values to Corrily's values.

##### Stripe

| Stripe's value       | Corrily's value |
| -------------------- | --------------- |
| `incomplete`         | `pending`       |
| `incomplete_expired` | `pending`       |
| `trialing`           | `trialing`      |
| `active`             | `active`        |
| `past_due`           | `active`        |
| `canceled`           | `canceled`      |
| `unpaid`             | `canceled`      |

##### Chargebee

| Chargebee's value | Corrily's value |
| ----------------- | --------------- |
| `future`          | `pending`       |
| `in_trial `       | `trialing`      |
| `active`          | `active`        |
| `non_renewing`    | `canceled`      |
| `paused`          | `canceled`      |
| `canceled`        | `canceled`      |

##### Recurly

| Chargebee's value | Corrily's value |
| ----------------- | --------------- |
| `future`          | `pending`       |
| `in_trial `       | `trialing`      |
| `active`          | `active`        |
| `expired`         | `canceled`      |
| `canceled`        | `canceled`      |

### Braintree

| Braintree's value | Corrily's value |
| ----------------- | --------------- |
| `Active`          | `active`        |
| `Canceled`        | `canceled`      |
| `Expired`         | `canceled`      |
| `PastDue`         | `active`        |
| `Pending`         | `pending`       |

### Handle charge events

Keeping Corrily up-to-date on important charge events helps Corrily confirm that the price it generated for a visitor was optimal. For example, a new subscription is a slight signal that a price was optimal. When the customer allows their card to be charged for the subscription, that's further confirmation that the price was optimal.

The following sections detail which charge events you need to handle, depending on your payment gateway.

#### Stripe

* [`invoice.paid`](https://stripe.com/docs/api/events/types#event_types-invoice.paid)

Stripe refers to one-time charge events as invoice events.

<Warning>
  <h3>Why not use Stripe charges instead?</h3>

  Stripe invoice events are closer to payments for individual subscriptions than Stripe charges are. In Stripe, charges pay for invoices which may contain payments for more than just a single subscription. If however it's easier for you to handle charges, you can also look to handle Stripe's `charge.captured`, `charge.refunded`, `charge.updated`, and `charge.succeeded` events.
</Warning>

#### Chargebee

* [`payment_succeeded`](https://apidocs.chargebee.com/docs/api/events?prod_cat_ver=2#payment_succeeded)
* [`payment_refunded `](https://apidocs.chargebee.com/docs/api/events?prod_cat_ver=2#payment_refunded)

On payment events (which pay for subscriptions) Chargebee sends your webhook server transaction, customer, and subscription objects. The transaction object contains the amount that was paid (or refunded, depending on the transaction `type`). The transaction object contains the data that needs to be passed to Corrily.

#### Recurly

* [`successful_payment_notification `](https://developers.recurly.com/reference/webhooks/#successful-payment)
* [`successful_refund_notification `](https://developers.recurly.com/reference/webhooks/#successful-refund)

#### Braintree

* [`Braintree::WebhookNotification::Kind::SubscriptionChargedSuccessfully`](https://developer.paypal.com/braintree/docs/reference/general/webhooks/subscription)
* [`Braintree::WebhookNotification::Kind::SubscriptionChargedUnsuccessfully `](https://developer.paypal.com/braintree/docs/reference/general/webhooks/subscription)

### Send charge data to Corrily

Use Corrily's [Create charge](../api-reference/charges/create-charge) and [Update charge](../api-reference/charges/update-charge) REST API endpoints to send the invoice data from your webhook server to Corrily.

#### Handle charge discounts

If you are providing a discount to a user, you should send Corrily the price that the user paid. You should not try to represent the discount with multiple calls to the Corrily API.

#### Map your payment gateway's charge statuses to Corrily's values

The following tables explain how to map your payment gateways status values to Corrily's values.

##### Stripe

| Stipe's value   | Corrily's value |
| --------------- | --------------- |
| `draft`         | `pending`       |
| `open`          | `pending`       |
| `paid`          | `succeeded`     |
| `uncollectible` | `failed`        |
| `void`          | `failed`        |

##### Chargebee

| Chargebee's value | Corrily's value |
| ----------------- | --------------- |
| `in_progress`     | `pending`       |
| `success`         | `succeeded`     |
| `voided`          | `failed`        |
| `failure`         | `failed`        |
| `timeout`         | `failed`        |
| `needs_attention` | `failed`        |

##### Recurly

| Recurly's value | Corrily's value |
| --------------- | --------------- |
| `pending`       | `pending`       |
| `processing`    | `pending`       |
| `success`       | `succeeded`     |
| `chargeback`    | `succeeded`     |
| `error`         | `failed`        |
| `declined`      | `failed`        |

##### Braintree

| Braintree's value                                                         | Corrily's value |
| ------------------------------------------------------------------------- | --------------- |
| `Braintree::WebhookNotification::Kind::SubscriptionChargedSuccessfully`   | `succeeded`     |
| `Braintree::WebhookNotification::Kind::SubscriptionChargedUnsuccessfully` | `failed`        |
