diff --git a/app/code/Midtrans/Snap/Block/Close.php b/app/code/Midtrans/Snap/Block/Close.php new file mode 100644 index 0000000..f82b2fa --- /dev/null +++ b/app/code/Midtrans/Snap/Block/Close.php @@ -0,0 +1,19 @@ +resultPageFactory = $pageFactory; + parent::__construct($context); + } + + public function execute() + { + if (!$this->_objectManager->get(\Magento\Checkout\Model\Session\SuccessValidator::class)->isValid()) { + return $this->resultRedirectFactory->create()->setPath('checkout/cart'); + } + $resultPage = $this->resultPageFactory->create(); + return $resultPage; + } +} \ No newline at end of file diff --git a/app/code/Midtrans/Snap/Controller/Payment/Cancel.php b/app/code/Midtrans/Snap/Controller/Payment/Cancel.php index a8e1679..6c690eb 100755 --- a/app/code/Midtrans/Snap/Controller/Payment/Cancel.php +++ b/app/code/Midtrans/Snap/Controller/Payment/Cancel.php @@ -33,10 +33,20 @@ public function execute() $orderId = $this->getValue(); $order = $om->get('Magento\Sales\Model\Order')->loadByIncrementId($orderId); - $order->setStatus(\Magento\Sales\Model\Order::STATE_CANCELED); - $order->addStatusToHistory(\Magento\Sales\Model\Order::STATE_CANCELED); - $order->save(); - $this->unSetValue(); + if ($order->getState() == \Magento\Sales\Model\Order::STATE_NEW) { + + $order->setStatus(\Magento\Sales\Model\Order::STATE_CANCELED); + $order->addStatusToHistory(\Magento\Sales\Model\Order::STATE_CANCELED); + $order->save(); + + $order->getPayment()->cancel(); + $order->registerCancellation(); + + $this->unSetValue(); + return $this->resultRedirectFactory->create()->setPath('snap/index/close'); + } else { + return $this->resultRedirectFactory->create()->setPath('checkout/cart'); + } } public function getValue() @@ -51,5 +61,4 @@ public function unSetValue() return $this->_coreSession->unsMessage(); } - } diff --git a/app/code/Midtrans/Snap/Controller/Payment/Notification.php b/app/code/Midtrans/Snap/Controller/Payment/Notification.php index 34834ef..3bed696 100755 --- a/app/code/Midtrans/Snap/Controller/Payment/Notification.php +++ b/app/code/Midtrans/Snap/Controller/Payment/Notification.php @@ -5,9 +5,10 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Controller\ResultFactory; -use Magento\Framework\App\CsrfAwareActionInterface; +//use Magento\Framework\App\CsrfAwareActionInterface; use Magento\Framework\App\RequestInterface; use Magento\Framework\App\Request\InvalidRequestException; +use Midtrans\Snap\Plugin\CsrfValidatorSkip; $object_manager = \Magento\Framework\App\ObjectManager::getInstance(); $filesystem = $object_manager->get('Magento\Framework\Filesystem'); @@ -15,7 +16,7 @@ $lib_file = $root->getAbsolutePath('lib/internal/midtrans-php/Midtrans.php'); require_once($lib_file); -class Notification extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface +class Notification extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Framework\Registry @@ -70,7 +71,7 @@ public function execute() $logger->info($_info); ##log notif snap - $order_note = "Midtrans HTTP notification received. "; + $order_note = "Midtrans Notification - "; if ($transaction == 'capture') { $order->setInstallmentTenor($notif->installment_term); @@ -133,14 +134,4 @@ public function execute() } $order->save(); } - - public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException - { - return null; - } - - public function validateForCsrf(RequestInterface $request): ?bool - { - return true; - } } \ No newline at end of file diff --git a/app/code/Midtrans/Snap/Plugin/CsrfValidatorSkip.php b/app/code/Midtrans/Snap/Plugin/CsrfValidatorSkip.php new file mode 100644 index 0000000..1bf097b --- /dev/null +++ b/app/code/Midtrans/Snap/Plugin/CsrfValidatorSkip.php @@ -0,0 +1,24 @@ +getModuleName() == 'snap') { + return; // Skip CSRF check + } + $proceed($request, $action); // Proceed Magento 2 core functionalities + } +} \ No newline at end of file diff --git a/app/code/Midtrans/Snap/composer.json b/app/code/Midtrans/Snap/composer.json index 020fb65..549c687 100755 --- a/app/code/Midtrans/Snap/composer.json +++ b/app/code/Midtrans/Snap/composer.json @@ -2,7 +2,7 @@ "name": "midtrans/snap", "description": "Midtrans-Snap Payment Method", "type": "magento2-module", - "version": "2.1.0", + "version": "2.2.0", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/app/code/Midtrans/Snap/etc/frontend/di.xml b/app/code/Midtrans/Snap/etc/frontend/di.xml index 35fbcbd..6d9edb7 100755 --- a/app/code/Midtrans/Snap/etc/frontend/di.xml +++ b/app/code/Midtrans/Snap/etc/frontend/di.xml @@ -16,4 +16,7 @@ + + + \ No newline at end of file diff --git a/app/code/Midtrans/Snap/etc/module.xml b/app/code/Midtrans/Snap/etc/module.xml index 77470bd..ac21102 100755 --- a/app/code/Midtrans/Snap/etc/module.xml +++ b/app/code/Midtrans/Snap/etc/module.xml @@ -1,7 +1,7 @@ - + diff --git a/app/code/Midtrans/Snap/view/frontend/layout/snap_index_close.xml b/app/code/Midtrans/Snap/view/frontend/layout/snap_index_close.xml new file mode 100644 index 0000000..d625017 --- /dev/null +++ b/app/code/Midtrans/Snap/view/frontend/layout/snap_index_close.xml @@ -0,0 +1,17 @@ + + + + Payment Close + + + + + Your order has been canceled! + + + + + + + \ No newline at end of file diff --git a/app/code/Midtrans/Snap/view/frontend/layout/snap_index_pending.xml b/app/code/Midtrans/Snap/view/frontend/layout/snap_index_pending.xml index c9e7762..6ad8888 100644 --- a/app/code/Midtrans/Snap/view/frontend/layout/snap_index_pending.xml +++ b/app/code/Midtrans/Snap/view/frontend/layout/snap_index_pending.xml @@ -1,8 +1,4 @@ - - - - diff --git a/app/code/Midtrans/Snap/view/frontend/templates/close.phtml b/app/code/Midtrans/Snap/view/frontend/templates/close.phtml new file mode 100644 index 0000000..16568b1 --- /dev/null +++ b/app/code/Midtrans/Snap/view/frontend/templates/close.phtml @@ -0,0 +1,7 @@ +cancelOrders(); ?> diff --git a/app/code/Midtrans/Snap/view/frontend/web/js/view/payment/method-renderer/snap-method.js b/app/code/Midtrans/Snap/view/frontend/web/js/view/payment/method-renderer/snap-method.js index 5b47776..5a0bc76 100755 --- a/app/code/Midtrans/Snap/view/frontend/web/js/view/payment/method-renderer/snap-method.js +++ b/app/code/Midtrans/Snap/view/frontend/web/js/view/payment/method-renderer/snap-method.js @@ -176,23 +176,13 @@ define( messageList.addErrorMessage({ message: result.status_message }); - window.location.replace(url.build('checkout/onepage/failure')); + window.location.replace(url.build('snap/payment/cancel')); console.log(result.status_message); }, onClose: function () { console.log("get to onclose") trackResult(data, merchant_id, 'Fullpayment Snap PopUp', 'close'); - $.ajax({ - url: url.build('snap/payment/cancel'), - cache: false, - success: function () { - messageList.addErrorMessage({ - message: 'customer closed the popup without finishing the payment' - }); - console.log('customer closed the popup without finishing the payment'); - window.location.replace(url.build('checkout/onepage/failure')); - } - }); + window.location.replace(url.build('snap/payment/cancel')); } }); var snapExecuted = true; diff --git a/app/code/Midtrans/Snapinst/composer.json b/app/code/Midtrans/Snapinst/composer.json index 60d0503..cd98d13 100755 --- a/app/code/Midtrans/Snapinst/composer.json +++ b/app/code/Midtrans/Snapinst/composer.json @@ -2,7 +2,7 @@ "name": "midtrans/snapinst", "description": "Midtrans-Snapint Payment Method", "type": "magento2-module", - "version": "2.1.0", + "version": "2.2.0", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/app/code/Midtrans/Snapinst/etc/module.xml b/app/code/Midtrans/Snapinst/etc/module.xml index 5e9e6ff..84cc83e 100755 --- a/app/code/Midtrans/Snapinst/etc/module.xml +++ b/app/code/Midtrans/Snapinst/etc/module.xml @@ -1,7 +1,7 @@ - + diff --git a/app/code/Midtrans/Snapinst/view/frontend/web/js/view/payment/method-renderer/snapinst-method.js b/app/code/Midtrans/Snapinst/view/frontend/web/js/view/payment/method-renderer/snapinst-method.js index d41af7c..543e51b 100755 --- a/app/code/Midtrans/Snapinst/view/frontend/web/js/view/payment/method-renderer/snapinst-method.js +++ b/app/code/Midtrans/Snapinst/view/frontend/web/js/view/payment/method-renderer/snapinst-method.js @@ -176,23 +176,13 @@ define( messageList.addErrorMessage({ message: result.status_message }); - window.location.replace(url.build('checkout/onepage/failure')); + window.location.replace(url.build('snap/payment/cancel')); console.log(result.status_message); }, onClose: function () { console.log("get to onclose") trackResult(data, merchant_id, 'Installment_Dragon Snap inst PopUp', 'close'); - $.ajax({ - url: url.build('snap/payment/cancel'), - cache: false, - success: function () { - messageList.addErrorMessage({ - message: 'customer closed the popup without finishing the payment' - }); - console.log('customer closed the popup without finishing the payment'); - window.location.replace(url.build('checkout/onepage/failure')); - } - }); + window.location.replace(url.build('snap/payment/cancel')); } }); var snapExecuted = true; diff --git a/app/code/Midtrans/Snapio/composer.json b/app/code/Midtrans/Snapio/composer.json index 9fa6e31..cdef233 100755 --- a/app/code/Midtrans/Snapio/composer.json +++ b/app/code/Midtrans/Snapio/composer.json @@ -2,7 +2,7 @@ "name": "midtrans/snapio", "description": "Midtrans-Snapint Payment Method", "type": "magento2-module", - "version": "2.1.0", + "version": "2.2.0", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/app/code/Midtrans/Snapio/etc/module.xml b/app/code/Midtrans/Snapio/etc/module.xml index a6d2399..cb70510 100755 --- a/app/code/Midtrans/Snapio/etc/module.xml +++ b/app/code/Midtrans/Snapio/etc/module.xml @@ -1,7 +1,7 @@ - + diff --git a/app/code/Midtrans/Snapio/view/frontend/web/js/view/payment/method-renderer/snapio-method.js b/app/code/Midtrans/Snapio/view/frontend/web/js/view/payment/method-renderer/snapio-method.js index 4eeeb58..daafd9c 100755 --- a/app/code/Midtrans/Snapio/view/frontend/web/js/view/payment/method-renderer/snapio-method.js +++ b/app/code/Midtrans/Snapio/view/frontend/web/js/view/payment/method-renderer/snapio-method.js @@ -175,23 +175,13 @@ define( messageList.addErrorMessage({ message: result.status_message }); - window.location.replace(url.build('checkout/onepage/failure')); + window.location.replace(url.build('snap/payment/cancel')); console.log(result.status_message); }, onClose: function () { console.log("get to onclose") trackResult(data, merchant_id, 'Installment_Offline Snap PopUp', 'close'); - $.ajax({ - url: url.build('snap/payment/cancel'), - cache: false, - success: function () { - messageList.addErrorMessage({ - message: 'customer closed the popup without finishing the payment' - }); - console.log('customer closed the popup without finishing the payment'); - window.location.replace(url.build('checkout/onepage/failure')); - } - }); + window.location.replace(url.build('snap/payment/cancel')); } }); var snapExecuted = true; diff --git a/app/code/Midtrans/Snapspec/composer.json b/app/code/Midtrans/Snapspec/composer.json index 799eae6..e0e4e7b 100755 --- a/app/code/Midtrans/Snapspec/composer.json +++ b/app/code/Midtrans/Snapspec/composer.json @@ -2,7 +2,7 @@ "name": "midtrans/snapspec", "description": "Midtrans-Snapspec Payment Method", "type": "magento2-module", - "version": "2.1.0", + "version": "2.2.0", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/app/code/Midtrans/Snapspec/etc/module.xml b/app/code/Midtrans/Snapspec/etc/module.xml index 4108a0b..bd45491 100755 --- a/app/code/Midtrans/Snapspec/etc/module.xml +++ b/app/code/Midtrans/Snapspec/etc/module.xml @@ -1,7 +1,7 @@ - + diff --git a/app/code/Midtrans/Snapspec/view/frontend/web/js/view/payment/method-renderer/snapspec-method.js b/app/code/Midtrans/Snapspec/view/frontend/web/js/view/payment/method-renderer/snapspec-method.js index d623678..21ca9e3 100755 --- a/app/code/Midtrans/Snapspec/view/frontend/web/js/view/payment/method-renderer/snapspec-method.js +++ b/app/code/Midtrans/Snapspec/view/frontend/web/js/view/payment/method-renderer/snapspec-method.js @@ -177,23 +177,13 @@ define( messageList.addErrorMessage({ message: result.status_message }); - window.location.replace(url.build('checkout/onepage/failure')); + window.location.replace(url.build('snap/payment/cancel')); console.log(result.status_message); }, onClose: function () { console.log("get to onclose") trackResult(data, merchant_id, 'Fullpayment Snap Specific PopUp', 'close'); - $.ajax({ - url: url.build('snap/payment/cancel'), - cache: false, - success: function () { - messageList.addErrorMessage({ - message: 'customer closed the popup without finishing the payment' - }); - console.log('customer closed the popup without finishing the payment'); - window.location.replace(url.build('checkout/onepage/failure')); - } - }); + window.location.replace(url.build('snap/payment/cancel')); } }); var snapExecuted = true;