Skip to content

Commit

Permalink
Show selected method in apple window
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Oct 10, 2024
1 parent 1904d91 commit 9fae4de
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
13 changes: 11 additions & 2 deletions resources/js/blocks/ApplePayButtonComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ 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},
shop: {countryCode, currencyCode = 'EUR', totalLabel = ''},
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',
Expand All @@ -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)
Expand Down Expand Up @@ -74,6 +82,7 @@ export const ApplePayButtonComponent = ({cart, extensions}) => {
})
}
session.onvalidatemerchant = (applePayValidateMerchantEvent) => {
console.log(selectedShippingMethod)
jQuery.ajax({
url: ajaxUrl,
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions src/Buttons/ApplePayButton/AppleAjaxRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
30 changes: 28 additions & 2 deletions src/Buttons/ApplePayButton/ResponsesToApple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Functional/ApplePayButton/ResponsesToAppleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 9fae4de

Please sign in to comment.