import React, { useEffect, useState } from 'react'
import {
useLanguage,
useConfig,
useUtils,
useToast,
ToastType,
MultiCartsPaymethodsAndWallets as MultiCartsPaymethodsAndWalletsController
} from 'ordering-components/native'
import { useTheme } from 'styled-components/native'
import { View, TouchableOpacity, FlatList, StyleSheet, KeyboardAvoidingView, Platform } from 'react-native'
import { Placeholder, PlaceholderLine, Fade } from 'rn-placeholder'
import { OText, OIcon, OModal, OButton } from '../shared'
import { getIconCard, flatArray } from '../../utils'
import { StripeElementsForm } from '../StripeElementsForm'
import { StripeCardsList } from '../StripeCardsList'
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import { useApplePay } from '@stripe/stripe-react-native';
import {
PMContainer,
PMItem,
WalletItem
} from './styles'
const MultiCartsPaymethodsAndWalletsUI = (props: any) => {
const {
businessIds,
paymethodsAndWallets,
walletsState,
walletsPaymethod,
paymethodSelected,
handleSelectPaymethod,
handleSelectWallet,
handlePaymethodDataChange,
setMethodPaySupported,
placeByMethodPay,
methodPaySupported,
setPlaceByMethodPay,
openCarts,
cartTotal,
handlePlaceOrder,
merchantId
} = props
const theme = useTheme()
const [, t] = useLanguage()
const [{ configs }] = useConfig()
const [{ parsePrice }] = useUtils()
const [, { showToast }] = useToast();
const { confirmApplePayPayment } = useApplePay()
const [addCardOpen, setAddCardOpen] = useState({ stripe: false, stripeConnect: false });
const [newCardAdded, setNewCardAdded] = useState(null)
const isWalletCashEnabled = configs?.wallet_cash_enabled?.value === '1'
const isWalletPointsEnabled = configs?.wallet_credit_point_enabled?.value === '1'
const walletName: any = {
cash: {
name: t('PAY_WITH_CASH_WALLET', 'Pay with Cash Wallet'),
isActive: isWalletCashEnabled
},
credit_point: {
name: t('PAY_WITH_CREDITS_POINTS_WALLET', 'Pay with Credit Points Wallet'),
isActive: isWalletPointsEnabled
}
}
const creditBalance: any = (wallet: any) => ` = ${parsePrice(wallet.balance / wallet.redemption_rate, { isTruncable: true })}`
const filterMethodsPay = (gateway: string) => Platform.OS === 'ios' ? gateway !== 'global_google_pay' : gateway !== 'global_apple_pay'
const methodsPay = ['global_google_pay', 'global_apple_pay']
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.creditCard
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 'global_apple_pay':
return theme.images.general.applePayMark
case 'global_google_pay':
return theme.images.general.googlePayMark
default:
return theme.images.general.creditCard
}
}
useEffect(() => {
if (methodsPay.includes(paymethodSelected?.gateway)) {
if (typeof paymethodSelected?.paymethod_data === 'string') {
const sourceId = JSON.parse(paymethodSelected?.paymethod_data)?.source_id
sourceId && handlePlaceOrder(confirmApplePayPayment)
}
}
}, [JSON.stringify(paymethodSelected)])
useEffect(() => {
if (cartTotal === 0) {
handlePaymethodDataChange(null)
handleSelectPaymethod(null)
}
}, [cartTotal])
const handleChangePaymethod = (paymethod: any) => {
if (cartTotal > 0) {
handleSelectPaymethod(paymethod)
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'))
;
}
const renderPaymethods = ({ item }: any) => {
return (
<>
{methodsPay.includes(item?.gateway) ? (
handleChangePaymethod({ ...item, paymethod: { gateway: item.gateway }, paymethod_id: item?.id })}
>
) : (
handleChangePaymethod({ ...item, paymethod: { gateway: item.gateway }, paymethod_id: item?.id })}
>
{t(item?.gateway.toUpperCase(), item?.name)}
)}
>
)
}
return (
{t('PAYMENT_METHODS', 'Payment Methods')}
{paymethodsAndWallets.loading ? (
{[...Array(3)].map((_, i) => (
))}
) : (
filterMethodsPay(p.gateway))}
renderItem={renderPaymethods}
keyExtractor={(paymethod: any) => paymethod?.id?.toString?.()}
/>
)}
{!paymethodsAndWallets.loading && !paymethodsAndWallets.error && paymethodsAndWallets.paymethods.length === 0 && (
{t('NO_PAYMENT_METHODS', 'No payment methods!')}
)}
{paymethodSelected?.paymethod?.gateway === 'stripe' && (
setAddCardOpen({ ...addCardOpen, stripe: true })}
/>
)}
{/* Google pay, Apple pay */}
{methodsPay.includes(paymethodSelected?.paymethod?.gateway) && (
cart?.business?.name)}
publicKey={paymethodSelected?.data?.publishable}
requirements={props.clientSecret}
handleSource={handlePaymethodDataChange}
onCancel={() => setAddCardOpen({ ...addCardOpen, stripe: false })}
setMethodPaySupported={setMethodPaySupported}
methodPaySupported={methodPaySupported}
placeByMethodPay={placeByMethodPay}
setPlaceByMethodPay={setPlaceByMethodPay}
methodsPay={methodsPay}
paymethod={paymethodSelected?.paymethod?.gateway}
cartTotal={cartTotal}
merchantId={merchantId}
/>
)}
{(paymethodsAndWallets.loading || walletsState.loading) ? (
<>
{[...Array(2).keys()].map(i => (
))}
>
) : (
<>
{walletsState?.result?.filter((wallet: any) =>
paymethodsAndWallets.wallets.find((item: any) => item.type === wallet.type))
.map((wallet: any, idx: any) => walletName[wallet.type]?.isActive &&
(
handleSelectWallet(!!!walletsPaymethod?.find((walletPay: any) => walletPay.wallet_id === wallet.id)?.id, wallet)}
>
{!!walletsPaymethod?.find((walletPay: any) => walletPay.wallet_id === wallet.id)?.id ? (
) : (
)}
{walletName[wallet.type]?.name}
{wallet.type === 'cash' && (
{parsePrice(wallet?.balance, { isTruncable: true })}
)}
{wallet.type === 'credit_point' && (
{`${wallet?.balance} ${t('POINTS', 'Points')}`}
{wallet?.balance > 0
? creditBalance(wallet)
: null}
)}
))}
>
)}
setAddCardOpen({ ...addCardOpen, stripe: false })}
style={{ backgroundColor: 'red' }}
>
setAddCardOpen({ ...addCardOpen, stripe: false })}
/>
)
}
const styles = StyleSheet.create({
btnAddStyle: {
marginVertical: 20,
borderRadius: 7.6,
shadowOpacity: 0,
height: 44,
borderWidth: 1
},
})
export const MultiCartsPaymethodsAndWallets = (props: any) => {
const multiCartsPaymethodsAndWalletsProps = {
...props,
UIComponent: MultiCartsPaymethodsAndWalletsUI
}
return
}