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 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 {
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 handlePaymentButton = useCallback(async () => {
switch (paymentType) {
case "OM": {
Keyboard.dismiss();
log.info("OM so we stays on screen !!");
log.info("Calling api with amount :: ", amountToPay);
try {
const { payment_url, order_id } =
await orangeTransactionInitializerMutation.mutateAsync(amountToPay);
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...");
const { status } = await transactionsStatusMutation.mutateAsync(order_id);
log.info("result of transactions", JSON.stringify(status, null, 2));
if (status === "SUCCESS") {
log.info("Transaction was a success, navigating to success page");
navigation.getParent()?.navigate("paymentResultScreen");
} else if (status === "FAILED") {
log.warn("Transaction was a failed, navigating to error page");
} else if (status === "INITIATED") {
log.warn(
"Transaction status was still 'INITIATED' after timeout, throwing an error",
);
}
} catch (error) {
log.error("handlePaymentButton |", error);
//TODO : handle error
}
break;
}
default:
log.info("Navigating to numberAndOtpForPaymentScreen");
navigation.navigate("numberAndOtpForPaymentScreen");
break;
}
}, [
amountToPay,
orangeTransactionInitializerMutation,
paymentType,
navigation,
transactionsStatusMutation,
handlePaymentUsingBrowser,
]);
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
);
};