This guide contains code snippets to help you manage subscriptions in your application and explanations on their usage.

The guide includes:

  • A SubscriptionManager class to help you perform subscription operations in your application, such as creating and updating subscriptions.
  • A set of examples showing how to implement retry logic for declined subscription payments.

Subscription manager class

The SubscriptionManager class is a utility class that helps you manage subscriptions in your code. It has methods to create, read, update, delete, and query subscriptions from the API. You can use the class in TypeScript or JavaScript projects.

The SubscriptionManager class uses your private Payabli API token to authenticate API requests. Make sure you keep your API token secure and do not expose it in your client-side code.

Constructor

The class is constructed with the following configuration:

entryPoint
string

The entrypoint value for your Payabli paypoint.

apiToken
string

Your Payabli API token.

environment
'sandbox' | 'production'
default:"sandbox"

The environment to use.

Methods

The class has the following methods:

create
(subscription: SubscriptionRequest) => Promise<any>

Creates a new subscription.

get
(subId: number) => Promise<any>

Fetches a subscription based on ID.

update
(subId: number, subscription: Partial<SubscriptionRequest>) => Promise<any>

Updates an existing subscription based on ID.

delete
(subId: number) => Promise<any>

Deletes a subscription based on ID.

list
() => Promise<any>

Fetches a list of all subscriptions.

The SubscriptionRequest type matches the structure of the request body for creating a subscription but doesn’t need an entryPoint to be manually defined. See Create a Subscription, Scheduled Payment, or Autopay for more information.

Examples

The class implementation contains the SubscriptionManager class and the types used in the class. The usage example initializes the class and shows how to create, update, get, and delete a subscription.

Subscription retry logic

Sometimes a subscription payment may fail for various reasons, such as insufficient funds, an expired card, or other issues. When a subscription payment declines, you may want to retry the payment or take other actions to ensure the subscription remains active, such as contacting the customer. Payabli doesn’t retry failed subscription payments automatically, but you can follow this guide to implement your own retry logic for declined subscription payments.

Retry flow

Before you can receive webhook notifications for declined payments, you need to create a notification for the DeclinedPayment event. After creating the notification, you can listen for the event in your server and implement the retry logic. Build retry logic based on this flow:

1

Receive Webhook

Set up an endpoint in your server to receive webhooks.

2

Listen for DeclinedPayment

For every webhook received, check if the Event field has a value of DeclinedPayment.

3

Fetch Transaction

If the Event field has a value of DeclinedPayment, query the transaction details using the transId field from the webhook payload.

4

Check for Subscription

From the transaction details, fetch the subscription ID which is stored in the ScheduleReference field. If this value is 0 or not found, this declined payment isn’t associated with a subscription.

5

Fetch Subscription

Use the subscription ID to fetch the subscription details.

6

Operate on Subscription

Use the subscription ID to perform business logic. Some examples include: updating the subscription with a new payment method, retrying the payment, or notifying the customer.

This section covers two examples for implementing retry logic for declined subscription payments:

  • Express.js: A single-file program using Express.js.
  • Next.js: A Next.js API route.

Both examples respond to the DeclinedPayment event for declined subscription payments and update the subscription to use a different payment method.

Examples

The following examples show how to implement retry logic for declined subscription payments.