-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathPayPalHostedFieldsProvider.tsx
More file actions
111 lines (102 loc) · 3.78 KB
/
PayPalHostedFieldsProvider.tsx
File metadata and controls
111 lines (102 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import React, { useState, useEffect, useRef } from "react";
import { PayPalHostedFieldsContext } from "../../context/payPalHostedFieldsContext";
import { useHostedFieldsRegister } from "./hooks";
import { useScriptProviderContext } from "../../hooks/scriptProviderHooks";
import { SDK_SETTINGS } from "../../constants";
import {
validateHostedFieldChildren,
generateMissingHostedFieldsError,
} from "./utils";
import {
PAYPAL_HOSTED_FIELDS_TYPES,
SCRIPT_LOADING_STATE,
} from "../../types/enums";
import { getPayPalWindowNamespace } from "../../utils";
import type { FC } from "react";
import type { PayPalHostedFieldsComponentProps } from "../../types/payPalHostedFieldTypes";
import type {
PayPalHostedFieldsComponent,
HostedFieldsHandler,
} from "@paypal/paypal-js";
/**
This `<PayPalHostedFieldsProvider />` provider component wraps the form field elements and accepts props like `createOrder()`.
This provider component is designed to be used with the `<PayPalHostedField />` component.
Warning: If you don't see anything in the screen probably your client is ineligible.
To handle this problem make sure to use the prop `notEligibleError` and pass a component with a custom message.
Take a look to this link if that is the case: https://developer.paypal.com/docs/checkout/advanced/integrate/
*/
export const PayPalHostedFieldsProvider: FC<
PayPalHostedFieldsComponentProps
> = ({ styles, createOrder, notEligibleError, children, installments }) => {
const [{ options, loadingStatus }] = useScriptProviderContext();
const [isEligible, setIsEligible] = useState<boolean>(true);
const [cardFields, setCardFields] = useState<HostedFieldsHandler>();
const [, setErrorState] = useState(null);
const hostedFieldsContainerRef = useRef<HTMLDivElement>(null);
const hostedFields = useRef<PayPalHostedFieldsComponent>();
const [registeredFields, registerHostedField] = useHostedFieldsRegister();
useEffect(() => {
validateHostedFieldChildren(
Object.keys(registeredFields.current) as PAYPAL_HOSTED_FIELDS_TYPES[],
);
// Only render the hosted fields when script is loaded and hostedFields is eligible
if (!(loadingStatus === SCRIPT_LOADING_STATE.RESOLVED)) {
return;
}
// Get the hosted fields from the [window.paypal.HostedFields] SDK
hostedFields.current = getPayPalWindowNamespace(
options[SDK_SETTINGS.DATA_NAMESPACE],
).HostedFields;
if (!hostedFields.current) {
throw new Error(
generateMissingHostedFieldsError({
components: options.components,
[SDK_SETTINGS.DATA_NAMESPACE]: options[SDK_SETTINGS.DATA_NAMESPACE],
}),
);
}
if (!hostedFields.current.isEligible()) {
return setIsEligible(false);
}
// Clean all the fields before the rerender
if (cardFields) {
cardFields.teardown();
}
hostedFields.current
.render({
// Call your server to set up the transaction
createOrder: createOrder,
fields: registeredFields.current,
installments,
styles,
})
.then((cardFieldsInstance) => {
if (hostedFieldsContainerRef.current) {
setCardFields(cardFieldsInstance);
}
})
.catch((err) => {
setErrorState(() => {
throw new Error(
`Failed to render <PayPalHostedFieldsProvider /> component. ${err}`,
);
});
});
}, [loadingStatus, styles]); // eslint-disable-line react-hooks/exhaustive-deps
return (
<div ref={hostedFieldsContainerRef}>
{isEligible ? (
<PayPalHostedFieldsContext.Provider
value={{
cardFields: cardFields,
registerHostedField,
}}
>
{children}
</PayPalHostedFieldsContext.Provider>
) : (
notEligibleError
)}
</div>
);
};