flutter_paymob 2.0.0
flutter_paymob: ^2.0.0 copied to clipboard
Accept Paymob card and mobile-wallet payments in Flutter through the official native Paymob Android and iOS SDKs (Intention API).
import 'package:flutter/material.dart';
import 'package:flutter_paymob/flutter_paymob.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
FlutterPaymob.instance.initialize(
publicKey: 'egy_pk_test_XXXXXXXX',
// Demo only. In a real app create the intention on your backend and call
// FlutterPaymob.instance.pay(clientSecret: ...) instead.
secretKey: 'egy_sk_test_XXXXXXXX',
cardIntegrationId: 123456,
walletIntegrationId: 654321,
customization: const PaymobCustomization(
appName: 'Flutter Paymob',
buttonBackgroundColor: Colors.deepPurple,
buttonTextColor: Colors.white,
showSaveCard: true,
),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Paymob',
theme: ThemeData(colorSchemeSeed: Colors.deepPurple, useMaterial3: true),
home: const PaymentPage(),
);
}
}
class PaymentPage extends StatefulWidget {
const PaymentPage({super.key});
@override
State<PaymentPage> createState() => _PaymentPageState();
}
class _PaymentPageState extends State<PaymentPage> {
final _billing = BillingData(
firstName: 'Ahmed',
lastName: 'Hosni',
email: 'test@example.com',
phoneNumber: '+201010101010',
);
bool _busy = false;
Future<void> _run(Future<PaymobPaymentResult> Function() payment) async {
setState(() => _busy = true);
try {
final result = await payment();
_show(switch (result.status) {
PaymentStatus.successful => 'Paid ✓ transaction ${result.transactionID}',
PaymentStatus.pending => 'Pending — wait for the webhook',
PaymentStatus.cancelled => 'Cancelled',
PaymentStatus.failure => 'Failed: ${result.message}',
});
} on PaymobException catch (e) {
_show(e.toString());
} finally {
if (mounted) setState(() => _busy = false);
}
}
void _show(String text) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(text)));
}
@override
Widget build(BuildContext context) {
final paymob = FlutterPaymob.instance;
return Scaffold(
appBar: AppBar(title: const Text('Flutter Paymob')),
body: Center(
child: _busy
? const CircularProgressIndicator.adaptive()
: Column(
mainAxisSize: MainAxisSize.min,
children: [
FilledButton(
onPressed: () => _run(() => paymob.payWithCard(
amount: 100, billingData: _billing)),
child: const Text('Pay with card'),
),
const SizedBox(height: 12),
FilledButton(
onPressed: () => _run(() => paymob.payWithWallet(
amount: 100, billingData: _billing)),
child: const Text('Pay with wallet'),
),
const SizedBox(height: 12),
OutlinedButton(
onPressed: () => _run(() =>
paymob.checkout(amount: 100, billingData: _billing)),
child: const Text('Checkout (all methods)'),
),
],
),
),
);
}
}