2024-05-20 16:37:11 +02:00
|
|
|
import {useEffect, useState} from '@wordpress/element';
|
|
|
|
|
2024-05-15 17:37:02 +02:00
|
|
|
import {
|
|
|
|
PayPalScriptProvider,
|
|
|
|
PayPalCardFieldsProvider,
|
|
|
|
PayPalCardFieldsForm,
|
|
|
|
} from "@paypal/react-paypal-js";
|
|
|
|
|
|
|
|
import {CheckoutHandler} from "./checkout-handler";
|
2024-05-20 18:46:44 +02:00
|
|
|
import {createOrder, onApprove} from "../card-fields-config";
|
2024-04-17 16:20:42 +02:00
|
|
|
|
2024-04-17 17:35:30 +02:00
|
|
|
export function CardFields({config, eventRegistration, emitResponse}) {
|
2024-05-20 16:37:11 +02:00
|
|
|
const {onPaymentSetup} = eventRegistration;
|
2024-04-17 17:35:30 +02:00
|
|
|
const {responseTypes} = emitResponse;
|
|
|
|
|
2024-05-20 16:37:11 +02:00
|
|
|
const [cardFieldsForm, setCardFieldsForm] = useState();
|
|
|
|
|
|
|
|
const getCardFieldsForm = (cardFieldsForm) => {
|
|
|
|
setCardFieldsForm(cardFieldsForm)
|
|
|
|
}
|
|
|
|
|
|
|
|
const wait = (milliseconds) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
console.log('start...')
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log('resolve')
|
|
|
|
resolve()
|
|
|
|
}, milliseconds)
|
|
|
|
})
|
2024-05-20 18:46:44 +02:00
|
|
|
};
|
2024-05-20 16:37:11 +02:00
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() =>
|
|
|
|
onPaymentSetup(() => {
|
|
|
|
async function handlePaymentProcessing() {
|
|
|
|
await cardFieldsForm.submit();
|
|
|
|
|
2024-05-20 18:46:44 +02:00
|
|
|
// TODO temporary workaround to wait for onApprove to resolve
|
2024-05-20 16:37:11 +02:00
|
|
|
await wait(3000)
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: responseTypes.SUCCESS,
|
2024-05-20 18:03:31 +02:00
|
|
|
}
|
2024-05-20 16:37:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return handlePaymentProcessing();
|
|
|
|
}),
|
|
|
|
[onPaymentSetup, cardFieldsForm]
|
|
|
|
);
|
|
|
|
|
2024-04-17 16:20:42 +02:00
|
|
|
return (
|
|
|
|
<>
|
2024-05-15 17:37:02 +02:00
|
|
|
<PayPalScriptProvider
|
|
|
|
options={{
|
2024-05-16 12:29:05 +02:00
|
|
|
clientId: config.scriptData.client_id,
|
2024-05-15 17:37:02 +02:00
|
|
|
components: "card-fields",
|
2024-05-16 12:29:05 +02:00
|
|
|
dataNamespace: 'ppcp-block-card-fields',
|
2024-05-15 17:37:02 +02:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<PayPalCardFieldsProvider
|
|
|
|
createOrder={createOrder}
|
|
|
|
onApprove={onApprove}
|
|
|
|
onError={(err) => {
|
2024-05-16 12:29:05 +02:00
|
|
|
console.error(err);
|
2024-05-15 17:37:02 +02:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<PayPalCardFieldsForm/>
|
2024-05-20 16:37:11 +02:00
|
|
|
<CheckoutHandler getCardFieldsForm={getCardFieldsForm}/>
|
2024-05-15 17:37:02 +02:00
|
|
|
</PayPalCardFieldsProvider>
|
|
|
|
</PayPalScriptProvider>
|
2024-04-17 16:20:42 +02:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|