import React, { useEffect, useState } from 'react';
import { FlatList, TouchableOpacity, View, StyleSheet, KeyboardAvoidingView, Platform } from 'react-native';
import {
Placeholder,
PlaceholderLine,
Fade
} from "rn-placeholder";
import {
PaymentOptions as PaymentOptionsController,
useLanguage,
} from 'ordering-components-external/native';
import { useTheme } from 'styled-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 } from '../shared';
import {
PMContainer,
PMItem,
PMCardSelected,
PMCardItemContent,
PMDropDownWrapper,
PMDropDownCont
} from './styles'
import { getIconCard, flatArray } from '../../utils';
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
} = props
const theme = useTheme();
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 [, t] = useLanguage();
const [addCardOpen, setAddCardOpen] = useState({ stripe: false, stripeConnect: false });
const [isShowMethods, setShowMethods] = useState(false);
const paymethodSelected = props.paySelected || props.paymethodSelected || isOpenMethod?.paymethod
// const [{ token }] = useSession()
// const [card, setCard] = useState(null);
// const stripeRedirectValues = [
// { name: t('SELECT_A_PAYMENT_METHOD', 'Select a payment method'), value: '-1' },
// ]
const handlePaymentMethodClick = (paymethod: any) => {
const isPopupMethod = ['stripe', 'stripe_direct', 'stripe_connect', 'stripe_redirect', 'paypal'].includes(paymethod?.gateway)
handlePaymethodClick(paymethod, isPopupMethod)
setShowMethods(false);
}
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)}
)
}
const excludeIds: any = [3, 32, 66]; //exclude paypal & connect & redirect
const pmData = paymethodsList.paymethods.sort((a: any, b: any) => a.id - b.id).filter((p: any) => !excludeIds.includes(p.id));
// useEffect(() => { console.log(JSON.stringify(paymethodSelected)) }, []);
return (
{paymethodsList.paymethods.length > 0 && (
setShowMethods(!isShowMethods)}>
{paymethodSelected?.paymethod?.name || t('SELECT_PAYMENT_METHOD', 'Select Paymethod')}
{isShowMethods && (
{pmData.map((item: any, idx: number) =>
console.log('asdasdasda')}>
{renderPaymethods({item})}
)}
)}
)}
{(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 */}
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 })}
/>
)}
*/}
)
}
const styles = StyleSheet.create({
viewStyle: {
marginRight: 10
},
cardsList: {
borderWidth: 1,
borderColor: 'red',
flex: 1,
height: 120
},
btnAddStyle: {
marginVertical: 20,
borderRadius: 7.6,
shadowOpacity: 0,
height: 44,
borderWidth: 1
},
})
export const PaymentOptions = (props: any) => {
const paymentOptions = {
...props,
UIComponent: PaymentOptionsUI
}
return (
)
}