Response.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Controller\Transparent;
  7. use Magento\Framework\App\CsrfAwareActionInterface;
  8. use Magento\Framework\App\Request\InvalidRequestException;
  9. use Magento\Framework\App\RequestInterface;
  10. use Magento\Framework\Registry;
  11. use Magento\Framework\App\Action\Context;
  12. use Magento\Framework\View\Result\LayoutFactory;
  13. use Magento\Framework\Controller\ResultInterface;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Magento\Payment\Block\Transparent\Iframe;
  16. use Magento\Paypal\Model\Payflow\Service\Response\Transaction;
  17. use Magento\Paypal\Model\Payflow\Service\Response\Validator\ResponseValidator;
  18. use Magento\Paypal\Model\Payflow\Transparent;
  19. use Magento\Sales\Api\PaymentFailuresInterface;
  20. use Magento\Framework\Session\Generic as Session;
  21. /**
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class Response extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface
  25. {
  26. /**
  27. * Core registry
  28. *
  29. * @var Registry
  30. */
  31. private $coreRegistry;
  32. /**
  33. * @var Transaction
  34. */
  35. private $transaction;
  36. /**
  37. * @var ResponseValidator
  38. */
  39. private $responseValidator;
  40. /**
  41. * @var LayoutFactory
  42. */
  43. private $resultLayoutFactory;
  44. /**
  45. * @var Transparent
  46. */
  47. private $transparent;
  48. /**
  49. * @var PaymentFailuresInterface
  50. */
  51. private $paymentFailures;
  52. /**
  53. * @var Session
  54. */
  55. private $sessionTransparent;
  56. /**
  57. * Constructor
  58. *
  59. * @param Context $context
  60. * @param Registry $coreRegistry
  61. * @param Transaction $transaction
  62. * @param ResponseValidator $responseValidator
  63. * @param LayoutFactory $resultLayoutFactory
  64. * @param Transparent $transparent
  65. * @param Session|null $sessionTransparent
  66. * @param PaymentFailuresInterface|null $paymentFailures
  67. */
  68. public function __construct(
  69. Context $context,
  70. Registry $coreRegistry,
  71. Transaction $transaction,
  72. ResponseValidator $responseValidator,
  73. LayoutFactory $resultLayoutFactory,
  74. Transparent $transparent,
  75. Session $sessionTransparent = null,
  76. PaymentFailuresInterface $paymentFailures = null
  77. ) {
  78. parent::__construct($context);
  79. $this->coreRegistry = $coreRegistry;
  80. $this->transaction = $transaction;
  81. $this->responseValidator = $responseValidator;
  82. $this->resultLayoutFactory = $resultLayoutFactory;
  83. $this->transparent = $transparent;
  84. $this->sessionTransparent = $sessionTransparent ?: $this->_objectManager->get(Session::class);
  85. $this->paymentFailures = $paymentFailures ?: $this->_objectManager->get(PaymentFailuresInterface::class);
  86. }
  87. /**
  88. * @inheritDoc
  89. */
  90. public function createCsrfValidationException(
  91. RequestInterface $request
  92. ): ?InvalidRequestException {
  93. return null;
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. public function validateForCsrf(RequestInterface $request): ?bool
  99. {
  100. return true;
  101. }
  102. /**
  103. * @return ResultInterface
  104. */
  105. public function execute()
  106. {
  107. $parameters = [];
  108. try {
  109. $response = $this->transaction->getResponseObject($this->getRequest()->getPostValue());
  110. $this->responseValidator->validate($response, $this->transparent);
  111. $this->transaction->savePaymentInQuote($response);
  112. } catch (LocalizedException $exception) {
  113. $parameters['error'] = true;
  114. $parameters['error_msg'] = $exception->getMessage();
  115. $this->paymentFailures->handle((int)$this->sessionTransparent->getQuoteId(), $parameters['error_msg']);
  116. }
  117. $this->coreRegistry->register(Iframe::REGISTRY_KEY, $parameters);
  118. $resultLayout = $this->resultLayoutFactory->create();
  119. $resultLayout->addDefaultHandle();
  120. $resultLayout->getLayout()->getUpdate()->load(['transparent_payment_response']);
  121. return $resultLayout;
  122. }
  123. }