import React, { useState, useEffect } from 'react'; import { View, StyleSheet, useWindowDimensions } from 'react-native'; import { useLanguage, useSession } from 'ordering-components/native'; import { StripeProvider, CardField, useConfirmSetupIntent, createPaymentMethod } from '@stripe/stripe-react-native'; import { useTheme } from 'styled-components/native'; import { ErrorMessage } from './styles'; import { StripeElementsForm as StripeFormController } from './naked'; import { OButton, OText } from '../shared'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; const StripeElementsFormUI = (props: any) => { const { publicKey, handleSource, values, businessId, requirements, stripeTokenHandler, } = props; const theme = useTheme(); const [, t] = useLanguage(); const [{ user }] = useSession(); const [card, setCard] = useState(null); const [isCompleted, setIsCompleted] = useState(false); const [errors, setErrors] = useState('') const { confirmSetupIntent, loading: confirmSetupLoading } = useConfirmSetupIntent(); const [createPmLoading, setCreatePmLoading] = useState(false); const { height } = useWindowDimensions(); const { top, bottom } = useSafeAreaInsets(); const billingDetails = { name: `${user.name} ${user.lastname}`, email: user.email, addressLine1: user.address }; const createPayMethod = async () => { try { setCreatePmLoading(true) const { paymentMethod } = await createPaymentMethod({ type: 'Card', billingDetails, }); setCreatePmLoading(false) handleSource && handleSource({ id: paymentMethod.id, type: 'card', card: { brand: paymentMethod.Card.brand, last4: paymentMethod.Card.last4 } }) // if (error) { // setErrors( // error?.code === 'Unknown' // ? t('ERROR_ADD_CARD', 'An error occurred while trying to add a card') // : error.message // ); // } } catch (error) { setErrors(error?.message || error?.toString()); } } const handleSaveCard = async () => { setErrors(''); if (!requirements) { createPayMethod(); return } try { const { setupIntent, error } = await confirmSetupIntent(requirements, { type: 'Card', billingDetails, }); if (setupIntent?.status === 'Succeeded') { stripeTokenHandler(setupIntent?.paymentMethodId, user, businessId); } if (error) { setErrors( error?.code === 'Unknown' ? t('ERROR_ADD_CARD', 'An error occurred while trying to add a card') : error.message ); } } catch (error) { setErrors(error?.message || error?.toString()); } }; useEffect(() => { if (card) { setIsCompleted( !!card?.last4 && !!card?.expiryMonth && !!card?.expiryYear && !!card?.brand && !!card?.postalCode ) } }, [card]) return ( {publicKey ? ( setCard(cardDetails)} /> {!!errors && ( {errors} )} ) : ( {t('SOMETHING_WRONG', 'Something is wrong!')} )} handleSaveCard() : () => { }} isLoading={confirmSetupLoading || values.loadingAdd || createPmLoading} /> ) } const styles = StyleSheet.create({ container: { width: '100%', paddingHorizontal: 40, justifyContent: 'space-between', paddingBottom: 12 }, btnAddStyle: { marginTop: 20, borderRadius: 7.6, shadowOpacity: 0, height: 44 }, }) export const StripeElementsForm = (props: any) => { const stripeProps = { ...props, UIComponent: StripeElementsFormUI, onSelectCard: (card: any) => { props.onSelectCard(card); if (card) { props.onCancel && props.onCancel(); } } } return }