Subscription utility code
This guide contains code snippets to help you manage subscriptions in your apps using Payabli’s server SDKs. To learn more about Payabli’s server SDKs, see the Server SDKs overview page.
The guide includes:
- A list of SDK methods for subscription management with links to the reference documentation for each method.
- A set of examples showing how to implement retry logic for declined subscription payments.
SDK subscription methods
This section covers API endpoints for subscription management and their corresponding methods in Payabli’s server SDKs. See the SDK reference documentation in each endpoint for more details on the request and response formats.
Create subscription
The POST /Subscription/add endpoint creates a new subscription for a paypoint.
See Create subscription for the full API reference.
The following table lists the methods in each SDK that correspond to this endpoint, along with links to the reference documentation for each method:
Request
This snippet shows how to create a subscription using the POST /Subscription/add endpoint.
Select a programming language in the dropdown to see the code example for that language.
Response
This snippet shows the response from the POST /Subscription/add endpoint.
Get subscription
The GET /Subscription/{subId} endpoint retrieves details for a subscription.
See Get subscription for the full API reference.
The following table lists the methods in each SDK that correspond to this endpoint, along with links to the reference documentation for each method:
Request
This snippet shows how to get a subscription using the GET /Subscription/{subId} endpoint.
Select a programming language in the dropdown to see the code example for that language.
Response
This snippet shows the response from the GET /Subscription/{subId} endpoint.
Update subscription
The PUT /Subscription/{subId} endpoint updates an existing subscription.
See Update subscription for the full API reference.
The following table lists the methods in each SDK that correspond to this endpoint, along with links to the reference documentation for each method:
Request
This snippet shows how to update a subscription using the PUT /Subscription/{subId} endpoint.
Select a programming language in the dropdown to see the code example for that language.
Response
This snippet shows the response from the PUT /Subscription/{subId} endpoint.
Delete subscription
The DELETE /Subscription/{subId} endpoint deletes a subscription and prevents future charges.
See Delete subscription for the full API reference.
The following table lists the methods in each SDK that correspond to this endpoint, along with links to the reference documentation for each method:
Request
This snippet shows how to delete a subscription using the DELETE /Subscription/{subId} endpoint.
Select a programming language in the dropdown to see the code example for that language.
Response
This snippet shows the response from the DELETE /Subscription/{subId} endpoint.
List subscriptions by paypoint
The GET /Query/subscriptions/{entry} endpoint returns all subscriptions for a paypoint.
See List subscriptions by paypoint for the full API reference.
The following table lists the methods in each SDK that correspond to this endpoint, along with links to the reference documentation for each method:
Request
This snippet shows how to list subscriptions by paypoint using the GET /Query/subscriptions/{entry} endpoint.
Select a programming language in the dropdown to see the code example for that language.
Response
This snippet shows the response from the GET /Query/subscriptions/{entry} endpoint.
List subscriptions by organization
The GET /Query/subscriptions/org/{orgId} endpoint returns all subscriptions across for an organization.
See List subscriptions by organization for the full API reference.
The following table lists the methods in each SDK that correspond to this endpoint, along with links to the reference documentation for each method:
Request
This snippet shows how to list subscriptions by organization using the GET /Query/subscriptions/org/{orgId} endpoint.
Select a programming language in the dropdown to see the code example for that language.
Response
This snippet shows the response from the GET /Query/subscriptions/org/{orgId} endpoint.
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:
Diagram: Subscription Retry Flow Process
This sequence diagram shows how to handle declined subscription payments:
- Server receives webhook payload
- Webhook handler checks if Event is
DeclinedPayment- If not
DeclinedPayment: Stop processing - If
DeclinedPayment: Continue to next step
- If not
- Webhook handler queries transaction using
transIdfrom webhook - Transaction API returns transaction details
- Webhook handler checks
ScheduleReferencefield in transaction- If
ScheduleReferenceis 0 or doesn’t exist: Not a subscription payment, stop processing - If
ScheduleReferenceexists: Continue with subscription ID
- If
- Webhook handler requests subscription details from Subscription API
- Subscription API returns subscription details
- Webhook handler updates subscription or retries payment
- Subscription API confirms operation completed
This flow enables custom retry logic for declined subscription payments. Payabli doesn’t automatically retry failed subscription payments.
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.
Related resources
See these related resources to help you get the most out of Payabli.
Prerequisites
- Manage subscriptions with the API - Learn how to create, update, and delete your scheduled, subscription, and autopay transactions with the Payabli API