import React, { useEffect, useState } from 'react'; import { View } from 'react-native'; import { useTheme } from 'styled-components/native'; import { PaymentOptions as PaymentOptionsController, useLanguage, ToastType, useToast } from 'ordering-components/native'; import { OText } from '../shared'; import NavBar from '../NavBar'; import { Container } from '../../layouts/Container'; import OptionCard from '../OptionCard'; import Spinner from 'react-native-loading-spinner-overlay'; import { LANDSCAPE, PORTRAIT, useDeviceOrientation } from '../../../../../src/hooks/DeviceOrientation'; import GridContainer from '../../layouts/GridContainer'; import { OModal, PaymentOptionStripe } from '../../../../../src'; import AntIconDesign from 'react-native-vector-icons/AntDesign' import MaterialIcon from 'react-native-vector-icons/MaterialCommunityIcons' const PaymentOptionsUI = (props: any) => { const { cart, onNavigationRedirect, isDisabled, paymethodData, handlePaymethodDataChange, errorCash, isLoading, paymethodsList, setPaymethodData, handlePaymethodClick, isOpenMethod, navigation, handlerClickPlaceOrder, placing, errors, paySelected, } = props; const theme = useTheme(); const [, { showToast }] = useToast(); const [, t] = useLanguage(); const [orientationState] = useDeviceOrientation(); const [userErrors, setUserErrors] = useState([]); const [isOpenModal, setIsOpenModal] = useState(false) const paymethodSelected = paySelected || props.paymethodSelected || isOpenMethod.paymethod; useEffect(() => { if (paymethodsList.paymethods.length === 1) { handlePaymethodClick && handlePaymethodClick(paymethodsList.paymethods[0]); } }, [paymethodsList.paymethods]); useEffect(() => { if (paymethodSelected?.gateway !== 'cash' && errorCash) { props.setErrorCash(false); } }, [paymethodSelected]); useEffect(() => { if (props.paySelected && props.paySelected?.data) { setPaymethodData && setPaymethodData(props.paySelected?.data); } }, [paySelected]); useEffect(() => { if ( !errors && !errorCash && isOpenMethod?.paymethod && !placing && !isLoading && paySelected && paySelected?.gateway !== 'stripe' ) { handlePlaceOrder(); return; } }, [isOpenMethod.paymethod, placing, isLoading, paySelected]); // const includePaymethods = ['cash', 'card_delivery', 'stripe']; const includePaymethods = ['cash', 'card_delivery']; const supportedMethods = paymethodsList.paymethods.filter((p: any) => includePaymethods.includes(p.gateway), ); const cashIndex: number = supportedMethods?.findIndex( (item: any) => item?.gateway === 'cash', ); const cardOnDeliveryIndex = supportedMethods?.findIndex( (item: any) => item?.gateway === 'card_delivery', ); const stripeIndex = supportedMethods?.findIndex( (item: any) => item?.gateway === 'stripe', ); const onSelectPaymethod = (paymethod: any, isPopupMethod: boolean) => { handlePaymethodClick(paymethod); }; const handlePlaceOrder = () => { if (!userErrors.length) { handlerClickPlaceOrder && handlerClickPlaceOrder(null, { on_behalf_of: props.customerName }); return; } let stringError = ''; Object.values(userErrors).map((item: any, i: number) => { stringError += i + 1 === userErrors.length ? `- ${item?.message || item}` : `- ${item?.message || item}\n`; }); showToast(ToastType.Error, stringError); }; const cardStyle = { width: orientationState?.orientation === PORTRAIT ? orientationState?.dimensions?.width - 40 : orientationState?.dimensions?.width * 0.47, height: orientationState?.orientation === PORTRAIT ? orientationState?.dimensions?.height * 0.34 : orientationState?.dimensions?.height * 0.55, }; const propsOfItems = { CASH_ID: cashIndex !== -1 ? { style: cardStyle, title: t('CASH', 'Cash'), description: t( 'GO_FOR_YOR_RECEIPT_AND_GO_TO_THE_FRONT_COUNTER', 'Pay with cash in the front counter', ), bgImage: theme.images.general.cash, callToActionText: t('LETS_GO', 'LETS_GO'), VectorIcon: () => , onClick: () => onSelectPaymethod(supportedMethods[cashIndex], false), ...supportedMethods[cashIndex], } : null, CARD_ON_DELIVERY_ID: cardOnDeliveryIndex !== -1 ? { style: cardStyle, title: t('CARD', supportedMethods[cardOnDeliveryIndex]?.name), description: t( 'WE_ACCEPT_EVERY_DEBIT_OR_CREDIT_CARD', 'We accept every debit or credit card', ), bgImage: theme.images.general.carddelivery, callToActionText: t("LETS_GO", "Let's go"), VectorIcon: () => , onClick: () => onSelectPaymethod(supportedMethods[cardOnDeliveryIndex], false), ...supportedMethods[cardOnDeliveryIndex], } : null, STRIPE: stripeIndex !== -1 ? { style: cardStyle, title: t('STRIPE', supportedMethods[stripeIndex]?.name), description: t('STRIPE', 'Stripe'), bgImage: theme.images.general.carddelivery, callToActionText: t('INSERT_INFO', 'Test info'), VectorIcon: () => , onClick: () => { onSelectPaymethod(supportedMethods[stripeIndex], false); setIsOpenModal(true) }, ...supportedMethods[stripeIndex], } : null, }; const goToBack = () => navigation?.goBack(); return ( <> {supportedMethods?.length > 0 && ( <> {t('HOW_WOULD_YOU', 'How would you')} {'\n'} {`${t('LIKE_TO_PAY', 'like to pay')}?`} {propsOfItems.CARD_ON_DELIVERY_ID && ( )} {propsOfItems?.CASH_ID && } {propsOfItems.STRIPE && ( )} )} setIsOpenModal(false)} onCancel={() => setIsOpenModal(false)} entireModal customClose > setIsOpenModal(false)} /> ); }; export const PaymentOptions = (props: any) => { const paymentOptions = { ...props, UIComponent: PaymentOptionsUI, }; return ; };