import React, { useEffect, useState } from 'react';
import { useTheme } from 'styled-components/native';
import { useApplePay } from '@stripe/stripe-react-native';
import { FlatList, TouchableOpacity, View, StyleSheet, KeyboardAvoidingView, Platform } from 'react-native';
import {
Placeholder,
PlaceholderLine,
Fade
} from "rn-placeholder";
import {
PaymentOptions as PaymentOptionsController,
useLanguage,
ToastType,
useToast,
} from 'ordering-components/native';
import { getIconCard } from '../../utils';
import { PaymentOptionCash } from '../PaymentOptionCash';
import { StripeElementsForm } from '../StripeElementsForm';
import { StripeCardsList } from '../StripeCardsList';
import { OText, OIcon, OModal, OButton } from '../shared';
import {
PMContainer,
PMItem,
PMCardSelected,
PMCardItemContent
} from './styles'
const stripeOptions: any = ['stripe_direct', 'stripe', 'stripe_connect']
const methodsPay = ['google_pay', 'apple_pay']
const stripeDirectMethods = ['stripe_direct', ...methodsPay]
const webViewPaymentGateway: any = ['paypal', 'square']
const PaymentOptionsUI = (props: any) => {
const {
cart,
errorCash,
isLoading,
isDisabled,
paymethodData,
paymethodsList,
setPaymethodData,
onNavigationRedirect,
handlePaymethodClick,
handlePaymethodDataChange,
isOpenMethod,
handlePaymentMethodClickCustom,
handlePlaceOrder,
merchantId
} = props
const theme = useTheme();
const [, t] = useLanguage();
const [, { showToast }] = useToast();
const { confirmApplePayPayment } = useApplePay()
const [addCardOpen, setAddCardOpen] = useState({ stripe: false, stripeConnect: false });
const paymethodSelected = props.paySelected || props.paymethodSelected || isOpenMethod?.paymethod
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
case 'apple_pay':
return theme.images.general.applePayMark
default:
return theme.images.general.creditCard
}
}
const handlePaymentMethodClick = (paymethod: any) => {
if (cart?.balance > 0) {
const isPopupMethod = ['stripe', 'stripe_direct', 'stripe_connect', 'stripe_redirect', 'paypal'].includes(paymethod?.gateway)
if (webViewPaymentGateway.includes(paymethod?.gateway)) {
handlePaymentMethodClickCustom(paymethod)
}
handlePaymethodClick(paymethod, isPopupMethod)
return
}
showToast(
ToastType.Error,
t('CART_BALANCE_ZERO', 'Sorry, the amount to pay is equal to zero and it is not necessary to select a payment method'))
;
}
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) {
requestAnimationFrame(() => {
setPaymethodData && setPaymethodData(props.paySelected?.data)
})
}
}, [props.paySelected])
useEffect(() => {
if (methodsPay.includes(paymethodSelected?.gateway) && paymethodData?.id && paymethodSelected?.data?.card) {
handlePlaceOrder(confirmApplePayPayment)
}
}, [paymethodData, paymethodSelected])
const renderPaymethods = ({ item }: any) => {
return (
<>
{item?.gateway === 'apple_pay' ? (
handlePaymentMethodClick(item)}
>
) : (
handlePaymentMethodClick(item)}
>
{t(item.gateway.toUpperCase(), item.name)}
)}
>
)
}
const excludeIds: any = [32];
return (
{paymethodsList.paymethods.length > 0 && (
a.id - b.id).filter((p: any) => !excludeIds.includes(p.id))}
renderItem={renderPaymethods}
keyExtractor={(paymethod: any) => paymethod?.id?.toString?.()}
/>
)}
{(paymethodsList.loading || isLoading) && (
{[...Array(3)].map((_, i) => (
))}
)}
{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!')}
)}
{paymethodSelected?.gateway === 'cash' && (
)}
{stripeOptions.includes(paymethodSelected?.gateway) &&
(paymethodData?.brand || paymethodData?.card?.brand) &&
(paymethodData?.last4 || paymethodData?.card?.last4) &&
(
{getIconCard((paymethodData?.brand || paymethodData?.card?.brand), 20)}
XXXX-XXXX-XXXX-{(paymethodData?.last4 || paymethodData?.card?.last4)}
)}
{/* Stripe */}
{isOpenMethod?.paymethod?.gateway === 'stripe' && !paymethodData?.id && (
setAddCardOpen({ ...addCardOpen, stripe: true })}
/>
handlePaymethodClick(null)}
/>
)}
setAddCardOpen({ ...addCardOpen, stripe: false })}
style={{ backgroundColor: 'red' }}
>
setAddCardOpen({ ...addCardOpen, stripe: false })}
/>
{/* Stripe direct, Google pay, Apple pay */}
handlePaymethodClick(null)}
>
handlePaymethodClick(null)}
merchantId={merchantId}
/>
{/* Stripe Connect */}
{isOpenMethod?.paymethod?.gateway === 'stripe_connect' && !paymethodData?.id && (
setAddCardOpen({ ...addCardOpen, stripeConnect: true })}
/>
handlePaymethodClick(null)}
/>
)}
setAddCardOpen({ ...addCardOpen, stripeConnect: false })}
>
setAddCardOpen({ ...addCardOpen, stripeConnect: false })}
/>
)
}
const styles = StyleSheet.create({
viewStyle: {
marginRight: 10
},
cardsList: {
borderWidth: 1,
borderColor: 'red',
flex: 1,
height: 120
},
btnAddStyle: {
marginVertical: 20,
},
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 (
)
}