25-7-28-2

This commit is contained in:
Hamza-Ayed
2025-07-28 12:21:28 +03:00
parent 660d60e1f5
commit 83a97baed1
549 changed files with 109870 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
import 'dart:convert';
import 'package:sefer_driver/views/widgets/elevated_btn.dart';
import 'package:get/get.dart';
import 'package:sefer_driver/constant/box_name.dart';
import 'package:sefer_driver/constant/links.dart';
import 'package:sefer_driver/controller/functions/crud.dart';
import 'package:sefer_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();
}
}

View File

@@ -0,0 +1,730 @@
import 'dart:convert';
import 'package:sefer_driver/constant/api_key.dart';
import 'package:sefer_driver/constant/style.dart';
import 'package:sefer_driver/controller/functions/tts.dart';
import 'package:sefer_driver/controller/payment/paymob/paymob_response.dart';
import 'package:sefer_driver/views/widgets/elevated_btn.dart';
import 'package:sefer_driver/views/widgets/error_snakbar.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:flutter_paypal/flutter_paypal.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:get/get.dart';
import 'package:local_auth/local_auth.dart';
import 'package:sefer_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 '../../print.dart';
import '../functions/crud.dart';
import '../functions/encrypt_decrypt.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();
// }
// }
late String clientSecret;
Future<void> makePaymentStripe(
double amount, String currency, Function method) async {
var newAmount = (amount * 100).toInt();
try {
// Check if local authentication is available
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',
);
if (didAuthenticate) {
// User authenticated successfully, proceed with payment
clientSecret = await getClientSecret(newAmount.toString(), currency);
await initializePaymentSheet(clientSecret);
await Stripe.instance.presentPaymentSheet();
method();
} else {
// Authentication failed, handle accordingly
}
} else {
// Local authentication not available, proceed with payment without authentication
clientSecret = await getClientSecret(newAmount.toString(), currency);
await initializePaymentSheet(clientSecret);
await Stripe.instance.presentPaymentSheet();
method();
}
} catch (e) {
rethrow;
}
}
Future<void> initializePaymentSheet(String clientSecret) async {
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
// intentConfiguration: IntentConfiguration.fromJson({}),
// applePay: const PaymentSheetApplePay(merchantCountryCode: 'US'),
// googlePay: const PaymentSheetGooglePay(merchantCountryCode: 'US'),
paymentIntentClientSecret: clientSecret,
merchantDisplayName: AppInformation.appName,
billingDetails: BillingDetails(
name: (box.read(BoxName.nameDriver)),
email: (box.read(BoxName.emailDriver)),
phone: (box.read(BoxName.phoneDriver)),
address: Address(
city: 'city',
country: box.read(BoxName.countryCode), //'United States'
line1: '',
line2: '',
postalCode: '12345',
state: box.read(BoxName.countryCode) // 'Boston'
)),
allowsDelayedPaymentMethods: true,
customerEphemeralKeySecret: Stripe.merchantIdentifier,
appearance: const PaymentSheetAppearance(
shapes: PaymentSheetShape(borderRadius: 12),
colors: PaymentSheetAppearanceColors(
background: AppColor.secondaryColor,
),
),
billingDetailsCollectionConfiguration:
const BillingDetailsCollectionConfiguration(
name: CollectionMode.automatic,
phone: CollectionMode.automatic,
email: CollectionMode.automatic,
// address: CollectionMode.automatic,
),
),
);
}
Future<String> getClientSecret(String amount, currency) async {
var res = await CRUD().postStripe(
link: 'https://api.stripe.com/v1/payment_intents',
payload: {
'amount': amount,
'currency': currency,
'payment_method_types[0]': 'card'
},
);
// Convert the res object to a JSON object
final jsonResponse = jsonDecode(res);
// Check if the client_secret property exists and is not null
if (jsonResponse.containsKey('client_secret') &&
jsonResponse['client_secret'] != null) {
// Return the client_secret property
return jsonResponse['client_secret'] as String;
} else {
throw Exception('Failed to fetch client secret');
}
}
Future<void> configure3dSecureFuture() async {
await Stripe.instance.openApplePaySetup();
}
Future<void> makePaymentPayPal(BuildContext context) async {
try {
// Check if local authentication is available
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',
);
if (didAuthenticate) {
// User authenticated successfully, proceed with payment
if (selectedAmount != 0) {
changePromoSheetDialogue();
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => UsePaypal(
sandboxMode: true,
clientId: AK.payPalClientId,
secretKey: AK.payPalSecret,
returnURL: AppInformation.website,
cancelURL: "${AppInformation.website}/cancel",
transactions: [
{
"amount": {
//sb-opsju26682403@personal.example.com
"total": '$selectedAmount',
"currency": box.read(BoxName.countryCode) == 'Egypt'
? 'EGP'
: "JOD",
"details": {
"subtotal": '$selectedAmount',
"shipping": '0',
"shipping_discount": 0
}
},
"description": "The payment transaction description.",
"payment_options": const {
"allowed_payment_method": "INSTANT_FUNDING_SOURCE"
},
"item_list": {
"items": [
{
"name": "${AppInformation.appName} Wallet ",
"quantity": 1,
"price": '$selectedAmount',
"currency": "USD"
}
],
// shipping address is not required though
"shipping_address": const {
"recipient_name":
"${AppInformation.appName} Wallet",
"line1": "Shafa Badran",
"line2": "",
"city": "Amman",
"country_code": "JO",
"postal_code": "13112",
"phone": "+962798583052",
"state": "Amman"
},
}
}
],
note: "Contact us for any questions on your order.".tr,
onSuccess: (Map params) async {
addPassengerWallet();
changePromoSheetDialogue();
await getPassengerWallet();
},
onError: (error) {
Toast.show(context, ' $error'.tr, AppColor.redColor);
},
onCancel: (params) {
Toast.show(context, 'Pyament Cancelled .'.tr,
AppColor.yellowColor);
}),
),
);
} else {
Toast.show(context, 'You will choose one of above !'.tr,
AppColor.redColor);
}
} else {
// Authentication failed, handle accordingly
}
}
} catch (e) {
rethrow;
}
}
Map licenseDetailsMap = {};
Future getLicenseInfo() async {
var res = await CRUD().get(
link: AppLink.getLicense,
payload: {'driverID': box.read(BoxName.driverID)});
licenseDetailsMap = jsonDecode(res);
}
Future createConnectAccount() async {
String url = 'https://api.stripe.com/v1/accounts';
await getLicenseInfo();
DateTime dob =
DateTime.parse(licenseDetailsMap['message'][0]['dateOfBirth']);
int currentTimestamp =
(DateTime.now().millisecondsSinceEpoch / 1000).round();
int day = dob.day;
int month = dob.month;
int year = dob.year;
await getIpAddress();
final body = {
"type": "custom",
"business_profile[name]": box.read(BoxName.nameDriver),
"business_profile[product_description]": "Captain",
"business_profile[support_address][city]": "San Francisco",
"business_profile[support_address][country]": 'US',
"business_profile[support_address][line1]":
licenseDetailsMap['message'][0]['address'].toString().trim()[0],
"business_profile[support_address][postal_code]":
licenseDetailsMap['message'][0]['postalCode'],
"business_profile[support_address][state]": "MA",
"business_profile[support_email]": "support@sefer.live",
"business_profile[support_phone]": "555-123-4567",
"business_profile[url]": "https://sefer.live",
"business_type": "individual",
"capabilities[card_payments][requested]": "true",
"capabilities[transfers][requested]": "true",
"company[address][city]": "ATTLEBORO",
"company[address][country]": "US",
"company[address][line1]": "1249 NEWPORT AVE",
"company[address][postal_code]": "02703 ",
"company[address][state]": "MA",
"company[name]": AppInformation.companyName,
"country": "us",
"default_currency": "usd",
"email": "support@sefer.live",
// "individual[ssn]": "123-45-6789", //
"individual[id_number]": licenseDetailsMap['message'][0]['documentNo'],
// "individual[id_type]": "drivers_license", //
"individual[address][city]": "ATTLEBORO",
"individual[address][country]": "US",
"individual[address][line1]": licenseDetailsMap['message'][0]['address'],
// "individual[address][postal_code]": licenseDetailsMap['message'][0]
// ['postalCode'],
"individual[address][state]": "MA",
// "individual[ssn_last_4]": '1111', ////////
"individual[dob][day]": day.toString(),
"individual[dob][month]": month.toString(),
"individual[dob][year]": year.toString(),
"individual[email]": box.read(BoxName.emailDriver),
"individual[first_name]":
licenseDetailsMap['message'][0]['name'].toString().split(' ')[0],
"individual[gender]":
licenseDetailsMap['message'][0]['sex'] == 'M' ? 'male' : 'female',
"individual[last_name]":
licenseDetailsMap['message'][0]['name'].toString().split(' ')[1],
// "individual[phone]": box.read(BoxName.phoneDriver),////////////
"tos_acceptance[date]": currentTimestamp.toString(),
"tos_acceptance[ip]": ip.toString()
};
final response = await CRUD().postStripe(
link: url,
payload: body,
);
final responseData = jsonDecode(response);
final accountId = responseData['id'];
box.write(BoxName.accountIdStripeConnect, accountId);
await updateCaptainAccountBank();
return accountId;
}
Future updateCaptainAccountBank() async {
var res = await CRUD().post(link: AppLink.updateAccountBank, payload: {
'id': box.read(BoxName.driverID),
'accountBank': box.read(BoxName.accountIdStripeConnect),
});
if (jsonDecode(res)['status'] == 'success') {
mySnackbarSuccess('Account Updated'.tr);
// Get.snackbar('Account Updated', '');
}
}
Future<String> createTransactionToCaptain(
String amount, String account) async {
String url = 'https://api.stripe.com/v1/transfers';
final body = {
'amount': amount, //amount
'currency': 'usd',
'destination': account //'acct_1OKIjQRgcWrsdyDT' //account id
};
final response = await CRUD().postStripe(
link: url,
payload: body,
);
final responseData = jsonDecode(response);
final transactionId = responseData['id'];
return transactionId;
}
Future getIpAddress() async {
var url = Uri.parse('https://api.ipify.org?format=json');
var response = await http.get(url);
if (response.statusCode == 200) {
ip = jsonDecode(response.body)['ip'];
} else {}
}
// '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();
final localAuth = LocalAuthentication();
super.onInit();
}
}

View File

@@ -0,0 +1,154 @@
import 'package:sefer_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"];
}
}

View File

@@ -0,0 +1,326 @@
import 'package:sefer_driver/constant/box_name.dart';
import 'package:sefer_driver/controller/functions/encrypt_decrypt.dart';
import 'package:sefer_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;
}
}

View File

@@ -0,0 +1,470 @@
import 'package:sefer_driver/constant/box_name.dart';
import 'package:sefer_driver/controller/functions/encrypt_decrypt.dart';
import 'package:sefer_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'),
),
],
);
},
);
}
}

View File