first commit
This commit is contained in:
71
siro_driver/lib/controller/payment/driver_payment_controller.dart
Executable file
71
siro_driver/lib/controller/payment/driver_payment_controller.dart
Executable file
@@ -0,0 +1,71 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:siro_driver/views/widgets/elevated_btn.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:siro_driver/constant/box_name.dart';
|
||||
import 'package:siro_driver/constant/links.dart';
|
||||
import 'package:siro_driver/controller/functions/crud.dart';
|
||||
import 'package:siro_driver/main.dart';
|
||||
|
||||
class DriverWalletHistoryController extends GetxController {
|
||||
bool isLoading = false;
|
||||
List archive = [];
|
||||
List weeklyList = [];
|
||||
|
||||
getArchivePayment() async {
|
||||
isLoading = true;
|
||||
update();
|
||||
var res = await CRUD().getWallet(
|
||||
link: AppLink.getWalletByDriver,
|
||||
payload: {'driverID': box.read(BoxName.driverID)});
|
||||
if (res == 'failure') {
|
||||
Get.defaultDialog(
|
||||
barrierDismissible: false,
|
||||
title: 'There is no data yet.'.tr,
|
||||
middleText: '',
|
||||
confirm: MyElevatedButton(
|
||||
title: 'Back'.tr,
|
||||
onPressed: () {
|
||||
Get.back();
|
||||
// Get.back();
|
||||
},
|
||||
));
|
||||
}
|
||||
archive = jsonDecode(res)['message'];
|
||||
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
|
||||
getWeekllyArchivePayment() async {
|
||||
isLoading = true;
|
||||
update();
|
||||
var res = await CRUD().getWallet(
|
||||
link: AppLink.getDriverWeekPaymentMove,
|
||||
payload: {'driverID': box.read(BoxName.driverID)});
|
||||
if (res == 'failure') {
|
||||
Get.defaultDialog(
|
||||
barrierDismissible: false,
|
||||
title: 'There is no data yet.'.tr,
|
||||
middleText: '',
|
||||
confirm: MyElevatedButton(
|
||||
title: 'Back'.tr,
|
||||
onPressed: () {
|
||||
Get.back();
|
||||
// Get.back();
|
||||
},
|
||||
));
|
||||
} else {
|
||||
weeklyList = jsonDecode(res)['message'];
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
// getArchivePayment();
|
||||
super.onInit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
// import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:siro_driver/constant/links.dart'; // افترض وجود هذا الملف
|
||||
import 'package:siro_driver/controller/functions/crud.dart'; // افترض وجود هذا الملف
|
||||
import '../../../main.dart'; // افترض وجود box هنا
|
||||
import '../../../constant/box_name.dart'; // افترض وجود هذا الملف
|
||||
|
||||
// Service class to handle MTN payment logic
|
||||
class MtnPaymentService {
|
||||
final String _baseUrl =
|
||||
"${AppLink.paymentServer}/ride/mtn_new"; // تأكد من تعديل المسار
|
||||
|
||||
// Function to create a new invoice
|
||||
Future<String?> createInvoice({
|
||||
required String userId,
|
||||
required String userType, // 'driver' or 'passenger'
|
||||
required double amount,
|
||||
required String mtnPhone,
|
||||
}) async {
|
||||
final url = "$_baseUrl/create_mtn_invoice.php";
|
||||
try {
|
||||
final response = await CRUD().postWallet(
|
||||
// استخدام نفس دالة CRUD
|
||||
link: url,
|
||||
payload: {
|
||||
'user_id': userId,
|
||||
'user_type': userType,
|
||||
'amount': amount.toString(),
|
||||
'mtn_phone': mtnPhone,
|
||||
},
|
||||
).timeout(const Duration(seconds: 15));
|
||||
|
||||
if (response != 'failure') {
|
||||
final data = response;
|
||||
if (data['status'] == 'success' && data['invoice_number'] != null) {
|
||||
debugPrint("MTN Invoice created: ${data['invoice_number']}");
|
||||
return data['invoice_number'].toString();
|
||||
} else {
|
||||
debugPrint("Failed to create MTN invoice: ${data['message']}");
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
debugPrint("Server error during MTN invoice creation.");
|
||||
return null;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Exception during MTN invoice creation: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to check invoice status (polling)
|
||||
Future<bool> checkInvoiceStatus(String invoiceNumber) async {
|
||||
// This should point to a new script on your server that checks mtn_invoices table
|
||||
final url = "$_baseUrl/check_mtn_invoice_status.php";
|
||||
try {
|
||||
final response = await CRUD().postWallet(link: url, payload: {
|
||||
'invoice_number': invoiceNumber,
|
||||
}).timeout(const Duration(seconds: 10));
|
||||
|
||||
if (response != 'failure') {
|
||||
final data = response;
|
||||
return data['status'] == 'success' &&
|
||||
data['invoice_status'] == 'completed';
|
||||
}
|
||||
return false;
|
||||
} catch (e) {
|
||||
debugPrint("Error checking MTN invoice status: $e");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum PaymentStatus {
|
||||
creatingInvoice,
|
||||
waitingForPayment,
|
||||
paymentSuccess,
|
||||
paymentTimeout,
|
||||
paymentError
|
||||
}
|
||||
|
||||
class PaymentScreenMtn extends StatefulWidget {
|
||||
final double amount;
|
||||
// يمكنك إضافة متغير لتحديد هل المستخدم سائق أم راكب
|
||||
final String userType; // 'driver' or 'passenger'
|
||||
|
||||
const PaymentScreenMtn({
|
||||
super.key,
|
||||
required this.amount,
|
||||
required this.userType,
|
||||
});
|
||||
|
||||
@override
|
||||
_PaymentScreenMtnState createState() => _PaymentScreenMtnState();
|
||||
}
|
||||
|
||||
class _PaymentScreenMtnState extends State<PaymentScreenMtn> {
|
||||
final MtnPaymentService _paymentService = MtnPaymentService();
|
||||
Timer? _pollingTimer;
|
||||
PaymentStatus _status = PaymentStatus.creatingInvoice;
|
||||
String? _invoiceNumber;
|
||||
// جلب البيانات من الـ box
|
||||
final String userId =
|
||||
box.read(BoxName.driverID) ?? box.read(BoxName.passengerID);
|
||||
final String phone = box.read(BoxName.phoneWallet);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_createAndPollInvoice();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pollingTimer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _createAndPollInvoice() async {
|
||||
setState(() => _status = PaymentStatus.creatingInvoice);
|
||||
|
||||
final invoiceNumber = await _paymentService.createInvoice(
|
||||
userId: userId,
|
||||
userType: widget.userType,
|
||||
amount: widget.amount,
|
||||
mtnPhone: phone,
|
||||
);
|
||||
|
||||
if (invoiceNumber != null && mounted) {
|
||||
setState(() {
|
||||
_invoiceNumber = invoiceNumber;
|
||||
_status = PaymentStatus.waitingForPayment;
|
||||
});
|
||||
_startPolling(invoiceNumber);
|
||||
} else if (mounted) {
|
||||
setState(() => _status = PaymentStatus.paymentError);
|
||||
}
|
||||
}
|
||||
|
||||
void _startPolling(String invoiceNumber) {
|
||||
const timeoutDuration = Duration(minutes: 15); // زيادة المهلة
|
||||
var elapsed = Duration.zero;
|
||||
|
||||
_pollingTimer = Timer.periodic(const Duration(seconds: 5), (timer) async {
|
||||
elapsed += const Duration(seconds: 5);
|
||||
if (elapsed >= timeoutDuration) {
|
||||
timer.cancel();
|
||||
if (mounted) setState(() => _status = PaymentStatus.paymentTimeout);
|
||||
return;
|
||||
}
|
||||
|
||||
debugPrint("Polling... Checking MTN invoice: $invoiceNumber");
|
||||
final isCompleted =
|
||||
await _paymentService.checkInvoiceStatus(invoiceNumber);
|
||||
if (isCompleted && mounted) {
|
||||
timer.cancel();
|
||||
setState(() => _status = PaymentStatus.paymentSuccess);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return PopScope(
|
||||
canPop: _status != PaymentStatus.waitingForPayment,
|
||||
onPopInvoked: (didPop) async {
|
||||
if (didPop) return;
|
||||
if (_status == PaymentStatus.waitingForPayment) {
|
||||
final shouldPop = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('هل أنت متأكد؟'),
|
||||
content: const Text(
|
||||
'إذا خرجت الآن، قد تفشل عملية الدفع. عليك إتمامها من تطبيق MTN.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('البقاء')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('الخروج')),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (shouldPop ?? false) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(title: const Text("الدفع عبر MTN Cash")),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Center(child: _buildContentByStatus()),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContentByStatus() {
|
||||
switch (_status) {
|
||||
case PaymentStatus.creatingInvoice:
|
||||
return const Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 20),
|
||||
Text("جاري إنشاء فاتورة دفع...", style: TextStyle(fontSize: 16)),
|
||||
],
|
||||
);
|
||||
case PaymentStatus.waitingForPayment:
|
||||
return _buildWaitingForPaymentUI();
|
||||
case PaymentStatus.paymentSuccess:
|
||||
return _buildSuccessUI();
|
||||
case PaymentStatus.paymentTimeout:
|
||||
case PaymentStatus.paymentError:
|
||||
return _buildErrorUI();
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildWaitingForPaymentUI() {
|
||||
final currencyFormat = NumberFormat.decimalPattern('ar_SY');
|
||||
|
||||
return SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
// **مهم**: استبدل هذا المسار بمسار شعار MTN الصحيح في مشروعك
|
||||
Image.asset('assets/images/cashMTN.png', width: 120),
|
||||
const SizedBox(height: 24),
|
||||
Text("تعليمات الدفع", style: Theme.of(context).textTheme.titleLarge),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
"المبلغ المطلوب: ${currencyFormat.format(widget.amount)} ل.س",
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Card(
|
||||
elevation: 1.5,
|
||||
shape:
|
||||
RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
_StepTile(number: 1, text: "افتح تطبيق MTN Cash Mobile."),
|
||||
_StepTile(
|
||||
number: 2,
|
||||
text: "اذهب إلى قسم 'دفع الفواتير' أو 'خدمات الدفع'."),
|
||||
_StepTile(
|
||||
number: 3,
|
||||
text: "ابحث عن 'Intaleq App' في قائمة المفوترين."),
|
||||
_StepTile(
|
||||
number: 4,
|
||||
text:
|
||||
"أدخل رقم هاتفك المسجل لدينا للاستعلام عن الفاتورة."),
|
||||
_StepTile(
|
||||
number: 5,
|
||||
text:
|
||||
"ستظهر لك فاتورة بالمبلغ المطلوب. قم بتأكيد الدفع."),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const LinearProgressIndicator(minHeight: 2),
|
||||
const SizedBox(height: 12),
|
||||
Text("بانتظار تأكيد الدفع من MTN...",
|
||||
style: TextStyle(color: Colors.grey.shade700)),
|
||||
const SizedBox(height: 4),
|
||||
const Text("هذه الشاشة ستتحدث تلقائيًا عند اكتمال الدفع",
|
||||
style: TextStyle(color: Colors.grey),
|
||||
textAlign: TextAlign.center),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSuccessUI() {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.check_circle, color: Colors.green, size: 80),
|
||||
const SizedBox(height: 20),
|
||||
const Text("تم الدفع بنجاح!",
|
||||
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
const Text("تمت إضافة النقاط إلى حسابك.",
|
||||
style: TextStyle(fontSize: 16)),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text("العودة إلى المحفظة"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildErrorUI() {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.error, color: Colors.red, size: 80),
|
||||
const SizedBox(height: 20),
|
||||
Text(
|
||||
_status == PaymentStatus.paymentTimeout
|
||||
? "انتهى الوقت المحدد للدفع"
|
||||
: "حدث خطأ أثناء إنشاء الفاتورة",
|
||||
style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text("يرجى المحاولة مرة أخرى.", style: TextStyle(fontSize: 16)),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: _createAndPollInvoice,
|
||||
child: const Text("المحاولة مرة أخرى"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ويدجت مساعد لعرض خطوات التعليمات بشكل أنيق
|
||||
class _StepTile extends StatelessWidget {
|
||||
final int number;
|
||||
final String text;
|
||||
const _StepTile({required this.number, required this.text});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
leading: CircleAvatar(
|
||||
radius: 14,
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
child: Text("$number",
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold)),
|
||||
),
|
||||
title: Text(text),
|
||||
);
|
||||
}
|
||||
}
|
||||
412
siro_driver/lib/controller/payment/payment_controller.dart
Executable file
412
siro_driver/lib/controller/payment/payment_controller.dart
Executable file
@@ -0,0 +1,412 @@
|
||||
import 'dart:convert';
|
||||
import 'package:siro_driver/constant/api_key.dart';
|
||||
import 'package:siro_driver/constant/style.dart';
|
||||
import 'package:siro_driver/controller/functions/tts.dart';
|
||||
import 'package:siro_driver/controller/payment/paymob/paymob_response.dart';
|
||||
import 'package:siro_driver/views/widgets/elevated_btn.dart';
|
||||
import 'package:siro_driver/views/widgets/error_snakbar.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
import 'package:siro_driver/views/widgets/mydialoug.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/colors.dart';
|
||||
import '../../constant/info.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../main.dart';
|
||||
import '../functions/crud.dart';
|
||||
import '../functions/toast.dart';
|
||||
import 'paymob/paymob_wallet.dart';
|
||||
|
||||
class PaymentController extends GetxController {
|
||||
bool isLoading = false;
|
||||
bool isWalletChecked = true;
|
||||
bool isCashChecked = false;
|
||||
bool isWalletFound = false;
|
||||
bool isPromoSheetDialogue = false;
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final promo = TextEditingController();
|
||||
// double totalPassenger =
|
||||
// double.parse(Get.find<MapDriverController>().totalPricePassenger);
|
||||
int? selectedAmount = 0;
|
||||
List<dynamic> totalPassengerWalletDetails = [];
|
||||
final walletphoneController = TextEditingController();
|
||||
String passengerTotalWalletAmount = '';
|
||||
String ip = '1';
|
||||
DateTime now = DateTime.now();
|
||||
late int timestamp;
|
||||
|
||||
void updateSelectedAmount(int value) {
|
||||
selectedAmount = value;
|
||||
update();
|
||||
}
|
||||
|
||||
void changePromoSheetDialogue() {
|
||||
isPromoSheetDialogue = !isPromoSheetDialogue;
|
||||
update();
|
||||
}
|
||||
|
||||
getPassengerWallet() async {
|
||||
isLoading = true;
|
||||
update();
|
||||
|
||||
await CRUD().getWallet(
|
||||
link: AppLink.getWalletByPassenger,
|
||||
payload: {'passenger_id': box.read(BoxName.passengerID)}).then((value) {
|
||||
box.write(BoxName.passengerWalletTotal,
|
||||
jsonDecode(value)['message'][0]['total'].toString());
|
||||
});
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
|
||||
addPassengerWallet() async {
|
||||
isLoading = true;
|
||||
update();
|
||||
// double sallaryAccountNowBeforeAdding =
|
||||
// double.parse(box.read(BoxName.passengerWalletTotal).toString());
|
||||
await CRUD().postWallet(link: AppLink.addPassengersWallet, payload: {
|
||||
'passenger_id': box.read(BoxName.passengerID).toString(),
|
||||
'balance': selectedAmount.toString()
|
||||
}).then((value) {
|
||||
getPassengerWallet();
|
||||
// sallaryAccountNowBeforeAdding = sallaryAccountNowBeforeAdding +
|
||||
// double.parse(selectedAmount.toString());
|
||||
// box.write(BoxName.passengerWalletTotal, sallaryAccountNowBeforeAdding);
|
||||
});
|
||||
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
|
||||
// void onChangedPaymentMethodWallet(bool? value) {
|
||||
// if (box.read(BoxName.passengerWalletTotal) == null ||
|
||||
// double.parse(box.read(BoxName.passengerWalletTotal).toString()) <
|
||||
// totalPassenger) {
|
||||
// isWalletChecked = false;
|
||||
// isWalletChecked ? isCashChecked = true : isCashChecked = true;
|
||||
// update();
|
||||
// } else {
|
||||
// isWalletChecked = !isWalletChecked;
|
||||
// isWalletChecked ? isCashChecked = false : isCashChecked = true;
|
||||
// update();
|
||||
// }
|
||||
// }
|
||||
|
||||
// void onChangedPaymentMethodCash(bool? value) {
|
||||
// if (box.read(BoxName.passengerWalletTotal) == null ||
|
||||
// double.parse(box.read(BoxName.passengerWalletTotal)) < totalPassenger) {
|
||||
// isWalletChecked = false;
|
||||
// isCashChecked = !isCashChecked;
|
||||
// isCashChecked ? isWalletChecked = false : isWalletChecked = false;
|
||||
// update();
|
||||
// } else {
|
||||
// isCashChecked = !isCashChecked;
|
||||
// isCashChecked ? isWalletChecked = false : isWalletChecked = true;
|
||||
// update();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// 'https://accept.paymob.com/unifiedcheckout/?publicKey=egy_pk_live_mbjDC9Ni6FSHKmsz8sOHiVk2xd7oWRve&clientSecret=egy_sk_live_c0904e9cf04506ae64f818d4e075b4a957e3713fdf7a22cb7da30a29e72442b5'
|
||||
|
||||
Future<void> payWithPayMob(
|
||||
BuildContext context, String amount, currency, Function method) async {
|
||||
String newAmount = (double.parse(amount) * 100).toStringAsFixed(2);
|
||||
try {
|
||||
bool isAvailable = await LocalAuthentication().isDeviceSupported();
|
||||
if (isAvailable) {
|
||||
// Authenticate the user
|
||||
bool didAuthenticate = await LocalAuthentication().authenticate(
|
||||
localizedReason: 'Use Touch ID or Face ID to confirm payment',
|
||||
// options: AuthenticationOptions(
|
||||
biometricOnly: true,
|
||||
sensitiveTransaction: true,
|
||||
);
|
||||
if (didAuthenticate) {
|
||||
final PaymobResponse? response = await PaymobPayment.instance.pay(
|
||||
context: context,
|
||||
currency: currency, //"EGP",
|
||||
amountInCents: newAmount, // 19.00 EGP
|
||||
|
||||
billingData: PaymobBillingData(),
|
||||
onPayment: (PaymobResponse response) {},
|
||||
);
|
||||
|
||||
if (response!.responseCode == 'APPROVED') {
|
||||
Get.defaultDialog(
|
||||
barrierDismissible: false,
|
||||
title: 'Payment Successful'.tr,
|
||||
titleStyle: AppStyle.title,
|
||||
content: Text(
|
||||
'The payment was approved.'.tr,
|
||||
style: AppStyle.title,
|
||||
),
|
||||
confirm: MyElevatedButton(
|
||||
title: 'OK'.tr,
|
||||
kolor: AppColor.greenColor,
|
||||
onPressed: () async {
|
||||
Get.back();
|
||||
method();
|
||||
},
|
||||
),
|
||||
);
|
||||
} else {
|
||||
Get.defaultDialog(
|
||||
barrierDismissible: false,
|
||||
// backgroundColor: AppColor.redColor,
|
||||
title: 'Payment Failed'.tr,
|
||||
content: Text(
|
||||
'The payment was not approved. Please try again.'.tr,
|
||||
textAlign: TextAlign.center,
|
||||
style: AppStyle.title,
|
||||
),
|
||||
confirm: MyElevatedButton(
|
||||
title: 'OK'.tr,
|
||||
kolor: AppColor.redColor,
|
||||
onPressed: () async {
|
||||
Get.back();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
Get.snackbar("Authentication Failed",
|
||||
"Please enable Face ID or Fingerprint in your settings.");
|
||||
|
||||
// Authentication failed, handle accordingly
|
||||
}
|
||||
} else {
|
||||
MyDialog().getDialog('Biometric Authentication'.tr,
|
||||
'You should use Touch ID or Face ID to confirm payment'.tr, () {
|
||||
Get.back();
|
||||
});
|
||||
// Authentication failed, handle accordingly
|
||||
}
|
||||
// else {
|
||||
// final PaymobResponse? response = await PaymobPayment.instance.pay(
|
||||
// context: context,
|
||||
// currency: currency, //"EGP",
|
||||
// amountInCents: newAmount, // 19.00 EGP
|
||||
// billingData: PaymobBillingData(),
|
||||
// onPayment: (PaymobResponse response) {},
|
||||
// );
|
||||
|
||||
// if (response!.responseCode == 'APPROVED') {
|
||||
// Get.defaultDialog(
|
||||
// barrierDismissible: false,
|
||||
// title: 'Payment Successful'.tr,
|
||||
// titleStyle: AppStyle.title,
|
||||
// // backgroundColor: AppColor.greenColor,
|
||||
// content: Text(
|
||||
// 'The payment was approved.'.tr,
|
||||
// style: AppStyle.title,
|
||||
// ),
|
||||
// confirm: MyElevatedButton(
|
||||
// kolor: AppColor.greenColor,
|
||||
// title: 'OK'.tr,
|
||||
// onPressed: () async {
|
||||
// Get.back();
|
||||
// method();
|
||||
// },
|
||||
// ),
|
||||
// );
|
||||
// } else {
|
||||
// Get.defaultDialog(
|
||||
// barrierDismissible: false,
|
||||
// // backgroundColor: AppColor.redColor,
|
||||
// title: 'Payment Failed'.tr,
|
||||
// content: Column(
|
||||
// children: [
|
||||
// IconButton(
|
||||
// onPressed: () {
|
||||
// Get.find<TextToSpeechController>().speakText(
|
||||
// 'The payment was not approved. Please try again.'.tr,
|
||||
// );
|
||||
// },
|
||||
// icon: const Icon(Icons.headphones),
|
||||
// ),
|
||||
// Text(
|
||||
// 'The payment was not approved. Please try again.'.tr,
|
||||
// textAlign: TextAlign.center,
|
||||
// style: AppStyle.title,
|
||||
// ),
|
||||
// Text(
|
||||
// '${'The reason is'.tr} ${response.message!.tr}',
|
||||
// textAlign: TextAlign.center,
|
||||
// style: AppStyle.title.copyWith(color: AppColor.redColor),
|
||||
// ),
|
||||
// ],
|
||||
// ),
|
||||
// confirm: MyElevatedButton(
|
||||
// title: 'OK'.tr,
|
||||
// kolor: AppColor.redColor,
|
||||
// onPressed: () async {
|
||||
// Get.back();
|
||||
// },
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
|
||||
// }
|
||||
} catch (e) {
|
||||
Get.defaultDialog(
|
||||
title: 'Error'.tr,
|
||||
content: Text(
|
||||
'An error occurred during the payment process.'.tr,
|
||||
style: AppStyle.title,
|
||||
),
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> payWithPayMobWallet(
|
||||
BuildContext context, String amount, currency, Function method) async {
|
||||
String newAmount = (double.parse(amount) * 100).toStringAsFixed(2);
|
||||
try {
|
||||
bool isAvailable = await LocalAuthentication().isDeviceSupported();
|
||||
if (isAvailable) {
|
||||
// Authenticate the user
|
||||
bool didAuthenticate = await LocalAuthentication().authenticate(
|
||||
localizedReason: 'Use Touch ID or Face ID to confirm payment',
|
||||
// options: AuthenticationOptions(
|
||||
biometricOnly: true,
|
||||
sensitiveTransaction: true,
|
||||
// )
|
||||
);
|
||||
if (didAuthenticate) {
|
||||
final PaymobResponseWallet? response =
|
||||
await PaymobPaymentWallet.instance.pay(
|
||||
context: context,
|
||||
currency: currency, //"EGP",
|
||||
amountInCents: newAmount, // 19.00 EGP
|
||||
|
||||
billingData: PaymobBillingDataWallet(),
|
||||
onPayment: (PaymobResponseWallet response) {},
|
||||
);
|
||||
if (response!.success == true &&
|
||||
response.message.toString() == 'Approved') {
|
||||
Toast.show(context, 'Payment Successful'.tr, AppColor.greenColor);
|
||||
method();
|
||||
} else {
|
||||
Get.defaultDialog(
|
||||
barrierDismissible: false,
|
||||
// backgroundColor: AppColor.redColor,
|
||||
title: 'Payment Failed'.tr,
|
||||
content: Column(
|
||||
children: [
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
Get.find<TextToSpeechController>().speakText(
|
||||
'The payment was not approved. Please try again.'.tr,
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.headphones),
|
||||
),
|
||||
Text(
|
||||
'The payment was not approved. Please try again.'.tr,
|
||||
textAlign: TextAlign.center,
|
||||
style: AppStyle.title,
|
||||
),
|
||||
Text(
|
||||
'${'The reason is'.tr} ${response.message!.tr}',
|
||||
textAlign: TextAlign.center,
|
||||
style: AppStyle.title.copyWith(color: AppColor.redColor),
|
||||
),
|
||||
],
|
||||
),
|
||||
confirm: MyElevatedButton(
|
||||
title: 'OK'.tr,
|
||||
kolor: AppColor.redColor,
|
||||
onPressed: () async {
|
||||
Get.back();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Authentication failed, handle accordingly
|
||||
MyDialog().getDialog('Authentication failed'.tr, ''.tr, () {
|
||||
Get.back();
|
||||
});
|
||||
}
|
||||
} else {
|
||||
MyDialog().getDialog('Biometric Authentication'.tr,
|
||||
'You should use Touch ID or Face ID to confirm payment'.tr, () {
|
||||
Get.back();
|
||||
});
|
||||
// final PaymobResponse? response = await PaymobPayment.instance.pay(
|
||||
// context: context,
|
||||
// currency: currency, //"EGP",
|
||||
// amountInCents: newAmount, // 19.00 EGP
|
||||
// billingData: PaymobBillingData(),
|
||||
// onPayment: (PaymobResponse response) {},
|
||||
// );
|
||||
|
||||
// // if (response!.responseCode == 'APPROVED') {
|
||||
// if (response!.responseCode == '200' && response.success == true) {
|
||||
// Toast.show(context, 'Payment Successful'.tr, AppColor.greenColor);
|
||||
// method();
|
||||
// Get.defaultDialog(
|
||||
// barrierDismissible: false,
|
||||
// title: 'Payment Successful'.tr,
|
||||
// titleStyle: AppStyle.title,
|
||||
// // backgroundColor: AppColor.greenColor,
|
||||
// content: Text(
|
||||
// 'The payment was approved.'.tr,
|
||||
// style: AppStyle.title,
|
||||
// ),
|
||||
// confirm: MyElevatedButton(
|
||||
// kolor: AppColor.greenColor,
|
||||
// title: 'OK'.tr,
|
||||
// onPressed: () async {
|
||||
// Get.back();
|
||||
// method();
|
||||
// },
|
||||
// ),
|
||||
// );
|
||||
// } else {
|
||||
// Get.defaultDialog(
|
||||
// barrierDismissible: false,
|
||||
// // backgroundColor: AppColor.redColor,
|
||||
// title: 'Payment Failed'.tr,
|
||||
// content: Text(
|
||||
// 'The payment was not approved. Please try again.'.tr,
|
||||
// textAlign: TextAlign.center,
|
||||
// style: AppStyle.title,
|
||||
// ),
|
||||
// confirm: MyElevatedButton(
|
||||
// title: 'OK'.tr,
|
||||
// kolor: AppColor.redColor,
|
||||
// onPressed: () async {
|
||||
// Get.back();
|
||||
// },
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
}
|
||||
} catch (e) {
|
||||
Get.defaultDialog(
|
||||
title: 'Error'.tr,
|
||||
content: Text(
|
||||
'An error occurred during the payment process.'.tr,
|
||||
style: AppStyle.title,
|
||||
),
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
timestamp = now.millisecondsSinceEpoch;
|
||||
if (box.read(BoxName.passengerWalletTotal) == null) {
|
||||
box.write(BoxName.passengerWalletTotal, '0');
|
||||
}
|
||||
// getPassengerWallet();
|
||||
super.onInit();
|
||||
}
|
||||
}
|
||||
154
siro_driver/lib/controller/payment/paymob.dart
Executable file
154
siro_driver/lib/controller/payment/paymob.dart
Executable file
@@ -0,0 +1,154 @@
|
||||
import 'package:siro_driver/constant/box_name.dart';
|
||||
import 'package:dio/dio.dart' as dio;
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../../constant/api_key.dart';
|
||||
import '../../main.dart';
|
||||
import '../functions/encrypt_decrypt.dart';
|
||||
|
||||
class PaymobManager extends GetxController {
|
||||
String authanticationToken1 = "";
|
||||
String orderId1 = "";
|
||||
|
||||
Future<String> getPaymentKey(int amount, String currency) async {
|
||||
try {
|
||||
String authanticationToken = await _getAuthanticationToken();
|
||||
|
||||
int orderId = await _getOrderId(
|
||||
authanticationToken: authanticationToken,
|
||||
amount: (100 * amount).toString(),
|
||||
currency: currency,
|
||||
);
|
||||
|
||||
String paymentKey = await _getPaymentKey(
|
||||
authanticationToken: authanticationToken,
|
||||
amount: (100 * amount).toString(),
|
||||
currency: currency,
|
||||
orderId: orderId.toString(),
|
||||
);
|
||||
authanticationToken1 = authanticationToken.toString();
|
||||
orderId1 = orderId.toString();
|
||||
update();
|
||||
return paymentKey;
|
||||
} catch (e) {
|
||||
throw Exception();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> payWithPayMob(int amount, String currency) async {
|
||||
// 1. Fetch Payment Key (Assuming PaymobManager is a custom class)
|
||||
String paymentToken;
|
||||
try {
|
||||
paymentToken = await PaymobManager().getPaymentKey(amount, currency);
|
||||
} on Exception catch (e) {
|
||||
// Handle errors gracefully, e.g., display error message to user
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Prepare Payment Data Payload
|
||||
final Map<String, dynamic> data = {
|
||||
"source": {
|
||||
"identifier": "01010101010", // Replace with actual source identifier
|
||||
"subtype": "WALLET",
|
||||
},
|
||||
"payment_token": paymentToken,
|
||||
};
|
||||
|
||||
// 3. Make Payment Request using Dio
|
||||
final dio = Dio();
|
||||
try {
|
||||
final response = await dio.post(
|
||||
'https://accept.paymobsolutions.com/api/acceptance/payments/pay',
|
||||
data: data,
|
||||
);
|
||||
|
||||
// 4. Handle Payment Response
|
||||
if (response.statusCode == 200) {
|
||||
final paymentData = response.data; // Assuming JSON response
|
||||
|
||||
// Navigate to success screen or display success message
|
||||
launchUrl(Uri.parse(paymentData['iframe_redirection_url']));
|
||||
} else {
|
||||
// Payment failed: Handle errors (e.g., display error message)
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
// Handle network or Dio-related errors
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _getStatusAfterPaid() async {
|
||||
final dio.Response response = await Dio().post(
|
||||
"https://accept.paymob.com/api/ecommerce/orders/transaction_inquiry",
|
||||
data: {
|
||||
"auth_token": authanticationToken1,
|
||||
"merchant_order_id": "970960",
|
||||
"order_id": orderId1
|
||||
});
|
||||
return response.data["success"];
|
||||
}
|
||||
|
||||
Future<String> _getAuthanticationToken() async {
|
||||
final dio.Response response =
|
||||
await Dio().post("https://accept.paymob.com/api/auth/tokens", data: {
|
||||
"api_key": AK.payMobApikey,
|
||||
'username': AK.usernamePayMob,
|
||||
"password": AK.passwordPayMob,
|
||||
});
|
||||
return response.data["token"];
|
||||
}
|
||||
|
||||
Future<int> _getOrderId({
|
||||
required String authanticationToken,
|
||||
required String amount,
|
||||
required String currency,
|
||||
}) async {
|
||||
final dio.Response response = await Dio()
|
||||
.post("https://accept.paymob.com/api/ecommerce/orders", data: {
|
||||
"auth_token": authanticationToken,
|
||||
"amount_cents": amount,
|
||||
"currency": currency,
|
||||
"delivery_needed": "false",
|
||||
"items": [],
|
||||
});
|
||||
return response.data["id"];
|
||||
}
|
||||
|
||||
Future<String> _getPaymentKey({
|
||||
required String authanticationToken,
|
||||
required String orderId,
|
||||
required String amount,
|
||||
required String currency,
|
||||
}) async {
|
||||
final dio.Response response = await Dio()
|
||||
.post("https://accept.paymob.com/api/acceptance/payment_keys", data: {
|
||||
"expiration": 200,
|
||||
"auth_token": authanticationToken.toString(),
|
||||
"order_id": orderId.toString(),
|
||||
"integration_id":
|
||||
4556056, ////todo wallet or online card int.parse(AK.integrationIdPayMob),
|
||||
"lock_order_when_paid": "false",
|
||||
"amount_cents": amount,
|
||||
"currency": currency,
|
||||
"billing_data": {
|
||||
"first_name": (box.read(BoxName.nameDriver)),
|
||||
"last_name": (box.read(BoxName.lastNameDriver)),
|
||||
"email": (box.read(BoxName.emailDriver)),
|
||||
"phone_number": (box.read(BoxName.phoneDriver)),
|
||||
"apartment": "NA",
|
||||
"floor": "NA",
|
||||
"street": "NA",
|
||||
"building": "NA",
|
||||
"shipping_method": "NA",
|
||||
"postal_code": "NA",
|
||||
"city": "NA",
|
||||
"country": box.read(BoxName.countryCode),
|
||||
"state": "NA"
|
||||
},
|
||||
});
|
||||
|
||||
return response.data["token"];
|
||||
}
|
||||
}
|
||||
326
siro_driver/lib/controller/payment/paymob/paymob_response.dart
Executable file
326
siro_driver/lib/controller/payment/paymob/paymob_response.dart
Executable file
@@ -0,0 +1,326 @@
|
||||
import 'package:siro_driver/constant/box_name.dart';
|
||||
import 'package:siro_driver/controller/functions/encrypt_decrypt.dart';
|
||||
import 'package:siro_driver/main.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
class PaymobResponse {
|
||||
bool success;
|
||||
String? transactionID;
|
||||
String? responseCode;
|
||||
String? message;
|
||||
|
||||
PaymobResponse({
|
||||
this.transactionID,
|
||||
required this.success,
|
||||
this.responseCode,
|
||||
this.message,
|
||||
});
|
||||
|
||||
factory PaymobResponse.fromJson(Map<String, dynamic> json) {
|
||||
return PaymobResponse(
|
||||
success: json['success'] == 'true',
|
||||
transactionID: json['id'],
|
||||
message: json['message'],
|
||||
responseCode: json['txn_response_code'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PaymobPayment {
|
||||
static PaymobPayment instance = PaymobPayment();
|
||||
|
||||
bool _isInitialized = false;
|
||||
|
||||
final Dio _dio = Dio();
|
||||
final _baseURL = 'https://accept.paymob.com/api/';
|
||||
late String _apiKey;
|
||||
late int _integrationID;
|
||||
late int _iFrameID;
|
||||
late String _iFrameURL;
|
||||
late int _userTokenExpiration;
|
||||
|
||||
/// Initializing PaymobPayment instance.
|
||||
Future<bool> initialize({
|
||||
/// It is a unique identifier for the merchant which used to authenticate your requests calling any of Accept's API.
|
||||
/// from dashboard Select Settings -> Account Info -> API Key
|
||||
required String apiKey,
|
||||
|
||||
/// from dashboard Select Developers -> Payment Integrations -> Online Card ID
|
||||
required int integrationID,
|
||||
|
||||
/// from paymob Select Developers -> iframes
|
||||
required int iFrameID,
|
||||
|
||||
/// The expiration time of this payment token in seconds. (The maximum is 3600 seconds which is an hour)
|
||||
int userTokenExpiration = 300,
|
||||
}) async {
|
||||
if (_isInitialized) {
|
||||
return true;
|
||||
}
|
||||
_dio.options.baseUrl = _baseURL;
|
||||
_dio.options.validateStatus = (status) => true;
|
||||
_apiKey = apiKey;
|
||||
_integrationID = integrationID;
|
||||
_iFrameID = iFrameID;
|
||||
_iFrameURL =
|
||||
'https://accept.paymobsolutions.com/api/acceptance/iframes/$_iFrameID?payment_token=';
|
||||
_isInitialized = true;
|
||||
_userTokenExpiration = userTokenExpiration;
|
||||
return _isInitialized;
|
||||
}
|
||||
|
||||
/// Get authentication token, which is valid for one hour from the creation time.
|
||||
Future<String> _getAuthToken() async {
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'auth/tokens',
|
||||
data: {
|
||||
'api_key': _apiKey,
|
||||
},
|
||||
);
|
||||
|
||||
return response.data['token'];
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// At this step, you will register an order to Accept's database, so that you can pay for it later using a transaction
|
||||
Future<int> _addOrder({
|
||||
required String authToken,
|
||||
required String currency,
|
||||
required String amount,
|
||||
required List items,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'ecommerce/orders',
|
||||
data: {
|
||||
"auth_token": authToken,
|
||||
"delivery_needed": "false",
|
||||
"amount_cents": amount,
|
||||
"currency": currency,
|
||||
"items": items,
|
||||
},
|
||||
);
|
||||
|
||||
return response.data['id'];
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// At this step, you will obtain a payment_key token. This key will be used to authenticate your payment request. It will be also used for verifying your transaction request metadata.
|
||||
Future<String> _getPurchaseToken({
|
||||
required String authToken,
|
||||
required String currency,
|
||||
required int orderID,
|
||||
required String amount,
|
||||
required PaymobBillingData billingData,
|
||||
}) async {
|
||||
final response = await _dio.post(
|
||||
'acceptance/payment_keys',
|
||||
data: {
|
||||
"auth_token": authToken,
|
||||
"amount_cents": amount,
|
||||
"expiration": _userTokenExpiration,
|
||||
"order_id": orderID,
|
||||
"billing_data": billingData,
|
||||
"currency": currency,
|
||||
"integration_id": _integrationID,
|
||||
"lock_order_when_paid": "false"
|
||||
},
|
||||
);
|
||||
final message = response.data['message'];
|
||||
if (message != null) {
|
||||
throw Exception(message);
|
||||
}
|
||||
return response.data['token'];
|
||||
}
|
||||
|
||||
/// Proceed to pay with only calling this function.
|
||||
/// Opens a WebView at Paymob redirectedURL to accept user payment info.
|
||||
Future<PaymobResponse?> pay(
|
||||
{
|
||||
/// BuildContext for navigation to WebView
|
||||
required BuildContext context,
|
||||
|
||||
/// Which Currency you would pay in.
|
||||
required String currency,
|
||||
|
||||
/// Payment amount in cents EX: 20000 is an 200 EGP
|
||||
required String amountInCents,
|
||||
|
||||
/// Optional Callback if you can use return result of pay function or use this callback
|
||||
void Function(PaymobResponse response)? onPayment,
|
||||
|
||||
/// list of json objects contains the contents of the purchase.
|
||||
List? items,
|
||||
|
||||
/// The billing data related to the customer related to this payment.
|
||||
PaymobBillingData? billingData}) async {
|
||||
if (!_isInitialized) {
|
||||
throw Exception(
|
||||
'PaymobPayment is not initialized call:`PaymobPayment.instance.initialize`');
|
||||
}
|
||||
final authToken = await _getAuthToken();
|
||||
final orderID = await _addOrder(
|
||||
authToken: authToken,
|
||||
currency: currency,
|
||||
amount: amountInCents,
|
||||
items: items ?? [],
|
||||
);
|
||||
final purchaseToken = await _getPurchaseToken(
|
||||
authToken: authToken,
|
||||
currency: currency,
|
||||
orderID: orderID,
|
||||
amount: amountInCents,
|
||||
billingData: billingData ?? PaymobBillingData(),
|
||||
);
|
||||
if (context.mounted) {
|
||||
final response = await PaymobIFrame.show(
|
||||
context: context,
|
||||
redirectURL: _iFrameURL + purchaseToken,
|
||||
onPayment: onPayment,
|
||||
);
|
||||
return response;
|
||||
}
|
||||
return null;
|
||||
} //51624
|
||||
}
|
||||
|
||||
class PaymobBillingData {
|
||||
String? email;
|
||||
String? firstName;
|
||||
String? lastName;
|
||||
String? phoneNumber;
|
||||
String? apartment;
|
||||
String? floor;
|
||||
String? street;
|
||||
String? building;
|
||||
String? postalCode;
|
||||
String? city;
|
||||
String? state;
|
||||
String? country;
|
||||
String? shippingMethod;
|
||||
|
||||
PaymobBillingData({
|
||||
this.email,
|
||||
this.firstName,
|
||||
this.lastName,
|
||||
this.phoneNumber,
|
||||
this.apartment,
|
||||
this.floor,
|
||||
this.street,
|
||||
this.building,
|
||||
this.postalCode,
|
||||
this.city,
|
||||
this.state,
|
||||
this.country,
|
||||
this.shippingMethod,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
"email": (box.read(BoxName.emailDriver)),
|
||||
"first_name": box.read(BoxName.nameDriver),
|
||||
"last_name": box.read(BoxName.nameDriver),
|
||||
"phone_number": (box.read(BoxName.phoneDriver)),
|
||||
"apartment": apartment ?? "NA",
|
||||
"floor": floor ?? "NA",
|
||||
"building": building ?? "NA",
|
||||
"street": street ?? "NA",
|
||||
"postal_code": postalCode ?? "NA",
|
||||
"city": city ?? "NA",
|
||||
"state": state ?? "NA",
|
||||
"country": country ?? "NA",
|
||||
"shipping_method": shippingMethod ?? "NA",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class PaymobIFrame extends StatefulWidget {
|
||||
const PaymobIFrame({
|
||||
Key? key,
|
||||
required this.redirectURL,
|
||||
this.onPayment,
|
||||
}) : super(key: key);
|
||||
|
||||
final String redirectURL;
|
||||
final void Function(PaymobResponse)? onPayment;
|
||||
|
||||
static Future<PaymobResponse?> show({
|
||||
required BuildContext context,
|
||||
required String redirectURL,
|
||||
void Function(PaymobResponse)? onPayment,
|
||||
}) =>
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) {
|
||||
return PaymobIFrame(
|
||||
onPayment: onPayment,
|
||||
redirectURL: redirectURL,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@override
|
||||
State<PaymobIFrame> createState() => _PaymobIFrameState();
|
||||
}
|
||||
|
||||
class _PaymobIFrameState extends State<PaymobIFrame> {
|
||||
WebViewController? controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setNavigationDelegate(
|
||||
NavigationDelegate(
|
||||
onNavigationRequest: (NavigationRequest request) {
|
||||
if (request.url.contains('txn_response_code') &&
|
||||
request.url.contains('success') &&
|
||||
request.url.contains('id')) {
|
||||
final params = _getParamFromURL(request.url);
|
||||
final response = PaymobResponse.fromJson(params);
|
||||
if (widget.onPayment != null) {
|
||||
widget.onPayment!(response);
|
||||
}
|
||||
Navigator.pop(context, response);
|
||||
return NavigationDecision.prevent;
|
||||
}
|
||||
return NavigationDecision.navigate;
|
||||
},
|
||||
),
|
||||
)
|
||||
..loadRequest(Uri.parse(widget.redirectURL));
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: controller == null
|
||||
? const Center(
|
||||
child: CircularProgressIndicator.adaptive(),
|
||||
)
|
||||
: SafeArea(
|
||||
child: WebViewWidget(
|
||||
controller: controller!,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _getParamFromURL(String url) {
|
||||
final uri = Uri.parse(url);
|
||||
Map<String, dynamic> data = {};
|
||||
uri.queryParameters.forEach((key, value) {
|
||||
data[key] = value;
|
||||
});
|
||||
return data;
|
||||
}
|
||||
}
|
||||
470
siro_driver/lib/controller/payment/paymob/paymob_wallet.dart
Executable file
470
siro_driver/lib/controller/payment/paymob/paymob_wallet.dart
Executable file
@@ -0,0 +1,470 @@
|
||||
import 'package:siro_driver/constant/box_name.dart';
|
||||
import 'package:siro_driver/controller/functions/encrypt_decrypt.dart';
|
||||
import 'package:siro_driver/main.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
import '../../../print.dart';
|
||||
|
||||
class PaymobResponseWallet {
|
||||
final bool success;
|
||||
final String? transactionID;
|
||||
final String? responseCode;
|
||||
final String? message;
|
||||
|
||||
PaymobResponseWallet({
|
||||
required this.success,
|
||||
this.transactionID,
|
||||
this.responseCode,
|
||||
this.message,
|
||||
});
|
||||
|
||||
factory PaymobResponseWallet.fromJson(Map<String, dynamic> json) {
|
||||
return PaymobResponseWallet(
|
||||
success: json['success'] == 'true',
|
||||
transactionID: json['id'],
|
||||
responseCode: json['txn_response_code'],
|
||||
message: json['data']['message'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PaymobPaymentWallet {
|
||||
static PaymobPaymentWallet instance = PaymobPaymentWallet();
|
||||
|
||||
bool _isInitializedWallet = false;
|
||||
|
||||
final Dio _dio = Dio();
|
||||
final _baseURL = 'https://accept.paymob.com/api/';
|
||||
late String _apiKey;
|
||||
late int _integrationID;
|
||||
late int _iFrameID;
|
||||
late String _iFrameURL;
|
||||
late int _userTokenExpiration;
|
||||
|
||||
/// Initializing PaymobPayment instance.
|
||||
Future<bool> initialize({
|
||||
/// It is a unique identifier for the merchant which used to authenticate your requests calling any of Accept's API.
|
||||
/// from dashboard Select Settings -> Account Info -> API Key
|
||||
required String apiKey,
|
||||
|
||||
/// from dashboard Select Developers -> Payment Integrations -> Online Card ID
|
||||
required int integrationID,
|
||||
|
||||
/// from paymob Select Developers -> iframes
|
||||
required int iFrameID,
|
||||
|
||||
/// The expiration time of this payment token in seconds. (The maximum is 3600 seconds which is an hour)
|
||||
int userTokenExpiration = 300,
|
||||
}) async {
|
||||
if (_isInitializedWallet) {
|
||||
return true;
|
||||
}
|
||||
_dio.options.baseUrl = _baseURL;
|
||||
_dio.options.validateStatus = (status) => true;
|
||||
_apiKey = apiKey;
|
||||
_integrationID = integrationID;
|
||||
_iFrameID = iFrameID;
|
||||
_iFrameURL =
|
||||
'https://accept.paymobsolutions.com/api/acceptance/iframes/$_iFrameID?payment_token=';
|
||||
_isInitializedWallet = true;
|
||||
_userTokenExpiration = userTokenExpiration;
|
||||
return _isInitializedWallet;
|
||||
}
|
||||
|
||||
/// Get authentication token, which is valid for one hour from the creation time.
|
||||
Future<String> _getAuthToken() async {
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'auth/tokens',
|
||||
data: {
|
||||
'api_key': _apiKey,
|
||||
},
|
||||
);
|
||||
return response.data['token'];
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// At this step, you will register an order to Accept's database, so that you can pay for it later using a transaction
|
||||
Future<int> _addOrder({
|
||||
required String authToken,
|
||||
required String currency,
|
||||
required String amount,
|
||||
required List items,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'ecommerce/orders',
|
||||
data: {
|
||||
"auth_token": authToken,
|
||||
"delivery_needed": "false",
|
||||
"amount_cents": amount,
|
||||
"currency": currency,
|
||||
"items": items,
|
||||
},
|
||||
);
|
||||
return response.data['id'];
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// At this step, you will obtain a payment_key token. This key will be used to authenticate your payment request. It will be also used for verifying your transaction request metadata.
|
||||
Future<String> _getPurchaseToken({
|
||||
required String authToken,
|
||||
required String currency,
|
||||
required int orderID,
|
||||
required String amount,
|
||||
required PaymobBillingDataWallet billingData,
|
||||
}) async {
|
||||
final response = await _dio.post(
|
||||
'acceptance/payment_keys',
|
||||
data: {
|
||||
"auth_token": authToken,
|
||||
"amount_cents": amount,
|
||||
"expiration": _userTokenExpiration,
|
||||
"order_id": orderID,
|
||||
"billing_data": billingData,
|
||||
"currency": currency,
|
||||
"integration_id": _integrationID,
|
||||
"lock_order_when_paid": "false"
|
||||
},
|
||||
);
|
||||
// final message = response.data['message'];
|
||||
// if (message != null) {
|
||||
// throw Exception(message);
|
||||
// }
|
||||
return response.data['token'];
|
||||
}
|
||||
|
||||
Future<String> _getWalletUrl({
|
||||
required String paymentToken,
|
||||
}) async {
|
||||
final Map<String, dynamic> data = {
|
||||
"source": {
|
||||
"identifier": box.read(BoxName.phoneWallet).toString(),
|
||||
"subtype": "WALLET",
|
||||
},
|
||||
"payment_token": paymentToken,
|
||||
};
|
||||
final dio = Dio();
|
||||
try {
|
||||
final response = await dio.post(
|
||||
'https://accept.paymobsolutions.com/api/acceptance/payments/pay',
|
||||
data: data,
|
||||
);
|
||||
|
||||
// 4. Handle Payment Response
|
||||
if (response.statusCode == 200) {
|
||||
final paymentData = response.data; // Assuming JSON response
|
||||
|
||||
return paymentData['iframe_redirection_url'];
|
||||
// Navigate to success screen or display success message
|
||||
} else {
|
||||
// Payment failed: Handle errors (e.g., display error message)
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
// Handle network or Dio-related errors
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/// Proceed to pay with only calling this function.
|
||||
/// Opens a WebView at Paymob redirectedURL to accept user payment info.
|
||||
Future<PaymobResponseWallet?> pay(
|
||||
{
|
||||
/// BuildContext for navigation to WebView
|
||||
required BuildContext context,
|
||||
|
||||
/// Which Currency you would pay in.
|
||||
required String currency,
|
||||
|
||||
/// Payment amount in cents EX: 20000 is an 200 EGP
|
||||
required String amountInCents,
|
||||
|
||||
/// Optional Callback if you can use return result of pay function or use this callback
|
||||
void Function(PaymobResponseWallet response)? onPayment,
|
||||
|
||||
/// list of json objects contains the contents of the purchase.
|
||||
List? items,
|
||||
|
||||
/// The billing data related to the customer related to this payment.
|
||||
PaymobBillingDataWallet? billingData}) async {
|
||||
if (!_isInitializedWallet) {
|
||||
throw Exception(
|
||||
'PaymobPayment is not initialized call:`PaymobPayment.instance.initialize`');
|
||||
}
|
||||
final authToken = await _getAuthToken();
|
||||
final orderID = await _addOrder(
|
||||
authToken: authToken,
|
||||
currency: currency,
|
||||
amount: amountInCents,
|
||||
items: items ?? [],
|
||||
);
|
||||
final purchaseToken = await _getPurchaseToken(
|
||||
authToken: authToken,
|
||||
currency: currency,
|
||||
orderID: orderID,
|
||||
amount: amountInCents,
|
||||
billingData: billingData ??
|
||||
PaymobBillingDataWallet(
|
||||
// email: box.read(BoxName.email) ?? box.read(BoxName.emailDriver),
|
||||
// firstName: box.read(BoxName.name) ?? box.read(BoxName.nameDriver),
|
||||
// lastName:
|
||||
// box.read(BoxName.lastNameDriver) ?? box.read(BoxName.name),
|
||||
// phoneNumber:
|
||||
// box.read(BoxName.phone) ?? box.read(BoxName.phoneDriver),
|
||||
),
|
||||
);
|
||||
final urlWallet = await _getWalletUrl(paymentToken: purchaseToken);
|
||||
Log.print('urlWallet: ${urlWallet}');
|
||||
|
||||
if (context.mounted) {
|
||||
final response = await PaymobIFrameWallet.show(
|
||||
context: context,
|
||||
redirectURL: urlWallet,
|
||||
onPayment: onPayment,
|
||||
);
|
||||
return response;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class PaymobBillingDataWallet {
|
||||
String? email;
|
||||
String? firstName;
|
||||
String? lastName;
|
||||
String? phoneNumber;
|
||||
String? apartment;
|
||||
String? floor;
|
||||
String? street;
|
||||
String? building;
|
||||
String? postalCode;
|
||||
String? city;
|
||||
String? state;
|
||||
String? country;
|
||||
String? shippingMethod;
|
||||
|
||||
PaymobBillingDataWallet({
|
||||
this.email,
|
||||
this.firstName,
|
||||
this.lastName,
|
||||
this.phoneNumber,
|
||||
this.apartment,
|
||||
this.floor,
|
||||
this.street,
|
||||
this.building,
|
||||
this.postalCode,
|
||||
this.city,
|
||||
this.state,
|
||||
this.country,
|
||||
this.shippingMethod,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
"email": (box.read(BoxName.emailDriver)),
|
||||
"first_name": box.read(BoxName.name) ?? box.read(BoxName.nameDriver),
|
||||
"last_name": box.read(BoxName.name) ?? box.read(BoxName.nameDriver),
|
||||
"phone_number": (box.read(BoxName.phoneWallet)),
|
||||
"apartment": apartment ?? "NA",
|
||||
"floor": floor ?? "NA",
|
||||
"building": building ?? "NA",
|
||||
"street": street ?? "NA",
|
||||
"postal_code": postalCode ?? "NA",
|
||||
"city": city ?? "NA",
|
||||
"state": state ?? "NA",
|
||||
"country": country ?? "NA",
|
||||
"shipping_method": shippingMethod ?? "NA",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class PaymobIFrameWallet extends StatefulWidget {
|
||||
const PaymobIFrameWallet({
|
||||
Key? key,
|
||||
required this.redirectURL,
|
||||
this.onPayment,
|
||||
}) : super(key: key);
|
||||
|
||||
final String redirectURL;
|
||||
final void Function(PaymobResponseWallet)? onPayment;
|
||||
|
||||
static Future<PaymobResponseWallet?> show({
|
||||
required BuildContext context,
|
||||
required String redirectURL,
|
||||
void Function(PaymobResponseWallet)? onPayment,
|
||||
}) =>
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) {
|
||||
return PaymobIFrameWallet(
|
||||
onPayment: onPayment,
|
||||
redirectURL: redirectURL,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@override
|
||||
State<PaymobIFrameWallet> createState() => _PaymobIFrameState();
|
||||
}
|
||||
|
||||
// class _PaymobIFrameState extends State<PaymobIFrameWallet> {
|
||||
// WebViewController? controller;
|
||||
|
||||
// @override
|
||||
// void initState() {
|
||||
// controller = WebViewController()
|
||||
// ..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
// ..setNavigationDelegate(
|
||||
// NavigationDelegate(
|
||||
// onNavigationRequest: (NavigationRequest request) {
|
||||
// Log.print('request.url: ${request.url}');
|
||||
// if (request.url.contains('txn_response_code') &&
|
||||
// request.url.contains('success') &&
|
||||
// request.url.contains('id')) {
|
||||
// final params = _getParamFromURL(request.url);
|
||||
// final response = PaymobResponseWallet.fromJson(params);
|
||||
// if (widget.onPayment != null) {
|
||||
// widget.onPayment!(response);
|
||||
// }
|
||||
// Navigator.pop(context, response);
|
||||
// return NavigationDecision.prevent;
|
||||
// }
|
||||
// return NavigationDecision.navigate;
|
||||
// },
|
||||
// ),
|
||||
// )
|
||||
// ..loadRequest(Uri.parse(widget.redirectURL));
|
||||
// super.initState();
|
||||
// }
|
||||
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// return Scaffold(
|
||||
// body: controller == null
|
||||
// ? const Center(
|
||||
// child: CircularProgressIndicator.adaptive(),
|
||||
// )
|
||||
// : SafeArea(
|
||||
// child: WebViewWidget(
|
||||
// controller: controller!,
|
||||
// ),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
|
||||
// Map<String, dynamic> _getParamFromURL(String url) {
|
||||
// final uri = Uri.parse(url);
|
||||
// final queryParams = uri.queryParameters;
|
||||
// final data = <String, dynamic>{};
|
||||
|
||||
// queryParams.forEach((key, value) {
|
||||
// if (key.contains('.')) {
|
||||
// final parts = key.split('.');
|
||||
// data.putIfAbsent(parts.first, () => <String, dynamic>{});
|
||||
// (data[parts.first] as Map<String, dynamic>)[parts.last] = value;
|
||||
// } else {
|
||||
// data[key] = value;
|
||||
// }
|
||||
// });
|
||||
|
||||
// return data;
|
||||
// }
|
||||
// }
|
||||
|
||||
class _PaymobIFrameState extends State<PaymobIFrameWallet> {
|
||||
WebViewController? controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setNavigationDelegate(
|
||||
NavigationDelegate(
|
||||
onNavigationRequest: (NavigationRequest request) {
|
||||
Log.print('request.url: ${request.url}');
|
||||
|
||||
if (request.url.contains('txn_response_code') &&
|
||||
request.url.contains('success') &&
|
||||
request.url.contains('id')) {
|
||||
final params = _getParamFromURL(request.url);
|
||||
final response = PaymobResponseWallet.fromJson(params);
|
||||
|
||||
if (widget.onPayment != null) {
|
||||
widget.onPayment!(response);
|
||||
}
|
||||
|
||||
Navigator.pop(context, response);
|
||||
|
||||
// Show a dialog after successful payment
|
||||
// _showSuccessDialog(response);
|
||||
|
||||
return NavigationDecision.prevent;
|
||||
}
|
||||
return NavigationDecision.navigate;
|
||||
},
|
||||
),
|
||||
)
|
||||
..loadRequest(Uri.parse(widget.redirectURL));
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: controller == null
|
||||
? const Center(
|
||||
child: CircularProgressIndicator.adaptive(),
|
||||
)
|
||||
: SafeArea(
|
||||
child: WebViewWidget(
|
||||
controller: controller!,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _getParamFromURL(String url) {
|
||||
final uri = Uri.parse(url);
|
||||
final queryParams = uri.queryParameters;
|
||||
final data = <String, dynamic>{};
|
||||
|
||||
queryParams.forEach((key, value) {
|
||||
if (key.contains('.')) {
|
||||
final parts = key.split('.');
|
||||
data.putIfAbsent(parts.first, () => <String, dynamic>{});
|
||||
(data[parts.first] as Map<String, dynamic>)[parts.last] = value;
|
||||
} else {
|
||||
data[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void _showSuccessDialog(PaymobResponseWallet response) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text('Payment Successful'),
|
||||
content: Text('Transaction ID: EGP'),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(); // Close the dialog
|
||||
},
|
||||
child: Text('OK'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:siro_driver/controller/functions/crud.dart';
|
||||
|
||||
class PayoutService {
|
||||
final String _baseUrl =
|
||||
"https://walletintaleq.intaleq.xyz/v1/main/sms_webhook";
|
||||
static const double payoutFee = 5000.0; // عمولة السحب الثابتة
|
||||
|
||||
/// دالة لإنشاء طلب سحب جديد على السيرفر
|
||||
///
|
||||
/// تعيد رسالة النجاح من السيرفر، أو رسالة خطأ في حال الفشل.
|
||||
Future<String?> requestPayout({
|
||||
required String driverId,
|
||||
walletType,
|
||||
payoutPhoneNumber,
|
||||
required double amount,
|
||||
}) async {
|
||||
final url = ("$_baseUrl/request_payout.php");
|
||||
try {
|
||||
// هنا يمكنك إضافة هيدرز المصادقة (JWT) بنفس طريقتك المعتادة
|
||||
final response = await CRUD().postWallet(link: url, payload: {
|
||||
'driverId': driverId,
|
||||
'amount': amount.toString(),
|
||||
'phone': payoutPhoneNumber.toString(),
|
||||
'wallet_type': walletType.toString(),
|
||||
}).timeout(const Duration(seconds: 20));
|
||||
|
||||
if (response != 'failure') {
|
||||
final data = (response);
|
||||
if (data['status'] == 'success') {
|
||||
debugPrint("Payout request successful: ${data['message']}");
|
||||
return data['message']; // إرجاع رسالة النجاح
|
||||
} else {
|
||||
debugPrint("Payout request failed: ${data['message']}");
|
||||
return "فشل الطلب: ${data['message']}"; // إرجاع رسالة الخطأ من السيرفر
|
||||
}
|
||||
} else {
|
||||
return "خطأ في الاتصال بالسيرفر: ${response.statusCode}";
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Exception during payout request: $e");
|
||||
return "حدث خطأ غير متوقع. يرجى المحاولة مرة أخرى.";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,564 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
// تأكد من استيراد الملفات الصحيحة حسب مشروع السائق الخاص بك
|
||||
import 'package:siro_driver/constant/links.dart';
|
||||
import 'package:siro_driver/controller/functions/crud.dart';
|
||||
import '../../../constant/box_name.dart';
|
||||
import '../../../main.dart';
|
||||
// import '../../../print.dart'; // إذا كنت تستخدمه
|
||||
|
||||
// --- خدمة الدفع للسائق (نفس المنطق الخاص بالسائق) ---
|
||||
class PaymentService {
|
||||
final String _baseUrl = "${AppLink.paymentServer}/ride/shamcash";
|
||||
|
||||
Future<String?> createInvoice({required double amount}) async {
|
||||
final url = "$_baseUrl/create_invoice_shamcash.php";
|
||||
try {
|
||||
final response = await CRUD().postWallet(
|
||||
link: url,
|
||||
payload: {
|
||||
'driverID': box.read(BoxName.driverID), // استخدام driverID
|
||||
'amount': amount.toString(),
|
||||
},
|
||||
).timeout(const Duration(seconds: 15));
|
||||
|
||||
if (response != 'failure') {
|
||||
final data = response;
|
||||
if (data['status'] == 'success' && data['invoice_number'] != null) {
|
||||
return data['invoice_number'].toString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> checkInvoiceStatus(String invoiceNumber) async {
|
||||
final url = "$_baseUrl/check_status.php";
|
||||
try {
|
||||
final response = await CRUD().postWallet(link: url, payload: {
|
||||
'invoice_number': invoiceNumber,
|
||||
}).timeout(const Duration(seconds: 10));
|
||||
|
||||
if (response != 'failure') {
|
||||
final data = response;
|
||||
return data['status'] == 'success' &&
|
||||
data['invoice_status'] == 'completed';
|
||||
}
|
||||
return false;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum PaymentStatus {
|
||||
creatingInvoice,
|
||||
waitingForPayment,
|
||||
paymentSuccess,
|
||||
paymentTimeout,
|
||||
paymentError
|
||||
}
|
||||
|
||||
class PaymentScreenSmsProvider extends StatefulWidget {
|
||||
final double amount;
|
||||
final String providerName;
|
||||
final String providerLogo;
|
||||
final String qrImagePath;
|
||||
|
||||
const PaymentScreenSmsProvider({
|
||||
super.key,
|
||||
required this.amount,
|
||||
this.providerName = 'شام كاش',
|
||||
this.providerLogo = 'assets/images/shamCash.png',
|
||||
this.qrImagePath = 'assets/images/shamcashsend.png',
|
||||
});
|
||||
|
||||
@override
|
||||
_PaymentScreenSmsProviderState createState() =>
|
||||
_PaymentScreenSmsProviderState();
|
||||
}
|
||||
|
||||
class _PaymentScreenSmsProviderState extends State<PaymentScreenSmsProvider>
|
||||
with SingleTickerProviderStateMixin {
|
||||
final PaymentService _paymentService = PaymentService();
|
||||
Timer? _pollingTimer;
|
||||
PaymentStatus _status = PaymentStatus.creatingInvoice;
|
||||
String? _invoiceNumber;
|
||||
|
||||
// العنوان الثابت للدفع (كما في تطبيق الراكب)
|
||||
final String _paymentAddress = "80f23afe40499b02f49966c3340ae0fc";
|
||||
|
||||
// متغيرات الأنيميشن (الوميض)
|
||||
late AnimationController _blinkController;
|
||||
late Animation<Color?> _colorAnimation;
|
||||
late Animation<double> _shadowAnimation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// إعداد الأنيميشن (وميض أحمر)
|
||||
_blinkController = AnimationController(
|
||||
duration: const Duration(milliseconds: 800),
|
||||
vsync: this,
|
||||
)..repeat(reverse: true);
|
||||
|
||||
_colorAnimation = ColorTween(
|
||||
begin: Colors.red.shade700,
|
||||
end: Colors.red.shade100,
|
||||
).animate(_blinkController);
|
||||
|
||||
_shadowAnimation = Tween<double>(begin: 2.0, end: 15.0).animate(
|
||||
CurvedAnimation(parent: _blinkController, curve: Curves.easeInOut),
|
||||
);
|
||||
|
||||
_createAndPollInvoice();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pollingTimer?.cancel();
|
||||
_blinkController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _createAndPollInvoice() async {
|
||||
setState(() => _status = PaymentStatus.creatingInvoice);
|
||||
final invoiceNumber =
|
||||
await _paymentService.createInvoice(amount: widget.amount);
|
||||
|
||||
if (invoiceNumber != null && mounted) {
|
||||
setState(() {
|
||||
_invoiceNumber = invoiceNumber;
|
||||
_status = PaymentStatus.waitingForPayment;
|
||||
});
|
||||
_startPolling(invoiceNumber);
|
||||
} else if (mounted) {
|
||||
setState(() => _status = PaymentStatus.paymentError);
|
||||
}
|
||||
}
|
||||
|
||||
void _startPolling(String invoiceNumber) {
|
||||
const timeoutDuration = Duration(minutes: 5);
|
||||
var elapsed = Duration.zero;
|
||||
|
||||
_pollingTimer = Timer.periodic(const Duration(seconds: 5), (timer) async {
|
||||
elapsed += const Duration(seconds: 5);
|
||||
if (elapsed >= timeoutDuration) {
|
||||
timer.cancel();
|
||||
if (mounted) setState(() => _status = PaymentStatus.paymentTimeout);
|
||||
return;
|
||||
}
|
||||
final isCompleted =
|
||||
await _paymentService.checkInvoiceStatus(invoiceNumber);
|
||||
if (isCompleted && mounted) {
|
||||
timer.cancel();
|
||||
setState(() => _status = PaymentStatus.paymentSuccess);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> _onPopInvoked() async {
|
||||
if (_status == PaymentStatus.waitingForPayment) {
|
||||
return (await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('إلغاء العملية؟', textAlign: TextAlign.right),
|
||||
content: const Text(
|
||||
'الخروج الآن سيؤدي لإلغاء متابعة عملية الدفع.',
|
||||
textAlign: TextAlign.right),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('البقاء')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('خروج',
|
||||
style: TextStyle(color: Colors.red))),
|
||||
],
|
||||
),
|
||||
)) ??
|
||||
false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WillPopScope(
|
||||
onWillPop: _onPopInvoked,
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.grey[50],
|
||||
appBar: AppBar(
|
||||
title: Text("دفع عبر ${widget.providerName}"),
|
||||
centerTitle: true,
|
||||
elevation: 0,
|
||||
backgroundColor: Colors.white,
|
||||
foregroundColor: Colors.black,
|
||||
),
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Center(child: _buildContentByStatus()),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContentByStatus() {
|
||||
switch (_status) {
|
||||
case PaymentStatus.creatingInvoice:
|
||||
return const Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 20),
|
||||
Text("جاري إنشاء رقم البيان...", style: TextStyle(fontSize: 16)),
|
||||
],
|
||||
);
|
||||
case PaymentStatus.waitingForPayment:
|
||||
return _buildWaitingForPaymentUI();
|
||||
case PaymentStatus.paymentSuccess:
|
||||
return _buildSuccessUI();
|
||||
case PaymentStatus.paymentTimeout:
|
||||
case PaymentStatus.paymentError:
|
||||
return _buildErrorUI();
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildWaitingForPaymentUI() {
|
||||
final currencyFormat = NumberFormat.decimalPattern('ar_SY');
|
||||
final invoiceText = _invoiceNumber ?? '---';
|
||||
|
||||
return SingleChildScrollView(
|
||||
physics: const BouncingScrollPhysics(),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
// 1. المبلغ المطلوب
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 15),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [Colors.blue.shade800, Colors.blue.shade600]),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.blue.withOpacity(0.25),
|
||||
blurRadius: 15,
|
||||
offset: const Offset(0, 8))
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
const Text("المبلغ المطلوب شحنه",
|
||||
style: TextStyle(color: Colors.white70, fontSize: 14)),
|
||||
const SizedBox(height: 5),
|
||||
Text(
|
||||
"${currencyFormat.format(widget.amount)} ل.س",
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 28,
|
||||
fontWeight: FontWeight.bold),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 25),
|
||||
|
||||
// 2. رقم البيان (الإطار الأحمر الوامض)
|
||||
AnimatedBuilder(
|
||||
animation: _blinkController,
|
||||
builder: (context, child) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(
|
||||
color: _colorAnimation.value ?? Colors.red,
|
||||
width: 3.0, // إطار سميك
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: (_colorAnimation.value ?? Colors.red)
|
||||
.withOpacity(0.4),
|
||||
blurRadius: _shadowAnimation.value,
|
||||
spreadRadius: 2,
|
||||
)
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.warning_rounded,
|
||||
color: Colors.red.shade800, size: 28),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
"هام جداً: لا تنسَ!",
|
||||
style: TextStyle(
|
||||
color: Colors.red.shade900,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
const Text(
|
||||
"يجب نسخ (رقم البيان) هذا ووضعه في تطبيق شام كاش لضمان نجاح العملية.",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
color: Colors.black87,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 15),
|
||||
InkWell(
|
||||
onTap: () {
|
||||
Clipboard.setData(ClipboardData(text: invoiceText));
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: const Text("تم نسخ رقم البيان ✅",
|
||||
textAlign: TextAlign.center),
|
||||
backgroundColor: Colors.red.shade700,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(10)),
|
||||
margin: const EdgeInsets.all(20),
|
||||
),
|
||||
);
|
||||
},
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 15, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red.shade50,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border:
|
||||
Border.all(color: Colors.red.shade200, width: 1),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text("رقم البيان (Invoice No)",
|
||||
style: TextStyle(
|
||||
fontSize: 12, color: Colors.grey)),
|
||||
Text(invoiceText,
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 2.0,
|
||||
color: Colors.red.shade900)),
|
||||
],
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red.shade100,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Icon(Icons.copy_rounded,
|
||||
color: Colors.red.shade900, size: 24),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 25),
|
||||
|
||||
// 3. عنوان الدفع (للتسهيل على السائق)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.grey.shade300),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text("عنوان الدفع (Payment Address)",
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey)),
|
||||
const SizedBox(height: 8),
|
||||
InkWell(
|
||||
onTap: () {
|
||||
Clipboard.setData(ClipboardData(text: _paymentAddress));
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: const Text("تم نسخ عنوان الدفع ✅",
|
||||
textAlign: TextAlign.center),
|
||||
backgroundColor: Colors.green.shade600,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(10)),
|
||||
margin: const EdgeInsets.all(20),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
_paymentAddress,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontFamily: 'Courier',
|
||||
color: Colors.black87,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
const Icon(Icons.copy, size: 18, color: Colors.grey),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 30),
|
||||
|
||||
// 4. الـ QR Code
|
||||
const Text("امسح الرمز للدفع",
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87)),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => Dialog(
|
||||
backgroundColor: Colors.transparent,
|
||||
child: InteractiveViewer(
|
||||
child: Image.asset(widget.qrImagePath),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(15),
|
||||
border: Border.all(color: Colors.grey.shade300),
|
||||
),
|
||||
child: Image.asset(
|
||||
widget.qrImagePath,
|
||||
width: 150,
|
||||
height: 150,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (c, o, s) =>
|
||||
const Icon(Icons.qr_code_2, size: 100, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 40),
|
||||
|
||||
// مؤشر الانتظار
|
||||
const LinearProgressIndicator(backgroundColor: Colors.white),
|
||||
const SizedBox(height: 10),
|
||||
const Text("ننتظر إشعار الدفع تلقائياً...",
|
||||
style: TextStyle(color: Colors.grey, fontSize: 12)),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSuccessUI() {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.verified_rounded, color: Colors.green, size: 100),
|
||||
const SizedBox(height: 20),
|
||||
const Text("تم الدفع بنجاح!",
|
||||
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 10),
|
||||
const Text("تم إضافة الرصيد إلى محفظتك",
|
||||
style: TextStyle(color: Colors.grey)),
|
||||
const SizedBox(height: 40),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.green,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12))),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text("متابعة", style: TextStyle(fontSize: 18)),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildErrorUI() {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error_outline_rounded, color: Colors.red.shade400, size: 80),
|
||||
const SizedBox(height: 20),
|
||||
Text(
|
||||
_status == PaymentStatus.paymentTimeout
|
||||
? "انتهى الوقت"
|
||||
: "لم يتم التحقق",
|
||||
style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 15),
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 30),
|
||||
child: Text(
|
||||
"لم يصلنا إشعار الدفع. هل تأكدت من وضع (رقم البيان) في الملاحظات؟",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Colors.grey, height: 1.5)),
|
||||
),
|
||||
const SizedBox(height: 40),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton.icon(
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 15),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12))),
|
||||
onPressed: _createAndPollInvoice,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text("حاول مرة أخرى"),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 15),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text("إلغاء", style: TextStyle(color: Colors.grey)),
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
0
siro_driver/lib/controller/payment/stripe.dart
Executable file
0
siro_driver/lib/controller/payment/stripe.dart
Executable file
Reference in New Issue
Block a user