import React from 'react' import { useEffect } from '@wordpress/element' import { RegisterPaymentMethodContentProps } from './RegisterPaymentMethodContentProps' export const Shift4PaymentForm = ({ eventRegistration, emitResponse, billing }: RegisterPaymentMethodContentProps) => { const { cartTotal, currency } = billing const { onPaymentSetup } = eventRegistration useEffect(() => { if (!window.shift4Initialised) { const initialize = () => { if (typeof window.initShift4 === 'function') { window.initShift4() window.shift4Initialised = true window.shift4UpdatedCheckout() } } if (window.shift4JsLoaded) { initialize() } else { document.addEventListener('shift4JsLoaded', initialize) } } else { window.shift4UpdatedCheckout() } const unsubscribe = onPaymentSetup(async () => { window.clearError() await window.shift4PaymentFormSubmit({ amount: cartTotal.value, currency: currency.code }) const data = window.paymentMethodDataRef.current; if (!!data && Object.keys(data).length > 0) { return { type: emitResponse.responseTypes.SUCCESS, meta: { paymentMethodData: { ...window.paymentMethodDataRef.current } }, } } return { type: emitResponse.responseTypes.ERROR, message: 'There was an error' } }) return () => { unsubscribe() } }, [ emitResponse.responseTypes.ERROR, emitResponse.responseTypes.SUCCESS, onPaymentSetup, ]) return (
) }