# Customize subscription prorations

Use scripts to customize prorations for subscription changes.

The `billing.prorations` extension point lets you customize how Stripe prorates subscription changes. Write your business logic in TypeScript and run it as a script on Stripe’s managed runtime.

## Use a Stripe-authored script 

Use a [Stripe-authored proration script](https://docs.stripe.com/billing/scripts/stripe-authored/proration.md) to round proration intervals or charge and credit full product prices.

## Author your own script

To create and upload the Stripe app with your script, follow the [prorations extension walkthrough](https://docs.stripe.com/extensions/scripts/build-prorations-extension.md). Use `billing.prorations` as the extension point ID.

Configuration-field TSDoc comments such as `@displayName` set field labels in the Dashboard configuration form, following the [configuration comment convention](https://docs.stripe.com/extensions/scripts/define-config-and-schemas.md#labels-and-defaults).

Before creating your script, learn how [subscription prorations](https://docs.stripe.com/billing/subscriptions/prorations.md) work and review the [global limitations for user-authored scripts](https://docs.stripe.com/billing/scripts/author-your-own.md#before-you-begin).

## Customize proration calculations 

> The `prorateItems` method is available only in private preview and is planned for deprecation.

Implement `prorateItems` to return custom proration factors and displayed line item periods for invoice items.

> You’re responsible for ensuring your configuration is correct. Stripe doesn’t take responsibility for any issues that arise as a result of your configuration.
> 
> If your script raises an error (for example, returns an invalid result, throws an exception, or times out), Stripe defaults to standard functionality for the operation that caused the failure.

[Set up real-time failure notifications](https://docs.stripe.com/extensions/scripts/monitor-and-debug.md#receive-failures-in-real-time) to understand when and why Billing scripts fail.

### Before you begin 

Proration calculation scripts have the following limitations:

- Configured at the account level. You can’t apply different scripts to individual customers or subscriptions.
- One-off invoice items without full service periods (start and end dates) aren’t included in proration requests. This is because these items have prices not associated with a recurring time interval (day, month, year, and so on).
- Only items related to proration events (upgrade, downgrade, and cancel subscription) will be available to modify their proration factor and line item period.
- You can’t control *when* prorations are created. To control the creation of prorations, use the [proration_behavior](https://docs.stripe.com/api/subscriptions/update.md#update_subscription-proration_behavior) parameter.

For details about updating subscriptions, see [Changing subscriptions](https://docs.stripe.com/billing/subscriptions/change.md) and the [Subscription API](https://docs.stripe.com/api/subscriptions.md).

### Proration factor 

The proration factor is a multiplier to the item’s full cost to compute the final prorated cost. The standard formula for proration factor is the item’s service period duration (time of use) divided by the billing period duration.

For a non-prorated item, the factor is 1 because the item’s service period duration is equivalent to the billing period duration.

For a prorated item triggered in the middle of the billing period, the factor is 0.5 because the service period is half of the billing period.

The proration factor is a positive number for DEBIT items (charges) and negative for CREDIT items (credits).

The denominator, or basis period, depends on whether the proration is for a debit or credit:

- **Debit prorations**: A debit proration is a charge for the remaining time on a subscription item before its next cycle. The proration factor your script returns is the fraction of the original billing period (represented by the `priceIntervalDuration` field) that the remaining time represents. The service period is from the time the update takes effect to the next time the item cycles, and its duration is equal to the original proration factor multiplied by the `priceIntervalDuration`.
- **Credit prorations**: A credit proration credits a portion of the remaining time charged after an item is updated (for example, to add more seats). The credit item will have a corresponding debit reflecting the original service period for the debit. The proration factor of a credit proration is negative and represents the fraction of the debit to credit back. For example, if a debit is for a 30 day period, and you intend to credit the remaining 6 days, the proration factor would be `-0.2`.

### Input and output types

#### Extension method

Implement the `prorateItems` method to customize proration calculations:

```typescript
export default class MyProrations implements Billing.Prorations<MyProrationsConfig> {
  prorateItems(
    request: ProrateItemsInput,
    configuration: MyProrationsConfig,
    context: Context,
  ): ProrateItemsResult {
    // ...
  }
}
```

##### Parameters

| Field | Type | Description |
| --- | --- | --- |
| `request` | `ProrateItemsInput` | The items requiring proration calculation. |
| `configuration` | `MyProrationsConfig` | Your custom configuration object. You define the structure based on your needs. |
| `context` | `Context` | Runtime context including the extension identifier, livemode status, and optional Stripe-specific context values. |

##### Returns

| Field | Description |
| --- | --- |
| `ProrateItemsResult` | The calculated proration factors for each item. |

#### Input 

```typescript
export interface ProrateItemsInput {
  items: Array<ProratableItem>;
}

export type ProratableItem = {
  key: string;
  type: ItemType;
  isProration: boolean;
  servicePeriod: TimeRange;
  currentProrationFactor: Decimal;
  priceIntervalDuration: number;
  correspondingDebit?: PreviousDebit;
} & (
  | { priceKind: 'price'; price: Price }
  | { priceKind: 'licenseFee'; licenseFee: LicenseFee }
  | { priceKind: 'rateCardRate'; rateCardRate: RateCardRate }
  | { priceKind: 'customPricingUnitOverageRate'; customPricingUnitOverageRate: CustomPricingUnitOverageRate }
  | { priceKind: 'other'; otherPriceKind: string }
);

export type ItemType = 'credit' | 'debit';
```

##### Field descriptions 

| Field | Type | Description |
| --- | --- | --- |
| `items` | `Array<ProratableItem>` | The list of items that can have their proration factor and line item period modified by your script. |
| `key` | `string` | The key of the item for managing modification of many items during runtime. |
| `type` | `ItemType` | Either `credit` or `debit` based on whether the item is a credit being given back to the customer or a debit being owed by the customer. |
| `priceKind` | `'price' \| 'licenseFee' \| 'rateCardRate' \| 'customPricingUnitOverageRate' \| 'other'` | The discriminant that tells you which pricing payload is available on the item. |
| `isProration` | `boolean` | Whether the item is generated from a proration event. |
| `servicePeriod` | `TimeRange` | Period used to calculate the current proration factor. |
| `currentProrationFactor` | `number` | The default proration factor calculated by Stripe. |
| `priceIntervalDuration` | `number` | Amount of time in seconds for the whole period of the item. For example, a monthly subscription has the number of seconds in a month. |
| `correspondingDebit` | `PreviousDebit` | Information about the corresponding debit that a CREDIT item credits against. For example, if a credit item is crediting for an already prorated debit, this field has information about that debit. |

##### Example input

```json
{
  "items": [
    {
      "key": "ubi_123456",
      "type": "debit",
      "priceKind": "price",
      "price": {
        "id": "price_123456",
        "metadata": {},
        "recurring": {
          "interval": "month",
          "intervalCount": 1,
          "usageType": "licensed",
          "meter": null
        },
        "type": "recurring",
        "unitAmount": "0.2e4",
        "product": {
          "name": "Basic plan",
          "id": "prod_123456",
          "metadata": {
            "abc": "123"
          }
        },
        "tiersMode": null,
        "tiers": null,
        "currency": "usd",
        "billingScheme": "per_unit"
      },
      "isProration": true,
      "servicePeriod": {
        "startDate": "2024-01-15T12:00:00Z",
        "endDate": "2024-02-01T00:00:00Z"
      },
      "currentProrationFactor": 0.5,
      "priceIntervalDuration": 2678400
    },
    {
      "key": "ubi_654321",
      "type": "credit",
      "priceKind": "price",
      "price": {
        "id": "price_123456",
        "metadata": {},
        "recurring": {
          "interval": "month",
          "intervalCount": 1,
          "usageType": "licensed",
          "meter": null
        },
        "type": "recurring",
        "unitAmount": "0.2e4",
        "product": {
          "name": "Basic plan",
          "id": "prod_123456",
          "metadata": {
            "abc": "123"
          }
        },
        "tiersMode": null,
        "tiers": null,
        "currency": "usd",
        "billingScheme": "per_unit"
      },
      "isProration": true,
      "servicePeriod": {
        "startDate": "2024-01-15T12:00:00Z",
        "endDate": "2024-02-01T00:00:00Z"
      },
      "currentProrationFactor": -0.5,
      "priceIntervalDuration": 2678400,
      "correspondingDebit": {
        "servicePeriod": {
          "startDate": "2024-01-01T00:00:00Z",
          "endDate": "2024-02-01T00:00:00Z"
        }
      }
    }
  ]
}
```

#### Output 

```typescript
export interface ProrateItemsResult {
  items: Array<ItemWithProration>;
}

export interface ItemWithProration {
  key: string;
  prorationFactor: Decimal;
  lineItemPeriod: TimeRange;
}
```

##### Field descriptions 

| Field | Type | Description |
| --- | --- | --- |
| `key` | `string` | The unique identifier of the item. This must be a key of an item in the input. |
| `prorationFactor` | `Decimal` | The overridden proration factor that you set on each item. Positive values represent charges (DEBIT items), negative values represent credits (CREDIT items). Can be set to the original `currentProrationFactor` value from the input if no change is needed. |
| `lineItemPeriod` | `TimeRange` | The displayed period for the invoice line item. Can be set to the original `servicePeriod` value from the input if no change is needed. |

##### Example output

```json
{
  "items": [
    {
      "key": "ubi_654321",
      "prorationFactor": 0.51612903,
      "lineItemPeriod": {
        "startDate": "2024-01-15T00:00:00Z",
        "endDate": "2024-02-01T00:00:00Z"
      }
    },
    {
      "key": "ubi_123456",
      "prorationFactor": -0.51612903,
      "lineItemPeriod": {
        "startDate": "2024-01-15T00:00:00Z",
        "endDate": "2024-02-01T00:00:00Z"
      }
    }
  ]
}
```

### Example implementation

Here’s a complete example that implements a custom proration calculation rounding up to the nearest whole day:

```typescript
import type { Billing, Context } from '@stripe/extensibility-sdk/extensions';
import { Decimal } from '@stripe/extensibility-sdk';

interface MyProrationsConfig extends Record<string, unknown> {
  roundToNearestDay: boolean;
}

export default class MyProrations implements Billing.Prorations<MyProrationsConfig> {
  prorateItems(
    request: Billing.Prorations.ProrateItemsInput,
    configuration: MyProrationsConfig,
    _context: Context
  ): Billing.Prorations.ProrateItemsResult {
    const items: Array<Billing.Prorations.ItemWithProration> = request.items.map((item) => {
      const prorationFactor = calculateProrationFactor(
        item,
        configuration.roundToNearestDay,
      );

      return {
        key: item.key,
        prorationFactor,
        lineItemPeriod: item.servicePeriod,
      };
    });

    return {items};
  }
}

function calculateProrationFactor(
  item: Billing.Prorations.ProratableItem,
  roundToNearestDay: boolean,
) {
  if (!roundToNearestDay) {
    return item.currentProrationFactor;
  }

  const periodStart = new Date(item.servicePeriod.startDate).getTime() / 1000;
  const periodEnd = new Date(item.servicePeriod.endDate).getTime() / 1000;
  const periodDuration = periodEnd - periodStart;

  if (item.type === 'debit') {
    const daysUsed = Math.ceil(periodDuration / 86400);
    const totalDays = Math.ceil(item.priceIntervalDuration / 86400);

    return Decimal.from(daysUsed).div(Decimal.from(totalDays), 12, 'half-even');
  }

  const debitPeriodStart = new Date(item.correspondingDebit!.servicePeriod.startDate).getTime() / 1000;
  const debitPeriodEnd = new Date(item.correspondingDebit!.servicePeriod.endDate).getTime() / 1000;
  const debitPeriodDuration = debitPeriodEnd - debitPeriodStart;

  const daysUsed = Math.ceil(periodDuration / 86400);
  const totalDays = Math.ceil(debitPeriodDuration / 86400);

  return Decimal.from(daysUsed)
    .div(Decimal.from(totalDays), 12, 'half-even')
    .neg();
}
```

#### Key implementation points

This example illustrates the following points. You should keep these in mind when you author your own script:

- **Map each item**: Process each `ProratableItem` and return an `ItemWithProration`.
- **Calculate factor**: The `prorationFactor` represents the portion of the billing period to charge or credit. Positive values represent charges (DEBIT items), negative values represent credits (CREDIT items). The magnitude indicates the portion of the price to apply for DEBIT items (for example, `0.5` means 50% charge). For CREDIT items it represents the portion of the previous debit to credit back (`-0.75` means 75% credit of the previous debit charge).
- **Preserve keys**: Always return the same `key` to match items in the request.
- **Respect item types**: Make sure DEBIT items have positive factors and CREDIT items have negative factors to maintain correct billing behavior. Otherwise we’ll ignore the returned proration factor for the item.
- **Time handling**: Use the `servicePeriod` dates to calculate usage duration.
- **Configuration**: Define your own configuration type for customizable behavior.

### Data validation requirements

Your script must meet these requirements to execute successfully. Violations result in a validation error.

- Return an `ItemWithProration` for every key in the input. Every proration item must have a corresponding entry in the output.
- Each `ItemWithProration` must include both `prorationFactor` (number) and `lineItemPeriod` (TimeRange with `startDate` and `endDate`).
- The maximum value accepted for `prorationFactor` is 1,000. In the input, `currentProrationFactor` is guaranteed to always be less than 1,000.
- DEBIT items must have a positive `prorationFactor`. CREDIT items must have a negative `prorationFactor`.
- The end date on the `lineItemPeriod` must equal the end date on the service period.
- The proration factor must equal the line item period divided by the `priceIntervalDuration` for debits or the duration of the corresponding debit’s `servicePeriod` for credits.

### Test your script

Before deploying to production:

1. Test proration scenarios in a [sandbox](https://docs.stripe.com/sandboxes.md).
2. Verify that computed proration amounts are what you expect.

### Best practices

- **Return valid factors**: Ensure proration factors have reasonable absolute values. Extremely high absolute values will result in over-crediting or over-debiting your customer. Remember that negative values represent credits (CREDIT items) and positive values represent charges (DEBIT items).
- **Respect item types**: Maintain the appropriate sign for each item type. DEBIT items are assigned positive proration factors, and CREDIT items are assigned negative proration factors.
- **Return valid line item periods**: These are the periods that will be shown alongside the items on the invoice to your customer.
- **Handle all items**: Process every item in the request and return a corresponding response.
- **Use default values when needed**: Return the input item’s `currentProrationFactor` and `servicePeriod` when your logic doesn’t need to override that item.
- **Consider time zones**: All dates are in UTC.
- **Test thoroughly**: Use a sandbox to validate your logic before production.
- **Document your logic**: Add comments explaining your proration calculation method.
- Additionally:
  - For CREDIT items, use `correspondingDebit.servicePeriod` to calculate the correct credit proportion.
  - Return `lineItemPeriod` values that reflect the period displayed to the customer on the invoice.
  - Test with both upgrades (DEBIT) and downgrades (CREDIT).

## Next steps

- Learn more about [scripts](https://docs.stripe.com/billing/scripts.md)
- See the [Stripe Billing Extensions repository on GitHub](https://github.com/stripe/billing-extensions) for additional examples
