PlaceOrder.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Braintree\Gateway\Config\PayPal\Config;
  8. use Magento\Braintree\Model\Paypal\Helper;
  9. use Magento\Checkout\Model\Session;
  10. use Magento\Framework\App\Action\Context;
  11. use Magento\Framework\App\Action\HttpPostActionInterface;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Framework\Controller\ResultFactory;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17. * Class PlaceOrder
  18. */
  19. class PlaceOrder extends AbstractAction implements HttpPostActionInterface
  20. {
  21. /**
  22. * @var Helper\OrderPlace
  23. */
  24. private $orderPlace;
  25. /**
  26. * Logger for exception details
  27. *
  28. * @var LoggerInterface
  29. */
  30. private $logger;
  31. /**
  32. * Constructor
  33. *
  34. * @param Context $context
  35. * @param Config $config
  36. * @param Session $checkoutSession
  37. * @param Helper\OrderPlace $orderPlace
  38. * @param LoggerInterface|null $logger
  39. */
  40. public function __construct(
  41. Context $context,
  42. Config $config,
  43. Session $checkoutSession,
  44. Helper\OrderPlace $orderPlace,
  45. LoggerInterface $logger = null
  46. ) {
  47. parent::__construct($context, $config, $checkoutSession);
  48. $this->orderPlace = $orderPlace;
  49. $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
  50. }
  51. /**
  52. * @inheritdoc
  53. *
  54. * @throws LocalizedException
  55. */
  56. public function execute()
  57. {
  58. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  59. $agreement = array_keys($this->getRequest()->getPostValue('agreement', []));
  60. $quote = $this->checkoutSession->getQuote();
  61. try {
  62. $this->validateQuote($quote);
  63. $this->orderPlace->execute($quote, $agreement);
  64. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  65. return $resultRedirect->setPath('checkout/onepage/success', ['_secure' => true]);
  66. } catch (\Exception $e) {
  67. $this->logger->critical($e);
  68. $this->messageManager->addExceptionMessage(
  69. $e,
  70. 'The order #' . $quote->getReservedOrderId() . ' cannot be processed.'
  71. );
  72. }
  73. return $resultRedirect->setPath('checkout/cart', ['_secure' => true]);
  74. }
  75. }