diff --git a/Helper/PurchaseHelper.php b/Helper/PurchaseHelper.php index 3026ba6a..28be6cc2 100644 --- a/Helper/PurchaseHelper.php +++ b/Helper/PurchaseHelper.php @@ -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 diff --git a/Observer/Cancel.php b/Observer/Cancel.php new file mode 100644 index 00000000..ff1d375f --- /dev/null +++ b/Observer/Cancel.php @@ -0,0 +1,70 @@ +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()); + } + } +} diff --git a/etc/events.xml b/etc/events.xml index be57e4b3..95cb76e0 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -6,4 +6,8 @@ + + + +