Index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Paypal\Controller\Ipn;
  8. use Magento\Framework\App\CsrfAwareActionInterface;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\App\Request\InvalidRequestException;
  11. use Magento\Framework\App\RequestInterface;
  12. use Magento\Framework\Exception\RemoteServiceUnavailableException;
  13. use Magento\Sales\Model\OrderFactory;
  14. /**
  15. * Unified IPN controller for all supported PayPal methods
  16. */
  17. class Index extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface
  18. {
  19. /**
  20. * @var \Psr\Log\LoggerInterface
  21. */
  22. protected $_logger;
  23. /**
  24. * @var \Magento\Paypal\Model\IpnFactory
  25. */
  26. protected $_ipnFactory;
  27. /**
  28. * @var OrderFactory
  29. */
  30. private $orderFactory;
  31. /**
  32. * @param \Magento\Framework\App\Action\Context $context
  33. * @param \Magento\Paypal\Model\IpnFactory $ipnFactory
  34. * @param \Psr\Log\LoggerInterface $logger
  35. * @param OrderFactory $orderFactory
  36. */
  37. public function __construct(
  38. \Magento\Framework\App\Action\Context $context,
  39. \Magento\Paypal\Model\IpnFactory $ipnFactory,
  40. \Psr\Log\LoggerInterface $logger,
  41. OrderFactory $orderFactory = null
  42. ) {
  43. $this->_logger = $logger;
  44. $this->_ipnFactory = $ipnFactory;
  45. $this->orderFactory = $orderFactory ?: ObjectManager::getInstance()->get(OrderFactory::class);
  46. parent::__construct($context);
  47. }
  48. /**
  49. * @inheritDoc
  50. */
  51. public function createCsrfValidationException(
  52. RequestInterface $request
  53. ): ?InvalidRequestException {
  54. return null;
  55. }
  56. /**
  57. * @inheritDoc
  58. */
  59. public function validateForCsrf(RequestInterface $request): ?bool
  60. {
  61. return true;
  62. }
  63. /**
  64. * Instantiate IPN model and pass IPN request to it
  65. *
  66. * @return void
  67. * @SuppressWarnings(PHPMD.ExitExpression)
  68. */
  69. public function execute()
  70. {
  71. if (!$this->getRequest()->isPost()) {
  72. return;
  73. }
  74. try {
  75. $data = $this->getRequest()->getPostValue();
  76. $this->_ipnFactory->create(['data' => $data])->processIpnRequest();
  77. $incrementId = $this->getRequest()->getPostValue()['invoice'];
  78. $this->_eventManager->dispatch(
  79. 'paypal_checkout_success',
  80. [
  81. 'order' => $this->orderFactory->create()->loadByIncrementId($incrementId)
  82. ]
  83. );
  84. } catch (RemoteServiceUnavailableException $e) {
  85. $this->_logger->critical($e);
  86. $this->getResponse()->setStatusHeader(503, '1.1', 'Service Unavailable')->sendResponse();
  87. /** @todo eliminate usage of exit statement */
  88. exit;
  89. } catch (\Exception $e) {
  90. $this->_logger->critical($e);
  91. $this->getResponse()->setHttpResponseCode(500);
  92. }
  93. }
  94. }