Skip to content

Commit

Permalink
Merge pull request #34 from signifyd/3.6.1
Browse files Browse the repository at this point in the history
3.6.1
  • Loading branch information
LorenDunlop-Signifyd authored Mar 20, 2020
2 parents d4d45f8 + 009e89a commit 4635eb0
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 179 deletions.
9 changes: 9 additions & 0 deletions Controller/Webhooks/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Filesystem\Driver\File;
use Signifyd\Connect\Model\Casedata;

/**
* Controller action for handling webhook posts from Signifyd service
Expand Down Expand Up @@ -147,6 +148,14 @@ public function processRequest($request, $hash, $topic)
$this->getResponse()->setStatusCode(Http::STATUS_CODE_400);
return;
}

if ($case->getMagentoStatus() == Casedata::WAITING_SUBMISSION_STATUS) {
$message = "Case {$requestJson->orderId} it is not ready to be updated";
$this->getResponse()->appendBody($message);
$this->logger->debug("API: {$message}");
$this->getResponse()->setStatusCode(Http::STATUS_CODE_400);
return;
}
} else {
$message = 'Invalid JSON provided on request body';
$this->getResponse()->appendBody($message);
Expand Down
155 changes: 155 additions & 0 deletions Helper/OrderHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Signifyd\Connect\Helper;

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Invoice;

class OrderHelper
{
/**
* @param Order $order
* @return string
*/
public function getCannotHoldReason(Order $order)
{
$notHoldableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if ($order->getState() == Order::STATE_HOLDED) {
$completeCase = true;
}

if (in_array($order->getState(), $notHoldableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_HOLD) === false) {
$reason = "order action flag is set to do not hold";
} else {
$reason = "unknown reason";
}

return $reason;
}

/**
* @param Order $order
* @return string
*/
public function getCannotUnholdReason(Order $order)
{
if ($order->getState() != Order::STATE_HOLDED && $order->isPaymentReview() == false) {
$reason = "order is not holded";
$completeCase = true;
} elseif ($order->isPaymentReview()) {
$reason = 'order is in payment review';
} elseif ($order->getActionFlag(Order::ACTION_FLAG_UNHOLD) === false) {
$reason = "order action flag is set to do not unhold";
} else {
$reason = "unknown reason";
}

return $reason;
}

/**
* @param Order $order
* @return string
*/
public function getCannotCancelReason(Order $order)
{
$notCancelableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if (in_array($order->getState(), $notCancelableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif (!$order->canReviewPayment() && $order->canFetchPaymentReviewUpdate()) {
$reason = "conflict with payment review";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_CANCEL) === false) {
$reason = "order action flag is set to do not cancel";
} else {
$allInvoiced = true;
foreach ($order->getAllItems() as $item) {
if ($item->getQtyToInvoice()) {
$allInvoiced = false;
break;
}
}
if ($allInvoiced) {
$reason = "all order items are invoiced";
$completeCase = true;
} else {
$reason = "unknown reason";
}
}

return $reason;
}

/**
* @param Order $order
* @return string
*/
public function getCannotInvoiceReason(Order $order)
{
$notInvoiceableStates = [
Order::STATE_CANCELED,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_COMPLETE,
Order::STATE_CLOSED,
Order::STATE_HOLDED
];

if (in_array($order->getState(), $notInvoiceableStates)) {
$reason = "order is on {$order->getState()} state";
} elseif ($order->getActionFlag(Order::ACTION_FLAG_INVOICE) === false) {
$reason = "order action flag is set to do not invoice";
} else {
$canInvoiceAny = false;

foreach ($order->getAllItems() as $item) {
if ($item->getQtyToInvoice() > 0 && !$item->getLockedDoInvoice()) {
$canInvoiceAny = true;
break;
}
}

if ($canInvoiceAny) {
$reason = "unknown reason";
} else {
$reason = "no items can be invoiced";
$completeCase = true;
}
}

return $reason;
}

/**
* @param Invoice $invoice
* @return bool
* @throws LocalizedException
*/
public function isInvoiceValid(Invoice $invoice)
{
if ($invoice->isEmpty()) {
throw new LocalizedException(__('failed to generate invoice'));
}

if ($invoice->getTotalQty() == 0) {
throw new LocalizedException(__('no items found to invoice'));
}

return true;
}
}
14 changes: 10 additions & 4 deletions Helper/PurchaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,14 @@ protected function makePurchase(Order $order)
}

$purchase->products = [];

/** @var \Magento\Sales\Model\Order\Item $item */
foreach ($items as $item) {
$purchase->products[] = $this->makeProduct($item);
$children = $item->getChildrenItems();

if (is_array($children) == false || empty($children)) {
$purchase->products[] = $this->makeProduct($item);
}
}

$purchase->totalPrice = $order->getGrandTotal();
Expand Down Expand Up @@ -437,7 +443,7 @@ public function createNewCase($order)
public function postCaseToSignifyd($caseData, $order)
{
$id = $this->configHelper->getSignifydApi($order)->createCase($caseData);

if ($id) {
$this->logger->debug("Case sent. Id is $id", ['entity' => $order]);
$order->addStatusHistoryComment("Signifyd: case created {$id}");
Expand Down Expand Up @@ -483,7 +489,7 @@ public function cancelCaseOnSignifyd(Order $order)

$this->logger->debug('Cancelling case ' . $case->getId(), ['entity' => $order]);
$disposition = $this->configHelper->getSignifydApi($order)->cancelGuarantee($case->getCode());

$this->logger->debug("Cancel disposition result {$disposition}", ['entity' => $order]);

if ($disposition == 'CANCELED') {
Expand Down Expand Up @@ -528,7 +534,7 @@ protected function getAvsCode(Order $order)

$avsCode = $avsAdapter->getData($order);
$avsCode = trim(strtoupper($avsCode));

if ($avsAdapter->validate($avsCode)) {
return $avsCode;
} else {
Expand Down
Loading

0 comments on commit 4635eb0

Please sign in to comment.