Skip to content

Commit

Permalink
fix: fixes issues with native payments to pcash
Browse files Browse the repository at this point in the history
- ensures amount is always correct
- ensures event handler is always attached
- cleans up event handler on unmount
  • Loading branch information
mohitb35 committed Jan 17, 2024
1 parent 37cf052 commit 8ccd0d1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/Donations/Components/DonationsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function DonationsForm(): ReactElement {
utmMedium,
utmSource,
isPackageWanted,
setPaymentRequest,
} = React.useContext(QueryParamContext);
const { t, i18n } = useTranslation(["common", "country", "donate"]);

Expand All @@ -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]);
Expand Down
10 changes: 6 additions & 4 deletions src/Donations/Components/PaymentsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function PaymentsForm(): ReactElement {
utmMedium,
utmSource,
isPackageWanted,
setPaymentRequest,
} = React.useContext(QueryParamContext);

const [stripePromise, setStripePromise] =
Expand All @@ -100,6 +101,7 @@ function PaymentsForm(): ReactElement {

React.useEffect(() => {
setPaymentType("CARD");
setPaymentRequest(null);
}, []);

const sofortCountries = ["AT", "BE", "DE", "IT", "NL", "ES"];
Expand All @@ -111,7 +113,7 @@ function PaymentsForm(): ReactElement {
| string
| PaymentMethod
| PaypalApproveData
| PaypalErrorData,
| PaypalErrorData
) => {
if (!paymentSetup || !donationID) {
console.log("Missing payment options"); //TODOO - better error handling
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -289,7 +291,7 @@ function PaymentsForm(): ReactElement {
query: { ...router.query, step: CONTACT },
},
undefined,
{ shallow: true },
{ shallow: true }
);
}}
className="d-flex"
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions src/Donations/PaymentMethods/PaymentRequestCustomButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check notice on line 56 in src/Donations/PaymentMethods/PaymentRequestCustomButton.tsx

View check run for this annotation

codefactor.io / CodeFactor

src/Donations/PaymentMethods/PaymentRequestCustomButton.tsx#L35-L56

Complex Method
useEffect(() => {
setPaymentRequest(null);
}, []);

useEffect(() => {
if (
stripe &&
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -125,7 +119,10 @@ export const PaymentRequestCustomButton = ({
);
}
return () => {
if (paymentRequest && !paymentLoading) {
if (
paymentRequest &&
paymentRequest.hasRegisteredListener("paymentmethod")
) {
paymentRequest.off("paymentmethod", () => {
setPaymentLoading(false);
});
Expand Down

0 comments on commit 8ccd0d1

Please sign in to comment.