diff --git a/src/Donations/Components/PaymentsForm.tsx b/src/Donations/Components/PaymentsForm.tsx index bd76c60f..edd57654 100644 --- a/src/Donations/Components/PaymentsForm.tsx +++ b/src/Donations/Components/PaymentsForm.tsx @@ -334,7 +334,8 @@ function PaymentsForm(): ReactElement { } > - {paymentError} + {/* TODO - better error handling */} + {t(paymentError)} )} {!isCreatingDonation && diff --git a/src/Donations/PaymentMethods/PaymentFunctions.ts b/src/Donations/PaymentMethods/PaymentFunctions.ts index 144f4d9d..a19e402b 100644 --- a/src/Donations/PaymentMethods/PaymentFunctions.ts +++ b/src/Donations/PaymentMethods/PaymentFunctions.ts @@ -17,6 +17,7 @@ import { Donation, DonationRequestData } from "src/Common/Types/donation"; import { PaymentMethod } from "@stripe/stripe-js/types/api/payment-methods"; import { PaymentIntentResult, Stripe, StripeError } from "@stripe/stripe-js"; import { Dispatch, SetStateAction } from "react"; +import { APIError, handleError } from "@planet-sdk/common"; export function buildPaymentProviderRequest( gateway: string, @@ -149,7 +150,11 @@ export async function createDonationFunction({ return donation.data as Donation; } } catch (error) { - if (error.status === 400) { + // hack to address errors when donation was not created. Previous incorrect error handling logic is commented below for reference. + // TODO - better error handling + const serializedErrors = handleError(error as APIError); + setPaymentError(serializedErrors[0]?.message || error.message); + /* if (error.status === 400) { setPaymentError(error.data.message); } else if (error.status === 500) { setPaymentError("Something went wrong please try again soon!"); @@ -159,7 +164,7 @@ export async function createDonationFunction({ ); } else { setPaymentError(error.message); - } + } */ setIsPaymentProcessing(false); } } diff --git a/src/Layout/QueryParamContext.tsx b/src/Layout/QueryParamContext.tsx index 2592f7bc..cfecf790 100644 --- a/src/Layout/QueryParamContext.tsx +++ b/src/Layout/QueryParamContext.tsx @@ -189,10 +189,21 @@ const QueryParamProvider: FC = ({ children }) => { }, [currency, enabledCurrencies]); useEffect(() => { + // hack to address errors when donation was not created. + // TODO - better error handling if (paymentError) { - router.replace({ - query: { ...router.query, step: THANK_YOU }, - }); + if (paymentError === "validationError") { + setErrors([ + { + message: paymentError, + errorType: "validation_error", + }, + ]); + } else { + router.replace({ + query: { ...router.query, step: THANK_YOU }, + }); + } } }, [paymentError]); useEffect(() => {