Skip to content

Commit

Permalink
Merge branch 'mag1-171' into 2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ebanolopes committed Dec 12, 2017
2 parents f8c50b5 + f526351 commit 7c55b46
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Helper/PurchaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,41 @@ public function postCaseToSignifyd($caseData, $order)
return $id;
}

/**
* @param Order $order
* @return bool
*/
public function cancelCaseOnSignifyd(Order $order)
{
$case = $this->getCase($order);
if ($case->isEmpty()) {
$this->_logger->debug("Guarantee cancel skipped: case not found for order " . $order->getIncrementId());
return false;
}

$this->_logger->debug('Cancelling case ' . $case->getId());

$guarantee = $case->getData('guarantee');
if (empty($guarantee) || in_array($guarantee, array('DECLINED', 'N/A'))) {
$this->_logger->debug("Guarantee cancel skipped: current guarantee is {$guarantee}");
return false;
}

$disposition = $this->_api->cancelGuarantee($case->getCode());
$this->_logger->debug("Cancel disposition result {$disposition}");

if ($disposition == 'CANCELED') {
$case->setData('guarantee', $disposition);
$case->save();

$order->setSignifydGuarantee($disposition);
$order->save();
return true;
}

return false;
}

/**
* Check if case has guaranty
* @param $order
Expand Down
70 changes: 70 additions & 0 deletions Observer/Cancel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright 2017 SIGNIFYD Inc. All rights reserved.
* See LICENSE.txt for license details.
*/

namespace Signifyd\Connect\Observer;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;
use Signifyd\Connect\Helper\PurchaseHelper;
use Signifyd\Connect\Helper\LogHelper;
use Signifyd\Connect\Helper\SignifydAPIMagento;

/**
* Observer for purchase event. Sends order data to Signifyd service
*/
class Cancel implements ObserverInterface
{
/**
* @var \Signifyd\Connect\Helper\LogHelper
*/
protected $logger;

/**
* @var \Signifyd\Connect\Helper\PurchaseHelper
*/
protected $helper;

/**
* @var SignifydAPIMagento
*/
protected $api;

/**
* @var ScopeConfigInterface
*/
protected $coreConfig;

public function __construct(
LogHelper $logger,
PurchaseHelper $helper,
SignifydAPIMagento $api,
ScopeConfigInterface $coreConfig
) {
$this->logger = $logger;
$this->helper = $helper;
$this->api = $api;
$this->coreConfig = $coreConfig;
}

public function execute(Observer $observer)
{
if(!$this->api->enabled()) return;

try {
/** @var $order Order */
$order = $observer->getEvent()->getOrder();

// Check if case already exists for this order
if ($this->helper->doesCaseExist($order)) {
$result = $this->helper->cancelCaseOnSignifyd($order);
}
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
}
}
}
4 changes: 4 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<event name="sales_order_save_commit_after">
<observer name="onPurchase" instance="Signifyd\Connect\Observer\Purchase" />
</event>

<event name="order_cancel_after">
<observer name="onCancel" instance="Signifyd\Connect\Observer\Cancel" />
</event>
</config>

0 comments on commit 7c55b46

Please sign in to comment.