Commit af94398e by G

the navigation to the result screen when the orange transaction is a success is…

the navigation to the result screen when the orange transaction is a success is handled by the hook itself. this solve the issue with navigation still happening even tho the transaction was in progress or failed
parent 018283f7
import { useModalsManagerContext } from "@/contexts/ModalsManagerContext"; import { useModalsManagerContext } from "@/contexts/ModalsManagerContext";
import type { IpaymentStackNavigator } from "@/navigations/Types";
import { import {
type IorangePaymentStarter, type IorangePaymentStarter,
getTransactionStatus, getTransactionStatus,
...@@ -8,6 +9,7 @@ import ErrorModal from "@components/modals/ErrorModal"; ...@@ -8,6 +9,7 @@ import ErrorModal from "@components/modals/ErrorModal";
import InformationModal from "@components/modals/InformationModal"; import InformationModal from "@components/modals/InformationModal";
import LoadingModal from "@components/modals/LoadingModal"; import LoadingModal from "@components/modals/LoadingModal";
import { LOG } from "@logger"; import { LOG } from "@logger";
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useMutation, useQueryClient } from "@tanstack/react-query";
import * as WebBrowser from "expo-web-browser"; import * as WebBrowser from "expo-web-browser";
...@@ -24,7 +26,13 @@ const paymentObjectDefault: IorangePaymentStarter = { ...@@ -24,7 +26,13 @@ const paymentObjectDefault: IorangePaymentStarter = {
numero: "0707070707", numero: "0707070707",
commentaire: "Un commentaire", commentaire: "Un commentaire",
}; };
const useOrangeMoney = () => { const useOrangeMoney = (
navigation?: NativeStackNavigationProp<
IpaymentStackNavigator,
"paymentAmountInputScreen",
"IpaymentStackNavigator"
>,
) => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const [isBrowserOpen, setIsBrowserOpen] = useState(false); const [isBrowserOpen, setIsBrowserOpen] = useState(false);
...@@ -83,29 +91,28 @@ const useOrangeMoney = () => { ...@@ -83,29 +91,28 @@ const useOrangeMoney = () => {
const openBrowserThenCheckStatus = async (paymentUrl: string, orderId: string) => { const openBrowserThenCheckStatus = async (paymentUrl: string, orderId: string) => {
try { try {
await handlePaymentUsingBrowser(paymentUrl); await handlePaymentUsingBrowser(paymentUrl);
log.info("Verifying transaction status..."); log.info("openBrowserThenCheckStatus | Verifying transaction status...");
showModal(<LoadingModal message="Vérification du statut de la transaction..." />); showModal(<LoadingModal message="Vérification du statut de la transaction..." />);
await transactionsStatusMutation.mutateAsync(orderId); await transactionsStatusMutation.mutateAsync(orderId);
navigation?.getParent()?.navigate("paymentResultScreen");
} catch (error) { } catch (error) {
log.error("handlePaymentButton |", error); log.error("openBrowserThenCheckStatus |", error);
if (error instanceof Error) { if (error instanceof Error) {
if (error.name === "ORANGE_PAYMENT_IN_PROGRESS") { if (error.name === "ORANGE_PAYMENT_IN_PROGRESS") {
log.warn("handlePaymentButton | ORANGE_PAYMENT_IN_PROGRESS"); log.warn("openBrowserThenCheckStatus | ORANGE_PAYMENT_IN_PROGRESS");
showModal( showModal(
<InformationModal <InformationModal
message="Le payment est toujours en cours." message="Le payment est toujours en cours."
actionLabel="Rééssayer" actionLabel="Rééssayer"
onPress={async () => onPress={() => openBrowserThenCheckStatus(paymentUrl, orderId)}
await openBrowserThenCheckStatus(paymentUrl, orderId)
}
/>, />,
); );
} else if (error.name === "ORANGE_PAYMENT_FAILED") { } else if (error.name === "ORANGE_PAYMENT_FAILED") {
showModal(<ErrorModal message="Le paiment à échoué." />); showModal(<ErrorModal message="Le paiment à échoué." />);
log.error("handlePaymentButton | ORANGE_PAYMENT_FAILED"); log.error("openBrowserThenCheckStatus | ORANGE_PAYMENT_FAILED");
} }
} else { } else {
log.error("handlePaymentButton |", error); log.error("openBrowserThenCheckStatus |", error);
closeModal(); closeModal();
throw error; throw error;
} }
...@@ -117,9 +124,14 @@ const useOrangeMoney = () => { ...@@ -117,9 +124,14 @@ const useOrangeMoney = () => {
showModal(<LoadingModal message="Initialization de la transaction." />); showModal(<LoadingModal message="Initialization de la transaction." />);
const { payment_url, order_id } = const { payment_url, order_id } =
await orangeTransactionInitializerMutation.mutateAsync(amount); await orangeTransactionInitializerMutation.mutateAsync(amount);
log.info("orangePaymentTransactionHandler |", payment_url, order_id);
log.info("Opening browser for payment...");
await openBrowserThenCheckStatus(payment_url, order_id); await openBrowserThenCheckStatus(payment_url, order_id);
} catch (error) { } catch (error) {
log.error("makePayment |", error); log.error("makePayment |", error);
throw error;
} finally {
closeModal(); // just to be ultra sure that the modal is closed
} }
}; };
......
...@@ -32,7 +32,7 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI ...@@ -32,7 +32,7 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI
isCheckingForTransactionStatus, isCheckingForTransactionStatus,
transactionsStatusMutation, transactionsStatusMutation,
orangePaymentTransactionHandler, orangePaymentTransactionHandler,
} = useOrangeMoney(); } = useOrangeMoney(navigation);
const insets = useSafeAreaInsets(); const insets = useSafeAreaInsets();
...@@ -59,7 +59,7 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI ...@@ -59,7 +59,7 @@ const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountI
// await orangePaymentSequence(); // await orangePaymentSequence();
try { try {
await orangePaymentTransactionHandler(amountToPay); await orangePaymentTransactionHandler(amountToPay);
navigation.getParent()?.navigate("paymentResultScreen"); // navigation.getParent()?.navigate("paymentResultScreen");
} catch (error) { } catch (error) {
log.error("handlePaymentButton |", error); log.error("handlePaymentButton |", error);
} }
......
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