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

Resolve delivery methods issue on Apple Pay #712

Merged
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
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