From b3a05ae3d8667d56ebe1e8f98ecf3e0ec096406f Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Wed, 4 Sep 2024 12:02:23 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20bug=20with=20defaultShippi?= =?UTF-8?q?ngId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/js/GooglepayButton.js | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index 3e1fc2eb4..98e22f55b 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -557,19 +557,23 @@ class GooglepayButton extends PaymentButton { * @return {Object} Sanitized object. */ sanitizeShippingOptions( responseData ) { - const cleanOptions = []; + // Sanitize the shipping options. + const cleanOptions = responseData.shippingOptions.map( ( item ) => ( { + id: item.id, + label: item.label, + description: item.description, + } ) ); - responseData.shippingOptions.forEach( ( item ) => { - cleanOptions.push( { - id: item.id, - label: item.label, - description: item.description, - } ); - } ); + // Ensure that the default option is valid. + let defaultOptionId = responseData.defaultSelectedOptionId; + if ( ! cleanOptions.some( ( item ) => item.id === defaultOptionId ) ) { + defaultOptionId = cleanOptions[ 0 ].id; + } - responseData.shippingOptions = cleanOptions; - - return { ...responseData, shippingOptions: cleanOptions }; + return { + defaultSelectedOptionId: defaultOptionId, + shippingOptions: cleanOptions, + }; } /**