Sample integration
This guide shows you how to build a subscription flow for a SaaS service with multiple flat‑rate plans.
At a high-level, the steps include:
- Model products and define multiple flat‑rate plans.
- Create a checkout for subscription plans.
- Handle billing events with webhooks.
- Test and troubleshoot the subscription lifecycle.
Upon completion of these steps, customers will be able to select a plan, complete checkout, and manage their recurring payments. You will set up backend and frontend logic to create subscriptions, process billing events, and support customer self‑service.
Example business scenario
Imagine a project management SaaS web application that provides two subscription plans:
- Basic: $10/month, includes core features.
- Pro: $20/month, includes advanced features.
Both plans have a fixed monthly fee, regardless of usage, and each comes with a 14‑day free trial so customers can explore the service before subscribing. This setup is known as a flat‑price subscription model.
Other billing models you may encounter include:
- Tiered pricing: Different rates apply when usage exceeds specified thresholds.
- Usage‑based billing: Charges based on units consumed, such as API calls or storage.
For this guide, we’ll use flat pricing with multiple plans because it is the most straightforward and commonly used structure for SaaS integrations.
Before you begin
Before you begin, ensure your SaaS application includes both a frontend (web client) and a backend (server), as these components must work together during integration. You will also need:
- An Airwallex account (sandbox environment is recommended).
- Your API key and client secret, available from the Airwallex web app.
Step 1: Create products and prices
A Product represents what you sell. A Price specifies the billing interval, currency, and amount. For each subscription plan, follow these steps:
- Create a Product object for each plan (for example, Basic, Pro).
- Create a corresponding Price object for each product, defining the monthly charge (for example, $10/month for Basic, $20/month for Pro).
You can set up Products and Prices in two ways:
Airwallex web app (ideal if you prefer a UI-driven workflow)
- Log in to your Airwallex web application.
- Go to Products & Prices.
- Create a new Product (for example, Basic Plan).
- Add a Price to the Product, specifying the currency, amount, and billing interval.
Airwallex Billing APIs
To integrate via API, start by creating a Product API that represents your subscription plan (for example, Basic). The API response will return an id that uniquely identifies the Product object.
The second API call creates a Price, specifying the amount to charge and the billing frequency. To associate the price with its product, you must provide the id returned from the product creation API call.
Step 2: Build a checkout
Enable customers to subscribe using Hosted Billing Checkout.
Step 3: Handle webhook events
Airwallex notifies your system about billing events. Set up a webhook endpoint to manage subscription lifecycle changes as follows:
- Receive event notifications: Airwallex sends JSON payloads to your webhook URL whenever a key event occurs, such as a payment succeeding or failing.
- Parse the event type: Inspect the
event.type
field in the payload to identify the specific event. - Take appropriate action: For example, provision access for new subscriptions, continue service after successful payments, or alert the customer when a payment fails.
- Acknowledge receipt: Return a 200 OK response so Airwallex knows your system successfully processed the event.
See Billing event types for webhooks emitted for subscriptions.
Example
Step 4: Test your integration
Use Airwallex sandbox test card numbers to simulate checkout flows. To test longer subscription cycles, use the Test Clock feature (coming soon) to fast-forward time, making it easy to test renewals, trial expirations, and cancellations without real-time delays.
Verify your setup by checking the following:
- Dashboard visibility: After checkout, new subscriptions should appear in your dashboard on the Airwallex web app.
- Webhook events: Ensure webhook events are triggered and processed successfully by your application.
- Customer Portal: Any customer changes including updating cards, switching plans, or cancellation should be reflected both in the portal and your backend system.
- Test clock usage: Accelerate subscription cycles with the test clock, allowing you to quickly test renewals, trial expirations, and cancellations.