Skip to content

Commit

Permalink
Merge branch 'pr-712' into release-week-45
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Nov 6, 2023
2 parents 8d59ed7 + 99e6343 commit 3ff1049
Showing 1 changed file with 18 additions and 76 deletions.
94 changes: 18 additions & 76 deletions Controller/ApplePay/ShippingMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterfaceFactory;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
use Magento\Quote\Api\GuestCartRepositoryInterface;
use Magento\Quote\Api\PaymentMethodManagementInterface;
use Magento\Quote\Api\ShippingMethodManagementInterface;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Address\Total as AddressTotal;

class ShippingMethods extends Action
Expand All @@ -33,47 +30,25 @@ class ShippingMethods extends Action
private $guestCartRepository;

/**
* @var AddressInterfaceFactory
*/
private $addressFactory;

/**
* @var PaymentMethodManagementInterface
*/
private $paymentMethodManagement;

/**
* @var PaymentInterfaceFactory
* @var ShippingMethodManagementInterface
*/
private $paymentInterfaceFactory;
private $shippingMethodManagement;

/**
* @var CheckoutSession
*/
private $checkoutSession;

/**
* @var ShippingMethodManagementInterface
*/
private $shippingMethodManagement;

public function __construct(
Context $context,
CartRepositoryInterface $cartRepository,
GuestCartRepositoryInterface $guestCartRepository,
ShippingMethodManagementInterface $shippingMethodManagement,
AddressInterfaceFactory $addressFactory,
PaymentMethodManagementInterface $paymentMethodManagement,
PaymentInterfaceFactory $paymentInterfaceFactory,
CheckoutSession $checkoutSession
CheckoutSession $checkoutSession,
GuestCartRepositoryInterface $guestCartRepository
) {
parent::__construct($context);

$this->guestCartRepository = $guestCartRepository;
$this->shippingMethodManagement = $shippingMethodManagement;
$this->addressFactory = $addressFactory;
$this->paymentMethodManagement = $paymentMethodManagement;
$this->paymentInterfaceFactory = $paymentInterfaceFactory;
$this->guestCartRepository = $guestCartRepository;
$this->cartRepository = $cartRepository;
$this->checkoutSession = $checkoutSession;
}
Expand All @@ -82,28 +57,25 @@ public function execute()
{
$cart = $this->getCart();

$address = $this->addressFactory->create();
/**
* @var Address $address
*/
$address = $cart->getShippingAddress();
$address->setData(null);
$address->setCountryId($this->getRequest()->getParam('countryCode'));
$address->setPostcode($this->getRequest()->getParam('postalCode'));

$cart->setShippingAddress($address);

$cart->collectTotals();
$this->cartRepository->save($cart);

if ($this->getRequest()->getParam('shippingMethod')) {
$this->addShippingMethod($cart, $this->getRequest()->getParam('shippingMethod')['identifier']);
$address->setCollectShippingRates(true);
$address->setShippingMethod($this->getRequest()->getParam('shippingMethod')['identifier']);
}

$methods = $this->shippingMethodManagement->getList($cart->getId());
$this->setDefaultShippingMethod($cart, $methods);

/** @var PaymentInterface $payment */
$payment = $this->paymentInterfaceFactory->create();
$payment->setMethod('mollie_methods_applepay');
$this->paymentMethodManagement->set($cart->getId(), $payment);
$cart = $this->cartRepository->get($cart->getId());
$cart->setPaymentMethod('mollie_methods_applepay');
$cart->getPayment()->importData(['method' => 'mollie_methods_applepay']);
$this->cartRepository->save($cart);
$cart->collectTotals();

$methods = $this->shippingMethodManagement->getList($cart->getId());
$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);

return $response->setData([
Expand All @@ -125,36 +97,6 @@ public function execute()
]);
}

/**
* @param CartInterface $cart
* @param \Magento\Quote\Api\Data\ShippingMethodInterface[] $methods
*/
private function setDefaultShippingMethod(CartInterface $cart, array $methods)
{
if ($cart->getShippingAddress()->getShippingMethod()) {
return;
}

$method = array_shift($methods);
if (!$method) {
return;
}

$this->addShippingMethod($cart, $method->getCarrierCode() . '_' . $method->getMethodCode());
$this->cartRepository->save($cart);
}

private function addShippingMethod(CartInterface $cart, string $identifier)
{
$address = $cart->getShippingAddress();

$address->setShippingMethod($identifier);
$address->setCollectShippingRates(true);
$address->save();

$address->collectShippingRates();
}

/**
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return CartInterface
Expand Down

0 comments on commit 3ff1049

Please sign in to comment.