mobibox_pay 1.0.3
mobibox_pay: ^1.0.3 copied to clipboard
A Flutter plugin to seamlessly integrate with the Mobibox payment gateway
example/lib/main.dart
import 'package:mobibox_pay/mobi_pay.dart';
import 'package:mobibox_pay/mobi_pay_checkout_widget.dart';
import 'package:flutter/material.dart';
import 'package:mobi_pay_example/thank_you.dart';
void main() {
runApp(const MaterialApp(
home: MyApp(), // The home is now our main page widget
));}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final MobiPay _mobiPay = MobiPay();
@override
void initState() {
super.initState();
_mobiPay.initialize(
backendUrl: 'https://domain.payment.com',
merchantKey: 'xxxxxx-xxxx-xxx-xxxx-xxxxxxxxxxx',
password: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
);
}
void _startPayment() {
showModalBottomSheet(
context: context,
isScrollControlled: true,
showDragHandle: true,
useSafeArea: true,
builder: (_) {
// --- FIX #1: Define isLoading OUTSIDE the StatefulBuilder's builder ---
bool isLoading = true;
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
// The rest of the logic remains the same.
return Stack(
children: [
MobiPayCheckout(
mobiPay: _mobiPay,
formId: "xxxx-xxxxx-xxxx-xxxxx-xxxxx",
orderNumber: "order-${DateTime.now().millisecondsSinceEpoch}",
orderAmount: "1.00",
orderCurrency: "USD",
orderDescription: "Test Purchase",
customerName: "John Smith",
customerEmail: "[email protected]",
billingCountry: "US",
billingState: "NY",
billingCity: "New York",
billingAddress: "123 Main St",
billingZip: "10001",
billingPhone: "+15551234567",
billingDistrict: "test",
billingHouseNumber: "test",
successUrl: "https://example.com/success",
cancelUrl: "https://example.com/cancel",
errorUrl: "https://example.com/error",
expiryUrl: "https://example.com/expiry",
onSuccessRedirect: (url) {
print("✅ Success: $url");
if (context.mounted) {
if (context.mounted) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ThankYouPage()),
);
}
}
},
onCancelRedirect: (url) {
print("❌ Cancel: $url");
if (context.mounted) Navigator.of(context).pop();
},
onErrorRedirect: (url) {
print("🔥 Error Redirect: $url");
if (context.mounted) Navigator.of(context).pop();
},
onRedirectCallback: (url) {
// --- FIX #2: Run the delay only ONCE ---
// We can check if isLoading is still true before scheduling another delay.
if (isLoading) {
if (context.mounted) {
// This will now correctly trigger a rebuild where isLoading is false.
setState(() {
isLoading = false;
});
}
}
},
onError: (e) {
print("🐞 Exception: ${e.toString()}");
if (context.mounted) Navigator.of(context).pop();
},
),
// This will now correctly disappear after the delay.
if (isLoading)
const Center(
child: CircularProgressIndicator(color: Colors.white,),
),
],
);
},
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('MobiPay Example'),
),
body: Center(
child: ElevatedButton(
onPressed: _startPayment,
child: const Text('Start Simplified Payment'),
),
),
);
}
}