From c1b7ae5bfc7c03298844333ddbd7c1fb36a19489 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Tue, 4 Jun 2024 16:57:23 -0300 Subject: [PATCH] Fix: throw error message instead of the error object when submitting an donation made with Stripe Element gateway (#7399) --- .../stripePaymentElementGateway.tsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/PaymentGateways/Gateways/Stripe/StripePaymentElementGateway/stripePaymentElementGateway.tsx b/src/PaymentGateways/Gateways/Stripe/StripePaymentElementGateway/stripePaymentElementGateway.tsx index ae9478a996..9d5f44d4e1 100644 --- a/src/PaymentGateways/Gateways/Stripe/StripePaymentElementGateway/stripePaymentElementGateway.tsx +++ b/src/PaymentGateways/Gateways/Stripe/StripePaymentElementGateway/stripePaymentElementGateway.tsx @@ -8,6 +8,7 @@ import { import {Elements, PaymentElement, useElements, useStripe} from '@stripe/react-stripe-js'; import {applyFilters} from '@wordpress/hooks'; import type {Gateway, GatewaySettings} from '@givewp/forms/types'; +import {__, sprintf} from '@wordpress/i18n'; let stripePromise = null; let stripePaymentMethod = null; @@ -123,7 +124,21 @@ const stripePaymentElementGateway: StripeGateway = { const {error: submitError} = await this.elements.submit(); if (submitError) { - throw new Error(submitError); + let errorMessage = __('Invalid Payment Data.', 'give'); + + if (typeof submitError === 'string') { + errorMessage = sprintf(__('Invalid Payment Data. Error Details: %s', 'give'), submitError); + } + + if (submitError.hasOwnProperty('code') && submitError.hasOwnProperty('message')) { + errorMessage = sprintf( + __('Invalid Payment Data. Error Details: %s (code: %s)', 'give'), + submitError.message, + submitError.code + ); + } + + throw new Error(errorMessage); } return {