Subscription utility code
Use example code to enhance your subscription management via the API
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:
The entrypoint value for your Payabli paypoint.
Your Payabli API token.
The environment to use.
Methods
The class has the following methods:
Creates a new subscription.
Fetches a subscription based on ID.
Updates an existing subscription based on ID.
Deletes a subscription based on ID.
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.
Class Implementation
The SubscriptionManager
class is framework-agnostic and doesn’t have any dependencies. You can use it universally.
Usage Example
This example uses the SubscriptionManager
class to make API calls for creating, updating, getting, and deleting subscriptions.
See the comments in the code to understand how each method is used.
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:
Listen for DeclinedPayment
For every webhook received, check if the Event
field has a value of DeclinedPayment
.
Fetch Transaction
If the Event
field has a value of DeclinedPayment
, query the transaction details using the transId
field from the webhook payload.
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.
Create DeclinedPayment Notification
Before implementing the retry logic, you need to create a webhook notification for the DeclinedPayment
event.
After the notification is created, you can listen for the event in a server and implement the retry logic.
For more information, see Manage Notifications.
Express.js Single-File Example
The Express.js example can be used as a standalone server in a server-side JavaScript or TypeScript runtime such as Node, Bun, or Deno.
Next.js API Route Example
The Next.js example can’t be used as a standalone server but can be dropped into a Next.js project. See the Next.js API Routes documentation for more information.