|
|
|
|
@ -1,8 +1,18 @@
|
|
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/core/api_consts.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/core/app_state.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/core/cache_consts.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/core/dependencies.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/core/enums.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/core/myFatoorah_config.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/core/utils/utils.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/features/payfort/models/payfort_request_insert_model.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/features/payfort/models/payfort_response_insert_model.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/features/payfort/payfort_repo.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/generated/codegen_loader.g.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/services/error_handler_service.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/theme/colors.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart';
|
|
|
|
|
@ -11,10 +21,12 @@ import 'package:myfatoorah_flutter/myfatoorah_flutter.dart';
|
|
|
|
|
class MyFatoorahViewModel extends ChangeNotifier {
|
|
|
|
|
final AppState appState;
|
|
|
|
|
final ErrorHandlerService errorHandlerService;
|
|
|
|
|
final PayfortRepo payfortRepo;
|
|
|
|
|
|
|
|
|
|
MyFatoorahViewModel({
|
|
|
|
|
required this.appState,
|
|
|
|
|
required this.errorHandlerService,
|
|
|
|
|
required this.payfortRepo,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Payment Method ID
|
|
|
|
|
@ -25,26 +37,94 @@ class MyFatoorahViewModel extends ChangeNotifier {
|
|
|
|
|
num amount = 10.0,
|
|
|
|
|
paymentDescription = "",
|
|
|
|
|
String displayCurrencyIso = MFCurrencyISO.KUWAIT_KWD,
|
|
|
|
|
required String paymentOption,
|
|
|
|
|
required String liveServiceID,
|
|
|
|
|
required String isScheduleLiveCare,
|
|
|
|
|
required String appointmentDate,
|
|
|
|
|
required String appointmentNo,
|
|
|
|
|
required String doctorID,
|
|
|
|
|
required String fileNumber,
|
|
|
|
|
required String serviceID,
|
|
|
|
|
required String projectID,
|
|
|
|
|
Function(MFGetPaymentStatusResponse)? onSuccess,
|
|
|
|
|
Function(String)? onError,
|
|
|
|
|
}) async {
|
|
|
|
|
LoaderBottomSheet.showLoader();
|
|
|
|
|
|
|
|
|
|
await MFSDK.init(MyFatoorahConfig.configAPIKey, ApiConsts.myFatoorahCountry, ApiConsts.myFatoorahEnvironmentType);
|
|
|
|
|
|
|
|
|
|
MFSDK.setUpActionBar(toolBarTitle: paymentDescription, toolBarTitleColor: "#ffffff", toolBarBackgroundColor: '#ED1C2B', isShowToolBar: true);
|
|
|
|
|
|
|
|
|
|
var request = MFExecutePaymentRequest(paymentMethodId: 2, invoiceValue: amount);
|
|
|
|
|
var request = MFExecutePaymentRequest(paymentMethodId: paymentOption.toLowerCase() == "mada" ? 6 : 2, invoiceValue: amount);
|
|
|
|
|
request.displayCurrencyIso = displayCurrencyIso;
|
|
|
|
|
request.customerName = ApiConsts.appEnvironmentType == AppEnvironmentTypeEnum.uat ? "Test test" : "${appState.getAuthenticatedUser()!.firstName} ${appState.getAuthenticatedUser()!.lastName}";
|
|
|
|
|
request.customerEmail = "CustID_${appState.getAuthenticatedUser()!.patientId}@HMG.com";
|
|
|
|
|
|
|
|
|
|
await MFSDK.executePayment(request, appState.isArabic() ? MFLanguage.ARABIC : MFLanguage.ENGLISH, (invoiceId) {
|
|
|
|
|
await MFSDK.executePayment(request, appState.isArabic() ? MFLanguage.ARABIC : MFLanguage.ENGLISH, (invoiceId) async {
|
|
|
|
|
PayfortRequestInsertModel payfortRequestInsertModel = PayfortRequestInsertModel();
|
|
|
|
|
|
|
|
|
|
payfortRequestInsertModel.commandType = "SALE";
|
|
|
|
|
payfortRequestInsertModel.amount = amount;
|
|
|
|
|
payfortRequestInsertModel.customerEmail = getIt.get<AppState>().getAuthenticatedUser()!.emailAddress;
|
|
|
|
|
payfortRequestInsertModel.orderDescription = paymentDescription;
|
|
|
|
|
payfortRequestInsertModel.serviceID = serviceID;
|
|
|
|
|
payfortRequestInsertModel.paymentOption = paymentOption;
|
|
|
|
|
payfortRequestInsertModel.clientRequestID = invoiceId;
|
|
|
|
|
payfortRequestInsertModel.projectID = projectID;
|
|
|
|
|
payfortRequestInsertModel.fileNumber = getIt.get<AppState>().getAuthenticatedUser()!.patientId.toString();
|
|
|
|
|
payfortRequestInsertModel.customerName = "${getIt.get<AppState>().getAuthenticatedUser()!.firstName} ${getIt.get<AppState>().getAuthenticatedUser()!.lastName}";
|
|
|
|
|
payfortRequestInsertModel.currency = getIt.get<AppState>().getAuthenticatedUser()!.outSa! == 0 ? "SAR" : "AED";
|
|
|
|
|
payfortRequestInsertModel.liveServiceID = liveServiceID;
|
|
|
|
|
payfortRequestInsertModel.latitude = getIt.get<AppState>().userLat.toString();
|
|
|
|
|
payfortRequestInsertModel.longitude = getIt.get<AppState>().userLong.toString();
|
|
|
|
|
payfortRequestInsertModel.merchantReference = invoiceId;
|
|
|
|
|
payfortRequestInsertModel.deviceToken = await Utils.getStringFromPrefs(CacheConst.pushToken);
|
|
|
|
|
payfortRequestInsertModel.language = getIt.get<AppState>().isArabic() ? "ar" : "en";
|
|
|
|
|
payfortRequestInsertModel.integrationId = "6"; // PayTabs integration ID
|
|
|
|
|
payfortRequestInsertModel.isSchedule = isScheduleLiveCare;
|
|
|
|
|
payfortRequestInsertModel.appointmentDate = appointmentDate;
|
|
|
|
|
payfortRequestInsertModel.appointmentNo = appointmentNo;
|
|
|
|
|
payfortRequestInsertModel.doctorID = doctorID;
|
|
|
|
|
|
|
|
|
|
// Call request insert API before payment
|
|
|
|
|
await paytabsRequestInsert(payfortRequestInsertModel: payfortRequestInsertModel);
|
|
|
|
|
|
|
|
|
|
LoaderBottomSheet.hideLoader();
|
|
|
|
|
print(invoiceId);
|
|
|
|
|
}).then((value) {
|
|
|
|
|
if (onSuccess != null) {
|
|
|
|
|
onSuccess(value);
|
|
|
|
|
}).then((value) async {
|
|
|
|
|
var valJson = value.toString();
|
|
|
|
|
print(valJson);
|
|
|
|
|
|
|
|
|
|
// // Create PayfortResponseInsertModel (same as Payfort implementation)
|
|
|
|
|
PayfortResponseInsertModel payfortResponseInsertModel = PayfortResponseInsertModel();
|
|
|
|
|
|
|
|
|
|
payfortResponseInsertModel.fortId = value.invoiceTransactions!.first.transactionId ?? "";
|
|
|
|
|
payfortResponseInsertModel.commandType = "SALE";
|
|
|
|
|
// payfortResponseInsertModel.amount = paytabsTransactionResponseModel.tranTotal ?? 0;
|
|
|
|
|
payfortResponseInsertModel.amount = amount;
|
|
|
|
|
payfortResponseInsertModel.sadadOLP = "";
|
|
|
|
|
payfortResponseInsertModel.paymentOption = value.invoiceTransactions!.first.paymentGateway ?? "";
|
|
|
|
|
payfortResponseInsertModel.customerIP = "";
|
|
|
|
|
payfortResponseInsertModel.eCI = "";
|
|
|
|
|
payfortResponseInsertModel.responseMessage = value.invoiceTransactions!.first.transactionStatus ?? "";
|
|
|
|
|
payfortResponseInsertModel.cardNumber = value.invoiceTransactions!.first.cardNumber ?? "";
|
|
|
|
|
payfortResponseInsertModel.cardBin = "";
|
|
|
|
|
payfortResponseInsertModel.installmentsNumber = 0;
|
|
|
|
|
payfortResponseInsertModel.status = (value.invoiceStatus ?? "").toLowerCase() == "paid" ? "14" : "00";
|
|
|
|
|
payfortResponseInsertModel.merchantRef = value.invoiceId.toString();
|
|
|
|
|
payfortResponseInsertModel.isRefund = false;
|
|
|
|
|
payfortResponseInsertModel.remmeberMe = true;
|
|
|
|
|
payfortResponseInsertModel.reconciliationReference = "";
|
|
|
|
|
|
|
|
|
|
// Call response insert API after payment
|
|
|
|
|
await paytabsResponseInsert(payfortResponseInsertModel: payfortResponseInsertModel);
|
|
|
|
|
|
|
|
|
|
if ((value.invoiceStatus ?? "").toLowerCase() == "paid") {
|
|
|
|
|
if (onSuccess != null) {
|
|
|
|
|
onSuccess(value);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (onError != null) {
|
|
|
|
|
onError(LocaleKeys.paymentFailedPleaseTryAgain.tr());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).catchError((error) {
|
|
|
|
|
if (onError != null) {
|
|
|
|
|
@ -52,4 +132,60 @@ class MyFatoorahViewModel extends ChangeNotifier {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initApplePayPayment(num invoiceValue, MFApplePayButton mfApplePayButton) async {
|
|
|
|
|
await MFSDK.init(MyFatoorahConfig.configAPIKey, ApiConsts.myFatoorahCountry, ApiConsts.myFatoorahEnvironmentType);
|
|
|
|
|
|
|
|
|
|
await MFSDK.initSession(MFInitiateSessionRequest(), MFLanguage.ENGLISH).then((value) async {
|
|
|
|
|
var executePaymentRequest = MFExecutePaymentRequest(invoiceValue: 121, displayCurrencyIso: MFCurrencyISO.KUWAIT_KWD);
|
|
|
|
|
|
|
|
|
|
await mfApplePayButton.executeApplePayButton(
|
|
|
|
|
executePaymentRequest,
|
|
|
|
|
(String invoiceId) {
|
|
|
|
|
print("Payment Success! Invoice ID: $invoiceId");
|
|
|
|
|
},
|
|
|
|
|
).catchError((error) {
|
|
|
|
|
// If initialized properly, this error won't occur
|
|
|
|
|
print("Apple Pay Error: ${error.message}");
|
|
|
|
|
});
|
|
|
|
|
}).catchError((error) {
|
|
|
|
|
log(error.message);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> paytabsRequestInsert({required PayfortRequestInsertModel payfortRequestInsertModel, Function(dynamic)? onSuccess, Function(String)? onError}) async {
|
|
|
|
|
final result = await payfortRepo.payfortRequestInsert(payfortRequestInsertModel: payfortRequestInsertModel);
|
|
|
|
|
|
|
|
|
|
result.fold(
|
|
|
|
|
(failure) async => await errorHandlerService.handleError(failure: failure),
|
|
|
|
|
(apiResponse) {
|
|
|
|
|
if (apiResponse.messageStatus == 2) {
|
|
|
|
|
// dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
|
|
|
|
|
} else if (apiResponse.messageStatus == 1) {
|
|
|
|
|
notifyListeners();
|
|
|
|
|
if (onSuccess != null) {
|
|
|
|
|
onSuccess(apiResponse);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> paytabsResponseInsert({required PayfortResponseInsertModel payfortResponseInsertModel, Function(dynamic)? onSuccess, Function(String)? onError}) async {
|
|
|
|
|
final result = await payfortRepo.payfortResponseInsert(payfortResponseInsertModel: payfortResponseInsertModel);
|
|
|
|
|
|
|
|
|
|
result.fold(
|
|
|
|
|
(failure) async => await errorHandlerService.handleError(failure: failure),
|
|
|
|
|
(apiResponse) {
|
|
|
|
|
if (apiResponse.messageStatus == 2) {
|
|
|
|
|
// dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
|
|
|
|
|
} else if (apiResponse.messageStatus == 1) {
|
|
|
|
|
notifyListeners();
|
|
|
|
|
if (onSuccess != null) {
|
|
|
|
|
onSuccess(apiResponse);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|