|
| 1 | +<?php |
| 2 | +namespace Fave\PaymentGateway\Controller\Callback; |
| 3 | + |
| 4 | +use Magento\Framework\App\CsrfAwareActionInterface; |
| 5 | +use Magento\Framework\App\RequestInterface; |
| 6 | +use Magento\Framework\App\Request\InvalidRequestException; |
| 7 | +use Magento\Sales\Model\Service\InvoiceService; |
| 8 | +use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; |
| 9 | +use Magento\Framework\Controller\ResultInterface; |
| 10 | +use Magento\Framework\Controller\Result\JsonFactory; |
| 11 | +use Magento\Payment\Gateway\ConfigInterface; |
| 12 | +use Magento\Sales\Model\Order\Invoice; |
| 13 | +use Magento\Sales\Api\InvoiceRepositoryInterface; |
| 14 | +use Magento\Framework\DB\Transaction; |
| 15 | +use Magento\Checkout\Model\Session; |
| 16 | + |
| 17 | +class Index extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface |
| 18 | +{ |
| 19 | + protected $_pageFactory; |
| 20 | + protected $_curl; |
| 21 | + protected $_storeManager; |
| 22 | + protected $orderRepository; |
| 23 | + protected $transactionBuilder; |
| 24 | + protected $invoiceService; |
| 25 | + protected $invoiceSender; |
| 26 | + private $resultJsonFactory; |
| 27 | + private $config; |
| 28 | + private $order; |
| 29 | + private $checkoutSession; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + \Magento\Framework\App\Action\Context $context, |
| 33 | + \Magento\Framework\View\Result\PageFactory $pageFactory, |
| 34 | + \Magento\Framework\HTTP\Client\Curl $curl, |
| 35 | + \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, |
| 36 | + \Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder, |
| 37 | + InvoiceService $invoiceService, |
| 38 | + InvoiceRepositoryInterface $invoiceRepository, |
| 39 | + Transaction $transaction, |
| 40 | + InvoiceSender $invoiceSender, |
| 41 | + JsonFactory $resultJsonFactory, |
| 42 | + ConfigInterface $config, |
| 43 | + \Magento\Sales\Api\Data\OrderInterface $order, |
| 44 | + Session $checkoutSession, |
| 45 | + \Magento\Store\Model\StoreManagerInterface $storeManager) |
| 46 | + { |
| 47 | + $this->_pageFactory = $pageFactory; |
| 48 | + $this->_curl = $curl; |
| 49 | + $this->_storeManager = $storeManager; |
| 50 | + $this->orderRepository = $orderRepository; |
| 51 | + $this->transactionBuilder = $transactionBuilder; |
| 52 | + $this->invoiceService = $invoiceService; |
| 53 | + $this->invoiceRepository = $invoiceRepository; |
| 54 | + $this->transaction = $transaction; |
| 55 | + $this->invoiceSender = $invoiceSender; |
| 56 | + $this->resultJsonFactory = $resultJsonFactory; |
| 57 | + $this->config = $config; |
| 58 | + $this->order = $order; |
| 59 | + $this->checkoutSession = $checkoutSession; |
| 60 | + return parent::__construct($context); |
| 61 | + } |
| 62 | + |
| 63 | + public function execute() |
| 64 | + { |
| 65 | + $post = $this->getRequest()->getContent(); |
| 66 | + $array = json_decode($post, true); |
| 67 | + $omni_reference = $array['omni_reference']; |
| 68 | + $statusCode = $array['status_code']; |
| 69 | + |
| 70 | + $route_params = $this->getRequest()->getParams(); |
| 71 | + $orderId = $route_params['order_id']; |
| 72 | + |
| 73 | + $order = $this->order->loadByIncrementId($orderId); |
| 74 | + |
| 75 | + $payment = $order->getPayment(); |
| 76 | + $trxId = $payment->getLastTransId(); |
| 77 | + $additional_info = $payment->getAdditionalInformation(); |
| 78 | + |
| 79 | + if (array_key_exists('status_code', $additional_info)) { |
| 80 | + $comments = 'This transaction has been acknowledged.'; |
| 81 | + } |
| 82 | + else { |
| 83 | + $comments = $this-> addTransactionToOrder($orderId, $order, $statusCode); |
| 84 | + } |
| 85 | + |
| 86 | + $resultJson = $this->resultJsonFactory->create(); |
| 87 | + return $resultJson->setData([ |
| 88 | + 'order_id' => $orderId, |
| 89 | + 'status_code' => $statusCode, |
| 90 | + 'message' => $comments |
| 91 | + ]); |
| 92 | + |
| 93 | + exit; |
| 94 | + } |
| 95 | + |
| 96 | + public function addTransactionToOrder($orderId, $order, $statusCode) { |
| 97 | + try { |
| 98 | + |
| 99 | + |
| 100 | + // Prepare payment object |
| 101 | + $payment = $order->getPayment(); |
| 102 | + $trxId = $payment->getLastTransId(); |
| 103 | + $uuid = $trxId . '-' . $orderId; |
| 104 | + |
| 105 | + $payment->setParentTransactionId($trxId); |
| 106 | + $payment->setLastTransId($uuid); |
| 107 | + $payment->setTransactionId($uuid); |
| 108 | + $payment->setAdditionalInformation('status_code', $statusCode); |
| 109 | + $payment->canRefund(); |
| 110 | + |
| 111 | + |
| 112 | + $trxId = $payment->getLastTransId(); |
| 113 | + $orderCurrencyCode = $this->_storeManager->getStore()->getCurrentCurrency()->getCode(); |
| 114 | + |
| 115 | + $formatedPrice = $orderCurrencyCode . ' ' . number_format((float)$order->getGrandTotal(), 2, '.', ''); |
| 116 | + |
| 117 | + // Prepare transaction |
| 118 | + $transaction = $this->transactionBuilder->setPayment($payment) |
| 119 | + ->setOrder($order) |
| 120 | + ->setTransactionId($uuid) |
| 121 | + ->setFailSafe(true) |
| 122 | + ->build(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_CAPTURE); |
| 123 | + |
| 124 | + if ($statusCode == "4") { |
| 125 | + $this->cancelOrder($order); |
| 126 | + $comments = __('Payment of %1 was declined.', $formatedPrice); |
| 127 | + } |
| 128 | + elseif ($statusCode == "2") { |
| 129 | + $comments = __('Payment of %1 has been completed successfully.', $formatedPrice); |
| 130 | + $this-> createInvoice($order, $formatedPrice); |
| 131 | + } |
| 132 | + else { |
| 133 | + $comments = __('There was a problem processing the payment of %1. Please contact Fave for more information', $formatedPrice); |
| 134 | + } |
| 135 | + |
| 136 | + $payment->addTransactionCommentsToOrder($transaction, $comments); |
| 137 | + $payment->save(); |
| 138 | + |
| 139 | + return $comments; |
| 140 | + |
| 141 | + } catch (Exception $e) { |
| 142 | + $this->messageManager->addExceptionMessage($e, $e->getMessage()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private function cancelOrder($order) { |
| 147 | + if ($order->canCancel()) { |
| 148 | + try { |
| 149 | + $order->cancel(); |
| 150 | + |
| 151 | + // remove status history set in _setState |
| 152 | + $order->getStatusHistoryCollection(true); |
| 153 | + $order->save(); |
| 154 | + } catch (Exception $e) { |
| 155 | + $this->messageManager->addExceptionMessage($e, $e->getMessage()); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public function createInvoice($order, $formatedPrice) { |
| 161 | + // Prepare the invoice |
| 162 | + $invoice = $this->invoiceService->prepareInvoice($order); |
| 163 | + $invoice->setRequestedCaptureCase(Invoice::CAPTURE_ONLINE); |
| 164 | + $invoice->setState(Invoice::STATE_PAID); |
| 165 | + $invoice->setTransactionId($order->getPayment()->getLastTransId()); |
| 166 | + $invoice->setBaseGrandTotal((float)$order->getGrandTotal()); |
| 167 | + $invoice->register(); |
| 168 | + $invoice->getOrder()->setIsInProcess(true); |
| 169 | + $invoice->pay(); |
| 170 | + |
| 171 | + |
| 172 | + // Create the transaction |
| 173 | + $transactionSave = $this->transaction |
| 174 | + ->addObject($invoice) |
| 175 | + ->addObject($order); |
| 176 | + $transactionSave->save(); |
| 177 | + |
| 178 | + // Update the order |
| 179 | + $order->setTotalPaid($order->getTotalPaid()); |
| 180 | + $order->setBaseTotalPaid($order->getBaseTotalPaid()); |
| 181 | + $order->save(); |
| 182 | + |
| 183 | + // Save the invoice |
| 184 | + $this->invoiceRepository->save($invoice); |
| 185 | + } |
| 186 | + |
| 187 | + public function createCsrfValidationException(RequestInterface $request): ? InvalidRequestException |
| 188 | + { |
| 189 | + return null; |
| 190 | + } |
| 191 | + |
| 192 | + public function validateForCsrf(RequestInterface $request): ?bool |
| 193 | + { |
| 194 | + return true; |
| 195 | + } |
| 196 | +} |
| 197 | + |
0 commit comments