diff --git a/src/hooks/useOrangeMoney.tsx b/src/hooks/useOrangeMoney.tsx index 7347fdf..d921f2e 100644 --- a/src/hooks/useOrangeMoney.tsx +++ b/src/hooks/useOrangeMoney.tsx @@ -6,12 +6,12 @@ import { getTransactionsData, } from "@/utils/requests/orangePayment"; import ErrorModal from "@components/modals/ErrorModal"; -import InformationModal from "@components/modals/InformationModal"; import LoadingModal from "@components/modals/LoadingModal"; import { LOG } from "@logger"; import type { NativeStackNavigationProp } from "@react-navigation/native-stack"; import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { AxiosError } from "axios"; import * as WebBrowser from "expo-web-browser"; import { useState } from "react"; @@ -88,40 +88,84 @@ const useOrangeMoney = ( // }, }); + // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: + const checkStatus = async (orderId: string, retry = 0): Promise => { + // Check the transactions 'retry' time. + let numberOfTries = 0; + + while (numberOfTries <= retry) { + log.verbose("useOrangePayment | checkStatus | Try No.", numberOfTries + 1); + try { + await transactionsStatusMutation.mutateAsync(orderId); + return; + } catch (error) { + // errors are throwned from getTransactions status when the staus is either initiated or failed. + if (error instanceof AxiosError && error.response?.status === 404) { + log.error("useOrangeMoney | checkStatus | Transaction not found"); + return error.response.data.error || `Transaction ${orderId} not found !!`; + } + if (error instanceof AxiosError && error.name === "ORANGE_PAYMENT_FAILED") { + return "La transaction à échoué."; + } + if (error instanceof AxiosError && error.name === "ORANGE_UNKNOWN_PAYMENT_ERROR") { + return "Une erreur est survenue."; + } + } + + numberOfTries += 1; + } + + return "La transaction est toujours en cours."; + }; + const openBrowserThenCheckStatus = async (paymentUrl: string, orderId: string) => { try { await handlePaymentUsingBrowser(paymentUrl); log.info("openBrowserThenCheckStatus | Verifying transaction status..."); showModal(); - await transactionsStatusMutation.mutateAsync(orderId); - closeModal(); - navigation?.getParent()?.navigate("paymentResultScreen"); + // await transactionsStatusMutation.mutateAsync(orderId); + const message = await checkStatus(orderId); + if (message) { + showModal(); + } + // closeModal(); } catch (error: unknown) { - log.error("openBrowserThenCheckStatus Catch Block|", error); - if (error instanceof Error) { - log.debug("1"); - if (error.name === "ORANGE_PAYMENT_IN_PROGRESS") { - log.debug("2"); - - log.warn("openBrowserThenCheckStatus | ORANGE_PAYMENT_IN_PROGRESS"); - await showModal( - openBrowserThenCheckStatus(paymentUrl, orderId)} - />, - ); - log.debug("3"); - } else if (error.name === "ORANGE_PAYMENT_FAILED") { - showModal(); - log.error("openBrowserThenCheckStatus | ORANGE_PAYMENT_FAILED"); - } - log.debug("4 --", error.name); + log.verbose("Error catching"); + if (error instanceof AxiosError) { + log.error("openBrowserThenCheckStatus Catch Block|", error); + showModal( + , + ); } else { - log.error("openBrowserThenCheckStatus Else Block|", error); - closeModal(); - throw error; + showModal(); } + // log.error("openBrowserThenCheckStatus Catch Block|", error); + // if (error instanceof Error) { + // log.debug("1"); + // if (error.name === "ORANGE_PAYMENT_IN_PROGRESS") { + // log.debug("2"); + + // log.warn("openBrowserThenCheckStatus | ORANGE_PAYMENT_IN_PROGRESS"); + // await showModal( + // openBrowserThenCheckStatus(paymentUrl, orderId)} + // />, + // ); + // log.debug("3"); + // } else if (error.name === "ORANGE_PAYMENT_FAILED") { + // showModal(); + // log.error("openBrowserThenCheckStatus | ORANGE_PAYMENT_FAILED"); + // } + // log.debug("4 --", error.name); + // } else { + // log.error("openBrowserThenCheckStatus Else Block|", error); + // closeModal(); + // throw error; + // } } };