libyan_payments_for_mypay 1.0.4
libyan_payments_for_mypay: ^1.0.4 copied to clipboard
Libyan Payments for MyPay — native in-app Flutter checkout (OTP, Masrafi/Yasr/Sahara Pay, banking card). Session credentials only; never embed client secrets.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:libyan_payments_for_mypay/libyan_payments_for_mypay.dart';
void main() {
runApp(const MyPayExampleApp());
}
class MyPayExampleApp extends StatelessWidget {
const MyPayExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyPay Flutter Example',
debugShowCheckedModeBanner: false,
locale: const Locale('ar'),
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF0B6E4F)),
useMaterial3: true,
),
home: const CheckoutDemoPage(),
);
}
}
class CheckoutDemoPage extends StatefulWidget {
const CheckoutDemoPage({super.key});
@override
State<CheckoutDemoPage> createState() => _CheckoutDemoPageState();
}
class _CheckoutDemoPageState extends State<CheckoutDemoPage> {
static const _gateways = <(String alias, String label)>[
('sadad', 'SADAD'),
('moamalat', 'Banking card'),
('edfali', 'Edfali'),
('mobicash', 'Mobicash'),
('masrafi-pay', 'Masrafi Pay'),
('yasr-pay', 'Yasr Pay'),
('sahary-pay', 'Sahara Pay'),
];
final _sessionIdCtrl = TextEditingController();
final _sessionTokenCtrl = TextEditingController();
String _gatewayAlias = 'sadad';
bool _sandbox = true;
bool _busy = false;
String? _lastResult;
@override
void dispose() {
_sessionIdCtrl.dispose();
_sessionTokenCtrl.dispose();
super.dispose();
}
Future<void> _pay() async {
final sessionId = _sessionIdCtrl.text.trim();
final sessionToken = _sessionTokenCtrl.text.trim();
if (sessionId.isEmpty || sessionToken.isEmpty) {
setState(() => _lastResult = 'أدخل session_id و session_token من سيرفرك.');
return;
}
setState(() {
_busy = true;
_lastResult = null;
});
try {
final result = await MyPayCheckout.present(
context: context,
sessionId: sessionId,
sessionToken: sessionToken,
environment:
_sandbox ? MyPayEnvironment.sandbox : MyPayEnvironment.production,
gatewayAlias: _gatewayAlias,
locale: const Locale('ar'),
theme: const MyPayCheckoutTheme(
primaryColor: Color(0xFF0B6E4F),
showMyPayBadge: true,
),
);
setState(() {
_lastResult = result.isSuccess
? 'نجاح الواجهة (${result.status.name}) — أكّد الطلب عبر webhook موقّع قبل التسليم.'
: 'انتهى التدفق: ${result.status.name}${result.message != null ? ' — ${result.message}' : ''}. الـ webhook يبقى مصدر الحقيقة.';
});
} catch (e) {
setState(() => _lastResult = 'خطأ: $e');
} finally {
if (mounted) setState(() => _busy = false);
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
title: const Text('مثال MyPay للتاجر'),
backgroundColor: theme.colorScheme.inversePrimary,
),
body: SafeArea(
child: ListView(
padding: const EdgeInsets.all(20),
children: [
Card(
color: theme.colorScheme.surfaceContainerHighest,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'لا تضع client_id أو secret_id في التطبيق.',
style: theme.textTheme.titleSmall?.copyWith(
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 8),
Text(
'من سيرفرك: authentication/token → payment/create → '
'POST /checkout/sessions (mode: native)، ثم الصق '
'session_id و session_token هنا فقط.',
style: theme.textTheme.bodyMedium,
),
const SizedBox(height: 8),
Text(
'الدليل: docs.mypay.ly/project/plugins/flutter-package',
style: theme.textTheme.bodySmall,
),
],
),
),
),
const SizedBox(height: 20),
TextField(
controller: _sessionIdCtrl,
decoration: const InputDecoration(
labelText: 'session_id',
border: OutlineInputBorder(),
),
textInputAction: TextInputAction.next,
),
const SizedBox(height: 12),
TextField(
controller: _sessionTokenCtrl,
decoration: const InputDecoration(
labelText: 'session_token',
border: OutlineInputBorder(),
),
obscureText: true,
),
const SizedBox(height: 16),
DropdownButtonFormField<String>(
// ignore: deprecated_member_use
value: _gatewayAlias,
decoration: const InputDecoration(
labelText: 'gatewayAlias (يختاره تطبيقك)',
border: OutlineInputBorder(),
),
items: [
for (final g in _gateways)
DropdownMenuItem(value: g.$1, child: Text('${g.$2} (${g.$1})')),
],
onChanged: _busy
? null
: (v) {
if (v != null) setState(() => _gatewayAlias = v);
},
),
const SizedBox(height: 8),
SwitchListTile(
contentPadding: EdgeInsets.zero,
title: Text(_sandbox ? 'Sandbox' : 'Production'),
subtitle: const Text('يجب أن تطابق بيئة مفاتيح وجلسة السيرفر'),
value: _sandbox,
onChanged: _busy
? null
: (v) => setState(() => _sandbox = v),
),
const SizedBox(height: 12),
FilledButton(
onPressed: _busy ? null : _pay,
child: _busy
? const SizedBox(
height: 22,
width: 22,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('فتح الدفع (Themed UI)'),
),
const SizedBox(height: 12),
Text(
'لواجهة مخصصة (headless) راجع README الحزمة — هذا المثال يغطي '
'MyPayCheckout.present فقط.',
style: theme.textTheme.bodySmall,
),
if (_lastResult != null) ...[
const SizedBox(height: 20),
Text(
_lastResult!,
style: theme.textTheme.bodyLarge,
),
],
],
),
),
);
}
}