import 'dart:developer'; import 'package:hmg_patient_app/config/shared_pref_kay.dart'; import 'package:hmg_patient_app/core/enum/PayfortEnums.dart'; import 'package:hmg_patient_app/core/viewModels/project_view_model.dart'; import 'package:hmg_patient_app/models/Appointments/AppoimentAllHistoryResultList.dart'; import 'package:hmg_patient_app/models/Authentication/authenticated_user.dart'; import 'package:hmg_patient_app/models/Clinics/EROnlineCheckInPaymentDetailsResponse.dart'; import 'package:hmg_patient_app/models/LiveCare/ApplePayInsertRequest.dart'; import 'package:hmg_patient_app/pages/ToDoList/payment_method_select.dart'; import 'package:hmg_patient_app/pages/insurance/insurance_update_screen.dart'; import 'package:hmg_patient_app/pages/landing/landing_page.dart'; import 'package:hmg_patient_app/services/appointment_services/GetDoctorsList.dart'; import 'package:hmg_patient_app/services/clinic_services/get_clinic_service.dart'; import 'package:hmg_patient_app/services/livecare_services/livecare_provider.dart'; import 'package:hmg_patient_app/services/payfort_services/payfort_project_details_resp_model.dart'; import 'package:hmg_patient_app/services/payfort_services/payfort_view_model.dart'; import 'package:hmg_patient_app/theme/colors.dart'; import 'package:hmg_patient_app/uitl/app_shared_preferences.dart'; import 'package:hmg_patient_app/uitl/app_toast.dart'; import 'package:hmg_patient_app/uitl/date_uitl.dart'; import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart'; import 'package:hmg_patient_app/uitl/translations_delegate_base.dart'; import 'package:hmg_patient_app/uitl/utils.dart'; import 'package:hmg_patient_app/uitl/utils_new.dart'; import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart'; import 'package:hmg_patient_app/widgets/dragable_sheet.dart'; import 'package:hmg_patient_app/widgets/in_app_browser/InAppBrowser.dart'; import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart'; import 'package:hmg_patient_app/widgets/transitions/fade_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:provider/provider.dart'; class EROnlineCheckInPaymentDetails extends StatefulWidget { int projectID = 0; bool isERBookAppointment = false; String projectName = ""; EROnlineCheckInPaymentDetails({required this.projectID, required this.isERBookAppointment, required this.projectName}); @override State createState() => _EROnlineCheckInPaymentDetailsState(); } class _EROnlineCheckInPaymentDetailsState extends State with SingleTickerProviderStateMixin { late ProjectViewModel projectViewModel; EROnlineCheckInPaymentDetailsResponse? erOnlineCheckInPaymentDetailsResponse; String? selectedPaymentMethod; String? selectedInstallmentPlan; String transID = ""; late MyInAppBrowser browser; @override void initState() { WidgetsBinding.instance.addPostFrameCallback((_) { getEROnlineCheckInPaymentDetails(); }); super.initState(); } @override Widget build(BuildContext context) { projectViewModel = Provider.of(context); return AppScaffold( isShowAppBar: true, appBarTitle: TranslationBase.of(context).emergency + " ${TranslationBase.of(context).checkinOptions}", isShowDecPage: false, showNewAppBar: true, showNewAppBarTitle: true, backgroundColor: Color(0xffF8F8F8), body: Padding( padding: EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( width: double.infinity, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10), bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10)), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3), // changes position of shadow ), ], ), child: Padding( padding: EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( TranslationBase.of(context).patientInfo, style: TextStyle( fontSize: 18, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24), ), mHeight(12), Row( children: [ Text( TranslationBase.of(context).patientName + ":", style: TextStyle( fontWeight: FontWeight.w600, fontSize: 12, letterSpacing: -0.6, color: CustomColors.grey, ), ), mWidth(3), Text( projectViewModel.user.firstName! + " " + projectViewModel.user.lastName!, style: TextStyle( fontWeight: FontWeight.w600, fontSize: 14, letterSpacing: -0.48, ), ), ], ), Row( children: [ Text( TranslationBase.of(context).mrn + ":", style: TextStyle( fontWeight: FontWeight.w600, fontSize: 12, letterSpacing: -0.6, color: CustomColors.grey, ), ), mWidth(3), Text( projectViewModel.user.patientID.toString(), style: TextStyle( fontWeight: FontWeight.w600, fontSize: 14, letterSpacing: -0.48, ), ), ], ), ], ), ), ), mHeight(24), Container( width: double.infinity, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10), bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10)), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3), // changes position of shadow ), ], ), child: Padding( padding: EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "ER Visit Details", style: TextStyle( fontSize: 18, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24), ), mHeight(12), Row( children: [ Text( TranslationBase.of(context).hospital + ":", style: TextStyle( fontWeight: FontWeight.w600, fontSize: 12, letterSpacing: -0.6, color: CustomColors.grey, ), ), mWidth(3), Text( widget.projectName, style: TextStyle( fontWeight: FontWeight.w600, fontSize: 14, letterSpacing: -0.48, ), ), ], ), Row( children: [ Text( TranslationBase.of(context).clinicName + ":", style: TextStyle( fontWeight: FontWeight.w600, fontSize: 12, letterSpacing: -0.6, color: CustomColors.grey, ), ), mWidth(3), Text( "ER Clinic", style: TextStyle( fontWeight: FontWeight.w600, fontSize: 14, letterSpacing: -0.48, ), ), ], ), Row( children: [ Text( "Time Check-In" + ":", style: TextStyle( fontWeight: FontWeight.w600, fontSize: 12, letterSpacing: -0.6, color: CustomColors.grey, ), ), mWidth(3), Text( DateUtil.getMonthDayYearDateFormatted(DateTime.now()), style: TextStyle( fontWeight: FontWeight.w600, fontSize: 14, letterSpacing: -0.48, ), ), ], ), ], ), ), ) ], ), ), bottomSheet: erOnlineCheckInPaymentDetailsResponse != null ? Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10), bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10)), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3), // changes position of shadow ), ], ), padding: EdgeInsets.only(left: 21, right: 21, top: 15, bottom: 15), width: double.infinity, // color: Colors.white, child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ SizedBox(height: 12), Text( TranslationBase.of(context).YouCanPayByTheFollowingOptions, style: TextStyle( fontSize: 16.0, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.64, ), ), SizedBox( width: MediaQuery.of(context).size.width * 0.75, child: getPaymentMethods(), ), _amountView(TranslationBase.of(context).patientShareTotalToDo, erOnlineCheckInPaymentDetailsResponse!.patientShareWithTax.toString(), isBold: true, isTotal: true), SizedBox(height: 12), Column(children: [ DefaultButton( TranslationBase.of(context).payNow.toUpperCase(), () { makePayment(); }, color: CustomColors.green, disabledColor: CustomColors.grey2, ), erOnlineCheckInPaymentDetailsResponse!.isCash == true ? Column( children: [ mHeight(10.0), Text( TranslationBase.of(context).cashAmountUpdateInsurance, style: TextStyle( fontSize: 14.0, fontWeight: FontWeight.w600, color: Color(0xff2E303A), ), ), mHeight(5.0), DefaultButton( TranslationBase.of(context).updateInsuranceText, () { Navigator.pop(context, null); Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => LandingPage()), (Route route) => false, ); Navigator.push(context, FadePage(page: InsuranceUpdate())); }, color: Color(0xffEAA118), textColor: Colors.white, ), ], ) : SizedBox() ], )]), ) : Container(), ); } makePayment() { showDraggableDialog( context, PaymentMethod( onSelectedMethod: (String method, [String? selectedInstallmentPlan]) { selectedPaymentMethod = method; this.selectedInstallmentPlan = selectedInstallmentPlan; if (selectedPaymentMethod == "ApplePay") { if (projectViewModel.havePrivilege(103)) { startApplePay(); } else { AppoitmentAllHistoryResultList appo = new AppoitmentAllHistoryResultList(); appo.projectID = widget.projectID; openPayment(selectedPaymentMethod!, projectViewModel.user, erOnlineCheckInPaymentDetailsResponse!.patientShareWithTax!, AppoitmentAllHistoryResultList()); } } else { openPayment(selectedPaymentMethod!, projectViewModel.user, erOnlineCheckInPaymentDetailsResponse!.patientShareWithTax!, AppoitmentAllHistoryResultList()); } }, patientShare: erOnlineCheckInPaymentDetailsResponse!.patientShareWithTax, isFromAdvancePayment: false, ), ); } openPayment(String paymentMethod, AuthenticatedUser authenticatedUser, num amount, AppoitmentAllHistoryResultList appo) { transID = Utils.getAdvancePaymentTransID(widget.projectID, projectViewModel.user.patientID!); browser = new MyInAppBrowser(onExitCallback: onBrowserExit, appo: appo, onLoadStartCallback: onBrowserLoadStart); browser.openPaymentBrowser(amount, "ER Online Check-In Payment", transID, widget.projectID.toString(), authenticatedUser.emailAddress!, paymentMethod, authenticatedUser.patientType, authenticatedUser.firstName!, authenticatedUser.patientID, authenticatedUser, browser, false, "3", "", context); } onBrowserLoadStart(String url) { print("onBrowserLoadStart"); print(url); MyInAppBrowser.successURLS.forEach((element) { if (url.contains(element)) { if (browser.isOpened()) browser.close(); MyInAppBrowser.isPaymentDone = true; return; } }); MyInAppBrowser.errorURLS.forEach((element) { if (url.contains(element)) { if (browser.isOpened()) browser.close(); MyInAppBrowser.isPaymentDone = false; return; } }); } onBrowserExit(AppoitmentAllHistoryResultList appo, bool isPaymentMade) { print("onBrowserExit Called!!!!"); checkPaymentStatus(appo); } void startApplePay() async { transID = Utils.getAdvancePaymentTransID(widget.projectID, projectViewModel.user.patientID!); print("TransactionID: $transID"); GifLoaderDialogUtils.showMyDialog(context); LiveCareService service = new LiveCareService(); ApplePayInsertRequest applePayInsertRequest = new ApplePayInsertRequest(); PayfortProjectDetailsRespModel? payfortProjectDetailsRespModel; await context.read().getProjectDetailsForPayfort(projectId: widget.projectID, serviceId: ServiceTypeEnum.appointmentPayment.getIdFromServiceEnum()).then((value) { payfortProjectDetailsRespModel = value!; }); applePayInsertRequest.clientRequestID = transID; applePayInsertRequest.clinicID = 0; applePayInsertRequest.currency = projectViewModel.user.outSA == 1 ? "AED" : "SAR"; // applePayInsertRequest.customerEmail = projectViewModel.authenticatedUserObject.user.emailAddress; applePayInsertRequest.customerEmail = "CustID_${projectViewModel.user.patientID}@HMG.com"; applePayInsertRequest.customerID = projectViewModel.user.patientID; applePayInsertRequest.customerName = projectViewModel.user.firstName! + " " + projectViewModel.user.lastName!; applePayInsertRequest.deviceToken = await AppSharedPreferences().getString(PUSH_TOKEN); applePayInsertRequest.voipToken = await AppSharedPreferences().getString(ONESIGNAL_APNS_TOKEN); applePayInsertRequest.doctorID = 0; applePayInsertRequest.projectID = widget.projectID.toString(); applePayInsertRequest.serviceID = ServiceTypeEnum.advancePayment.getIdFromServiceEnum().toString(); applePayInsertRequest.channelID = 3; applePayInsertRequest.patientID = projectViewModel.user.patientID; applePayInsertRequest.patientTypeID = projectViewModel.user.patientType; applePayInsertRequest.patientOutSA = projectViewModel.user.outSA; applePayInsertRequest.appointmentDate = null; applePayInsertRequest.appointmentNo = 0; applePayInsertRequest.orderDescription = "ER Online Check-In Payment"; applePayInsertRequest.liveServiceID = "0"; applePayInsertRequest.latitude = "0.0"; applePayInsertRequest.longitude = "0.0"; applePayInsertRequest.amount = erOnlineCheckInPaymentDetailsResponse!.patientShareWithTax.toString(); applePayInsertRequest.isSchedule = "0"; applePayInsertRequest.language = projectViewModel.isArabic ? 'ar' : 'en'; applePayInsertRequest.languageID = projectViewModel.isArabic ? 1 : 2; applePayInsertRequest.userName = projectViewModel.user.patientID; applePayInsertRequest.responseContinueURL = "http://hmg.com/Documents/success.html"; applePayInsertRequest.backClickUrl = "http://hmg.com/Documents/success.html"; applePayInsertRequest.paymentOption = "ApplePay"; applePayInsertRequest.isMobSDK = true; applePayInsertRequest.merchantReference = transID; applePayInsertRequest.merchantIdentifier = payfortProjectDetailsRespModel!.merchantIdentifier; applePayInsertRequest.commandType = "PURCHASE"; applePayInsertRequest.signature = payfortProjectDetailsRespModel!.signature; applePayInsertRequest.accessCode = payfortProjectDetailsRespModel!.accessCode; applePayInsertRequest.shaRequestPhrase = payfortProjectDetailsRespModel!.shaRequest; applePayInsertRequest.shaResponsePhrase = payfortProjectDetailsRespModel!.shaResponse; applePayInsertRequest.returnURL = ""; service.applePayInsertRequest(applePayInsertRequest, context).then((res) async { if (res["MessageStatus"] == 1) { await context.read().initiateApplePayWithPayfort( customerName: projectViewModel.user.firstName! + " " + projectViewModel.user.lastName!, // customerEmail: projectViewModel.authenticatedUserObject.user.emailAddress, customerEmail: "CustID_${projectViewModel.user.patientID}@HMG.com", orderDescription: "ER Online Check-In Payment", orderAmount: erOnlineCheckInPaymentDetailsResponse!.patientShareWithTax, merchantReference: transID, payfortProjectDetailsRespModel: payfortProjectDetailsRespModel, currency: projectViewModel.user.outSA == 1 ? "AED" : "SAR", onFailed: (failureResult) async { GifLoaderDialogUtils.hideDialog(context); log("failureResult: ${failureResult.message.toString()}"); AppToast.showErrorToast(message: failureResult.message.toString()); }, onSuccess: (successResult) async { log("Payfort: ${successResult.responseMessage}"); await context.read().addPayfortApplePayResponse(projectViewModel.user.patientID!, result: successResult); GifLoaderDialogUtils.hideDialog(context); checkPaymentStatus(AppoitmentAllHistoryResultList()); }, projectId: widget.projectID, serviceTypeEnum: ServiceTypeEnum.appointmentPayment, ); } else { GifLoaderDialogUtils.hideDialog(context); AppToast.showErrorToast(message: "An error occurred while processing your request"); } }).catchError((err) { print(err); GifLoaderDialogUtils.hideDialog(context); AppToast.showErrorToast(message: err); }); } checkPaymentStatus(AppoitmentAllHistoryResultList appo) { GifLoaderDialogUtils.showMyDialog(context); DoctorsListService service = new DoctorsListService(); service.checkPaymentStatus(transID, false, context).then((res) { String paymentInfo = res['Response_Message']; if (paymentInfo == 'Success') { GifLoaderDialogUtils.hideDialog(context); if (widget.isERBookAppointment) { // createAdvancePayment(res, appo); ER_createAdvancePayment(res, appo); } else { autoGenerateInvoiceER(res); } } else { GifLoaderDialogUtils.hideDialog(context); AppToast.showErrorToast(message: res['Response_Message']); } }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); AppToast.showErrorToast(message: err); print(err); }); } ER_createAdvancePayment(payment_res, AppoitmentAllHistoryResultList appo) { GifLoaderDialogUtils.showMyDialog(context); DoctorsListService service = new DoctorsListService(); String paymentReference = payment_res['Fort_id'].toString(); service.ER_createAdvancePayment(appo, widget.projectID.toString(), payment_res['Amount'], payment_res['Fort_id'], payment_res['PaymentMethod'], context).then((res) { GifLoaderDialogUtils.hideDialog(context); addAdvancedNumberRequest( // Utils.isVidaPlusProject(projectViewModel, widget.projectID) // ? res['OnlineCheckInAppointments'][0]['AdvanceNumber_VP'].toString() // : res['OnlineCheckInAppointments'][0]['AdvanceNumber'].toString(), res['ER_AdvancePaymentResponse']['AdvanceNumber'].toString(), paymentReference, 0, appo, payment_res); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); AppToast.showErrorToast(message: err); print(err); }); } createAdvancePayment(paymentRes, AppoitmentAllHistoryResultList appo) { GifLoaderDialogUtils.showMyDialog(context); DoctorsListService service = new DoctorsListService(); String paymentReference = paymentRes['Fort_id'].toString(); service.HIS_createAdvancePayment(appo, widget.projectID.toString(), paymentRes['Amount'], paymentRes['Fort_id'], paymentRes['PaymentMethod'], projectViewModel.user.patientType, projectViewModel.user.firstName! + " " + projectViewModel.user.lastName!, projectViewModel.user.patientID, context) .then((res) { addAdvancedNumberRequest( Utils.isVidaPlusProject(projectViewModel, widget.projectID) ? res['OnlineCheckInAppointments'][0]['AdvanceNumber_VP'].toString() : res['OnlineCheckInAppointments'][0]['AdvanceNumber'].toString(), paymentReference, 0, appo, paymentRes); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); AppToast.showErrorToast(message: err); print(err); }); } addAdvancedNumberRequest(String advanceNumber, String paymentReference, dynamic appointmentID, AppoitmentAllHistoryResultList appo, paymentRes) { GifLoaderDialogUtils.showMyDialog(context); DoctorsListService service = new DoctorsListService(); service.addAdvancedNumberRequest(advanceNumber, paymentReference, appointmentID, context).then((res) { GifLoaderDialogUtils.hideDialog(context); if (widget.isERBookAppointment) { AppToast.showSuccessToast(message: "Your appointment has been booked successfully. Please perform Check-In once you arrive at the hospital."); // Navigator.pop(context); // Navigator.pop(context); // Navigator.pop(context); Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => LandingPage()), (Route r) => false); } else { autoGenerateInvoiceER(paymentRes); } }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); AppToast.showErrorToast(message: err.toString()); print(err); }); } autoGenerateInvoiceER(res) { GifLoaderDialogUtils.showMyDialog(context); DoctorsListService service = new DoctorsListService(); service.autoGenerateInvoiceERClinic(widget.projectID, 4, res['Fort_id'], res['Amount'], res['PaymentMethod'], res['CardNumber'], res['Merchant_Reference'], res['RRN'], false).then((res) { GifLoaderDialogUtils.hideDialog(context); _showMyDialog(TranslationBase.of(context).ERCheckInSuccess, context); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); AppToast.showErrorToast(message: err); print(err); }); } Future _showMyDialog(String message, BuildContext context) async { return showDialog( context: context, barrierDismissible: true, // user must tap button! builder: (BuildContext context) { return AlertDialog( title: const Text('Alert'), content: SingleChildScrollView( child: ListBody( children: [ Text(message), ], ), ), actions: [ TextButton( child: const Text('OK'), onPressed: () { Navigator.of(context).pop(); Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => LandingPage()), (Route r) => false); }, ), ], ); }, ); } getEROnlineCheckInPaymentDetails() { GifLoaderDialogUtils.showMyDialog(context); ClinicListService ancillaryOrdersService = new ClinicListService(); ancillaryOrdersService.getEROnlineCheckInPaymentDetails(widget.projectID, 10).then((response) { erOnlineCheckInPaymentDetailsResponse = EROnlineCheckInPaymentDetailsResponse.fromJson(response["ResponsePatientShare"]); GifLoaderDialogUtils.hideDialog(context); setState(() {}); }).catchError((err) { AppToast.showErrorToast(message: err.toString()); GifLoaderDialogUtils.hideDialog(context); }); } _amountView(String title, String value, {bool isBold = false, bool isTotal = false}) { return Padding( padding: const EdgeInsets.only(top: 10, bottom: 10), child: Row(children: [ Expanded( child: _getNormalText(title), ), Expanded( child: Row( children: [ projectViewModel.isArabic ? Container() : SvgPicture.asset("assets/images/new/Saudi_Riyal_Symbol.svg", width: 16, height: 16), mWidth(6), _getNormalText(value, isBold: isBold, isTotal: isTotal), mWidth(6), projectViewModel.isArabic ? SvgPicture.asset("assets/images/new/Saudi_Riyal_Symbol.svg", width: 16, height: 16) : Container(), ], ), ), ]), ); } _getNormalText(text, {bool isBold = false, bool isTotal = false}) { return Text( text, style: TextStyle( fontSize: isBold ? isTotal ? 16 : 12 : 11, letterSpacing: -0.5, color: isBold ? Color(0xff2E303A) : Color(0xff575757), fontWeight: isTotal ? FontWeight.bold : FontWeight.w600, ), ); } }