Subscriptions
Subscriptions charge a customer on a schedule. You define a plan, put a customer on it, and VectaPay collects each period and reports the outcome by webhook. For the dashboard walkthrough, see Recurring Payments.
The pieces
| Piece | What it is |
|---|---|
| Plan | A reusable price: amount, billing frequency, trial, retry policy. |
| Customer | The person being billed. Must exist first (dashboard → Customers). |
| Customer subscription | One customer on one price, with a status, next billing date, and charge mode. |
| Charge attempt | One attempt to collect one billing period. |
| Subscription event | Append-only history, readable with vectapay.subscriptions.activity(). |
| Webhook | Notification for the events you need to act on. |
A subscription copies the plan’s price, currency, and frequency at creation. Editing the plan later does not reprice existing subscriptions; use change plan.
How a period gets paid
chargeMode decides what the customer sees each cycle.
chargeMode | Each period | Customer does | Set by |
|---|---|---|---|
customer_approval | M-Pesa prompt if the customer has a phone. Email only: the attempt waits for you to start a card checkout. | Approves every period | Default |
automatic | Saved card charged silently | Nothing, after the first card payment | A reusable card is captured |
standing_order | Safaricom debits on the mandate’s schedule | Approves the mandate once | enableStandingOrder() |
Start with customer_approval; it works without a saved card or mandate.
Without a saved card or a standing order, the customer approves every period. An ignored prompt fails the period and starts recovery; it is not retried forever.
A first subscription
This assumes the customer exists and the plan has no trial.
TypeScript
import { VectaPayClient } from "@vectapay/sdk";
const vectapay = new VectaPayClient({ apiKey: process.env.VECTAPAY_API_KEY! });
// Prices are in shillings: 2500 is KES 2,500.
const plan = await vectapay.subscriptions.createPlan({
code: "starter-monthly",
name: "Starter",
billingInterval: "monthly",
amount: 2500,
status: "active",
});
const result = await vectapay.subscriptions.create({
customerId: "9b2f0f6e-3f4a-4c1e-9d6e-6f1b2c3d4e5f",
planId: plan.id,
externalReference: "member_8842",
idempotencyKey: "signup-member_8842",
});
// Phone customers get the first prompt automatically.
// Email-only customers need a checkout from your site:
const charge = await vectapay.subscriptions.initiateCharge(result.subscription.id, {
method: "card",
email: "amina@example.com",
returnUrl: "https://your-app.example.com/billing/return",
});
if (charge.checkoutRedirectUrl) redirect(charge.checkoutRedirectUrl);create returns as soon as the subscription is recorded. A phone customer’s prompt may already be on
their phone, but that does not mean they paid. Settlement is the webhook.
Settle from webhooks
| Event | Meaning |
|---|---|
subscription.created | Subscription exists; no money moved. |
subscription.charge_succeeded | A period was paid (see Settling a charge). |
subscription.charge_failed | An attempt failed. Read failureCode and retryAt. |
subscription.dunning_started / retry_scheduled / dunning_exhausted | Recovery progress. |
subscription.activated | A trial converted or a past-due subscription recovered. |
subscription.paused / subscription.resumed | Billing stopped or restarted. |
subscription.plan_changed | A plan change was requested (applied: false) or took effect (applied: true). |
subscription.billing_date_changed, end_scheduled, end_cleared | The schedule was edited. |
subscription.standing_order_rejected | A Ratiba mandate was declined or timed out; billing reverted to customer_approval. |
subscription.canceled | Cancellation requested or took effect. Check data.status. |
subscription.expired | Recovery window closed unpaid. |
Never grant access from payment.succeeded. A payment can succeed while the subscription attempt
fails (underpayment, wrong currency). The subscription.* event carries the period-level outcome.
Payload shapes are on Events.
Money
Plan prices are in shillings (amount: 2500 is KES 2,500.00). Everything the platform reports is a
money object:
{ "amount": "250000", "currency": "KES", "formatted": "KSh 2,500.00" }Display formatted. Use amount (integer minor units, as a string) only for reconciliation, and never
divide it yourself.
Before you start
- A provider account for each rail you use: M-Pesa for prompts and standing orders, Paystack for cards (dashboard → Providers).
- A customer record with the Customer ID copied from the dashboard.
- A webhook endpoint subscribed to the
subscription.*events you handle. Without one you are pollingactivity().
There is no test mode for subscriptions
Every subscription is live and moves real money, even with a test key. Two ways to test:
- Handler: Developer → Webhooks → Send test event sends a signed event with
environment: "test". See Testing Your Endpoint. - End to end: use the smallest amount the rail accepts, subscribe your own phone or card, then cancel. Never use a customer’s number or a “test” plan with a real customer.
Where to go next
| Page | Read it when |
|---|---|
| Plans | Pricing, frequency, trials, retries |
| Customer subscriptions | Creating, listing, looking up |
| Lifecycle | Pause, cancel, change plan or date |
| Charging | Reconciling, dunning, payment pages |
| Standing orders | M-Pesa automatic debits |