Commit 7b82a23c by G

handle orange payment logic inside the hook for cleaner component

parent 3cd6096a
import { useModalsManagerContext } from "@/contexts/ModalsManagerContext";
import { import {
type IorangePaymentStarter, type IorangePaymentStarter,
getTransactionStatus, getTransactionStatus,
getTransactionsData, getTransactionsData,
} from "@/utils/requests/orangePayment"; } from "@/utils/requests/orangePayment";
import ErrorModal from "@components/modals/ErrorModal";
import LoadingModal from "@components/modals/LoadingModal";
import { LOG } from "@logger"; import { LOG } from "@logger";
import { useMutation } from "@tanstack/react-query"; import { useMutation } from "@tanstack/react-query";
...@@ -22,6 +25,7 @@ const paymentObjectDefault: IorangePaymentStarter = { ...@@ -22,6 +25,7 @@ const paymentObjectDefault: IorangePaymentStarter = {
}; };
const useOrangeMoney = () => { const useOrangeMoney = () => {
const [isBrowserOpen, setIsBrowserOpen] = useState(false); const [isBrowserOpen, setIsBrowserOpen] = useState(false);
const { showModal, closeModal } = useModalsManagerContext();
const handlePaymentUsingBrowser = async (url: string) => { const handlePaymentUsingBrowser = async (url: string) => {
setIsBrowserOpen(true); setIsBrowserOpen(true);
...@@ -70,6 +74,48 @@ const useOrangeMoney = () => { ...@@ -70,6 +74,48 @@ const useOrangeMoney = () => {
}, },
}); });
const openBrowserThenCheckStatus = async (paymentUrl: string, orderId: string) => {
try {
await handlePaymentUsingBrowser(paymentUrl);
log.info("Verifying transaction status...");
showModal(<LoadingModal message="Vérification du statut de la transaction..." />);
await transactionsStatusMutation.mutateAsync(orderId);
} catch (error) {
log.error("handlePaymentButton |", error);
if (error instanceof Error) {
if (error.name === "ORANGE_PAYMENT_IN_PROGRESS") {
log.warn("handlePaymentButton | ORANGE_PAYMENT_IN_PROGRESS");
showModal(
<ErrorModal
message="Le payment est toujours en cours."
onPress={async () =>
await openBrowserThenCheckStatus(paymentUrl, orderId)
}
/>,
);
} else if (error.name === "ORANGE_PAYMENT_FAILED") {
showModal(<ErrorModal message="Le paiment à échoué." onPress={closeModal} />);
log.error("handlePaymentButton | ORANGE_PAYMENT_FAILED");
}
} else {
log.error("handlePaymentButton |", error);
closeModal();
throw error;
}
}
};
const makePayment = async (amount: number) => {
try {
showModal(<LoadingModal message="Initialization de la transaction." />);
const { payment_url, order_id } =
await orangeTransactionInitializerMutation.mutateAsync(amount);
await openBrowserThenCheckStatus(payment_url, order_id);
} catch (error) {
log.error("makePayment |", error);
}
};
return { return {
orangeTransactionInitializerMutation: orangeTransactionInitializerMutation, orangeTransactionInitializerMutation: orangeTransactionInitializerMutation,
...@@ -78,6 +124,7 @@ const useOrangeMoney = () => { ...@@ -78,6 +124,7 @@ const useOrangeMoney = () => {
isWaitingForOmPaymentUrl: orangeTransactionInitializerMutation.isPending, isWaitingForOmPaymentUrl: orangeTransactionInitializerMutation.isPending,
isCheckingForTransactionStatus: transactionsStatusMutation.isPending, isCheckingForTransactionStatus: transactionsStatusMutation.isPending,
transactionsStatusMutation, transactionsStatusMutation,
makePayment,
}; };
}; };
......
...@@ -8,8 +8,6 @@ import PaymentOption from "@components/PaymentOption"; ...@@ -8,8 +8,6 @@ import PaymentOption from "@components/PaymentOption";
import BackgroundDefault from "@components/backgrounds/BackgroundDefault"; import BackgroundDefault from "@components/backgrounds/BackgroundDefault";
import Box from "@components/bases/Box"; import Box from "@components/bases/Box";
import Text from "@components/bases/Text"; import Text from "@components/bases/Text";
import ErrorModal from "@components/modals/ErrorModal";
import LoadingModal from "@components/modals/LoadingModal";
import useOrangeMoney from "@hooks/useOrangeMoney"; import useOrangeMoney from "@hooks/useOrangeMoney";
import { LOG } from "@logger"; import { LOG } from "@logger";
import { useCallback, useState } from "react"; import { useCallback, useState } from "react";
...@@ -33,6 +31,7 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI ...@@ -33,6 +31,7 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI
isWaitingForOmPaymentUrl, isWaitingForOmPaymentUrl,
isCheckingForTransactionStatus, isCheckingForTransactionStatus,
transactionsStatusMutation, transactionsStatusMutation,
makePayment,
} = useOrangeMoney(); } = useOrangeMoney();
const insets = useSafeAreaInsets(); const insets = useSafeAreaInsets();
...@@ -52,55 +51,17 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI ...@@ -52,55 +51,17 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI
return setAmountToPay(0); return setAmountToPay(0);
}; };
const orangePaymentSequence = useCallback(async () => {
log.info("Calling api with amount :: ", amountToPay);
try {
showModal(<LoadingModal message="Initialization de la transaction." />);
const { payment_url, order_id } =
await orangeTransactionInitializerMutation.mutateAsync(amountToPay);
// closeModal();
showModal(<LoadingModal message="Ouverture de l'interface de paiement..." />);
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(<LoadingModal message="Vérification du statut de la transaction..." />);
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(
<ErrorModal
message="Le payment est toujours en cours."
onPress={closeModal}
/>,
);
} else if (error.name === "ORANGE_PAYMENT_FAILED") {
showModal(<ErrorModal message="Le paiment à échoué." onPress={closeModal} />);
log.error("handlePaymentButton | ORANGE_PAYMENT_FAILED");
}
}
//TODO : handle error
}
}, [
amountToPay,
orangeTransactionInitializerMutation,
navigation,
transactionsStatusMutation,
handlePaymentUsingBrowser,
closeModal,
showModal,
]);
const handlePaymentButton = useCallback(async () => { const handlePaymentButton = useCallback(async () => {
switch (paymentType) { switch (paymentType) {
case "OM": { case "OM": {
Keyboard.dismiss(); Keyboard.dismiss();
log.info("OM so we stays on screen !!"); log.info("OM so we stays on screen !!");
await orangePaymentSequence(); // await orangePaymentSequence();
try {
await makePayment(amountToPay);
} catch (error) {
log.error("handlePaymentButton |", error);
}
break; break;
} }
...@@ -109,7 +70,7 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI ...@@ -109,7 +70,7 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI
navigation.navigate("numberAndOtpForPaymentScreen"); navigation.navigate("numberAndOtpForPaymentScreen");
break; break;
} }
}, [paymentType, navigation, orangePaymentSequence]); }, [paymentType, navigation, amountToPay, makePayment]);
return ( return (
<BackgroundDefault> <BackgroundDefault>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment