flutter_xpay 0.1.0
flutter_xpay: ^0.1.0 copied to clipboard
Accept XPay (xpay.app) payments in Flutter: cards, Valu, Fawry and more through XPay Hosted Checkout.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_xpay/flutter_xpay.dart';
void main() {
// Values come from xpay_config.json:
// flutter run --dart-define-from-file=xpay_config.json
XPay.instance.initialize(
// Must be an HTTPS URL on your domain (localhost works in test mode only).
returnUrl: const String.fromEnvironment(
'XPAY_RETURN_URL',
defaultValue: 'https://localhost/xpay/return/{CHECKOUT_SESSION_ID}',
),
cancelUrl: const String.fromEnvironment(
'XPAY_CANCEL_URL',
defaultValue: 'https://localhost/xpay/failed',
),
// Test mode only. In production create the session on your server and
// call XPay.instance.openCheckout(context, checkoutUrl: session.url).
secretKey: const String.fromEnvironment('XPAY_SECRET_KEY'),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter XPay',
theme: ThemeData(colorSchemeSeed: Colors.indigo, useMaterial3: true),
home: const CheckoutPage(),
);
}
}
class CheckoutPage extends StatefulWidget {
const CheckoutPage({super.key});
@override
State<CheckoutPage> createState() => _CheckoutPageState();
}
class _CheckoutPageState extends State<CheckoutPage> {
bool _busy = false;
Future<void> _pay() async {
setState(() => _busy = true);
try {
final result = await XPay.instance.pay(
context,
lineItems: const [
XPayLineItem(name: 'T-shirt', unitAmount: 250, quantity: 2),
XPayLineItem(name: 'Shipping', unitAmount: 50),
],
locale: XPayLocale.ar,
metadata: {'orderId': '1042'},
);
_show(switch (result.status) {
XPayCheckoutStatus.paid => 'Paid ✓ (${result.sessionId})',
XPayCheckoutStatus.processing => 'Being confirmed — wait for the webhook',
XPayCheckoutStatus.failed => 'Payment failed',
XPayCheckoutStatus.dismissed => 'Checkout closed',
});
} on XPayException 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) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter XPay')),
body: Center(
child: _busy
? const CircularProgressIndicator.adaptive()
: FilledButton(
onPressed: _pay,
child: const Text('Pay 550 EGP'),
),
),
);
}
}