Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bp 3856 add csp allow list for apple pay #1083

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Block/Catalog/Product/View/Applepay.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public function __construct(
*/
public function canShowButton($page)
{
return $this->cart->getSummaryQty() &&
$this->isModuleActive() &&
return $this->isModuleActive() &&
in_array($page, $this->applepayConfigProvider->getAvailableButtons()) &&
$this->applepayConfigProvider->isApplePayEnabled($this->_storeManager->getStore());
}
Expand Down
51 changes: 35 additions & 16 deletions Service/Applepay/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function __construct(
public function process($request)
{
$cart_hash = $request->getParam('id');

if($cart_hash) {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cart_hash);
$cart = $this->cartRepository->get($cartId);
Expand All @@ -121,14 +121,21 @@ public function process($request)
}

$product = $request->getParam('product');

// Check if product data is present and valid
if (!$product || !is_array($product) || !isset($product['id']) || !is_numeric($product['id'])) {
throw new \Exception('Product data is missing or invalid.');
}


$cart->removeAllItems();

try {
$productToBeAdded = $this->productRepository->getById($product['id']);
} catch (NoSuchEntityException $e) {
throw new NoSuchEntityException(__('Could not find a product with ID "%id"', ['id' => $product['id']]));
}

$cartItem = new CartItem(
$productToBeAdded->getSku(),
$product['qty']
Expand All @@ -140,19 +147,19 @@ public function process($request)

$cart->addProduct($productToBeAdded, $this->requestBuilder->build($cartItem));
$this->cartRepository->save($cart);

$wallet = $request->getParam('wallet');

$shippingMethodsResult = [];
if (!$cart->getIsVirtual()) {
$shippingAddressData = $this->applepayModel->processAddressFromWallet($wallet, 'shipping');


$shippingAddress = $this->quoteAddressFactory->create();
$shippingAddress->addData($shippingAddressData);

$errors = $shippingAddress->validate();
$errors = $shippingAddress->validate();

try {
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
} catch (\Exception $e) {
Expand All @@ -161,16 +168,28 @@ public function process($request)
}
$this->quoteRepository->save($cart);
//this delivery address is already assigned to the cart
$shippingMethods = $this->appleShippingMethod->getAvailableMethods( $cart);
foreach ($shippingMethods as $index => $shippingMethod) {

try {
$shippingMethods = $this->appleShippingMethod->getAvailableMethods($cart);
} catch (\Exception $e) {
throw new \Exception(__('Unable to retrieve shipping methods.'));
}

foreach ($shippingMethods as $method) {
$shippingMethodsResult[] = [
'carrier_title' => $shippingMethod['carrier_title'],
'price_incl_tax' => round($shippingMethod['amount'], 2),
'method_code' => $shippingMethod['carrier_code'] . '_' . $shippingMethod['method_code'],
'method_title' => $shippingMethod['method_title'],
'carrier_title' => $method['carrier_title'],
'price_incl_tax' => round($method['amount']['value'], 2),
'method_code' => $method['carrier_code'] . '__SPLIT__' . $method['method_code'],
'method_title' => $method['method_title'],
];
}
$cart->getShippingAddress()->setShippingMethod($shippingMethodsResult[0]['method_code']);

if (!empty($shippingMethodsResult)) {
// Set the first available shipping method
$cart->getShippingAddress()->setShippingMethod($shippingMethodsResult[0]['method_code']);
} else {
throw new \Exception(__('No shipping methods are available for the provided address.'));
}
}
$cart->setTotalsCollectedFlag(false);
$cart->collectTotals();
Expand Down Expand Up @@ -200,4 +219,4 @@ public function gatherTotals($address, $quoteTotals)
'grand_total' => $quoteTotals['grand_total']->getValue()
];
}
}
}
4 changes: 2 additions & 2 deletions view/frontend/templates/catalog/product/view/applepay.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/
?>
<?php if ($block->canShowButton('Product')): ?>
<div id="apple-pay-catalog-product-view-component" data-bind="scope:'applepayproductcomponent'">
<!-- ko template: getTemplate() --><!-- /ko -->
<div id="apple-pay-catalog-product-view-component" data-bind="scope:'applepayproductcomponent'">
<!-- ko template: getTemplate() --><!-- /ko -->

<script>
if ('undefined' === typeof window.checkoutConfig) {
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/templates/checkout/cart/applepay.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}
showPayButton = function() {
require(['jquery', 'buckaroo/applepay/checkout-cart'], function (jQuery, applepayPay) {
applepayPay.showPayButton();
applepayPay.showPayButton('cart');
});
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions view/frontend/web/js/view/checkout/applepay/checkout-cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ define(
'use strict';

return {
showPayButton: function () {
showPayButton: function ($page) {
applepayPay.setQuote(quote);
applepayPay.showPayButton('cart');
applepayPay.showPayButton($page);

applepayPay.transactionResult.subscribe(
function () {
Expand Down
36 changes: 24 additions & 12 deletions view/frontend/web/js/view/checkout/applepay/pay.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ define(
}
}.bind(this),
})
.fail(function() {
this.timeoutRedirect();
}.bind(this));
.fail(function() {
this.timeoutRedirect();
}.bind(this));

return update;
} else if (this.payMode == 'cart') {
Expand Down Expand Up @@ -432,9 +432,9 @@ define(
}
}.bind(this),
})
.fail(function() {
this.timeoutRedirect();
}.bind(this));
.fail(function() {
this.timeoutRedirect();
}.bind(this));

return update;
} else {
Expand Down Expand Up @@ -548,9 +548,9 @@ define(
}
}.bind(this),
})
.fail(function() {
this.timeoutRedirect();
}.bind(this));
.fail(function() {
this.timeoutRedirect();
}.bind(this));

return update;

Expand Down Expand Up @@ -607,10 +607,22 @@ define(
initProductViewWatchers: function () {
this.devLog('==============applepaydebug/initProductViewWatchers');

this.productSelected.id = $('.price-box').attr('data-product-id');
this.productSelected.qty = $('#qty').val();
var self = this;
var productId = $('.price-box').attr('data-product-id');
var productQty = $('#qty').val();

if (!productId) {
console.error('Product ID not found on the page.');
return;
}

if (!productQty) {
productQty = 1;
}

this.productSelected.id = productId;
this.productSelected.qty = productQty;

var self = this;
$('#qty').change(function() {
self.productSelected.qty = $(this).val();
});
Expand Down
Loading