first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:siro_rider/views/widgets/elevated_btn.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:siro_rider/constant/box_name.dart';
|
||||
import 'package:siro_rider/constant/links.dart';
|
||||
import 'package:siro_rider/controller/functions/crud.dart';
|
||||
import 'package:siro_rider/main.dart';
|
||||
|
||||
class DriverWalletHistoryController extends GetxController {
|
||||
bool isLoading = false;
|
||||
List archive = [];
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
getArchivePayment();
|
||||
super.onInit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:siro_rider/constant/box_name.dart';
|
||||
import 'package:siro_rider/constant/links.dart';
|
||||
import 'package:siro_rider/controller/functions/crud.dart';
|
||||
import 'package:siro_rider/main.dart';
|
||||
|
||||
import '../../views/widgets/mydialoug.dart';
|
||||
|
||||
class PassengerWalletHistoryController extends GetxController {
|
||||
bool isLoading = false;
|
||||
List archive = [];
|
||||
|
||||
Future<void> getArchivePayment() async {
|
||||
try {
|
||||
isLoading = true;
|
||||
update();
|
||||
|
||||
var res = await CRUD().getWallet(
|
||||
link: AppLink.getPassengerWalletArchive,
|
||||
payload: {'passenger_id': box.read(BoxName.passengerID)},
|
||||
);
|
||||
|
||||
if (res != 'failure') {
|
||||
archive = jsonDecode(res)['message'];
|
||||
} else {
|
||||
MyDialog().getDialog('No wallet record found'.tr, '', () {
|
||||
Get.back();
|
||||
Get.back();
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// MyDialog().getDialog('An error occurred'.tr, '', () {
|
||||
// Get.back();
|
||||
// });
|
||||
} finally {
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
getArchivePayment();
|
||||
super.onInit();
|
||||
}
|
||||
}
|
||||
768
siro_rider/lib/controller/payment/payment_controller.dart
Normal file
768
siro_rider/lib/controller/payment/payment_controller.dart
Normal file
@@ -0,0 +1,768 @@
|
||||
import 'dart:convert';
|
||||
import 'package:siro_rider/constant/api_key.dart';
|
||||
import 'package:siro_rider/constant/style.dart';
|
||||
import 'package:siro_rider/controller/firebase/firbase_messge.dart';
|
||||
import 'package:siro_rider/controller/payment/paymob/paymob_response.dart';
|
||||
import 'package:siro_rider/views/home/map_page_passenger.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_rider/controller/home/map/ride_lifecycle_controller.dart';
|
||||
import 'package:siro_rider/controller/home/map/ride_state.dart';
|
||||
import 'package:siro_rider/controller/home/map/ride_state.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/colors.dart';
|
||||
import '../../constant/info.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../firebase/notification_service.dart';
|
||||
import '../functions/crud.dart';
|
||||
import '../functions/encrypt_decrypt.dart';
|
||||
import '../functions/toast.dart';
|
||||
import 'paymob/e_cash_screen.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();
|
||||
final walletphoneController = TextEditingController();
|
||||
double totalPassenger = Get.find<RideLifecycleController>().totalPassenger;
|
||||
int? selectedAmount = 0;
|
||||
List<dynamic> totalPassengerWalletDetails = [];
|
||||
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();
|
||||
}
|
||||
|
||||
String paymentToken = '';
|
||||
Future<String> generateTokenPassenger(String amount) async {
|
||||
var res =
|
||||
await CRUD().post(link: AppLink.addPaymentTokenPassenger, payload: {
|
||||
'passengerId': box.read(BoxName.passengerID).toString(),
|
||||
'amount': amount.toString(),
|
||||
});
|
||||
var d = jsonDecode(res);
|
||||
return d['message'];
|
||||
}
|
||||
|
||||
Future<String> generateTokenDriver(String amount) async {
|
||||
var res = await CRUD().post(link: AppLink.addPaymentTokenDriver, payload: {
|
||||
'driverID': Get.find<RideLifecycleController>().driverId,
|
||||
'amount': amount.toString(),
|
||||
});
|
||||
var d = jsonDecode(res);
|
||||
return d['message'];
|
||||
}
|
||||
|
||||
Future addSeferWallet(String paymentMethod, point) async {
|
||||
var seferToken = await generateTokenPassenger(point);
|
||||
await CRUD().postWallet(link: AppLink.addSeferWallet, payload: {
|
||||
'amount': point.toString(),
|
||||
'paymentMethod': paymentMethod,
|
||||
'passengerId': box.read(BoxName.passengerID).toString(),
|
||||
'token': seferToken,
|
||||
'driverId': 'passenger',
|
||||
});
|
||||
}
|
||||
|
||||
Future addPassengersWallet(String point) async {
|
||||
var token = await generateTokenPassenger(point);
|
||||
await CRUD().postWallet(link: AppLink.addPassengersWallet, payload: {
|
||||
'passenger_id': box.read(BoxName.passengerID).toString(),
|
||||
'balance': point,
|
||||
'token': token,
|
||||
});
|
||||
}
|
||||
|
||||
payToDriverForCancelAfterAppliedAndHeNearYou(String rideId) async {
|
||||
{
|
||||
double costOfWaiting5Minute = box.read(BoxName.countryCode) == 'Egypt'
|
||||
? (4 * .08) + (5 * 1)
|
||||
// 4 indicate foe 4 km ditance from driver start move to passenger
|
||||
: (4 * .06) + (5 * .06); //for Eygpt other like jordan .06 per minute
|
||||
var paymentTokenWait =
|
||||
await generateTokenDriver(costOfWaiting5Minute.toString());
|
||||
var res =
|
||||
await CRUD().postWallet(link: AppLink.addDrivePayment, payload: {
|
||||
'rideId': rideId,
|
||||
'amount': costOfWaiting5Minute.toString(),
|
||||
'payment_method': 'cancel-from-near',
|
||||
'passengerID': box.read(BoxName.passengerID).toString(),
|
||||
'token': paymentTokenWait,
|
||||
'driverID': Get.find<RideLifecycleController>().driverId.toString(),
|
||||
});
|
||||
var paymentTokenWait1 =
|
||||
await generateTokenDriver(costOfWaiting5Minute.toString());
|
||||
var res1 = await CRUD()
|
||||
.postWallet(link: AppLink.addDriversWalletPoints, payload: {
|
||||
'paymentID': 'rideId$rideId',
|
||||
'amount': (costOfWaiting5Minute).toStringAsFixed(0),
|
||||
'paymentMethod': 'cancel-from-near',
|
||||
'token': paymentTokenWait1,
|
||||
'driverID': Get.find<RideLifecycleController>().driverId.toString(),
|
||||
});
|
||||
|
||||
if (res != 'failure') {
|
||||
// Get.find<FirebaseMessagesController>().sendNotificationToDriverMAP(
|
||||
// 'Cancel',
|
||||
// 'Trip Cancelled. The cost of the trip will be added to your wallet.'
|
||||
// .tr,
|
||||
// Get.find<RideLifecycleController>().driverToken,
|
||||
// [],
|
||||
// 'cancel',
|
||||
// );
|
||||
await NotificationService.sendNotification(
|
||||
category: 'Cancel',
|
||||
target: Get.find<RideLifecycleController>().driverToken.toString(),
|
||||
title: 'Cancel'.tr,
|
||||
body:
|
||||
'Trip Cancelled. The cost of the trip will be added to your wallet.'
|
||||
.tr,
|
||||
isTopic: false, // Important: this is a token
|
||||
tone: 'cancel',
|
||||
driverList: [],
|
||||
);
|
||||
}
|
||||
var paymentTokenWaitPassenger1 =
|
||||
await generateTokenPassenger((costOfWaiting5Minute * -1).toString());
|
||||
await CRUD().post(link: AppLink.addPassengersWallet, payload: {
|
||||
'passenger_id': box.read(BoxName.passengerID).toString(),
|
||||
'balance': (costOfWaiting5Minute * -1).toString(),
|
||||
'token': paymentTokenWaitPassenger1,
|
||||
});
|
||||
Get.offAll(const MapPagePassenger());
|
||||
}
|
||||
}
|
||||
|
||||
addPassengerWallet() async {
|
||||
isLoading = true;
|
||||
update();
|
||||
|
||||
await addSeferWallet('visa-in', selectedAmount.toString());
|
||||
await addPassengersWallet(selectedAmount == 100
|
||||
? '100'
|
||||
: selectedAmount == 200
|
||||
? '215'
|
||||
: selectedAmount == 400
|
||||
? '450'
|
||||
: selectedAmount == 1000
|
||||
? '1140'
|
||||
: '0');
|
||||
|
||||
// getPassengerWallet();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void applyPromoCodeToPassenger() async {
|
||||
//TAWJIHI
|
||||
CRUD().get(link: AppLink.getPassengersPromo, payload: {
|
||||
'promo_code': promo.text,
|
||||
}).then((value) {
|
||||
var decod = jsonDecode(value);
|
||||
|
||||
if (decod["status"] == "success") {
|
||||
var firstElement = decod["message"][0];
|
||||
totalPassenger = totalPassenger -
|
||||
(totalPassenger * int.parse(firstElement['amount']));
|
||||
Get.find<RideLifecycleController>().promoTaken = true;
|
||||
update();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 'https://accept.paymob.com/unifiedcheckout/?publicKey=egy_pk_live_mbjDC9Ni6FSHKmsz8sOHiVk2xd7oWRve&clientSecret=egy_sk_live_c0904e9cf04506ae64f818d4e075b4a957e3713fdf7a22cb7da30a29e72442b5'
|
||||
// أضف هذا الرابط إلى ملف AppLink الخاص بك
|
||||
|
||||
// هذه هي الدالة الجديدة التي ستستخدمها لبدء الدفع
|
||||
Future<void> payWithEcash(BuildContext context, String amount) async {
|
||||
try {
|
||||
// 1. يمكنك استخدام نفس طريقة التحقق بالبصمة إذا أردت
|
||||
bool isAvailable = await LocalAuthentication().isDeviceSupported();
|
||||
if (isAvailable) {
|
||||
bool didAuthenticate = await LocalAuthentication().authenticate(
|
||||
localizedReason: 'Use Touch ID or Face ID to confirm payment',
|
||||
);
|
||||
|
||||
if (didAuthenticate) {
|
||||
// 2. استدعاء الـ Endpoint الجديد على السيرفر الخاص بك
|
||||
var res = await CRUD().postWallet(
|
||||
link: AppLink.payWithEcashPassenger,
|
||||
payload: {
|
||||
// ✅ أرسل البيانات التي يحتاجها السيرفر الخاص بـ ecash
|
||||
"amount": amount,
|
||||
"passengerId": box.read(BoxName.passengerID),
|
||||
},
|
||||
);
|
||||
|
||||
// 3. التأكد من أن السيرفر أعاد رابط الدفع بنجاح
|
||||
if (res != null &&
|
||||
res['status'] == 'success' &&
|
||||
res['message'] != null) {
|
||||
final String paymentUrl = res['message'];
|
||||
// 4. الانتقال إلى شاشة الدفع الجديدة الخاصة بـ ecash
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) =>
|
||||
EcashPaymentScreen(paymentUrl: paymentUrl),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
// عرض رسالة خطأ في حال فشل السيرفر في إنشاء الرابط
|
||||
Get.defaultDialog(
|
||||
title: 'Error'.tr,
|
||||
content: Text(
|
||||
'Failed to initiate payment. Please try again.'.tr,
|
||||
style: AppStyle.title,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
Get.defaultDialog(
|
||||
title: 'Error'.tr,
|
||||
content: Text(
|
||||
'An error occurred during the payment process.'.tr,
|
||||
style: AppStyle.title,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Future<void> payWithEcashDriver(BuildContext context, String amount) async {
|
||||
// try {
|
||||
// // يمكنك استخدام نفس طريقة التحقق بالبصمة إذا أردت
|
||||
// bool isAvailable = await LocalAuthentication().isDeviceSupported();
|
||||
// if (isAvailable) {
|
||||
// bool didAuthenticate = await LocalAuthentication().authenticate(
|
||||
// localizedReason: 'Use Touch ID or Face ID to confirm payment'.tr,
|
||||
// );
|
||||
|
||||
// if (didAuthenticate) {
|
||||
// // استدعاء الـ Endpoint الجديد على السيرفر الخاص بك
|
||||
// var res = await CRUD().postWallet(
|
||||
// link: AppLink.payWithEcashPassenger,
|
||||
// // link:
|
||||
// // 'https://wl.tripz-egypt.com/v1/main/ride/ecash/driver/payWithEcash.php',
|
||||
// payload: {
|
||||
// // أرسل البيانات التي يحتاجها السيرفر
|
||||
// "amount": amount,
|
||||
// // "driverId": box.read(BoxName.driverID), // تأكد من وجود هذا المتغير
|
||||
// "passengerId":
|
||||
// box.read(BoxName.passengerID), // تأكد من وجود هذا المتغير
|
||||
// },
|
||||
// );
|
||||
|
||||
// // التأكد من أن السيرفر أعاد رابط الدفع بنجاح
|
||||
// if (res != null &&
|
||||
// res['status'] == 'success' &&
|
||||
// res['message'] != null) {
|
||||
// final String paymentUrl = res['message'];
|
||||
// // الانتقال إلى شاشة الدفع الجديدة الخاصة بـ ecash للسائق
|
||||
// Navigator.push(
|
||||
// context,
|
||||
// MaterialPageRoute(
|
||||
// builder: (context) =>
|
||||
// EcashDriverPaymentScreen(paymentUrl: paymentUrl),
|
||||
// ),
|
||||
// );
|
||||
// } else {
|
||||
// // عرض رسالة خطأ في حال فشل السيرفر في إنشاء الرابط
|
||||
// Get.defaultDialog(
|
||||
// title: 'Error'.tr,
|
||||
// content: Text(
|
||||
// 'Failed to initiate payment. Please try again.'.tr,
|
||||
// style: AppStyle.title,
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (e) {
|
||||
// Get.defaultDialog(
|
||||
// title: 'Error'.tr,
|
||||
// content: Text(
|
||||
// 'An error occurred during the payment process.'.tr,
|
||||
// style: AppStyle.title,
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
/// شاشة جديدة ومبسطة خاصة بدفع السائقين عبر ecash
|
||||
|
||||
// Future<void> payWithMTNWallet(
|
||||
// BuildContext context, String amount, String currency) async {
|
||||
// // خزن سياق علوي آمن من البداية
|
||||
// final BuildContext safeContext =
|
||||
// Get.overlayContext ?? Get.context ?? context;
|
||||
|
||||
// // سبينر تحميل
|
||||
// if (!(Get.isDialogOpen ?? false)) {
|
||||
// Get.dialog(const Center(child: CircularProgressIndicator()),
|
||||
// barrierDismissible: false);
|
||||
// }
|
||||
|
||||
// try {
|
||||
// final phone = box.read(BoxName.phoneWallet) as String;
|
||||
// final passengerID = box.read(BoxName.passengerID).toString();
|
||||
// final formattedAmount = double.parse(amount).toStringAsFixed(0);
|
||||
|
||||
// Log.print("🚀 بدء عملية دفع MTN");
|
||||
// Log.print(
|
||||
// "📦 Payload: passengerID: $passengerID, amount: $formattedAmount, phone: $phone");
|
||||
|
||||
// // التحقق بالبصمة (اختياري) + حماية من الـ await
|
||||
// final localAuth = LocalAuthentication();
|
||||
// final isAuthSupported = await localAuth.isDeviceSupported();
|
||||
// if (isAuthSupported) {
|
||||
// final didAuth = await localAuth.authenticate(
|
||||
// localizedReason: 'استخدم بصمة الإصبع أو الوجه لتأكيد الدفع',
|
||||
// );
|
||||
// if (!didAuth) {
|
||||
// if (Get.isDialogOpen == true) Get.back();
|
||||
// Log.print("❌ المستخدم لم يؤكد بالبصمة/الوجه");
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 1) بدء الدفع
|
||||
// final responseData = await CRUD().postWalletMtn(
|
||||
// link: AppLink.payWithMTNStart,
|
||||
// payload: {
|
||||
// "amount": formattedAmount,
|
||||
// "passengerId": passengerID,
|
||||
// "phone": phone,
|
||||
// "lang": box.read(BoxName.lang) ?? 'ar',
|
||||
// },
|
||||
// );
|
||||
|
||||
// // Log.print("✅ استجابة الخادم (mtn_start_payment.php):");
|
||||
// // Log.print(responseData);
|
||||
// Log.print('responseData: ${responseData}');
|
||||
|
||||
// // فحص الاستجابة بقوة
|
||||
// late final Map<String, dynamic> startRes;
|
||||
// if (responseData is Map<String, dynamic>) {
|
||||
// startRes = responseData;
|
||||
// } else if (responseData is String) {
|
||||
// startRes = json.decode(responseData) as Map<String, dynamic>;
|
||||
// } else {
|
||||
// throw Exception("تم استلام نوع بيانات غير متوقع من الخادم.");
|
||||
// }
|
||||
|
||||
// if (startRes['status'] != 'success') {
|
||||
// final errorMsg = startRes['message']['Error']?.toString().tr ??
|
||||
// "فشل بدء عملية الدفع. حاول مرة أخرى.";
|
||||
// throw Exception(errorMsg);
|
||||
// }
|
||||
|
||||
// final messageData = startRes["message"] as Map<String, dynamic>;
|
||||
// final invoiceNumber = messageData["invoiceNumber"].toString();
|
||||
// final operationNumber = messageData["operationNumber"].toString();
|
||||
// final guid = messageData["guid"].toString();
|
||||
|
||||
// // Log.print(
|
||||
// // "📄 invoiceNumber: $invoiceNumber, 🔢 operationNumber: $operationNumber, 🧭 guid: $guid");
|
||||
|
||||
// // أغلق السبينر قبل إظهار حوار OTP
|
||||
// if (Get.isDialogOpen == true) Get.back();
|
||||
|
||||
// // 2) إدخال OTP بـ Get.defaultDialog (لا يستخدم context قابل للتلف)
|
||||
// String otpInput = "";
|
||||
// await Get.defaultDialog(
|
||||
// title: "أدخل كود التحقق",
|
||||
// barrierDismissible: false,
|
||||
// content: TextField(
|
||||
// keyboardType: TextInputType.number,
|
||||
// decoration: const InputDecoration(hintText: "كود OTP"),
|
||||
// onChanged: (v) => otpInput = v,
|
||||
// ),
|
||||
// confirm: TextButton(
|
||||
// onPressed: () {
|
||||
// if (otpInput.isEmpty ||
|
||||
// otpInput.length < 4 ||
|
||||
// otpInput.length > 8) {
|
||||
// Get.snackbar("تنبيه", "أدخل كود OTP صحيح (4–8 أرقام)");
|
||||
// return;
|
||||
// }
|
||||
// Get.back(result: otpInput);
|
||||
// },
|
||||
// child: const Text("تأكيد"),
|
||||
// ),
|
||||
// cancel: TextButton(
|
||||
// onPressed: () => Get.back(result: null),
|
||||
// child: const Text("إلغاء"),
|
||||
// ),
|
||||
// ).then((res) => otpInput = (res ?? "") as String);
|
||||
|
||||
// if (otpInput.isEmpty) {
|
||||
// Log.print("❌ لم يتم إدخال OTP");
|
||||
// return;
|
||||
// }
|
||||
// Log.print("🔐 تم إدخال OTP: $otpInput");
|
||||
|
||||
// // سبينر أثناء التأكيد
|
||||
// Get.dialog(const Center(child: CircularProgressIndicator()),
|
||||
// barrierDismissible: false);
|
||||
|
||||
// // 3) تأكيد الدفع
|
||||
// final confirmRes = await CRUD().postWalletMtn(
|
||||
// link: AppLink.payWithMTNConfirm,
|
||||
// payload: {
|
||||
// "invoiceNumber": invoiceNumber,
|
||||
// "operationNumber": operationNumber,
|
||||
// "guid": guid,
|
||||
// "otp": otpInput,
|
||||
// "phone": phone,
|
||||
// "lang": box.read(BoxName.lang) ?? 'ar',
|
||||
// },
|
||||
// );
|
||||
|
||||
// if (Get.isDialogOpen == true) Get.back();
|
||||
|
||||
// // Log.print("✅ استجابة mtn_confirm.php:");
|
||||
// // Log.print('confirmRes: ${confirmRes}');
|
||||
|
||||
// final ok = (confirmRes is Map && confirmRes['status'] == 'success');
|
||||
// if (ok) {
|
||||
// Get.defaultDialog(
|
||||
// title: "✅ نجاح",
|
||||
// content: const Text("تمت عملية الدفع وإضافة الرصيد إلى محفظتك."),
|
||||
// );
|
||||
// await getPassengerWallet();
|
||||
// } else {
|
||||
// final errorMsg = (confirmRes['message']['message']?.toString()) ??
|
||||
// "فشل في تأكيد الدفع";
|
||||
// Get.defaultDialog(title: "❌ فشل", content: Text(errorMsg.tr));
|
||||
// }
|
||||
// } catch (e, s) {
|
||||
// Log.print("🔥 خطأ أثناء الدفع عبر MTN:");
|
||||
// Log.print(e);
|
||||
// Log.print(s);
|
||||
// if (Get.isDialogOpen == true) Get.back();
|
||||
// Get.defaultDialog(
|
||||
// title: 'حدث خطأ',
|
||||
// content: Text(e.toString().replaceFirst("Exception: ", "")),
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<void> payWithSyriaTelWallet(String amount, String currency) async {
|
||||
// helper لفتح لودينغ بأمان
|
||||
Future<void> _showLoading() async {
|
||||
if (!(Get.isDialogOpen ?? false)) {
|
||||
Get.dialog(const Center(child: CircularProgressIndicator()),
|
||||
barrierDismissible: false);
|
||||
}
|
||||
}
|
||||
|
||||
// helper لإغلاق أي حوار مفتوح
|
||||
void _closeAnyDialog() {
|
||||
if (Get.isDialogOpen ?? false) {
|
||||
Get.back();
|
||||
}
|
||||
}
|
||||
|
||||
await _showLoading();
|
||||
try {
|
||||
final phone = box.read(BoxName.phoneWallet) as String;
|
||||
final passengerId = box.read(BoxName.passengerID).toString();
|
||||
final formattedAmount = double.parse(amount).toStringAsFixed(0);
|
||||
|
||||
Log.print("🚀 Syriatel payment start");
|
||||
Log.print(
|
||||
"📦 Payload => passengerId:$passengerId amount:$formattedAmount phone:$phone");
|
||||
|
||||
// مصادقة حيوية (اختياري)
|
||||
final auth = LocalAuthentication();
|
||||
if (await auth.isDeviceSupported()) {
|
||||
final ok = await auth.authenticate(
|
||||
localizedReason: 'استخدم بصمة الإصبع أو الوجه لتأكيد الدفع',
|
||||
);
|
||||
if (!ok) {
|
||||
_closeAnyDialog();
|
||||
Log.print("❌ User did not authenticate");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 1) بدء عملية الدفع
|
||||
final startRaw = await CRUD().postWalletMtn(
|
||||
link: AppLink.payWithSyriatelStart,
|
||||
payload: {
|
||||
"amount": formattedAmount,
|
||||
"passengerId": passengerId,
|
||||
"phone": phone,
|
||||
"lang": box.read(BoxName.lang) ?? 'ar',
|
||||
},
|
||||
);
|
||||
|
||||
Log.print("✅ Server response (start): $startRaw");
|
||||
|
||||
// تحويل الاستجابة إلى Map
|
||||
late final Map<String, dynamic> startRes;
|
||||
if (startRaw is Map<String, dynamic>) {
|
||||
startRes = startRaw;
|
||||
} else if (startRaw is String) {
|
||||
startRes = json.decode(startRaw) as Map<String, dynamic>;
|
||||
} else {
|
||||
throw Exception("Unexpected start response type");
|
||||
}
|
||||
|
||||
if (startRes['status'] != 'success') {
|
||||
final msg =
|
||||
(startRes['message'] ?? 'Failed to start payment').toString();
|
||||
throw Exception(msg);
|
||||
}
|
||||
|
||||
final messageData = startRes['message'] as Map<String, dynamic>;
|
||||
final transactionID = messageData['transactionID'].toString();
|
||||
Log.print("📄 transactionID: $transactionID");
|
||||
//
|
||||
// 2) اطلب من المستخدم إدخال OTP عبر Get.dialog (بدون context)
|
||||
_closeAnyDialog(); // أغلق اللودينغ أولاً
|
||||
final otpController = TextEditingController();
|
||||
final otp = await Get.dialog<String>(
|
||||
AlertDialog(
|
||||
title: const Text("أدخل كود التحقق"),
|
||||
content: TextField(
|
||||
controller: otpController,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: const InputDecoration(hintText: "كود OTP"),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: const Text("تأكيد"),
|
||||
onPressed: () => Get.back(result: otpController.text.trim()),
|
||||
),
|
||||
TextButton(
|
||||
child: const Text("إلغاء"),
|
||||
onPressed: () => Get.back(result: null),
|
||||
),
|
||||
],
|
||||
),
|
||||
barrierDismissible: false,
|
||||
);
|
||||
|
||||
if (otp == null || otp.isEmpty) {
|
||||
Log.print("❌ OTP not provided");
|
||||
return;
|
||||
}
|
||||
Log.print("🔐 OTP: $otp");
|
||||
|
||||
await _showLoading();
|
||||
|
||||
// 3) تأكيد الدفع
|
||||
final confirmRaw = await CRUD().postWallet(
|
||||
link: AppLink.payWithSyriatelConfirm,
|
||||
payload: {
|
||||
"transactionID": transactionID,
|
||||
"otp": otp,
|
||||
},
|
||||
);
|
||||
|
||||
_closeAnyDialog(); // أغلق اللودينغ
|
||||
|
||||
Log.print("✅ Response (confirm): $confirmRaw");
|
||||
|
||||
late final Map<String, dynamic> confirmRes;
|
||||
if (confirmRaw is Map<String, dynamic>) {
|
||||
confirmRes = confirmRaw;
|
||||
} else if (confirmRaw is String) {
|
||||
confirmRes = json.decode(confirmRaw) as Map<String, dynamic>;
|
||||
} else {
|
||||
throw Exception("Unexpected confirm response type");
|
||||
}
|
||||
|
||||
if (confirmRes['status'] == 'success') {
|
||||
Get.defaultDialog(
|
||||
title: "✅ نجاح",
|
||||
content: const Text("تمت عملية الدفع وإضافة الرصيد إلى محفظتك."),
|
||||
);
|
||||
} else {
|
||||
final msg = (confirmRes['message'] ?? 'فشل في تأكيد الدفع').toString();
|
||||
Get.defaultDialog(
|
||||
title: "❌ فشل",
|
||||
content: Text(msg),
|
||||
);
|
||||
}
|
||||
} catch (e, s) {
|
||||
Log.print("🔥 Error during Syriatel Wallet payment:\n$e\n$s");
|
||||
_closeAnyDialog();
|
||||
Get.defaultDialog(
|
||||
title: 'حدث خطأ',
|
||||
content: Text(e.toString().replaceFirst("Exception: ", "")),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
timestamp = now.millisecondsSinceEpoch;
|
||||
if (box.read(BoxName.passengerWalletTotal) == null) {
|
||||
box.write(BoxName.passengerWalletTotal, '0');
|
||||
}
|
||||
getPassengerWallet();
|
||||
final localAuth = LocalAuthentication();
|
||||
super.onInit();
|
||||
}
|
||||
}
|
||||
|
||||
class EcashDriverPaymentScreen extends StatefulWidget {
|
||||
final String paymentUrl;
|
||||
|
||||
const EcashDriverPaymentScreen({required this.paymentUrl, Key? key})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
State<EcashDriverPaymentScreen> createState() =>
|
||||
_EcashDriverPaymentScreenState();
|
||||
}
|
||||
|
||||
class _EcashDriverPaymentScreenState extends State<EcashDriverPaymentScreen> {
|
||||
late final WebViewController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setNavigationDelegate(NavigationDelegate(
|
||||
onPageFinished: (url) async {
|
||||
Log.print('Ecash Driver WebView URL Finished: $url');
|
||||
await Get.find<PaymentController>().getPassengerWallet();
|
||||
// هنا نتحقق فقط من أن المستخدم عاد إلى صفحة النجاح
|
||||
// لا حاجة لاستدعاء أي API هنا، فالـ Webhook يقوم بكل العمل
|
||||
if (url.contains("success.php")) {
|
||||
showProcessingDialog();
|
||||
}
|
||||
},
|
||||
))
|
||||
..loadRequest(Uri.parse(widget.paymentUrl));
|
||||
}
|
||||
|
||||
// دالة لعرض رسالة "العملية قيد المعالجة"
|
||||
void showProcessingDialog() {
|
||||
showDialog(
|
||||
barrierDismissible: false,
|
||||
context: Get.context!,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
title: Row(
|
||||
children: [
|
||||
Icon(Icons.check_circle, color: Colors.green),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
"Payment Successful".tr,
|
||||
style: TextStyle(
|
||||
color: Colors.green,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
content: Text(
|
||||
"Your payment is being processed and your wallet will be updated shortly."
|
||||
.tr,
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// أغلق مربع الحوار، ثم أغلق شاشة الدفع
|
||||
Navigator.pop(context); // Close the dialog
|
||||
Navigator.pop(context); // Close the payment screen
|
||||
},
|
||||
style: TextButton.styleFrom(
|
||||
backgroundColor: Colors.green,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
"OK".tr,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text('Complete Payment'.tr)),
|
||||
body: WebViewWidget(controller: _controller),
|
||||
);
|
||||
}
|
||||
}
|
||||
158
siro_rider/lib/controller/payment/paymob.dart
Normal file
158
siro_rider/lib/controller/payment/paymob.dart
Normal file
@@ -0,0 +1,158 @@
|
||||
import 'package:siro_rider/constant/box_name.dart';
|
||||
import 'package:dio/dio.dart' as dio;
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../../constant/api_key.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.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": box.read(BoxName.phone), //"01010101010"
|
||||
"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,
|
||||
});
|
||||
Log.print('token: ${response}');
|
||||
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": [],
|
||||
});
|
||||
Log.print('id: ${response}');
|
||||
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":
|
||||
4601103, ////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.name).toString().split(' ')[0]).toString(),
|
||||
"last_name":
|
||||
(box.read(BoxName.name).toString().split(' ')[1]).toString(),
|
||||
"email": (box.read(BoxName.email)),
|
||||
"phone_number": (box.read(BoxName.phone)),
|
||||
"apartment": "NA",
|
||||
"floor": "NA",
|
||||
"street": "NA",
|
||||
"building": "NA",
|
||||
"shipping_method": "NA",
|
||||
"postal_code": "NA",
|
||||
"city": "NA",
|
||||
"country": box.read(BoxName.countryCode),
|
||||
"state": "NA"
|
||||
},
|
||||
});
|
||||
Log.print('token: ${response}');
|
||||
return response.data["token"];
|
||||
}
|
||||
}
|
||||
100
siro_rider/lib/controller/payment/paymob/e_cash_screen.dart
Normal file
100
siro_rider/lib/controller/payment/paymob/e_cash_screen.dart
Normal file
@@ -0,0 +1,100 @@
|
||||
import 'package:siro_rider/print.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
// ✅ شاشة جديدة ومبسطة خاصة بـ ecash
|
||||
class EcashPaymentScreen extends StatefulWidget {
|
||||
final String paymentUrl;
|
||||
|
||||
const EcashPaymentScreen({required this.paymentUrl, Key? key})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
State<EcashPaymentScreen> createState() => _EcashPaymentScreenState();
|
||||
}
|
||||
|
||||
class _EcashPaymentScreenState extends State<EcashPaymentScreen> {
|
||||
late final WebViewController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setNavigationDelegate(NavigationDelegate(
|
||||
onPageFinished: (url) {
|
||||
Log.print('Ecash WebView URL Finished: $url');
|
||||
|
||||
// ✅ هنا نتحقق فقط من أن المستخدم عاد إلى صفحة النجاح
|
||||
// هذه الصفحة هي التي حددناها في `APP_REDIRECT_URL_SUCCESS` في ملف PHP
|
||||
if (url.contains("success.php")) {
|
||||
// لا نستدعي أي API هنا. الـ Webhook على السيرفر يقوم بكل العمل.
|
||||
// فقط نعرض للمستخدم رسالة بأن العملية قيد المراجعة ونغلق الشاشة.
|
||||
showProcessingDialog();
|
||||
}
|
||||
},
|
||||
))
|
||||
..loadRequest(Uri.parse(widget.paymentUrl));
|
||||
}
|
||||
|
||||
// دالة لعرض رسالة "العملية قيد المعالجة"
|
||||
void showProcessingDialog() {
|
||||
showDialog(
|
||||
barrierDismissible: false,
|
||||
context: Get.context!,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
title: Row(
|
||||
children: [
|
||||
Icon(Icons.check_circle, color: Colors.green),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
"Payment Successful".tr,
|
||||
style: TextStyle(
|
||||
color: Colors.green,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
content: Text(
|
||||
"Your payment is being processed and your wallet will be updated shortly."
|
||||
.tr,
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// أغلق مربع الحوار، ثم أغلق شاشة الدفع
|
||||
Navigator.pop(context); // Close the dialog
|
||||
Navigator.pop(context); // Close the payment screen
|
||||
},
|
||||
style: TextButton.styleFrom(
|
||||
backgroundColor: Colors.green,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
"OK".tr,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text('Complete Payment'.tr)),
|
||||
body: WebViewWidget(controller: _controller),
|
||||
);
|
||||
}
|
||||
}
|
||||
487
siro_rider/lib/controller/payment/paymob/paymob_response.dart
Normal file
487
siro_rider/lib/controller/payment/paymob/paymob_response.dart
Normal file
@@ -0,0 +1,487 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:siro_rider/constant/box_name.dart';
|
||||
import 'package:siro_rider/constant/links.dart';
|
||||
import 'package:siro_rider/controller/functions/crud.dart';
|
||||
import 'package:siro_rider/main.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../../../print.dart';
|
||||
import '../../functions/encrypt_decrypt.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.email) ?? box.read(BoxName.emailDriver),
|
||||
"first_name":
|
||||
(box.read(BoxName.name).toString().split(' ')[0]).toString(),
|
||||
"last_name": (box.read(BoxName.name).toString().split(' ')[1]).toString(),
|
||||
"phone_number": (box.read(BoxName.phone)),
|
||||
"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": box.read(BoxName.passengerID) ?? "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;
|
||||
}
|
||||
}
|
||||
|
||||
class PaymentScreen extends StatefulWidget {
|
||||
final String iframeUrl;
|
||||
|
||||
const PaymentScreen({required this.iframeUrl, Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<PaymentScreen> createState() => _PaymentScreenState();
|
||||
}
|
||||
|
||||
class _PaymentScreenState extends State<PaymentScreen> {
|
||||
late final WebViewController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setNavigationDelegate(NavigationDelegate(
|
||||
onPageFinished: (url) {
|
||||
Log.print('url onPageFinished : ${url}');
|
||||
if (url.contains("success")) {
|
||||
_fetchPaymentStatus(); // ✅ استدعاء الويب هوك بعد نجاح الدفع
|
||||
} else if (url.contains("failed")) {
|
||||
showCustomDialog(
|
||||
title: "Error".tr,
|
||||
message: 'Payment Failed'.tr, // يتم جلب رسالة الخطأ من الخادم
|
||||
isSuccess: false,
|
||||
);
|
||||
}
|
||||
},
|
||||
))
|
||||
..loadRequest(Uri.parse(widget.iframeUrl));
|
||||
}
|
||||
|
||||
// ✅ استدعاء الويب هوك بعد انتهاء الدفع
|
||||
Future<void> _fetchPaymentStatus() async {
|
||||
final String userId = (box.read(BoxName.phoneWallet)); // ضع user_id الحقيقي
|
||||
final String apiUrl = AppLink.paymetVerifyPassenger;
|
||||
|
||||
try {
|
||||
final response = await CRUD().getWallet(link: apiUrl, payload: {
|
||||
'user_id': userId,
|
||||
'passengerId': box.read(BoxName.passengerID),
|
||||
'paymentMethod': 'visa-in',
|
||||
});
|
||||
|
||||
if (response != 'failure' && response != 'token_expired') {
|
||||
try {
|
||||
final jsonData = jsonDecode(response);
|
||||
|
||||
if (jsonData['status'] == 'success') {
|
||||
// تأكد أن 'message' هو String وليس Map
|
||||
// final message = jsonData['message'];
|
||||
showCustomDialog(
|
||||
title: "Payment Status",
|
||||
message: jsonData['message'], // يتم جلب الرسالة من الخادم
|
||||
isSuccess: true,
|
||||
);
|
||||
} else {
|
||||
showCustomDialog(
|
||||
title: "Error",
|
||||
message: jsonData['message'], // يتم جلب رسالة الخطأ من الخادم
|
||||
isSuccess: false,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
showCustomDialog(
|
||||
title: "Error",
|
||||
message: response, // يتم جلب رسالة الخطأ من الخادم
|
||||
isSuccess: false,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
showCustomDialog(
|
||||
title: "Error".tr,
|
||||
message: response, // يتم جلب رسالة الخطأ من الخادم
|
||||
isSuccess: false,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
showCustomDialog(
|
||||
title: "Error".tr,
|
||||
message: 'Server error'.tr, // يتم جلب رسالة الخطأ من الخادم
|
||||
isSuccess: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void showCustomDialog({
|
||||
required String title,
|
||||
required String message,
|
||||
required bool isSuccess,
|
||||
}) {
|
||||
showDialog(
|
||||
barrierDismissible: false,
|
||||
context: Get.context!,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
title: Row(
|
||||
children: [
|
||||
Icon(
|
||||
isSuccess ? Icons.check_circle : Icons.error,
|
||||
color: isSuccess ? Colors.green : Colors.red,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
color: isSuccess ? Colors.green : Colors.red,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
content: Text(
|
||||
message,
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
style: TextButton.styleFrom(
|
||||
backgroundColor: isSuccess ? Colors.green : Colors.red,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
"OK",
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('إتمام الدفع')),
|
||||
body: WebViewWidget(controller: _controller),
|
||||
);
|
||||
}
|
||||
}
|
||||
382
siro_rider/lib/controller/payment/paymob/paymob_wallet.dart
Normal file
382
siro_rider/lib/controller/payment/paymob/paymob_wallet.dart
Normal file
@@ -0,0 +1,382 @@
|
||||
import 'package:siro_rider/constant/box_name.dart';
|
||||
import 'package:siro_rider/main.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
import '../../../print.dart';
|
||||
import '../../functions/encrypt_decrypt.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.email)),
|
||||
"first_name":
|
||||
(box.read(BoxName.name).toString().split(' ')[0]).toString(),
|
||||
"last_name":
|
||||
(box.read(BoxName.name).toString().split(' ')[1]).toString() ??
|
||||
'Intaleq',
|
||||
"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) {
|
||||
if (request.url.contains('txn_response_code') &&
|
||||
// request.url.contains('successfully') &&
|
||||
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;
|
||||
}
|
||||
}
|
||||
0
siro_rider/lib/controller/payment/stripe.dart
Normal file
0
siro_rider/lib/controller/payment/stripe.dart
Normal file
Reference in New Issue
Block a user