Build Paystack Subscriptions in Flutter
To subscribe a customer in Flutter, call your backend to initialize a transaction with a Paystack plan code. Open the authorization URL in a WebView. After payment, Paystack creates the subscription automatically. Your backend receives the subscription.create webhook. From Flutter, check subscription status by calling your backend, which queries the Paystack subscription API.
Subscription Flow in Flutter
The subscription flow mirrors the one-time payment flow. The difference is on your backend:
- Flutter calls your backend with the customer email and plan
- Backend initializes a transaction with a Paystack
plancode - Backend returns the
authorization_url - Flutter opens the URL in a WebView
- Customer pays — Paystack creates the subscription and stores their card
- Paystack fires
subscription.createwebhook to your backend - Backend marks the customer as subscribed in your database
- Flutter calls your backend status endpoint and shows the active plan
Backend: Initialize Subscription
// Node.js backend
app.post('/api/subscribe', async (req, res) => {
var { email, planCode } = req.body;
var reference = 'sub_' + Date.now();
var response = await fetch('https://api.paystack.co/transaction/initialize', {
method: 'POST',
headers: {
Authorization: 'Bearer ' + process.env.PAYSTACK_SECRET_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
plan: planCode,
reference,
callback_url: 'https://yourapp.com/subscription/callback',
}),
});
var data = await response.json();
if (!data.status) return res.status(400).json({ error: data.message });
res.json({
authorization_url: data.data.authorization_url,
reference: data.data.reference,
});
});
Flutter: Subscribe Screen
// lib/subscription_screen.dart
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'payment_screen.dart';
class SubscriptionScreen extends StatefulWidget {
final String userEmail;
const SubscriptionScreen({super.key, required this.userEmail});
@override
State<SubscriptionScreen> createState() => _SubscriptionScreenState();
}
class _SubscriptionScreenState extends State<SubscriptionScreen> {
bool _isLoading = false;
Future<void> _subscribe(String planCode, String planName) async {
setState(() => _isLoading = true);
var response = await http.post(
Uri.parse('https://your-backend.com/api/subscribe'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'email': widget.userEmail, 'planCode': planCode}),
);
var data = jsonDecode(response.body);
setState(() => _isLoading = false);
if (data['authorization_url'] != null) {
var result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PaymentScreen(
authUrl: data['authorization_url'],
reference: data['reference'],
),
),
);
if (result?['success'] == true) {
await _checkSubscriptionStatus();
}
}
}
Future<void> _checkSubscriptionStatus() async {
var response = await http.get(
Uri.parse('https://your-backend.com/api/subscription/status?email=' + widget.userEmail),
);
var data = jsonDecode(response.body);
if (data['active'] == true) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Subscribed to ${data['plan_name']}!')),
);
Navigator.of(context).pop(true);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Choose a Plan')),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: ListView(
padding: const EdgeInsets.all(16),
children: [
_PlanCard(
name: 'Pro Monthly',
price: 'NGN 5,000/month',
planCode: 'PLN_xxxxxxxxxxxx',
onSubscribe: _subscribe,
),
const SizedBox(height: 12),
_PlanCard(
name: 'Pro Annual',
price: 'NGN 50,000/year',
planCode: 'PLN_yyyyyyyyyyyy',
onSubscribe: _subscribe,
),
],
),
);
}
}
class _PlanCard extends StatelessWidget {
final String name;
final String price;
final String planCode;
final Function(String, String) onSubscribe;
const _PlanCard({
required this.name,
required this.price,
required this.planCode,
required this.onSubscribe,
});
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(name),
subtitle: Text(price),
trailing: ElevatedButton(
onPressed: () => onSubscribe(planCode, name),
child: const Text('Subscribe'),
),
),
);
}
}
Learn More
This guide is part of the Paystack integration guides by language and framework series.
Key Takeaways
- ✓The subscription flow in Flutter is the same as one-time checkout — just pass a plan code when initializing.
- ✓After checkout, Paystack creates the subscription automatically. Your backend receives the webhook.
- ✓Flutter checks subscription status by calling your backend API, not Paystack directly.
- ✓Store subscription status in your backend database and expose it via an API the Flutter app polls.
- ✓Use Provider or Riverpod to manage subscription state across screens in Flutter.
- ✓Display the next billing date and plan details in your app from data stored in your backend.
Frequently Asked Questions
- How does Flutter know when a subscription is active?
- Flutter does not know directly. Your backend receives the subscription.create webhook from Paystack and marks the user as subscribed in the database. Flutter polls your backend status endpoint to check. After the checkout WebView closes, call the status endpoint and update the UI.
- Can I cancel a Paystack subscription from Flutter?
- Yes. Create a cancel endpoint on your backend that calls POST /subscription/disable with the subscription code and email token. Flutter calls that backend endpoint. The backend handles the Paystack API call and returns success or failure to Flutter.
- How do I show the next billing date in Flutter?
- Paystack provides next_payment_date in the subscription.create webhook and the subscription status API. Store this date in your database and expose it from your backend. Flutter fetches and displays it on the subscription management screen.
- What state management should I use for subscription state in Flutter?
- Provider or Riverpod work well. Create a SubscriptionProvider that fetches subscription status from your backend. Refresh it after successful checkout. Any screen that needs to show gated content can watch this provider and adjust the UI accordingly.
Ready to build real-world apps?
Join the McTaba Labs full-stack marathon (4 months full-time · 6 months part-time). Learn M-Pesa, USSD, and WhatsApp engineering while shipping 8 production apps.
Apply to the McTaba Marathon