This commit is contained in:
Hamza Aleghwairyeen
2024-04-21 01:26:42 +03:00
parent 731769edc0
commit a2d1479513
18 changed files with 1087 additions and 117 deletions

View File

@@ -1,6 +1,8 @@
import 'dart:convert';
import 'package:SEFER/constant/api_key.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/controller/functions/tts.dart';
import 'package:SEFER/controller/payment/paymob/paymob_response.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
@@ -9,7 +11,6 @@ import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:get/get.dart';
import 'package:local_auth/local_auth.dart';
import 'package:SEFER/controller/home/map_passenger_controller.dart';
import 'package:paymob_payment/paymob_payment.dart';
import '../../constant/box_name.dart';
import '../../constant/colors.dart';
@@ -18,6 +19,7 @@ import '../../constant/links.dart';
import '../../main.dart';
import '../functions/crud.dart';
import '../functions/toast.dart';
import 'paymob/paymob_wallet.dart';
class PaymentController extends GetxController {
bool isLoading = false;
@@ -55,7 +57,6 @@ class PaymentController extends GetxController {
box.write(BoxName.passengerWalletTotal,
jsonDecode(value)['message'][0]['total'].toString());
});
isLoading = false;
update();
}
@@ -479,11 +480,17 @@ class PaymentController extends GetxController {
context: context,
currency: currency, //"EGP",
amountInCents: newAmount, // 19.00 EGP
billingData: PaymobBillingData(),
onPayment: (PaymobResponse response) {
print('Success: ${response.success}');
print('Transaction ID: ${response.transactionID}');
print('Response Code: ${response.responseCode}');
// print('Message: ${response.message}');
print(box.read(BoxName.passengerWalletTotal));
print(box.read(BoxName.name));
print(box.read(BoxName.phone));
// print();
},
);
@@ -533,6 +540,178 @@ class PaymentController extends GetxController {
context: context,
currency: currency, //"EGP",
amountInCents: newAmount, // 19.00 EGP
billingData: PaymobBillingData(),
onPayment: (PaymobResponse response) {
// print('Success: ${response.success}');
// print('Transaction ID: ${response.transactionID}');
// print('Response Code: ${response.responseCode}');
// print('Message: ${response.message}');
},
);
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',
);
if (didAuthenticate) {
final PaymobResponseWallet? response =
await PaymobPaymentWallet.instance.pay(
context: context,
currency: currency, //"EGP",
amountInCents: newAmount, // 19.00 EGP
billingData: PaymobBillingDataWallet(),
onPayment: (PaymobResponseWallet response) {
print('Success: ${response.success}');
print('Transaction ID: ${response.transactionID}');
print('Response Code: ${response.responseCode}');
print('Message: ${response.message}');
// print(box.read(BoxName.passengerWalletTotal));//
// print(box.read(BoxName.name));
// print(box.read(BoxName.phone));
// print();
},
);
if (response!.success == true && response.responseCode == '200') {
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: 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
print('Authentication failed');
}
} else {
final PaymobResponse? response = await PaymobPayment.instance.pay(
context: context,
currency: currency, //"EGP",
amountInCents: newAmount, // 19.00 EGP
billingData: PaymobBillingData(),
onPayment: (PaymobResponse response) {
// print('Success: ${response.success}');
// print('Transaction ID: ${response.transactionID}');