import React, { useEffect, useState, useRef } from 'react'; import { FlatList, TouchableOpacity, View, StyleSheet, KeyboardAvoidingView, Platform, ActivityIndicator, I18nManager } from 'react-native'; import { Placeholder, PlaceholderLine, Fade } from "rn-placeholder"; import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons'; import { PaymentOptions as PaymentOptionsController, useLanguage, useSession, useApi, useOrder, ToastType, useToast } from 'ordering-components/native'; import { PaymentOptionCash } from '../PaymentOptionCash'; import { StripeElementsForm } from '../StripeElementsForm'; import { StripeCardsList } from '../StripeCardsList'; // import { PaymentOptionStripe } from '../PaymentOptionStripe'; // import { StripeRedirectForm } from '../StripeRedirectForm'; // import { PaymentOptionPaypal } from '../PaymentOptionPaypal' // import { NotFoundSource } from '../NotFoundSource' import { OText, OIcon, OModal, OButton, OBottomPopup } from '../shared'; import { PMContainer, PMItem, PMCardSelected, PMCardItemContent } from './styles' import { getIconCard } from '../../utils'; import { WebView } from 'react-native-webview'; import { useTheme } from 'styled-components/native'; import { useWindowDimensions } from 'react-native'; const stripeOptions: any = ['stripe_direct', 'stripe', 'stripe_connect'] // const stripeRedirectOptions = [ // { name: 'Bancontact', value: 'bancontact' }, // { name: 'Alipay', value: 'alipay' }, // { name: 'Giropay', value: 'giropay' }, // { name: 'iDEAL', value: 'ideal' } // ] const PaymentOptionsUI = (props: any) => { const { cart, errorCash, isLoading, isDisabled, paymethodData, paymethodsList, setPaymethodData, onNavigationRedirect, handlePaymethodClick, handlePaymethodDataChange, isOpenMethod, renderFooter } = props const theme = useTheme(); const [, t] = useLanguage(); const [addCardOpen, setAddCardOpen] = useState({ stripe: false, stripeConnect: false }); const [showGateway, setShowGateway] = useState({ closedByUsed: false, open: false }); const [prog, setProg] = useState(true); const [progClr, setProgClr] = useState('#424242'); const [, { showToast }] = useToast(); const webviewRef = useRef(null) const [ordering] = useApi() const [, { confirmCart }] = useOrder() const [{ token, user }] = useSession() const paymethodSelected = props.paySelected || props.paymethodSelected || isOpenMethod?.paymethod const [isOpenPopup, setOpenPopup] = useState(false); const { height } = useWindowDimensions(); const getPayIcon = (method: string) => { switch (method) { case 'cash': return theme.images.general.cash case 'card_delivery': return theme.images.general.carddelivery case 'paypal': return theme.images.general.paypal case 'stripe': return theme.images.general.stripe case 'stripe_direct': return theme.images.general.stripecc case 'stripe_connect': return theme.images.general.stripes case 'stripe_redirect': return theme.images.general.stripesb default: return theme.images.general.creditCard } } const onMessage = (e: any) => { let data = e.nativeEvent.data; let payment = JSON.parse(data); console.log(data); if (payment.error) { showToast(ToastType.Error, payment.result) } else { showToast(ToastType.Success, t('ORDER_PLACED_SUCCESSfULLY', 'The order was placed successfullyS')) onNavigationRedirect && onNavigationRedirect('OrderDetails', { orderId: payment.result.order.uuid, goToBusinessList: true }) } setShowGateway({ closedByUser: false, open: false }) } const handlePaymentMethodClick = (paymethod: any) => { const isPopupMethod = ['stripe', 'stripe_direct', 'stripe_connect', 'stripe_redirect', 'paypal'].includes(paymethod?.gateway) handlePaymethodClick(paymethod, isPopupMethod) if (paymethod?.gateway === 'paypal') { setShowGateway({ closedByUser: false, open: true }) } if (!paymethod?.gateway.includes('paypal')) { setOpenPopup(true); } } const onFailPaypal = async () => { if (showGateway.closedByUser === true) { const { result } = await confirmCart(cart.uuid) } } useEffect(() => { onFailPaypal() }, [showGateway.closedByUser]) 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) } }, [props.paySelected]) const renderPaymethods = ({ item }: any) => { return ( handlePaymentMethodClick(item)} > {t(item.gateway.toUpperCase(), item.name)} {paymethodSelected?.id === item.id && ( )} ) } const excludeIds: any = [3, 32, 66]; //exclude paypal & connect & redirect return ( {paymethodsList.paymethods.length > 0 && ( a.id - b.id).filter((p: any) => !excludeIds.includes(p.id))} renderItem={renderPaymethods} keyExtractor={(paymethod: any) => paymethod?.id?.toString?.()} ListFooterComponent={renderFooter} /> )} {(paymethodsList.loading || isLoading) && ( {[...Array(3)].map((_, i) => ( ))} )} {/* Select cards or detail of payment method on a PopUp */} setOpenPopup(false)}> {paymethodSelected?.gateway === 'cash' && } {isOpenMethod?.paymethod?.gateway === 'stripe' && !paymethodData?.id && ( setAddCardOpen({ ...addCardOpen, stripe: true })} /> handlePaymethodClick(null)} /> )} {paymethodsList.error && paymethodsList.error.length > 0 && ( {paymethodsList?.error[0]?.message || paymethodsList?.error[0]} )} {!(paymethodsList.loading || isLoading) && !paymethodsList.error && (!paymethodsList?.paymethods || paymethodsList.paymethods.length === 0) && ( {t('NO_PAYMENT_METHODS', 'No payment methods!')} )} {stripeOptions.includes(paymethodSelected?.gateway) && (paymethodData?.brand || paymethodData?.card?.brand) && (paymethodData?.last4 || paymethodData?.card?.last4) && ( {getIconCard((paymethodData?.brand || paymethodData?.card?.brand), 17)} XXXX-XXXX-XXXX-{(paymethodData?.last4 || paymethodData?.card?.last4)} )} setOpenPopup(false)} style={{height: 42, borderRadius: 3, borderWidth: 1, marginTop: 20}} textStyle={{color: theme.colors.primary, fontSize: 14, fontWeight: '600'}} imgRightSrc={null} /> setAddCardOpen({ ...addCardOpen, stripe: false })} > setAddCardOpen({ ...addCardOpen, stripe: false })} /> {/* Stripe direct */} handlePaymethodClick(null)} > handlePaymethodClick(false)} /> {/* Stripe Connect */} {isOpenMethod?.paymethod?.gateway === 'stripe_connect' && !paymethodData?.id && ( setAddCardOpen({ ...addCardOpen, stripeConnect: true })} /> handlePaymethodClick(null)} /> )} setAddCardOpen({ ...addCardOpen, stripeConnect: false })} > setAddCardOpen({ ...addCardOpen, stripeConnect: false })} /> {/* Stripe Redirect */} {/* handlePaymethodClick(null)} > */} {/* Paypal */} {/* handlePaymethodClick(null)} title={t('PAY_WITH_PAYPAL', 'Pay with PayPal')} > {paymethodSelected?.gateway === 'paypal' && ( onNavigationRedirect && onNavigationRedirect('OrderDetails', { orderId: uuid })} /> )} */} setShowGateway({ open: false, closedByUser: true })} onAccept={() => setShowGateway({ open: false, closedByUser: true })} onClose={() => setShowGateway({ open: false, closedByUser: true })} entireModal > {t('PAYPAL_GATEWAY', 'PayPal GateWay')} { setProg(true); setProgClr('#424242'); }} onLoadProgress={() => { setProg(true); setProgClr('#00457C'); }} onLoadEnd={(e) => { const message = { action: 'init', data: { urlPlace: `${ordering.root}/carts/${cart?.uuid}/place`, urlConfirm: `${ordering.root}/carts/${cart?.uuid}/confirm`, payData: { paymethod_id: paymethodSelected?.id, amount: cart?.total, delivery_zone_id: cart?.delivery_zone_id, user_id: user?.id }, userToken: token, clientId: isOpenMethod?.paymethod?.credentials?.client_id } } setProg(false); webviewRef.current.postMessage(JSON.stringify(message)) }} /> ) } const styles = StyleSheet.create({ viewStyle: { marginRight: 10 }, cardsList: { borderWidth: 1, borderColor: 'red', flex: 1, height: 120 }, btnAddStyle: { marginTop: 22, marginBottom: 12, height: 42, borderRadius: 3, }, btnCon: { height: 45, width: '70%', elevation: 1, backgroundColor: '#00457C', borderRadius: 3, }, btn: { flex: 1, alignItems: 'center', justifyContent: 'center', }, btnTxt: { color: '#fff', fontSize: 18, }, }) export const PaymentOptions = (props: any) => { const paymentOptions = { ...props, UIComponent: PaymentOptionsUI } return ( ) }