import { useModalsManagerContext } from "@/contexts/ModalsManagerContext";
import type { PaymentStackScreenComponentProps } from "@/navigations/Types";
import BeasyLogoIcon from "@components/BeasyLogoIcon";
import Button from "@components/Button";
import GoBackIconButton from "@components/GoBackIconButton";
import Input from "@components/Input";
import PaymentOption from "@components/PaymentOption";
import BackgroundDefault from "@components/backgrounds/BackgroundDefault";
import Box from "@components/bases/Box";
import Text from "@components/bases/Text";
import ErrorModal from "@components/modals/ErrorModal";
import LoadingModal from "@components/modals/LoadingModal";
import useOrangeMoney from "@hooks/useOrangeMoney";
import { LOG } from "@logger";
import { useCallback, useState } from "react";
import { Keyboard, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
const log = LOG.extend("PaymentAmountInputScreen");
const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountInputScreen"> = ({
route,
navigation,
}) => {
log.debug("PaymentAmountInputScreen");
const { paymentType } = route.params;
const [amountToPay, setAmountToPay] = useState(0);
const { showModal, closeModal } = useModalsManagerContext();
const {
orangeTransactionInitializerMutation,
handlePaymentUsingBrowser,
isBrowserOpen,
isWaitingForOmPaymentUrl,
isCheckingForTransactionStatus,
transactionsStatusMutation,
} = useOrangeMoney();
const insets = useSafeAreaInsets();
log.debug({
isWaitingForOmPaymentUrl,
isCheckingForTransactionStatus,
isBrowserOpen,
});
const updateAmountToPay = (amount: string) => {
const amountParsed = Number.parseInt(amount);
if (!Number.isNaN(amountParsed)) {
return setAmountToPay(amountParsed);
}
return setAmountToPay(0);
};
const orangePaymentSequence = useCallback(async () => {
log.info("Calling api with amount :: ", amountToPay);
try {
showModal();
const { payment_url, order_id } =
await orangeTransactionInitializerMutation.mutateAsync(amountToPay);
// closeModal();
showModal();
log.info("Url de paiement orange", payment_url);
log.info("Ouverture du navigateur sur la page de paiement...");
await handlePaymentUsingBrowser(payment_url);
log.info("Verifying transaction status...");
showModal();
const { status } = await transactionsStatusMutation.mutateAsync(order_id);
navigation.getParent()?.navigate("paymentResultScreen");
} catch (error: unknown) {
log.error("handlePaymentButton |", error);
if (error instanceof Error) {
if (error.name === "ORANGE_PAYMENT_IN_PROGRESS") {
log.warn("handlePaymentButton | ORANGE_PAYMENT_IN_PROGRESS");
showModal(
,
);
} else if (error.name === "ORANGE_PAYMENT_FAILED") {
showModal();
log.error("handlePaymentButton | ORANGE_PAYMENT_FAILED");
}
}
//TODO : handle error
}
}, [
amountToPay,
orangeTransactionInitializerMutation,
navigation,
transactionsStatusMutation,
handlePaymentUsingBrowser,
closeModal,
showModal,
]);
const handlePaymentButton = useCallback(async () => {
switch (paymentType) {
case "OM": {
Keyboard.dismiss();
log.info("OM so we stays on screen !!");
await orangePaymentSequence();
break;
}
default:
log.info("Navigating to numberAndOtpForPaymentScreen");
navigation.navigate("numberAndOtpForPaymentScreen");
break;
}
}, [paymentType, navigation, orangePaymentSequence]);
return (
{/* */}
{transactionsStatusMutation.isPending && }
navigation.goBack()} />
{/*
*/}
Montant à payé
{amountToPay}
{}} paymentMethod={paymentType} />
updateAmountToPay(e)}
/>
{/* */}
);
};
export default PaymentAmountInputScreen;
const LoadingScreen = () => {
return (
Verification du status de la transaction.
Veuillez patienter
);
};