Skip to content

Commit

Permalink
Plugin now un-cancels an order when the order is canceled and a trans…
Browse files Browse the repository at this point in the history
…action is paid.

This can happen when a merchant uses second chance
  • Loading branch information
Andy Pieters committed Feb 7, 2017
1 parent 3c93da9 commit aed3fdd
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
59 changes: 58 additions & 1 deletion Controller/Checkout/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Paynl\Payment\Controller\Checkout;

use Magento\Framework\Exception\LocalizedException;

/**
* Description of Index
*
Expand Down Expand Up @@ -69,6 +71,52 @@ public function __construct(
parent::__construct($context);
}

private function uncancel(\Magento\Sales\Model\Order $order){
if ($order->isCanceled()) {
$state = \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT;
$productStockQty = [];
foreach ($order->getAllVisibleItems() as $item) {
$productStockQty[$item->getProductId()] = $item->getQtyCanceled();
foreach ($item->getChildrenItems() as $child) {
$productStockQty[$child->getProductId()] = $item->getQtyCanceled();
$child->setQtyCanceled(0);
$child->setTaxCanceled(0);
$child->setDiscountTaxCompensationCanceled(0);
}
$item->setQtyCanceled(0);
$item->setTaxCanceled(0);
$item->setDiscountTaxCompensationCanceled(0);
$this->_eventManager->dispatch('sales_order_item_uncancel', ['item' => $item]);
}
$this->_eventManager->dispatch(
'sales_order_uncancel_inventory',
[
'order' => $order,
'product_qty' => $productStockQty
]
);
$order->setSubtotalCanceled(0);
$order->setBaseSubtotalCanceled(0);
$order->setTaxCanceled(0);
$order->setBaseTaxCanceled(0);
$order->setShippingCanceled(0);
$order->setBaseShippingCanceled(0);
$order->setDiscountCanceled(0);
$order->setBaseDiscountCanceled(0);
$order->setTotalCanceled(0);
$order->setBaseTotalCanceled(0);
$order->setState($state);
$order->setStatus($state);

$order->addStatusHistoryComment(__('Pay.nl Uncanceled order'), false);

$this->_eventManager->dispatch('order_uncancel_after', ['order' => $order]);
} else {
throw new LocalizedException(__('We cannot un-cancel this order.'));
}
return $order;
}

public function execute()
{
$skipFraudDetection = false;
Expand Down Expand Up @@ -104,6 +152,15 @@ public function execute()
}

if ($transaction->isPaid()) {
$message = "PAID";
if($order->isCanceled()){
try{
$this->uncancel($order);
} catch(LocalizedException $e){
return $this->_result->setContents('FALSE| Cannot un-cancel order: '.$e->getMessage());
}
$message .= " order was uncanceled";
}
$payment = $order->getPayment();
$payment->setTransactionId(
$transaction->getId()
Expand Down Expand Up @@ -140,7 +197,7 @@ public function execute()
)->save();

}
return $this->_result->setContents("TRUE| PAID");
return $this->_result->setContents("TRUE| ".$message);

} elseif($transaction->isCanceled()){
$order->cancel()->save();
Expand Down
80 changes: 80 additions & 0 deletions Observer/SubtractInventoryObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace Paynl\Payment\Observer;

use Magento\CatalogInventory\Api\StockManagementInterface;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as StockProcessor;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;

class SubtractInventoryObserver implements ObserverInterface
{
/**
* @var StockManagementInterface
*/
protected $stockManagement;

/**
* @var \Magento\CatalogInventory\Observer\ItemsForReindex
*/
protected $itemsForReindex;

/**
* @var LoggerInterface
*/
protected $logger;

/**
* SubtractInventoryObserver constructor.
* @param StockManagementInterface $stockManagement
* @param StockProcessor $stockIndexerProcessor
* @param LoggerInterface $logger
*/
public function __construct(
StockManagementInterface $stockManagement,
StockProcessor $stockIndexerProcessor,
LoggerInterface $logger
)
{
$this->stockManagement = $stockManagement;
$this->stockIndexerProcessor = $stockIndexerProcessor;
$this->logger = $logger;
}

/**
* Subtract items qtys from stock related with uncancel products.
*
* @param EventObserver $observer
* @return $this
*/
public function execute(EventObserver $observer)
{
$order = $observer->getEvent()->getOrder();
$productQty = $observer->getEvent()->getProductQty();

if ($order->getInventoryProcessed()) {
return $this;
}

/**
* Reindex items
*/
$itemsForReindex = $this->stockManagement->registerProductsSale(
$productQty,
$order->getStore()->getWebsiteId()
);


$productIds = [];
foreach ($itemsForReindex as $item) {
$item->save();
$productIds[] = $item->getProductId();
}
if (!empty($productIds)) {
$this->stockIndexerProcessor->reindexList($productIds);
}

$order->setInventoryProcessed(true);
return $this;
}
}
6 changes: 6 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_uncancel_inventory">
<observer name="inventory" instance="Paynl\Payment\Observer\SubtractInventoryObserver"/>
</event>
</config>

0 comments on commit aed3fdd

Please sign in to comment.