From b4cd6bb121e0041c1a91d57bcd840309d89deb04 Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Fri, 23 Aug 2024 11:52:00 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20critical=20Google=20Pay=20?= =?UTF-8?q?error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/js/GooglepayButton.js | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index f6ceafc61..6b67dab72 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -515,7 +515,9 @@ class GooglepayButton extends PaymentButton { ) ) { paymentDataRequestUpdate.newShippingOptionParameters = - updatedData.shipping_options; + this.sanitizeShippingOptions( + updatedData.shipping_options + ); } if ( updatedData.total && hasRealCart ) { @@ -541,6 +543,30 @@ class GooglepayButton extends PaymentButton { } ); } + /** + * Google Pay throws an error, when the shippingOptions entries contain + * custom properties. This function strips unsupported properties from the + * provided ajax response. + * + * @param {Object} responseData Data returned from the ajax endpoint. + * @return {Object} Sanitized object. + */ + sanitizeShippingOptions( responseData ) { + const cleanOptions = []; + + responseData.shippingOptions.forEach( ( item ) => { + cleanOptions.push( { + id: item.id, + label: item.label, + description: item.description, + } ); + } ); + + responseData.shippingOptions = cleanOptions; + + return { ...responseData, shippingOptions: cleanOptions }; + } + /** * Returns the shipping costs as numeric value. *