diff --git a/resources/js/blocks/ApplePayButtonComponent.js b/resources/js/blocks/ApplePayButtonComponent.js index 47d481fa..78bd5529 100644 --- a/resources/js/blocks/ApplePayButtonComponent.js +++ b/resources/js/blocks/ApplePayButtonComponent.js @@ -5,7 +5,6 @@ export const ApplePayButtonComponent = ({cart, extensions}) => { const mollieApplePayBlockDataCart = window.mollieApplePayBlockDataCart || window.mollieBlockData.mollieApplePayBlockDataCart const nonce = document.getElementById("woocommerce-process-checkout-nonce").value let updatedContactInfo = [] - let selectedShippingMethod = [] let redirectionUrl = '' const { product: {needShipping = true, subtotal}, @@ -13,10 +12,18 @@ export const ApplePayButtonComponent = ({cart, extensions}) => { ajaxUrl, } = mollieApplePayBlockDataCart + const findSelectedShippingMethod = (shippingRates) => { + let shippingRate = shippingRates.find((shippingMethod) => shippingMethod.selected === true) + return shippingRate ? shippingRate.rate_id : '' + } + let applePaySession = () => { const session = new ApplePaySession(3, request(countryCode, currencyCode, totalLabel, subtotal)) - console.log('ApplePaySession', session) + const store = wp.data.select('wc/store/cart') + const shippingRates = store.getShippingRates()[0].shipping_rates + let selectedShippingMethod = findSelectedShippingMethod(shippingRates, selectedShippingMethod) session.onshippingmethodselected = function (event) { + console.log(selectedShippingMethod) jQuery.ajax({ url: ajaxUrl, method: 'POST', @@ -31,6 +38,7 @@ export const ApplePayButtonComponent = ({cart, extensions}) => { }, success: (applePayShippingMethodUpdate, textStatus, jqXHR) => { let response = applePayShippingMethodUpdate.data + console.log(response) selectedShippingMethod = event.shippingMethod if (applePayShippingMethodUpdate.success === false) { response.errors = createAppleErrors(response.errors) @@ -74,6 +82,7 @@ export const ApplePayButtonComponent = ({cart, extensions}) => { }) } session.onvalidatemerchant = (applePayValidateMerchantEvent) => { + console.log(selectedShippingMethod) jQuery.ajax({ url: ajaxUrl, method: 'POST', diff --git a/src/Buttons/ApplePayButton/AppleAjaxRequests.php b/src/Buttons/ApplePayButton/AppleAjaxRequests.php index 59a82fb9..ec1b28b1 100644 --- a/src/Buttons/ApplePayButton/AppleAjaxRequests.php +++ b/src/Buttons/ApplePayButton/AppleAjaxRequests.php @@ -191,7 +191,7 @@ public function updateShippingContact() } $paymentDetails = $this->whichCalculateTotals($applePayRequestDataObject); - $response = $this->responseTemplates->appleFormattedResponse($paymentDetails); + $response = $this->responseTemplates->appleFormattedResponse($paymentDetails, $applePayRequestDataObject); $this->responseTemplates->responseSuccess($response); } @@ -212,7 +212,7 @@ public function updateShippingMethod() $this->responseTemplates->responseWithDataErrors($applePayRequestDataObject->errors()); } $paymentDetails = $this->whichCalculateTotals($applePayRequestDataObject); - $response = $this->responseTemplates->appleFormattedResponse($paymentDetails); + $response = $this->responseTemplates->appleFormattedResponse($paymentDetails, $applePayRequestDataObject); $this->responseTemplates->responseSuccess($response); } diff --git a/src/Buttons/ApplePayButton/ResponsesToApple.php b/src/Buttons/ApplePayButton/ResponsesToApple.php index b4b580c2..2f9f34cd 100644 --- a/src/Buttons/ApplePayButton/ResponsesToApple.php +++ b/src/Buttons/ApplePayButton/ResponsesToApple.php @@ -84,11 +84,15 @@ public function responseWithDataErrors($errorList) * * @return array */ - public function appleFormattedResponse(array $paymentDetails) + public function appleFormattedResponse(array $paymentDetails, $applePayRequestDataObject) { $response = []; if ($paymentDetails['shippingMethods']) { - $response['newShippingMethods'] = $paymentDetails['shippingMethods']; + $selectedShippingMethod = $applePayRequestDataObject->shippingMethod(); + $response['newShippingMethods'] = $this->reorderShippingMethods( + $paymentDetails['shippingMethods'], + $selectedShippingMethod + ); } $response['newLineItems'] = $this->appleNewLineItemsResponse( @@ -101,6 +105,28 @@ public function appleFormattedResponse(array $paymentDetails) return $response; } + /** + * Reorders the shipping methods to have the selected shipping method on top so we see it as selected + * @param array $methods + * @param array $selectedShippingMethod + * @return array + */ + private function reorderShippingMethods(array $methods, array $selectedShippingMethod): array + { + $reordered_methods = []; + + foreach ($methods as $key => $method) { + if ($method['identifier'] === $selectedShippingMethod['identifier']) { + $reordered_methods[] = $method; + unset($methods[$key]); + break; + } + } + + return array_merge($reordered_methods, array_values($methods)); + } + + /** * Returns a success response to be handled by the script */ diff --git a/tests/php/Functional/ApplePayButton/ResponsesToAppleTest.php b/tests/php/Functional/ApplePayButton/ResponsesToAppleTest.php index 5f5c793e..01636dab 100644 --- a/tests/php/Functional/ApplePayButton/ResponsesToAppleTest.php +++ b/tests/php/Functional/ApplePayButton/ResponsesToAppleTest.php @@ -82,7 +82,7 @@ public function testAppleFormattedResponseWithoutShippingMethod() $logger = $this->helperMocks->loggerMock(); $appleGateway = $this->mollieGateway('applepay', false, true); $responsesTemplate = new ResponsesToApple($logger, $appleGateway); - $response = $responsesTemplate->appleFormattedResponse($paymentDetails); + $response = $responsesTemplate->appleFormattedResponse($paymentDetails, $applePayRequestDataObject); self::assertEquals($response, $expectedResponse); } @@ -143,7 +143,7 @@ public function testAppleFormattedResponseWithShippingMethod() $logger = $this->helperMocks->loggerMock(); $appleGateway = $this->mollieGateway('applepay', false, true); $responsesTemplate = new ResponsesToApple($logger, $appleGateway); - $response = $responsesTemplate->appleFormattedResponse($paymentDetails); + $response = $responsesTemplate->appleFormattedResponse($paymentDetails, $applePayRequestDataObject); self::assertEquals($response, $expectedResponse); }