SaveShippingMethod.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Controller\Paypal;
  7. use Magento\Checkout\Model\Session;
  8. use Magento\Framework\View\Result\Page;
  9. use Magento\Framework\App\Action\Context;
  10. use Magento\Framework\Controller\ResultFactory;
  11. use Magento\Braintree\Gateway\Config\PayPal\Config;
  12. use Magento\Braintree\Model\Paypal\Helper\ShippingMethodUpdater;
  13. /**
  14. * Class SaveShippingMethod
  15. */
  16. class SaveShippingMethod extends AbstractAction
  17. {
  18. /**
  19. * @var ShippingMethodUpdater
  20. */
  21. private $shippingMethodUpdater;
  22. /**
  23. * Constructor
  24. *
  25. * @param Context $context
  26. * @param Config $config
  27. * @param Session $checkoutSession
  28. * @param ShippingMethodUpdater $shippingMethodUpdater
  29. */
  30. public function __construct(
  31. Context $context,
  32. Config $config,
  33. Session $checkoutSession,
  34. ShippingMethodUpdater $shippingMethodUpdater
  35. ) {
  36. parent::__construct($context, $config, $checkoutSession);
  37. $this->shippingMethodUpdater = $shippingMethodUpdater;
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function execute()
  43. {
  44. $isAjax = $this->getRequest()->getParam('isAjax');
  45. $quote = $this->checkoutSession->getQuote();
  46. try {
  47. $this->validateQuote($quote);
  48. $this->shippingMethodUpdater->execute(
  49. $this->getRequest()->getParam('shipping_method'),
  50. $quote
  51. );
  52. if ($isAjax) {
  53. /** @var Page $response */
  54. $response = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
  55. $layout = $response->addHandle('paypal_express_review_details')->getLayout();
  56. $response = $layout->getBlock('page.block')->toHtml();
  57. $this->getResponse()->setBody($response);
  58. return;
  59. }
  60. } catch (\Exception $e) {
  61. $this->messageManager->addExceptionMessage($e, $e->getMessage());
  62. }
  63. $path = $this->_url->getUrl('*/*/review', ['_secure' => true]);
  64. if ($isAjax) {
  65. $this->getResponse()->setBody(sprintf('<script>window.location.href = "%s";</script>', $path));
  66. return;
  67. }
  68. $this->_redirect($path);
  69. }
  70. }