From 8ccd0d129ce9355f27e8c5d3a3a0df1c27378943 Mon Sep 17 00:00:00 2001 From: mohitb35 <44917347+mohitb35@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:58:07 +0530 Subject: [PATCH] fix: fixes issues with native payments to pcash - ensures amount is always correct - ensures event handler is always attached - cleans up event handler on unmount --- src/Donations/Components/DonationsForm.tsx | 11 +++++++++++ src/Donations/Components/PaymentsForm.tsx | 10 ++++++---- .../PaymentRequestCustomButton.tsx | 17 +++++++---------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Donations/Components/DonationsForm.tsx b/src/Donations/Components/DonationsForm.tsx index 85562c6b..b26266f7 100644 --- a/src/Donations/Components/DonationsForm.tsx +++ b/src/Donations/Components/DonationsForm.tsx @@ -68,6 +68,7 @@ function DonationsForm(): ReactElement { utmMedium, utmSource, isPackageWanted, + setPaymentRequest, } = React.useContext(QueryParamContext); const { t, i18n } = useTranslation(["common", "country", "donate"]); @@ -78,6 +79,16 @@ function DonationsForm(): ReactElement { React.useState(false); const router = useRouter(); + React.useEffect(() => { + setPaymentRequest(null); + }, []); + + React.useEffect(() => { + if (isPlanetCashActive) { + setPaymentRequest(null); + } + }, [isPlanetCashActive]); + React.useEffect(() => { setMinAmt(getMinimumAmountForCurrency(currency)); }, [currency]); diff --git a/src/Donations/Components/PaymentsForm.tsx b/src/Donations/Components/PaymentsForm.tsx index de990ca3..0045585b 100644 --- a/src/Donations/Components/PaymentsForm.tsx +++ b/src/Donations/Components/PaymentsForm.tsx @@ -82,6 +82,7 @@ function PaymentsForm(): ReactElement { utmMedium, utmSource, isPackageWanted, + setPaymentRequest, } = React.useContext(QueryParamContext); const [stripePromise, setStripePromise] = @@ -100,6 +101,7 @@ function PaymentsForm(): ReactElement { React.useEffect(() => { setPaymentType("CARD"); + setPaymentRequest(null); }, []); const sofortCountries = ["AT", "BE", "DE", "IT", "NL", "ES"]; @@ -111,7 +113,7 @@ function PaymentsForm(): ReactElement { | string | PaymentMethod | PaypalApproveData - | PaypalErrorData, + | PaypalErrorData ) => { if (!paymentSetup || !donationID) { console.log("Missing payment options"); //TODOO - better error handling @@ -144,7 +146,7 @@ function PaymentsForm(): ReactElement { // Seems to work only for native pay. Should this be removed? const onPaymentFunction = async ( paymentMethod: PaymentMethod, - paymentRequest: PaymentRequest, + paymentRequest: PaymentRequest ) => { setPaymentType(paymentRequest._activeBackingLibraryName); //TODOO --_activeBackingLibraryName is a private variable? const gateway = "stripe"; @@ -289,7 +291,7 @@ function PaymentsForm(): ReactElement { query: { ...router.query, step: CONTACT }, }, undefined, - { shallow: true }, + { shallow: true } ); }} className="d-flex" @@ -420,7 +422,7 @@ function PaymentsForm(): ReactElement { totalCost={getFormatedCurrency( i18n.language, currency, - paymentSetup?.unitCost * quantity, + paymentSetup?.unitCost * quantity )} onPaymentFunction={(providerObject: PaymentMethod) => onSubmitPayment("stripe", "card", providerObject) diff --git a/src/Donations/PaymentMethods/PaymentRequestCustomButton.tsx b/src/Donations/PaymentMethods/PaymentRequestCustomButton.tsx index b75aaf27..11682608 100644 --- a/src/Donations/PaymentMethods/PaymentRequestCustomButton.tsx +++ b/src/Donations/PaymentMethods/PaymentRequestCustomButton.tsx @@ -54,10 +54,6 @@ export const PaymentRequestCustomButton = ({ // Tracks if native pay buttons were shown at least once to prevent layout jerks const [wasNativePayInit, setWasNativePayInit] = useState(false); - useEffect(() => { - setPaymentRequest(null); - }, []); - useEffect(() => { if ( stripe && @@ -85,11 +81,9 @@ export const PaymentRequestCustomButton = ({ }, [stripe, paymentRequest, country, currency, amount]); useEffect(() => { - if (stripe && paymentRequest) { - setPaymentRequest(null); - setCanMakePayment(false); - setPaymentLoading(false); - } + setPaymentRequest(null); + setCanMakePayment(false); + setPaymentLoading(false); }, [country, currency, amount]); useEffect(() => { @@ -125,7 +119,10 @@ export const PaymentRequestCustomButton = ({ ); } return () => { - if (paymentRequest && !paymentLoading) { + if ( + paymentRequest && + paymentRequest.hasRegisteredListener("paymentmethod") + ) { paymentRequest.off("paymentmethod", () => { setPaymentLoading(false); });