Skip to content

Commit

Permalink
Merge pull request #35 from signifyd/3.6.2
Browse files Browse the repository at this point in the history
3.6.2 Bug fixes
  • Loading branch information
ebanolopes authored Jul 15, 2020
2 parents 4635eb0 + 220f980 commit b4ca737
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 53 deletions.
16 changes: 12 additions & 4 deletions Controller/Webhooks/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,18 @@ public function processRequest($request, $hash, $topic)
$requestJson = json_decode($request);

if (json_last_error() == JSON_ERROR_NONE) {
// Test is only verifying that the endpoint is reachable. So we just complete here
if ($topic === 'cases/test') {
$this->getResponse()->setStatusCode(Http::STATUS_CODE_200);
return;
switch (($topic)) {
case 'cases/test':
// Test is only verifying that the endpoint is reachable. So we just complete here
$this->getResponse()->setStatusCode(Http::STATUS_CODE_200);
return;

case 'cases/creation':
$message = 'Case creation will not be processed by Magento';
$this->getResponse()->appendBody($message);
$this->logger->debug("API: {$message}");
$this->getResponse()->setStatusCode(Http::STATUS_CODE_200);
return;
}

/** @var $order \Magento\Sales\Model\Order */
Expand Down
97 changes: 90 additions & 7 deletions Cron/RetryCaseJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
namespace Signifyd\Connect\Cron;

use Magento\Framework\ObjectManagerInterface;
use Magento\Sales\Model\ResourceModel\Order as OrderResourceModel;
use Magento\Sales\Model\OrderFactory;
use Signifyd\Connect\Logger\Logger;
use Signifyd\Connect\Helper\PurchaseHelper;
use Signifyd\Connect\Helper\Retry;
use Signifyd\Connect\Model\Casedata;
use Signifyd\Connect\Model\ResourceModel\Casedata as CasedataResourceModel;
use Signifyd\Connect\Model\CasedataFactory;

class RetryCaseJob
{
Expand All @@ -35,16 +39,60 @@ class RetryCaseJob
*/
protected $caseRetryObj;

/**
* @var OrderResourceModel
*/
protected $orderResourceModel;

/**
* @var OrderFactory
*/
protected $orderFactory;

/**
* @var CasedataResourceModel
*/
protected $casedataResourceModel;

/**
* @var CasedataFactory\
*/
protected $casedataFactory;

/**
* @var \StripeIntegration\Payments\Model\Config
*/
protected $stripeConfig;

/**
* RetryCaseJob constructor.
* @param ObjectManagerInterface $objectManager
* @param PurchaseHelper $helper
* @param Logger $logger
* @param Retry $caseRetryObj
* @param OrderResourceModel $orderResourceModel
* @param OrderFactory $orderFactory
* @param CasedataResourceModel $casedataResourceModel
* @param CasedataFactory $casedataFactory
*/
public function __construct(
ObjectManagerInterface $objectManager,
PurchaseHelper $helper,
Logger $logger,
Retry $caseRetryObj
Retry $caseRetryObj,
OrderResourceModel $orderResourceModel,
OrderFactory $orderFactory,
CasedataResourceModel $casedataResourceModel,
CasedataFactory $casedataFactory
) {
$this->_objectManager = $objectManager;
$this->_helper = $helper;
$this->logger = $logger;
$this->caseRetryObj = $caseRetryObj;
$this->orderResourceModel = $orderResourceModel;
$this->orderFactory = $orderFactory;
$this->casedataResourceModel = $casedataResourceModel;
$this->casedataFactory = $casedataFactory;
}

/**
Expand All @@ -70,13 +118,15 @@ public function execute()
$result = $this->_helper->postCaseToSignifyd($caseData, $order);

if ($result) {
/** @var \Signifyd\Connect\Model\Casedata $caseObj */
$caseObj = $this->_objectManager->create(\Signifyd\Connect\Model\Casedata::class)
->load($case->getOrderIncrement())
->setCode($result)
/** @var Casedata $caseObj */
$caseObj = $this->casedataFactory->create();
$this->casedataResourceModel->load($caseObj, $case->getOrderIncrement());

$caseObj->setCode($result)
->setMagentoStatus(Casedata::IN_REVIEW_STATUS)
->setUpdated(strftime('%Y-%m-%d %H:%M:%S', time()));
$caseObj->save();

$this->casedataResourceModel->save($caseObj);
}
}

Expand Down Expand Up @@ -109,8 +159,41 @@ public function execute()
return $this;
}

/**
* @param $incrementId
* @return \Magento\Sales\Model\Order
*/
public function getOrder($incrementId)
{
return $this->_objectManager->get(\Magento\Sales\Model\Order::class)->loadByIncrementId($incrementId);
$order = $this->orderFactory->create();
$this->orderResourceModel->load($order, $incrementId, 'increment_id');

if ($order->getPayment()->getMethod() == 'stripe_payments') {
$this->reInitStripe($order);
}

return $order;
}

/**
* @param \Magento\Sales\Model\Order $order
* @return bool
*/
public function reInitStripe(\Magento\Sales\Model\Order $order)
{
if (class_exists(\StripeIntegration\Payments\Model\Config::class) === false) {
return false;
}

if ($this->stripeConfig === null) {
$this->stripeConfig = $this->_objectManager->get(\StripeIntegration\Payments\Model\Config::class);
}

if (version_compare(\StripeIntegration\Payments\Model\Config::$moduleVersion, '1.8.8') >= 0 &&
method_exists($this->stripeConfig, 'reInitStripe')) {
$this->stripeConfig->reInitStripe($order->getStoreId(), $order->getBaseCurrencyCode(), null);
}

return true;
}
}
5 changes: 3 additions & 2 deletions Helper/FulfillmentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ public function getTrackingNumbers(\Magento\Sales\Model\Order\Shipment $shipment
foreach ($trackingCollection->getItems() as $tracking) {
$number = trim($tracking->getNumber());

if (empty($number) == false) {
$trackingNumbers[] = $tracking->getNumber();
if (empty($number) === false && is_object($number) === false && is_array($number) === false &&
in_array($number, $trackingNumbers) == false) {
$trackingNumbers[] = $number;
}
}

Expand Down
9 changes: 0 additions & 9 deletions Helper/PurchaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,6 @@ protected function makeShipments(Order $order)
return $shipments;
}

public function isAdmin()
{
/** @var \Magento\Framework\ObjectManagerInterface $om */
$om = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\App\State $state */
$state = $om->get(\Magento\Framework\App\State::class);
return 'adminhtml' === $state->getAreaCode();
}

/**
* @param $mageAddress Address
* @return SignifydAddress
Expand Down
111 changes: 111 additions & 0 deletions Observer/Order/Save/Before.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Signifyd\Connect\Observer\Order\Save;

use Magento\Framework\App\State as AppState;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Model\StoreManagerInterface;
use Signifyd\Connect\Logger\Logger;
use Signifyd\Connect\Helper\ConfigHelper;
use Signifyd\Connect\Helper\PurchaseHelper;

class Before implements ObserverInterface
{
/**
* @var Logger;
*/
protected $logger;

/**
* @var AppState
*/
protected $appState;

/**
* @var ConfigHelper
*/
protected $configHelper;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var RequestInterface
*/
protected $request;

/**
* Before constructor.
* @param Logger $loger
* @param AppState $appState
* @param ConfigHelper $configHelper
* @param StoreManagerInterface $storeManager
* @param RequestInterface $request
*/
public function __construct(
Logger $loger,
AppState $appState,
ConfigHelper $configHelper,
StoreManagerInterface $storeManager,
RequestInterface $request
) {
$this->logger = $loger;
$this->appState = $appState;
$this->configHelper = $configHelper;
$this->storeManager = $storeManager;
$this->request = $request;
}

/**
* @param Observer $observer
* @param bool $checkOwnEventsMethods
*/
public function execute(Observer $observer, $checkOwnEventsMethods = true)
{
try {
/** @var $order Order */
$order = $observer->getEvent()->getOrder();

if (!is_object($order)) {
return;
}

if ($this->configHelper->isEnabled($order) == false) {
return;
}

// Saving store code to order, to know where the order is been created
if (empty($order->getData('origin_store_code')) && is_object($this->storeManager)) {
$isAdmin = ('adminhtml' === $this->appState->getAreaCode());
$storeCode = $this->storeManager->getStore($isAdmin ? 'admin' : true)->getCode();

if (!empty($storeCode)) {
$order->setData('origin_store_code', $storeCode);
}
}

// Fix for Magento bug https://github.com/magento/magento2/issues/7227
// x_forwarded_for should be copied from quote, but quote does not have the field on database
if (empty($order->getData('x_forwarded_for')) && is_object($this->request)) {
$xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR');

if (empty($xForwardIp) == false) {
$order->setData('x_forwarded_for', $xForwardIp);
}
}
} catch (\Exception $ex) {
$context = [];

if (isset($order) && $order instanceof Order) {
$context['entity'] = $order;
}

$this->logger->error($ex->getMessage(), $context);
}
}
}
34 changes: 5 additions & 29 deletions Observer/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
class Purchase implements ObserverInterface
{
/**
* @var \Signifyd\Connect\Logger\Logger;
* @var Logger;
*/
protected $logger;

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

Expand Down Expand Up @@ -126,33 +126,6 @@ public function execute(Observer $observer, $checkOwnEventsMethods = true)
return;
}

$saveOrder = false;

// Saving store code to order, to know where the order is been created
if (empty($order->getData('origin_store_code')) && is_object($this->storeManager)) {
$storeCode = $this->storeManager->getStore($this->helper->isAdmin() ? 'admin' : true)->getCode();

if (!empty($storeCode)) {
$order->setData('origin_store_code', $storeCode);
$saveOrder = true;
}
}

// Fix for Magento bug https://github.com/magento/magento2/issues/7227
// x_forwarded_for should be copied from quote, but quote does not have the field on database
if (empty($order->getData('x_forwarded_for')) && is_object($this->request)) {
$xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR');

if (empty($xForwardIp) == false) {
$order->setData('x_forwarded_for', $xForwardIp);
$saveOrder = true;
}
}

if ($saveOrder) {
$order->save();
}

// Check if a payment is available for this order yet
if ($order->getState() == \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT) {
return;
Expand Down Expand Up @@ -198,6 +171,9 @@ public function execute(Observer $observer, $checkOwnEventsMethods = true)
$case->save();
$message = 'Case for order:#' . $incrementId . ' was not sent because of an async payment method';
$this->logger->debug($message);

// Initial hold order
$this->holdOrder($order);
} catch (\Exception $ex) {
$this->logger->error($ex->__toString());
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=5.5.22"
},
"type": "magento2-module",
"version": "3.6.1",
"version": "3.6.2",
"autoload": {
"files": [
"registration.php"
Expand Down
4 changes: 4 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<observer name="signifydDebugLogs" instance="Signifyd\Connect\Observer\Debug\Order" />
</event>

<event name="sales_order_save_before">
<observer name="orderSaveBefore" instance="Signifyd\Connect\Observer\Order\Save\Before" />
</event>

<event name="checkout_submit_all_after">
<observer name="onCheckout" instance="Signifyd\Connect\Observer\Purchase" />
</event>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Signifyd_Connect" setup_version="3.6.1">
<module name="Signifyd_Connect" setup_version="3.6.2">
<sequence>
<module name="Magento_Sales" />
<module name="Magento_Payment" />
Expand Down

0 comments on commit b4ca737

Please sign in to comment.