Android_Retail/src/Pages/BookingScreen/Template/PaymentOptions.js

591 lines
20 KiB
JavaScript

import { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import {
FeatureAddon,
getPaymentOptionFeatureApi,
changeCommonPaymentOptions,
} from '../../../Features/BookingScreen/BookingData/BookingData';
import {
axiosRetailInstanceData,
axiosCommonInstanceData,
} from '../../../Features/AuthenicationToken/AuthenticationToken';
import { createAsyncThunk } from '@reduxjs/toolkit';
import { getSession } from '../../../Services/Others';
export const getGatewayConfig = createAsyncThunk(
'PaymentOptions/getGatewayConfig',
async ({ CompId, AppId, BranchId }) => {
if (
CompId != undefined &&
BranchId != undefined &&
AppId != undefined &&
CompId != null &&
BranchId != null &&
AppId != null
) {
return await axiosCommonInstanceData.get(
`/PaymentGatewayConfig?CompId=${CompId}&BranchId=${BranchId}&AppId=${AppId}&ActiveStatus=A`
);
}
}
);
export const getDeviceConfig = createAsyncThunk(
'PaymentOptions/getDeviceConfig',
async ({ CompId, AppId, BranchId, UserId }) => {
if (
CompId != undefined &&
BranchId != undefined &&
AppId != undefined &&
UserId != undefined &&
CompId != null &&
BranchId != null &&
AppId != null &&
UserId != null
) {
return await axiosRetailInstanceData.get(
`/PaymentDeviceAccess?compId=${CompId}&branchId=${BranchId}&appId=${AppId}&userId=${UserId}&ActiveStatus=A`
);
}
}
);
export const getPaymentUPIDetails = createAsyncThunk(
'getPaymentGatewayDetails',
async ({ CompId, AppId, BranchId, DetailType }) => {
if (
CompId != undefined &&
BranchId != undefined &&
AppId != undefined &&
CompId != null &&
BranchId != null &&
AppId != null
) {
return await axiosRetailInstanceData.get(
`/upi?CompId=${CompId}&BranchId=${BranchId}&AppId=${AppId}&detailType=${DetailType}&ActiveStatus=A`
);
}
}
);
export const usePaymentOptions = ({ AppId, CompId, BranchId, UserId }) => {
const dispatch = useDispatch();
const UserType = getSession('UserType');
useEffect(() => {
const fetchData = async () => {
let Data = { CompId, BranchId, AppId };
let Response = await dispatch(getPaymentOptionFeatureApi(Data)).unwrap();
let FeatureAddonData = [];
if (UserType !== 'Super Admin' && UserType !== 'Super Admin User') {
let featureAddon = await dispatch(
FeatureAddon({ AppId, UserId })
).unwrap();
if (featureAddon?.data?.statusCode === 1) {
FeatureAddonData = featureAddon?.data?.data?.[0]?.FeatAddonHdr?.[0];
}
}
if (Response?.data?.statusCode === 1) {
let PaymentDetails = Response?.data?.data?.[0]?.PaymentDetails?.find(
(item) => item.FlowName === 'Sales'
)?.OptionDetails;
let Formated_Payment_Details = PaymentDetails;
let Payment_Device = PaymentDetails?.find(
(item) => item.OptionName === 'Payment Device'
);
let Payment_Gateway = PaymentDetails?.find(
(item) => item.OptionName === 'Payment Gateway'
);
let PATC;
let PD;
let PG;
let PDConfigData;
let PGConfigData;
// Filtering Based On Access
if (Payment_Device !== undefined) {
let PDResponse = await dispatch(
getDeviceConfig({ CompId, BranchId, AppId, UserId })
).unwrap();
if (PDResponse?.data?.statusCode === 0) {
Formated_Payment_Details = Formated_Payment_Details?.filter(
(item) => item.OptionName !== 'Payment Device'
);
} else {
PDConfigData = PDResponse?.data?.data;
}
}
if (Payment_Gateway !== undefined) {
let PGResponse = await dispatch(
getGatewayConfig({ CompId, BranchId, AppId })
).unwrap();
if (PGResponse?.data?.statusCode === 0) {
Formated_Payment_Details = Formated_Payment_Details?.filter(
(item) => item.OptionName !== 'Payment Gateway'
);
} else {
PGConfigData = PGResponse?.data?.data;
}
}
if (
FeatureAddonData?.FeatureDtls?.find(
(item) =>
item?.FeatureAddonName?.toLowerCase() === 'customer display'
) === undefined
) {
Formated_Payment_Details = Formated_Payment_Details?.filter(
(item) => item.OptionName !== 'Payment Gateway'
);
}
// Filtering Based on Availablity
Payment_Device = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Payment Device'
);
Payment_Gateway = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Payment Gateway'
);
let CardCondition =
Payment_Device?.ModeDetails.find(
(item) => item.ModeName === 'Card'
) === undefined &&
Payment_Gateway?.ModeDetails.find(
(item) => item.ModeName === 'DebitCard'
) === undefined &&
Payment_Gateway?.ModeDetails.find(
(item) => item.ModeName === 'CreditCard'
) === undefined;
if (CardCondition) {
Formated_Payment_Details = Formated_Payment_Details.map((item) => {
if (item.OptionName === 'Pay at the counter') {
return {
...item,
ModeDetails: item.ModeDetails.filter(
(item) => item.ModeName !== 'Card'
),
};
}
return item;
});
}
Payment_Device = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Payment Device'
);
Payment_Gateway = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Payment Gateway'
);
let Pay_at_the_counter = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Pay at the counter'
)?.ModeDetails?.find((item) => item.ModeName === 'UPI');
let Pay_at_the_counter_witoutUPIMode = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Pay at the counter'
)?.ModeDetails?.filter((item) => item.ModeName !== 'UPI');
let UpiCondition =
Payment_Device?.ModeDetails.find(
(item) => item.ModeName === 'UPI'
) === undefined &&
Payment_Gateway?.ModeDetails.find(
(item) => item.ModeName === 'UPI'
) === undefined &&
Pay_at_the_counter !== undefined;
let BusinessUPI = Formated_Payment_Details?.find((item) => item.OptionName == 'Business Upi')
if (UpiCondition || BusinessUPI) {
let UpiDetailsResponse = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'U' })
).unwrap();
let BusinessUpi = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'BU' })
).unwrap();
let data = BusinessUpi?.data?.data
const hasNonUPI = Formated_Payment_Details
?.find(item => item.OptionName === 'Pay at the counter')
?.ModeDetails
?.every(e => e?.ModeName !== "UPI");
const hasChooseBusinessUpi = Formated_Payment_Details
?.find(item => item.OptionName === 'Business Upi')
?.ModeDetails
if (
UpiDetailsResponse?.data?.statusCode === 0 && BusinessUpi?.data?.statusCode === 0 && BusinessUpi?.data?.data?.length === 0 &&
UpiDetailsResponse?.data?.data?.length === 0
) {
Formated_Payment_Details = Formated_Payment_Details.map((item) => {
if (item.OptionName === 'Pay at the counter') {
return {
...item,
ModeDetails: Pay_at_the_counter_witoutUPIMode,
};
}
return item;
});
}
else if (BusinessUpi?.data?.data?.length === 1 && hasNonUPI && hasChooseBusinessUpi) {
Formated_Payment_Details = Formated_Payment_Details.map((item) => {
if (item.OptionName === 'Pay at the counter') {
let reqDatfromBusinessUpi = BusinessUPI?.ModeDetails?.[0]
return {
...item,
ModeDetails: [
...item?.ModeDetails,
{
"PaymentOptionId": reqDatfromBusinessUpi?.PaymentOptionId,
"PaymentModeId": reqDatfromBusinessUpi?.PaymentModeId,
"ModeId": reqDatfromBusinessUpi?.ModeId,
"ModeName": "UPI",
"ModeDefault": "Y",
"ModeImage": "",
"PayOpt": null,
"CardType": null,
"MerchantUPIId": null,
"MerchantMobile": null,
"MerchantNumber": null,
"RefreshToken": null,
"Token": null,
"MerchantId": null,
"ActiveStatus": reqDatfromBusinessUpi?.ActiveStatus,
"CreatedBy": 0,
"CreatedDate": "2026-03-03T19:07:54.393",
"UpdatedBy": null,
"UpdatedDate": null,
"MethodDetails": []
}
],
};
}
// IMPORTANT: return other items unchanged
return item;
});
}
else if (UpiDetailsResponse?.data?.statusCode === 0 && UpiDetailsResponse?.data?.data?.length === 0) {
Formated_Payment_Details = Formated_Payment_Details.map((item) => {
if (item.OptionName === 'Pay at the counter') {
return {
...item,
ModeDetails: Pay_at_the_counter_witoutUPIMode,
};
}
return item;
});
}
////
}
if (
Formated_Payment_Details.find(
(item) => item.OptionName === 'Pay at the counter'
) !== undefined
) {
let UPIDetails = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'U' })
).unwrap();
if (UPIDetails?.data?.statusCode === 1) {
PATC = UPIDetails?.data?.data;
}
}
if (
Formated_Payment_Details.find(
(item) => item.OptionName === 'Payment Device'
) !== undefined
) {
let UPIDetails = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'D' })
).unwrap();
if (UPIDetails?.data?.statusCode === 1) {
PD = UPIDetails?.data?.data;
}
}
if (
Formated_Payment_Details.find(
(item) => item.OptionName === 'Payment Gateway'
) !== undefined
) {
let UPIDetails = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'P' })
).unwrap();
if (UPIDetails?.data?.statusCode === 1) {
PG = UPIDetails?.data?.data;
}
}
if (PD === undefined && PG === undefined) {
let RemovedCardData = Formated_Payment_Details.find(
(item) => item.OptionName === 'Pay at the counter'
)?.ModeDetails.filter((item) => item.ModeName !== 'Card');
Formated_Payment_Details = Formated_Payment_Details.map((item) => {
if (item.OptionName === 'Pay at the counter') {
item.ModeDetails = RemovedCardData;
}
return item;
});
}
const desiredOrder = ['Cash', 'Card', 'Credit', 'UPI'];
const Payment_Details = Formated_Payment_Details?.map((item) => {
if (item.OptionName === 'Pay at the counter') {
item.ModeDetails?.sort((a, b) => {
return (
desiredOrder.indexOf(a.ModeName) -
desiredOrder.indexOf(b.ModeName)
);
});
}
return item;
});
let Array = Response?.data?.data?.[0]?.PaymentDetails;
let FinalData = Array?.map((item) => ({
...item,
OptionDetails: Payment_Details,
PaymentDetails: {
Payatthecounter: PATC ?? [],
PaymentDevice: PD ?? [],
PaymentGateway: PG ?? [],
},
PDConfigDetails: PDConfigData,
PGConfigDetails: PGConfigData,
}));
dispatch(changeCommonPaymentOptions(FinalData));
}
};
fetchData();
}, []);
};
// utils/paymentOptions.js
export const fetchAndSetPaymentOptions = async (
dispatch,
{ AppId, CompId, BranchId, UserId }
) => {
const UserType = getSession('UserType');
let Data = { CompId, BranchId, AppId };
let Response = await dispatch(getPaymentOptionFeatureApi(Data)).unwrap();
let FeatureAddonData = [];
if (UserType !== 'Super Admin' && UserType !== 'Super Admin User') {
let featureAddon = await dispatch(FeatureAddon({ AppId, UserId })).unwrap();
if (featureAddon?.data?.statusCode === 1) {
FeatureAddonData = featureAddon?.data?.data?.[0]?.FeatAddonHdr?.[0];
}
}
if (Response?.data?.statusCode === 1) {
let PaymentDetails = Response?.data?.data?.[0]?.PaymentDetails?.find(
(item) => item.FlowName === 'Sales'
)?.OptionDetails;
let Formated_Payment_Details = PaymentDetails;
let Payment_Device = PaymentDetails?.find(
(item) => item.OptionName === 'Payment Device'
);
let Payment_Gateway = PaymentDetails?.find(
(item) => item.OptionName === 'Payment Gateway'
);
let PATC, PD, PG, PDConfigData, PGConfigData;
if (Payment_Device !== undefined) {
let PDResponse = await dispatch(
getDeviceConfig({ CompId, BranchId, AppId, UserId })
).unwrap();
if (PDResponse?.data?.statusCode === 0) {
Formated_Payment_Details = Formated_Payment_Details?.filter(
(item) => item.OptionName !== 'Payment Device'
);
} else {
PDConfigData = PDResponse?.data?.data;
}
}
if (Payment_Gateway !== undefined) {
let PGResponse = await dispatch(
getGatewayConfig({ CompId, BranchId, AppId })
).unwrap();
if (PGResponse?.data?.statusCode === 0) {
Formated_Payment_Details = Formated_Payment_Details?.filter(
(item) => item.OptionName !== 'Payment Gateway'
);
} else {
PGConfigData = PGResponse?.data?.data;
}
}
if (
FeatureAddonData?.FeatureDtls?.find(
(item) => item?.FeatureAddonName?.toLowerCase() === 'customer display'
) === undefined
) {
Formated_Payment_Details = Formated_Payment_Details?.filter(
(item) => item.OptionName !== 'Payment Gateway'
);
}
Payment_Device = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Payment Device'
);
Payment_Gateway = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Payment Gateway'
);
let CardCondition =
Payment_Device?.ModeDetails.find((item) => item.ModeName === 'Card') ===
undefined &&
Payment_Gateway?.ModeDetails.find(
(item) => item.ModeName === 'DebitCard'
) === undefined &&
Payment_Gateway?.ModeDetails.find(
(item) => item.ModeName === 'CreditCard'
) === undefined;
if (CardCondition) {
Formated_Payment_Details = Formated_Payment_Details.map((item) => {
if (item.OptionName === 'Pay at the counter') {
return {
...item,
ModeDetails: item.ModeDetails.filter(
(mode) => mode.ModeName !== 'Card'
),
};
}
return item;
});
}
Payment_Device = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Payment Device'
);
Payment_Gateway = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Payment Gateway'
);
let Pay_at_the_counter = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Pay at the counter'
)?.ModeDetails?.find((item) => item.ModeName === 'UPI');
let Pay_at_the_counter_without_UPI = Formated_Payment_Details?.find(
(item) => item.OptionName === 'Pay at the counter'
)?.ModeDetails?.filter((item) => item.ModeName !== 'UPI');
let UpiCondition =
Payment_Device?.ModeDetails.find((item) => item.ModeName === 'UPI') ===
undefined &&
Payment_Gateway?.ModeDetails.find((item) => item.ModeName === 'UPI') ===
undefined &&
Pay_at_the_counter !== undefined;
if (UpiCondition) {
let UpiDetailsResponse = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'U' })
).unwrap();
if (
UpiDetailsResponse?.data?.statusCode === 0 &&
UpiDetailsResponse?.data?.data?.length === 0
) {
Formated_Payment_Details = Formated_Payment_Details.map((item) => {
if (item.OptionName === 'Pay at the counter') {
return {
...item,
ModeDetails: Pay_at_the_counter_without_UPI,
};
}
return item;
});
}
}
if (
Formated_Payment_Details.find(
(item) => item.OptionName === 'Pay at the counter'
)
) {
let UPIDetails = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'U' })
).unwrap();
if (UPIDetails?.data?.statusCode === 1) {
PATC = UPIDetails?.data?.data;
}
}
if (
Formated_Payment_Details.find(
(item) => item.OptionName === 'Payment Device'
)
) {
let UPIDetails = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'D' })
).unwrap();
if (UPIDetails?.data?.statusCode === 1) {
PD = UPIDetails?.data?.data;
}
}
if (
Formated_Payment_Details.find(
(item) => item.OptionName === 'Payment Gateway'
)
) {
let UPIDetails = await dispatch(
getPaymentUPIDetails({ CompId, AppId, BranchId, DetailType: 'P' })
).unwrap();
if (UPIDetails?.data?.statusCode === 1) {
PG = UPIDetails?.data?.data;
}
}
if (PD === undefined && PG === undefined) {
let RemovedCardData = Formated_Payment_Details.find(
(item) => item.OptionName === 'Pay at the counter'
)?.ModeDetails.filter((item) => item.ModeName !== 'Card');
Formated_Payment_Details = Formated_Payment_Details.map((item) => {
if (item.OptionName === 'Pay at the counter') {
item.ModeDetails = RemovedCardData;
}
return item;
});
}
const desiredOrder = ['Cash', 'Card', 'Credit', 'UPI'];
const Payment_Details = Formated_Payment_Details?.map((item) => {
if (item.OptionName === 'Pay at the counter') {
item.ModeDetails?.sort((a, b) => {
return (
desiredOrder.indexOf(a.ModeName) - desiredOrder.indexOf(b.ModeName)
);
});
}
return item;
});
let Array = Response?.data?.data?.[0]?.PaymentDetails;
let FinalData = Array?.map((item) => ({
...item,
OptionDetails: Payment_Details,
PaymentDetails: {
Payatthecounter: PATC ?? [],
PaymentDevice: PD ?? [],
PaymentGateway: PG ?? [],
},
PDConfigDetails: PDConfigData,
PGConfigDetails: PGConfigData,
}));
dispatch(changeCommonPaymentOptions(FinalData));
return FinalData;
}
dispatch(changeCommonPaymentOptions([]));
return [];
};