|
|
@@ -47,14 +47,17 @@ export default function PaypalApplepayButton({
|
|
|
// const [configCountryCode, setConfigCountryCode] = useState('');
|
|
|
|
|
|
// paypal sdk 是否加载完成
|
|
|
- const { loadingStatus, isHydrated } = usePayPal();
|
|
|
+ const {
|
|
|
+ loadingStatus,
|
|
|
+ isHydrated
|
|
|
+ } = usePayPal();
|
|
|
// Fetch eligibility(资格) for one-time payment flow
|
|
|
|
|
|
|
|
|
const {
|
|
|
error: eligibilityError,
|
|
|
eligiblePaymentMethods,
|
|
|
- isLoading: isEligibilityLoading,
|
|
|
+ // isLoading: isEligibilityLoading,
|
|
|
} = useEligibleMethods({
|
|
|
payload: {
|
|
|
currencyCode: currencyCode, // 需要根据用户选择的货币走
|
|
|
@@ -62,8 +65,9 @@ export default function PaypalApplepayButton({
|
|
|
},
|
|
|
});
|
|
|
const isLoading = loadingStatus === INSTANCE_LOADING_STATE.PENDING;
|
|
|
- const isApplepayEligible = eligiblePaymentMethods?.isEligible("applepay");
|
|
|
- const applePayConfig = eligiblePaymentMethods?.getDetails("applepay");
|
|
|
+ // const isApplepayEligible = eligiblePaymentMethods?.isEligible("applepay");
|
|
|
+ // const applePayConfig = eligiblePaymentMethods?.getDetails("applepay");
|
|
|
+ const applePayConfig = eligiblePaymentMethods?.isEligible("applepay") ? eligiblePaymentMethods.getDetails("applepay").config : null;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -209,7 +213,7 @@ export default function PaypalApplepayButton({
|
|
|
postalCode:shippingContact.postcode,
|
|
|
}
|
|
|
}
|
|
|
- console.log('applePayConfig ---- ', applePayConfig?.config);
|
|
|
+ console.log('applePayConfig ---- ', applePayConfig);
|
|
|
/*
|
|
|
useEffect(() => {
|
|
|
if(!sdkInstance || !isHydrated) return;
|
|
|
@@ -223,24 +227,32 @@ export default function PaypalApplepayButton({
|
|
|
getConfig();
|
|
|
}, [isHydrated, sdkInstance]);
|
|
|
*/
|
|
|
- const merchantCountry = applePayConfig?.config.merchantCountry;
|
|
|
+// const merchantCountry = applePayConfig?.merchantCountry;
|
|
|
|
|
|
- if(!isApplepayEligible) {
|
|
|
- return <p className="text-ly-12 text-ly-errorcolor text-center">Applepay is not available on your device.</p>;
|
|
|
- }
|
|
|
-
|
|
|
- return (
|
|
|
- <div className="box-border w-full">
|
|
|
- {isLoading || !isHydrated || isEligibilityLoading || applePayConfig === undefined ?
|
|
|
- <p className="text-ly-12 text-center">Applepay is loading...</p>
|
|
|
- :
|
|
|
+ const tips = isLoading || !isHydrated ? (
|
|
|
+ <p className="text-ly-12 text-center">
|
|
|
+ Applepay is loading1...
|
|
|
+ {String(isLoading)}
|
|
|
+
|
|
|
+ /{String(isHydrated)}
|
|
|
+ </p>
|
|
|
+ ) : eligibilityError ? (
|
|
|
+ <p className="text-ly-errorcolor text-ly-12 text-center">
|
|
|
+ {eligibilityError.message}
|
|
|
+ {/* Failed to load payment options. Please refresh the page. */}
|
|
|
+ </p>
|
|
|
+ ) : !eligiblePaymentMethods ? (
|
|
|
+ <p className="text-ly-12 text-center">Applepay is loading2...</p>
|
|
|
+ ) : !eligiblePaymentMethods.isEligible("applepay") ? (
|
|
|
+ <p className="text-ly-12 text-ly-errorcolor text-center">Apple Pay is not eligible for this transaction.</p>
|
|
|
+ ) : applePayConfig ? (
|
|
|
+
|
|
|
<ApplePayButtonBox
|
|
|
onClickHandler={validateCheckout}
|
|
|
- eligibilityError={eligibilityError}
|
|
|
- applePayConfig={applePayConfig.config}
|
|
|
+ applePayConfig={applePayConfig}
|
|
|
applePaySessionVersion={4}
|
|
|
paymentRequest={{
|
|
|
- countryCode: merchantCountry === 'C2' ? 'CN' : merchantCountry as string,
|
|
|
+ countryCode: applePayConfig.merchantCountry === 'C2' ? 'CN' : applePayConfig.merchantCountry as string,
|
|
|
currencyCode: currencyCode,
|
|
|
requiredBillingContactFields: ["postalAddress"],
|
|
|
requiredShippingContactFields: ["name", "email", "postalAddress"],
|
|
|
@@ -256,18 +268,21 @@ export default function PaypalApplepayButton({
|
|
|
onCancel={onCancel}
|
|
|
onError={onError}
|
|
|
/>
|
|
|
- }
|
|
|
- </div>
|
|
|
+
|
|
|
+ ) : (
|
|
|
+ <p className="text-ly-12 text-center">Loading Apple Pay configuration...</p>
|
|
|
+ );
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div className="box-border w-full"> {tips} </div>
|
|
|
);
|
|
|
}
|
|
|
|
|
|
// ApplePayButtonBox是 @paypal/react-paypal-js/sdk-v6 中的<ApplePayOneTimePaymentButton>组件的代码复制过来的
|
|
|
function ApplePayButtonBox({
|
|
|
- eligibilityError,
|
|
|
onClickHandler, // 点击苹果支付按钮
|
|
|
...hookProps
|
|
|
}: {
|
|
|
- eligibilityError: Error | null;
|
|
|
onClickHandler: () => Promise<boolean>;
|
|
|
} & UseApplePayOneTimePaymentSessionProps
|
|
|
) {
|
|
|
@@ -330,8 +345,7 @@ function ApplePayButtonBox({
|
|
|
buttonstyle="black"
|
|
|
type="pay"
|
|
|
locale="en"
|
|
|
- />
|
|
|
- { eligibilityError && <p className="text-ly-errorcolor text-ly-12 text-center">{eligibilityError.message}</p> }
|
|
|
+ />
|
|
|
</>
|
|
|
);
|
|
|
};
|