Flutter
Installation
Section titled “Installation”flutter pub add sanwo_flutter sanwo_paystackProvider packages
Section titled “Provider packages”| Provider | Package |
|---|---|
| Paystack | sanwo_paystack |
| Flutterwave | sanwo_flutterwave |
| Razorpay | sanwo_razorpay |
| Monnify | sanwo_monnify |
| Interswitch | sanwo_interswitch |
Requirements
Section titled “Requirements”- Flutter >= 3.10
- Dart >= 3.0
The SDK uses webview_flutter internally. On Android, no extra setup is needed. On iOS, webview_flutter requires iOS 12+.
Quick start
Section titled “Quick start”import 'package:sanwo_flutter/sanwo_flutter.dart';import 'package:sanwo_paystack/sanwo_paystack.dart';
final sanwo = Sanwo( provider: paystackProvider, publicKey: 'pk_test_xxxxxxxxxxxx',);
// In a button handler:final result = await sanwo( context: context, options: CheckoutOptions( amount: 500000, // 5,000.00 NGN in kobo currency: 'NGN', customer: CheckoutCustomer(email: 'customer@example.com'), ),);
switch (result.status) { case CheckoutStatus.successful: print('Paid! Ref: ${result.reference}'); break; case CheckoutStatus.cancelled: print('User cancelled'); break; case CheckoutStatus.failed: print('Payment failed: ${result.error}'); break;}final sanwo = Sanwo( provider: paystackProvider, publicKey: 'pk_test_xxxxxxxxxxxx', debug: false, // optional timeout: 120, // optional, seconds);| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
provider |
SanwoProvider |
Yes | — | Provider from a provider package |
publicKey |
String |
Yes | — | Your provider public key |
debug |
bool |
No | false |
Enable debug logging |
timeout |
int |
No | 120 |
Timeout in seconds |
Calling the instance
Section titled “Calling the instance”final result = await sanwo( context: context, options: CheckoutOptions(...),);The context parameter is required — the SDK uses it to present a full-screen webview_flutter page via Navigator.push.
CheckoutOptions
Section titled “CheckoutOptions”CheckoutOptions( amount: 500000, currency: 'NGN', customer: CheckoutCustomer(email: 'customer@example.com'), reference: 'order_abc123', // optional channels: ['card', 'bank'], // optional metadata: {'plan': 'premium'}, // optional)CheckoutResult
Section titled “CheckoutResult”enum CheckoutStatus { successful, cancelled, failed }
class CheckoutResult { final CheckoutStatus status; final String? reference; final String? error; final dynamic providerResponse;}Events
Section titled “Events”Subscribe to lifecycle events:
sanwo.on(SanwoEvent.success, (data) { print('Payment succeeded: ${data['reference']}');});
sanwo.on(SanwoEvent.cancelled, (_) { print('User cancelled checkout');});| Event | Description |
|---|---|
SanwoEvent.started |
Checkout initiated |
SanwoEvent.opened |
Checkout UI visible |
SanwoEvent.loaded |
Provider UI finished loading |
SanwoEvent.success |
Payment successful |
SanwoEvent.cancelled |
User dismissed checkout |
SanwoEvent.failed |
Payment failed |
SanwoEvent.closed |
Checkout UI dismissed |
Removing listeners
Section titled “Removing listeners”// Remove a specific listenersanwo.off(SanwoEvent.success, myCallback);
// Remove all listenerssanwo.removeAllListeners();Full example
Section titled “Full example”import 'package:flutter/material.dart';import 'package:sanwo_flutter/sanwo_flutter.dart';import 'package:sanwo_paystack/sanwo_paystack.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget { const MyApp({super.key});
@override Widget build(BuildContext context) { return MaterialApp( home: const CheckoutPage(), ); }}
class CheckoutPage extends StatefulWidget { const CheckoutPage({super.key});
@override State<CheckoutPage> createState() => _CheckoutPageState();}
class _CheckoutPageState extends State<CheckoutPage> { final sanwo = Sanwo( provider: paystackProvider, publicKey: 'pk_test_xxxxxxxxxxxx', );
bool _loading = false;
Future<void> _pay() async { setState(() => _loading = true);
final result = await sanwo( context: context, options: CheckoutOptions( amount: 500000, currency: 'NGN', customer: CheckoutCustomer(email: 'customer@example.com'), reference: 'order_${DateTime.now().millisecondsSinceEpoch}', ), );
setState(() => _loading = false);
if (!mounted) return;
switch (result.status) { case CheckoutStatus.successful: ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Paid! Ref: ${result.reference}')), ); break; case CheckoutStatus.cancelled: ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Payment cancelled')), ); break; case CheckoutStatus.failed: ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Failed: ${result.error}')), ); break; } }
@override void dispose() { sanwo.removeAllListeners(); super.dispose(); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('My Store')), body: Center( child: ElevatedButton( onPressed: _loading ? null : _pay, child: Text(_loading ? 'Processing…' : 'Pay ₦5,000'), ), ), ); }}Switching providers
Section titled “Switching providers”To switch from Paystack to Flutterwave, change the import and provider:
// Beforeimport 'package:sanwo_paystack/sanwo_paystack.dart';final sanwo = Sanwo(provider: paystackProvider, publicKey: 'pk_test_xxx');
// Afterimport 'package:sanwo_flutterwave/sanwo_flutterwave.dart';final sanwo = Sanwo(provider: flutterwaveProvider, publicKey: 'FLWPUBK_TEST-xxx');Everything else stays the same.