From 4c66e02315502c8d93ec885aca2b6ae00b223213 Mon Sep 17 00:00:00 2001 From: elisei Date: Tue, 22 Jun 2021 10:50:51 -0300 Subject: [PATCH] =?UTF-8?q?Moip=20=E2=99=A5=20Magento=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Envio de Email para pedido Cancelado --- Controller/Adminhtml/Order/MassUpdate.php | 2 +- Controller/Webhooks/Accept.php | 29 +++++--- Controller/Webhooks/Deny.php | 74 +++++++++++++++++---- Controller/Webhooks/Refund.php | 26 +++++--- Gateway/Request/DetailTotalsDataRequest.php | 13 ++-- 5 files changed, 105 insertions(+), 39 deletions(-) diff --git a/Controller/Adminhtml/Order/MassUpdate.php b/Controller/Adminhtml/Order/MassUpdate.php index 6174926..5a38b86 100755 --- a/Controller/Adminhtml/Order/MassUpdate.php +++ b/Controller/Adminhtml/Order/MassUpdate.php @@ -72,7 +72,7 @@ protected function massAction(AbstractCollection $collection) $method->fetchTransactionInfo($payment, $transactionId); $payment->getOrder()->save(); $state = $payment->getOrder()->getState(); - if ($state === 'processing') { + if($state === "processing") { $countUpdate++; } $countNotUpdate++; diff --git a/Controller/Webhooks/Accept.php b/Controller/Webhooks/Accept.php index 00bc6b2..5386239 100755 --- a/Controller/Webhooks/Accept.php +++ b/Controller/Webhooks/Accept.php @@ -15,6 +15,7 @@ use Magento\Framework\App\Request\InvalidRequestException; use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\Serialize\Serializer\Json; use Magento\Payment\Model\Method\Logger; use Magento\Sales\Api\Data\OrderInterfaceFactory; use Magento\Sales\Model\Order\CreditmemoFactory; @@ -76,12 +77,18 @@ public function validateForCsrf(RequestInterface $request): bool */ protected $storeManager; + /** + * @var Json + */ + protected $json; + /** * @param Context $context * @param logger $logger * @param Config $config * @param OrderInterfaceFactory $orderFactory * @param JsonFactory $resultJsonFactory + * @param Json $json */ public function __construct( Context $context, @@ -92,7 +99,8 @@ public function __construct( CreditmemoService $creditmemoService, Invoice $invoice, StoreManagerInterface $storeManager, - JsonFactory $resultJsonFactory + JsonFactory $resultJsonFactory, + Json $json ) { parent::__construct($context); $this->config = $config; @@ -103,6 +111,7 @@ public function __construct( $this->invoice = $invoice; $this->storeManager = $storeManager; $this->resultJsonFactory = $resultJsonFactory; + $this->json = $json; } /** @@ -121,28 +130,30 @@ public function execute() $resultPage = $this->resultJsonFactory->create(); $response = $this->getRequest()->getContent(); - $originalNotification = json_decode($response, true); + $originalNotification = $this->json->unserialize($response); $authorization = $this->getRequest()->getHeader('Authorization'); $storeId = $this->storeManager->getStore()->getId(); $storeCaptureToken = $this->config->getMerchantGatewayCaptureToken($storeId); if ($storeCaptureToken === $authorization) { - $orderMoip = $originalNotification['resource']['order']['id']; - $order = $this->orderFactory->create()->load($orderMoip, 'ext_order_id'); + $data = $originalNotification['resource']['order']; + $order = $this->orderFactory->create()->load($data['ownId'], 'ext_order_id'); $this->logger->debug([ 'webhook' => 'accept', - 'ext_order_id' => $originalNotification['resource']['order']['id'], + 'ext_order_id' => $data['ownId'], 'increment_order_id' => $order->getIncrementId(), + 'webhook_data' => $response ]); $payment = $order->getPayment(); if (!$order->getInvoiceCollection()->count()) { try { - $payment->accept(); + $isOnline = true; + $payment->accept($isOnline); $payment->save(); $order->save(); } catch (\Exception $exc) { $resultPage->setHttpResponseCode(500); $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'error' => 400, 'message' => $exc->getMessage(), ]) @@ -150,7 +161,7 @@ public function execute() } return $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'success' => 1, 'status' => $order->getStatus(), 'state' => $order->getState(), @@ -161,7 +172,7 @@ public function execute() $resultPage->setHttpResponseCode(400); return $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'error' => 400, 'message' => 'The transaction could not be refund', ]) diff --git a/Controller/Webhooks/Deny.php b/Controller/Webhooks/Deny.php index 027fef6..d443322 100755 --- a/Controller/Webhooks/Deny.php +++ b/Controller/Webhooks/Deny.php @@ -15,10 +15,12 @@ use Magento\Framework\App\Request\InvalidRequestException; use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\Serialize\Serializer\Json; use Magento\Payment\Model\Method\Logger; use Magento\Sales\Api\Data\OrderInterfaceFactory; use Magento\Sales\Model\Order\CreditmemoFactory; use Magento\Sales\Model\Order\Invoice; +use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender; use Magento\Sales\Model\Service\CreditmemoService; use Magento\Store\Model\StoreManagerInterface; use Moip\Magento2\Gateway\Config\Config; @@ -76,12 +78,23 @@ public function validateForCsrf(RequestInterface $request): bool */ protected $storeManager; + /** + * @var Json + */ + protected $json; + + /** + * @var orderCommentSender + */ + protected $orderCommentSender; + /** * @param Context $context * @param logger $logger * @param Config $config * @param OrderInterfaceFactory $orderFactory * @param JsonFactory $resultJsonFactory + * @param Json $json */ public function __construct( Context $context, @@ -92,7 +105,9 @@ public function __construct( CreditmemoService $creditmemoService, Invoice $invoice, StoreManagerInterface $storeManager, - JsonFactory $resultJsonFactory + JsonFactory $resultJsonFactory, + Json $json, + OrderCommentSender $orderCommentSender ) { parent::__construct($context); $this->config = $config; @@ -103,6 +118,8 @@ public function __construct( $this->invoice = $invoice; $this->storeManager = $storeManager; $this->resultJsonFactory = $resultJsonFactory; + $this->json = $json; + $this->orderCommentSender = $orderCommentSender; } /** @@ -112,37 +129,65 @@ public function __construct( */ public function execute() { - if (!$this->getRequest()->isPost()) { - $resultPage = $this->resultJsonFactory->create(); - $resultPage->setHttpResponseCode(404); + // if (!$this->getRequest()->isPost()) { + // $resultPage = $this->resultJsonFactory->create(); + // $resultPage->setHttpResponseCode(404); - return $resultPage; - } + // return $resultPage; + // } $resultPage = $this->resultJsonFactory->create(); $response = $this->getRequest()->getContent(); - $originalNotification = json_decode($response, true); + $originalNotification = $this->json->unserialize($response); $authorization = $this->getRequest()->getHeader('Authorization'); $storeId = $this->storeManager->getStore()->getId(); $storeCaptureToken = $this->config->getMerchantGatewayCancelToken($storeId); if ($storeCaptureToken === $authorization) { - $orderMoip = $originalNotification['resource']['order']['id']; - $order = $this->orderFactory->create()->load($orderMoip, 'ext_order_id'); + $data = $originalNotification['resource']['order']; + $order = $this->orderFactory->create()->load($data['id'], 'ext_order_id'); $this->logger->debug([ 'webhook' => 'deny', - 'ext_order_id' => $originalNotification['resource']['order']['id'], + 'ext_order_id' => $data['id'], 'increment_order_id' => $order->getIncrementId(), + 'webhook_data' => $response ]); $payment = $order->getPayment(); if (!$order->canCancel()) { try { - $payment->deny(); + $isOnline = true; + $payment->deny($isOnline); $payment->save(); + $cancelDetailsAdmin = __('We did not record the payment.'); + $cancelDetailsCus = __('The payment deadline has been exceeded.'); + if (isset($data['payments'])) { + foreach ($data['payments'] as $payment) { + if (isset($payment['cancellationDetails'])) { + $cancelCode = $payment['cancellationDetails']['code']; + $cancelDescription = $payment['cancellationDetails']['description']; + $cancelBy = $payment['cancellationDetails']['cancelledBy']; + $cancelDetailsAdmin = __('%1, code %2, by %3', $cancelDescription, $cancelCode, $cancelBy); + $cancelDetailsCus = __('%1', $cancelDescription); + } + } + } + /** customer information for cancel **/ + $history = $order->addStatusHistoryComment($cancelDetailsCus); + $history->setIsVisibleOnFront(1); + $history->setIsCustomerNotified(1); + // $order->sendOrderUpdateEmail(1, $cancelDetailsCus); + + /** admin information for cancel **/ + $history = $order->addStatusHistoryComment($cancelDetailsAdmin); + $history->setIsVisibleOnFront(0); + $history->setIsCustomerNotified(0); $order->save(); + + $this->orderCommentSender->send($order, 1, $cancelDetailsCus); + } catch (\Exception $exc) { $resultPage->setHttpResponseCode(500); $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'error' => 400, 'message' => $exc->getMessage(), ]) @@ -150,7 +195,7 @@ public function execute() } return $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'success' => 1, 'status' => $order->getStatus(), 'state' => $order->getState(), @@ -161,12 +206,13 @@ public function execute() $resultPage->setHttpResponseCode(201); return $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'error' => 400, 'message' => 'The transaction could not be refund', ]) ); } + $resultPage->setHttpResponseCode(401); return $resultPage; diff --git a/Controller/Webhooks/Refund.php b/Controller/Webhooks/Refund.php index 369441c..001f68d 100755 --- a/Controller/Webhooks/Refund.php +++ b/Controller/Webhooks/Refund.php @@ -15,6 +15,7 @@ use Magento\Framework\App\Request\InvalidRequestException; use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\Serialize\Serializer\Json; use Magento\Payment\Model\Method\Logger; use Magento\Sales\Api\Data\OrderInterfaceFactory; use Magento\Sales\Model\Order\CreditmemoFactory; @@ -75,6 +76,11 @@ public function validateForCsrf(RequestInterface $request): bool * @var storeManager */ protected $storeManager; + + /** + * @var Json + */ + protected $json; /** * @param Context $context @@ -82,6 +88,7 @@ public function validateForCsrf(RequestInterface $request): bool * @param Config $config * @param OrderInterfaceFactory $orderFactory * @param JsonFactory $resultJsonFactory + * @param Json $json */ public function __construct( Context $context, @@ -92,7 +99,8 @@ public function __construct( CreditmemoService $creditmemoService, Invoice $invoice, StoreManagerInterface $storeManager, - JsonFactory $resultJsonFactory + JsonFactory $resultJsonFactory, + Json $json ) { parent::__construct($context); $this->config = $config; @@ -103,6 +111,7 @@ public function __construct( $this->invoice = $invoice; $this->storeManager = $storeManager; $this->resultJsonFactory = $resultJsonFactory; + $this->json = $json; } /** @@ -121,17 +130,18 @@ public function execute() $resultPage = $this->resultJsonFactory->create(); $response = $this->getRequest()->getContent(); - $originalNotification = json_decode($response, true); + $originalNotification = $this->json->unserialize($response); $authorization = $this->getRequest()->getHeader('Authorization'); $storeId = $this->storeManager->getStore()->getId(); $storeCaptureToken = $this->config->getMerchantGatewayRefundToken($storeId); if ($storeCaptureToken === $authorization) { - $orderMoip = $originalNotification['resource']['order']['id']; - $order = $this->orderFactory->create()->load($orderMoip, 'ext_order_id'); + $data = $originalNotification['resource']['order']; + $order = $this->orderFactory->create()->load($data['ownId'], 'ext_order_id'); $this->logger->debug([ 'webhook' => 'refund', - 'ext_order_id' => $originalNotification['resource']['order']['id'], + 'ext_order_id' => $data['ownId'], 'increment_order_id' => $order->getIncrementId(), + 'webhook_data' => $response ]); $invoices = $order->getInvoiceCollection(); if ($invoices) { @@ -147,7 +157,7 @@ public function execute() } catch (\Exception $exc) { $resultPage->setHttpResponseCode(500); $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'error' => 400, 'message' => $exc->getMessage(), ]) @@ -155,7 +165,7 @@ public function execute() } return $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'success' => 1, 'status' => $order->getStatus(), 'state' => $order->getState(), @@ -166,7 +176,7 @@ public function execute() $resultPage->setHttpResponseCode(201); return $resultPage->setJsonData( - json_encode([ + $this->json->serialize([ 'error' => 400, 'message' => 'The transaction could not be refund', ]) diff --git a/Gateway/Request/DetailTotalsDataRequest.php b/Gateway/Request/DetailTotalsDataRequest.php index d2b84ce..84516dc 100755 --- a/Gateway/Request/DetailTotalsDataRequest.php +++ b/Gateway/Request/DetailTotalsDataRequest.php @@ -151,17 +151,16 @@ public function build(array $buildSubject) } } $total = $total - $orderAdapter->getShippingAmount(); + $discount = -1 * $orderAdapter->getDiscountAmount(); $result[self::TOTALS_AMOUNT] = [ self::TOTALS_AMOUNT_CURRENCY => $order->getCurrencyCode(), - self::TOTALS_AMOUNT_GRAND_TOTAL => $this->config->formatPrice($total), + self::TOTALS_AMOUNT_GRAND_TOTAL => ceil($this->config->formatPrice($total)), self::TOTALS_AMOUNT_SUBTOTALS => [ - self::TOTALS_AMOUNT_SUBTOTALS_SHIPPING => $this->config->formatPrice( + self::TOTALS_AMOUNT_SUBTOTALS_SHIPPING => ceil($this->config->formatPrice( $orderAdapter->getShippingAmount() - ), - self::TOTALS_AMOUNT_SUBTOTALS_DISCOUNT => -1 * $this->config->formatPrice( - $orderAdapter->getDiscountAmount() - ), - self::TOTALS_AMOUNT_SUBTOTALS_ADDITION => $this->config->formatPrice($addition), + )), + self::TOTALS_AMOUNT_SUBTOTALS_DISCOUNT => ceil($this->config->formatPrice($discount)), + self::TOTALS_AMOUNT_SUBTOTALS_ADDITION => ceil($this->config->formatPrice($addition)), ], ];