|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ipag\Payment\Controller\Redirect; |
| 4 | + |
| 5 | +use Magento\Sales\Model\OrderFactory; |
| 6 | +use Magento\Framework\App\Action\Context; |
| 7 | +use Magento\Framework\View\Result\PageFactory; |
| 8 | +use Magento\Sales\Api\OrderManagementInterface; |
| 9 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\Framework\Encryption\EncryptorInterface; |
| 12 | +use Magento\Framework\App\RequestInterface; |
| 13 | +use Magento\Framework\App\Request\InvalidRequestException; |
| 14 | +use Magento\Framework\App\CsrfAwareActionInterface; |
| 15 | + |
| 16 | +use Ipag\Classes\Services\CallbackService; |
| 17 | + |
| 18 | +class Result extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface |
| 19 | +{ |
| 20 | + protected $_logger; |
| 21 | + |
| 22 | + protected $_ipagHelper; |
| 23 | + |
| 24 | + protected $_ipagBoletoModel; |
| 25 | + |
| 26 | + protected $_ipagInvoiceInstallments; |
| 27 | + |
| 28 | + protected $_invoiceService; |
| 29 | + |
| 30 | + protected $orderFactory; |
| 31 | + |
| 32 | + protected $orderManagement; |
| 33 | + |
| 34 | + protected $orderRepository; |
| 35 | + |
| 36 | + protected $scopeConfig; |
| 37 | + |
| 38 | + protected $ipagOrderStatus; |
| 39 | + |
| 40 | + protected $invoiceSender; |
| 41 | + |
| 42 | + protected $productMetadata; |
| 43 | + |
| 44 | + protected $transactionFactory; |
| 45 | + |
| 46 | + protected $ipagLogger; |
| 47 | + |
| 48 | + protected $encryptor; |
| 49 | + |
| 50 | + protected $resultPageFactory; |
| 51 | + |
| 52 | + public function __construct( |
| 53 | + \Magento\Framework\App\Action\Context $context, |
| 54 | + \Psr\Log\LoggerInterface $logger, |
| 55 | + OrderFactory $orderFactory, |
| 56 | + OrderManagementInterface $orderManagement, |
| 57 | + OrderRepositoryInterface $orderRepository, |
| 58 | + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, |
| 59 | + \Ipag\Payment\Model\Source\OrderStatus $ipagOrderStatus, |
| 60 | + \Magento\Sales\Model\Service\InvoiceService $invoiceService, |
| 61 | + \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender, |
| 62 | + \Magento\Framework\App\ProductMetadataInterface $productMetadata, |
| 63 | + \Ipag\Payment\Helper\Data $ipagHelper, |
| 64 | + \Ipag\Payment\Logger\Logger $ipagLogger, |
| 65 | + \Ipag\Payment\Model\Method\Boleto $ipagBoletoModel, |
| 66 | + \Ipag\Payment\Model\IpagInvoiceInstallments $ipagInvoiceInstallments, |
| 67 | + \Magento\Framework\DB\TransactionFactory $transactionFactory, |
| 68 | + PageFactory $resultPageFactory, |
| 69 | + EncryptorInterface $encryptor |
| 70 | + ) |
| 71 | + { |
| 72 | + $this->_invoiceService = $invoiceService; |
| 73 | + $this->_logger = $logger; |
| 74 | + $this->orderFactory = $orderFactory; |
| 75 | + $this->orderManagement = $orderManagement; |
| 76 | + $this->orderRepository = $orderRepository; |
| 77 | + $this->scopeConfig = $scopeConfig; |
| 78 | + $this->ipagOrderStatus = $ipagOrderStatus; |
| 79 | + $this->invoiceSender = $invoiceSender; |
| 80 | + $this->_ipagHelper = $ipagHelper; |
| 81 | + $this->_ipagBoletoModel = $ipagBoletoModel; |
| 82 | + $this->_ipagInvoiceInstallments = $ipagInvoiceInstallments; |
| 83 | + $this->productMetadata = $productMetadata; |
| 84 | + $this->transactionFactory = $transactionFactory; |
| 85 | + $this->ipagLogger = $ipagLogger; |
| 86 | + $this->encryptor = $encryptor; |
| 87 | + $this->resultPageFactory = $resultPageFactory; |
| 88 | + |
| 89 | + parent::__construct($context); |
| 90 | + |
| 91 | + //compatibilidade com Magento 2.3 |
| 92 | + //o certo seria implementar a interface CsrfAwareActionInterface, mas isso quebra a retrocompatibilidade com Magento < 2.2 e PHP < 7.1 |
| 93 | + // compatibility block removed: do not execute controller from constructor |
| 94 | + } |
| 95 | + |
| 96 | + public function execute() |
| 97 | + { |
| 98 | + $token = $this->getRequest()->getParam('p'); |
| 99 | + |
| 100 | + $this->_logger->debug('redirect result controller called', ['token' => $token]); |
| 101 | + |
| 102 | + try { |
| 103 | + |
| 104 | + if (empty($token)) { |
| 105 | + $this->_logger->notice('Missing redirect token, redirecting to home'); |
| 106 | + return $this->redirectToHome(); |
| 107 | + } |
| 108 | + |
| 109 | + $decoded = base64_decode($token, true); |
| 110 | + |
| 111 | + if ($decoded === false) { |
| 112 | + $this->_logger->notice('Invalid base64 token, redirecting to home', ['token' => $token]); |
| 113 | + return $this->redirectToHome(); |
| 114 | + } |
| 115 | + |
| 116 | + $payload = $this->encryptor->decrypt($decoded); |
| 117 | + |
| 118 | + $data = json_decode($payload, true); |
| 119 | + |
| 120 | + if (empty($data) || empty($data['order'])) { |
| 121 | + $this->thrownException('redirect result controller: Invalid token payload', ['token' => $token, 'payload' => $payload]); |
| 122 | + } |
| 123 | + |
| 124 | + $orderId = $data['order']; |
| 125 | + $this->_logger->debug('Decoded redirect token', ['order' => $orderId]); |
| 126 | + |
| 127 | + $order = $this->orderFactory->create()->loadByIncrementId($orderId); |
| 128 | + |
| 129 | + if (!$order || !$order->getId()) { |
| 130 | + $this->thrownLocalizedException('Order not found for decoded token', ['order' => $orderId]); |
| 131 | + } |
| 132 | + |
| 133 | + $transactionId = $this->getRequest()->getParam('transaction_id'); |
| 134 | + |
| 135 | + if (!$transactionId) { |
| 136 | + $this->thrownLocalizedException('Missing transaction_id parameter in redirect request', ['order' => $orderId]); |
| 137 | + } |
| 138 | + |
| 139 | + $ipag = $this->_ipagHelper->AuthorizationValidate(); |
| 140 | + |
| 141 | + $response = $ipag->transaction()->setNumPedido($orderId)->consult(); |
| 142 | + |
| 143 | + if (isset($response->error)) { |
| 144 | + $this->thrownLocalizedException('iPag returned an error for order consult', ['order' => $orderId, 'error' => $response->error, 'message' => $response->errorMessage]); |
| 145 | + } |
| 146 | + |
| 147 | + if (!isset($response->order->orderId) || $response->order->orderId != $orderId) { |
| 148 | + $this->thrownLocalizedException('iPag returned invalid order data for order consult', ['order' => $orderId, 'response_order_id' => $response->order->orderId ?? null]); |
| 149 | + } |
| 150 | + |
| 151 | + if (isset($response->payment->status) && in_array($response->payment->status, ['5', '8'])) { |
| 152 | + return $this->redirectToResultSuccess(); |
| 153 | + } |
| 154 | + |
| 155 | + return $this->redirectToResultError(); |
| 156 | + |
| 157 | + } catch (LocalizedException $e) { |
| 158 | + $this->messageManager->addError($e->getMessage()); |
| 159 | + $this->_logger->error($e->getMessage()); |
| 160 | + return $this->redirectToResultError(); |
| 161 | + } catch (\Exception $e) { |
| 162 | + $this->messageManager->addError($e->getMessage()); |
| 163 | + $this->_logger->critical($e->getMessage()); |
| 164 | + return $this->redirectToHome(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private function thrownException($message, ...$rest) |
| 169 | + { |
| 170 | + $this->_logger->error($message, ...$rest); |
| 171 | + throw new \Exception($message); |
| 172 | + } |
| 173 | + |
| 174 | + private function thrownLocalizedException($message, ...$rest) |
| 175 | + { |
| 176 | + $this->_logger->error($message, ...$rest); |
| 177 | + throw new LocalizedException(__($message)); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Allow external POSTs by disabling default CSRF validation for this action. |
| 182 | + * |
| 183 | + * @param RequestInterface $request |
| 184 | + * @return InvalidRequestException|null |
| 185 | + */ |
| 186 | + public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException |
| 187 | + { |
| 188 | + return null; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Allow the request (disable CSRF check). |
| 193 | + * |
| 194 | + * @param RequestInterface $request |
| 195 | + * @return bool|null |
| 196 | + */ |
| 197 | + public function validateForCsrf(RequestInterface $request): ?bool |
| 198 | + { |
| 199 | + return true; |
| 200 | + } |
| 201 | + |
| 202 | + private function registerExceptionLog(...$rest) |
| 203 | + { |
| 204 | + $this->_logger->error(...$rest); |
| 205 | + } |
| 206 | + |
| 207 | + private function redirectToResultSuccess() |
| 208 | + { |
| 209 | + $resultPage = $this->resultPageFactory->create(); |
| 210 | + $resultPage->addHandle('ipag_redirect_success'); |
| 211 | + $this->_logger->debug('Returning ipag redirect success page', ['handle' => 'ipag_redirect_success']); |
| 212 | + return $resultPage; |
| 213 | + } |
| 214 | + |
| 215 | + private function redirectToResultError() |
| 216 | + { |
| 217 | + $resultPage = $this->resultPageFactory->create(); |
| 218 | + $resultPage->addHandle('ipag_redirect_error'); |
| 219 | + $this->_logger->debug('Returning ipag redirect error page', ['handle' => 'ipag_redirect_error']); |
| 220 | + return $resultPage; |
| 221 | + } |
| 222 | + |
| 223 | + private function redirectToHome() |
| 224 | + { |
| 225 | + $resultRedirect = $this->resultRedirectFactory->create(); |
| 226 | + $resultRedirect->setPath(''); |
| 227 | + return $resultRedirect; |
| 228 | + } |
| 229 | +} |
0 commit comments