Skip to content

Commit

Permalink
fix: prevents repeated Google/ApplePay charges
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitb35 committed Jan 3, 2024
1 parent de0c599 commit 0741a44
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/Donations/PaymentMethods/PaymentRequestCustomButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface PaymentButtonProps {
amount: number;
onPaymentFunction: (
paymentMethod: PaymentMethod,
paymentRequest: PaymentRequest
paymentRequest: PaymentRequest,
) => Promise<void>;
continueNext: () => void;
isPaymentPage: boolean;
Expand Down Expand Up @@ -65,7 +65,7 @@ export const PaymentRequestCustomButton = ({
requestPayerName: true,
requestPayerEmail: true,
});
console.log(pr);
console.log("Stripe PR: ", pr);
// Check the availability of the Payment Request API.
pr.canMakePayment().then((result) => {
if (result) {
Expand All @@ -84,7 +84,7 @@ export const PaymentRequestCustomButton = ({
}, [country, currency, amount]);

useEffect(() => {
console.log(paymentRequest);
console.log("Context PR: ", paymentRequest);
let subscribed = true;
if (paymentRequest) {
paymentRequest
Expand All @@ -106,29 +106,24 @@ export const PaymentRequestCustomButton = ({
useEffect(() => {
if (paymentRequest && !paymentLoading) {
setPaymentLoading(true);
paymentRequest.off("paymentmethod");
paymentRequest.on(
"paymentmethod",
({ complete, paymentMethod }: PaymentRequestPaymentMethodEvent) => {
onPaymentFunction(paymentMethod, paymentRequest);
complete("success");
setPaymentLoading(false);
}
},
);
}
return () => {
if (paymentRequest && !paymentLoading) {
paymentRequest.off(
"paymentmethod",
({ complete, paymentMethod }: PaymentRequestPaymentMethodEvent) => {
onPaymentFunction(paymentMethod, paymentRequest);
complete("success");
setPaymentLoading(false);
}
);
paymentRequest.off("paymentmethod", () => {
setPaymentLoading(false);
});
}
};
}, [paymentRequest, onPaymentFunction]);
console.log(paymentRequest);

return ready ? (
<div
Expand Down Expand Up @@ -217,7 +212,7 @@ interface NativePayProps {
amount: number;
onPaymentFunction: (
paymentMethod: PaymentMethod,
paymentRequest: PaymentRequest
paymentRequest: PaymentRequest,
) => Promise<void>;
paymentSetup: PaymentOptions;
continueNext: () => void;
Expand All @@ -239,7 +234,7 @@ export const NativePay = ({
const { i18n } = useTranslation();
const [stripePromise, setStripePromise] =
useState<null | Promise<Stripe | null>>(() =>
getStripe(paymentSetup, i18n.language)
getStripe(paymentSetup, i18n.language),
);

useEffect(() => {
Expand Down

0 comments on commit 0741a44

Please sign in to comment.