Skip to content

Commit

Permalink
Improvements to retry and some fixes.
Browse files Browse the repository at this point in the history
New retry flow with improved functionality. Fixing some small issues,
small improvements.
  • Loading branch information
cristian-vatca-osf committed Oct 12, 2016
1 parent 53666b9 commit f104fe6
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 209 deletions.
7 changes: 4 additions & 3 deletions Controller/Adminhtml/Utils/Send.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2015 SIGNIFYD Inc. All rights reserved.
* Copyright 2015 SIGNIFYD Inc. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace Signifyd\Connect\Controller\Adminhtml\Utils;
Expand Down Expand Up @@ -95,16 +95,17 @@ public function massAction(AbstractCollection $collection)
$this->_helper->createNewCase($order);

// Post case to signifyd service
$this->_helper->postCaseToSignifyd($orderData);
$this->_helper->postCaseToSignifyd($orderData, $order);
} catch (\Exception $ex) {
$this->_logger->error($ex->getMessage());
}
}

$this->messageManager->addSuccess(__('Success.'));
$this->messageManager->addSuccessMessage(__('Success.'));

$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath($this->getComponentRefererUrl());
return $resultRedirect;
}

}
137 changes: 9 additions & 128 deletions Controller/Webhooks/Index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2015 SIGNIFYD Inc. All rights reserved.
* Copyright 2015 SIGNIFYD Inc. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace Signifyd\Connect\Controller\Webhooks;
Expand All @@ -13,6 +13,8 @@
use Magento\Sales\Model\Order;
use Signifyd\Connect\Helper\SignifydAPIMagento;
use Signifyd\Connect\Helper\LogHelper;
use Signifyd\Connect\Model\Casedata;
use Signifyd\Connect\Model\CaseRetry;

/**
* Controller action for handling webhook posts from Signifyd service
Expand Down Expand Up @@ -118,134 +120,10 @@ protected function initRequest($request)
);
}

/**
* @param $caseData
*/
protected function updateCase($caseData)
{
/** @var $case \Signifyd\Connect\Model\Casedata */
$case = $caseData['case'];
$request = $caseData['request'];
$order = $caseData['order'];

$orderAction = array("action" => null, "reason" => '');
if (isset($request->score) && $case->getScore() != $request->score) {
$case->setScore($request->score);
$order->setSignifydScore($request->score);
$orderAction = $this->handleScoreChange($caseData) ?: $orderAction;
}

if (isset($request->status) && $case->getSignifydStatus() != $request->status) {
$case->setSignifydStatus($request->status);
$orderAction = $this->handleStatusChange($caseData) ?: $orderAction;
}

if (isset($request->guaranteeDisposition) && $case->getGuarantee() != $request->guaranteeDisposition) {
$case->setGuarantee($request->guaranteeDisposition);
$order->setSignifydGuarantee($request->guaranteeDisposition);
$orderAction = $this->handleGuaranteeChange($caseData) ?: $orderAction;
}
$case->setCode($request->caseId);
$case->setUpdated(strftime('%Y-%m-%d %H:%M:%S', time()));
$case->save();

$order->setSignifydCode($request->caseId);
$order->save();
$this->updateOrder($caseData, $orderAction);
}

/**
* @param array $caseData
* @param string $orderAction
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function updateOrder($caseData, $orderAction)
{
/** @var $order \Magento\Sales\Model\Order */
$order = $caseData['order'];
switch ($orderAction["action"]) {
case "hold":
if ($order->canHold()) {
$order->hold()->save();
}
break;
case "unhold":
if ($order->canUnhold()) {
$order->unhold()->save();
}
break;
case "cancel":
// Can't cancel if order is on hold
if ($order->canUnhold()) {
$order = $order->unhold();
}
if ($order->canCancel()) {
$order->cancel()->save();
}
break;
}
$order->addStatusHistoryComment("Signifyd set status to {$orderAction["action"]} because {$orderAction["reason"]}");
$order->save();
}

/**
* @param $caseData
* @throws \Magento\Framework\Exception\LocalizedException
* @return string
*/
protected function handleScoreChange($caseData)
{
$threshHold = (int)$this->_coreConfig->getValue('signifyd/advanced/hold_orders_threshold');
$holdBelowThreshold = $this->_coreConfig->getValue('signifyd/advanced/hold_orders');
if ($holdBelowThreshold && $caseData['request']->score <= $threshHold) {
return array("action" => "hold", "reason" => "score threshold failure");
}
return null;
}

/**
* @param $caseData
* @throws \Magento\Framework\Exception\LocalizedException
* @return string
*/
protected function handleStatusChange($caseData)
{
$holdBelowThreshold = $this->_coreConfig->getValue('signifyd/advanced/hold_orders');
if ($holdBelowThreshold && $caseData['request']->reviewDisposition == 'FRAUDULENT') {
return array("action" => "hold", "reason" => "review returned FRAUDULENT");
} else {
if ($holdBelowThreshold && $caseData['request']->reviewDisposition == 'GOOD') {
return array("action" => "unhold", "reason" => "review returned GOOD");
}
}
return null;
}

/**
* @param $caseData
* @throws \Magento\Framework\Exception\LocalizedException
* @return string
*/
protected function handleGuaranteeChange($caseData)
{
$negativeAction = $this->_coreConfig->getValue('signifyd/advanced/guarantee_negative_action');
$positiveAction = $this->_coreConfig->getValue('signifyd/advanced/guarantee_positive_action');

$request = $caseData['request'];
if ($request->guaranteeDisposition == 'DECLINED' && $negativeAction != 'nothing') {
return array("action" => $negativeAction, "reason" => "guarantee declined");
} else {
if ($request->guaranteeDisposition == 'APPROVED' && $positiveAction != 'nothing') {
return array("action" => $positiveAction, "reason" => "guarantee approved");
}
}
return null;
}

public function execute()
{
if (!$this->_api->enabled()) {
$this->_api->traceOut("This plugin is not currently enabled");
$this->getResponse()->appendBody("This plugin is not currently enabled");
$this->Result400();
return;
}
Expand All @@ -256,7 +134,7 @@ public function execute()
$hash = $request->getHeader('X-SIGNIFYD-SEC-HMAC-SHA256');
$topic = $request->getHeader('X-SIGNIFYD-TOPIC');
if ($hash == null) {
$this->_api->traceOut("You have successfully reached the webhook endpoint");
$this->getResponse()->appendBody("You have successfully reached the webhook endpoint");
$this->Result200();
return;
}
Expand All @@ -270,10 +148,13 @@ public function execute()

$request = json_decode($rawRequest);
$caseData = $this->initRequest($request);
$this->updateCase($caseData);
$caseObj = $this->_objectManager->create('Signifyd\Connect\Model\Casedata');
$caseObj->updateCase($caseData);
$this->Result200();
return;
} else {
$this->Result403();
return;
}
}

Expand Down
94 changes: 59 additions & 35 deletions Cron/RetryCaseJob.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Copyright 2016 SIGNIFYD Inc. All rights reserved.
* Copyright 2016 SIGNIFYD Inc. All rights reserved.
* See LICENSE.txt for license details.
*/

Expand All @@ -10,6 +10,8 @@
use Magento\Framework\ObjectManagerInterface;
use Signifyd\Connect\Helper\LogHelper;
use Signifyd\Connect\Helper\PurchaseHelper;
use Signifyd\Connect\Helper\Retry;
use Signifyd\Connect\Model\CaseRetry;

class RetryCaseJob
{
Expand All @@ -28,59 +30,81 @@ class RetryCaseJob
*/
protected $_helper;

/**
* @var \Signifyd\Connect\Helper\Retry
*/
protected $caseRetryObj;

public function __construct(
ObjectManagerInterface $objectManager,
PurchaseHelper $helper,
LogHelper $logger
LogHelper $logger,
Retry $caseRetryObj
) {
$this->_objectManager = $objectManager;
$this->_helper = $helper;
$this->_logger = $logger;
$this->caseRetryObj = $caseRetryObj;
}

/**
* Entry point to Cron job
* @return $this
*/
public function execute() {
$this->_logger->request("Starting retry job");
$this->processRetryQueue();
$this->retry();
return $this;
}

/**
* Run through up to $max items in the retry queue
* @param int $max The maximum numbers of items to process
* Main Retry Method to start retry cycle
*/
public function processRetryQueue($max = 99999)
public function retry()
{
/** @var $retryEntity \Signifyd\Connect\Model\CaseRetry */
$retryEntity = $this->_objectManager->get('Signifyd\Connect\Model\CaseRetry');
$failed_orders = $retryEntity->getCollection();
$process_count = 0;
try {
foreach ($failed_orders as $failed_order) {
$this->_logger->request("Order up");
/** @var $failed_order \Signifyd\Connect\Model\CaseRetry */
if ($process_count++ >= $max) {
return;
}
$order = $this->_objectManager->get('Magento\Sales\Model\Order')->loadByIncrementId($failed_order->getOrderIncrement());

$this->_logger->request("Load");
$orderData = $this->_helper->getCase($order);
if (!$this->_helper->doesCaseExist($order)) {
$this->_logger->request("No case");
$orderData = $this->_helper->processOrderData($order);
$this->_helper->createNewCase($order);
}
$this->_logger->request("Main retry method called");

$this->_logger->request("Start retry");
if ($this->_helper->retryCase($orderData, $order)) {
$this->_logger->request("Completed retry " . $failed_order->getOrderIncrement());
$failed_order->delete();
} else {
$this->_logger->error("Failed retry " . $failed_order->getOrderIncrement());
}
// Getting all the cases that were not submitted to Signifyd
$waitingCases = $this->caseRetryObj->getRetryCasesByStatus(CaseRetry::WAITING_SUBMISSION_STATUS);
foreach ($waitingCases as $case) {
$this->_logger->request("Signifyd: preparing for send case no: {$case['order_increment']}");
$order = $this->_objectManager
->get('Magento\Sales\Model\Order')
->loadByIncrementId($case['order_increment']);
$caseData = $this->_helper->processOrderData($order);
$result = $this->_helper->postCaseToSignifyd($caseData, $order);
if($result){
$caseObj = $this->_objectManager->create('Signifyd\Connect\Model\Casedata')
->load($case->getOrderIncrement())
->setCode($result)
->setMagentoStatus(CaseRetry::IN_REVIEW_STATUS)
->setUpdated(strftime('%Y-%m-%d %H:%M:%S', time()));
$caseObj->save();
}
} catch (\Exception $e) {
$this->_logger->error($e->__toString());
}

// Getting all the cases that are awaiting review from Signifyd
$inReviewCases = $this->caseRetryObj->getRetryCasesByStatus(CaseRetry::IN_REVIEW_STATUS);
foreach ($inReviewCases as $case) {
$this->_logger->request("Signifyd: preparing for review case no: {$case['order_increment']}");
$order = $this->_objectManager
->get('Magento\Sales\Model\Order')
->loadByIncrementId($case['order_increment']);
$result = $this->caseRetryObj->processInReviewCase($case, $order);
if($result){}
}

// Getting all the cases that need processing after the response was received
$inProcessingCases = $this->caseRetryObj->getRetryCasesByStatus(CaseRetry::PROCESSING_RESPONSE_STATUS);
foreach ($inProcessingCases as $case) {
$this->_logger->request("Signifyd: preparing for review case no: {$case['order_increment']}");
$order = $this->_objectManager
->get('Magento\Sales\Model\Order')
->loadByIncrementId($case['order_increment']);
$this->caseRetryObj->processResponseStatus($case, $order);
}

$this->_logger->request("Main retry method ended");
return;
}
}
2 changes: 1 addition & 1 deletion Helper/LogHelper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2015 SIGNIFYD Inc. All rights reserved.
* Copyright 2015 SIGNIFYD Inc. All rights reserved.
* See LICENSE.txt for license details.
*/

Expand Down
Loading

0 comments on commit f104fe6

Please sign in to comment.