Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push orders by cron #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Api/Data/PushOrderMetadataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Picqer\Integration\Api\Data;

interface PushOrderMetadataInterface
{
const STATUS_PROCESSED = 1;
const STATUS_UNPROCESSED = 0;

const COLUMN_ID = 'id';
const COLUMN_ORDER_ID = 'order_id';
const COLUMN_ORDER_STATUS = 'order_status';
const COLUMN_MESSAGE = 'message';
const COLUMN_STATUS = 'status';
const COLUMN_CREATED_AT = 'created_at';

public function getId();

public function getOrderId();

public function getCreatedAt();

public function setOrderId($orderId);

public function setOrderStatus($orderStatus);

public function setStatus($status);

public function setMessage($message);

}
24 changes: 24 additions & 0 deletions Api/PushOrderMetadataRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Picqer\Integration\Api;

use Picqer\Integration\Api\Data\PushOrderMetadataInterface;

/**
* @api
*/
interface PushOrderMetadataRepositoryInterface
{
/**
* @param PushOrderMetadataInterface $pushOrderMetadata
* @return mixed
*/
function setProcessed(PushOrderMetadataInterface $pushOrderMetadata, $message);

/**
* @return PushOrderMetadataInterface[]
*/
function getUnprocessedList();

function save(PushOrderMetadataInterface $pushOrderMetadata);
}
102 changes: 102 additions & 0 deletions Cron/PushOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Picqer\Integration\Cron;

use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Store\Model\ScopeInterface;
use Picqer\Integration\Api\PushOrderMetadataRepositoryInterface;
use Psr\Log\LoggerInterface;

class PushOrder
{
const XML_PATH_ENABLED = 'picqer_integration_options/webhook_settings/cron_enabled';
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var PushOrderMetadataRepositoryInterface
*/
private $pushOrderMetadataRepository;
/**
* @var Curl
*/
private $_curl;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

public function __construct(
LoggerInterface $logger,
PushOrderMetadataRepositoryInterface $pushOrderMetadataRepository,
OrderRepositoryInterface $orderRepository,
Curl $_curl,
ScopeConfigInterface $scopeConfig
)
{
$this->logger = $logger;
$this->scopeConfig = $scopeConfig;
$this->pushOrderMetadataRepository = $pushOrderMetadataRepository;
$this->_curl = $_curl;
$this->orderRepository = $orderRepository;
}

public function execute()
{
if (!$this->scopeConfig->getValue(self::XML_PATH_ENABLED,ScopeInterface::SCOPE_STORE)) {
return $this;
}
$timeToCompare = date("Y-m-d H:i:s", strtotime('-1 days'));
foreach ($this->pushOrderMetadataRepository->getUnprocessedList() as $pushOrderMetadata) {
try {
if ($pushOrderMetadata->getCreatedAt() < $timeToCompare) {
throw new Exception(sprintf("Too late for push: %s < %s", $pushOrderMetadata->getCreatedAt(), $timeToCompare));
}
$message = $this->pushOrder($pushOrderMetadata->getOrderId());
} catch (Exception $e) {
$this->logger->error("Picqer push order error: " . $e->getMessage());
$message = $e->getMessage();
}
$this->pushOrderMetadataRepository->setProcessed($pushOrderMetadata, $message);
}

return $this;
}

private function pushOrder($orderId)
{
$order = $this->orderRepository->get((int)$orderId);

$subDomain = $this->scopeConfig->getValue('picqer_integration_options/webhook_settings/picqer_subdomain');
$magentoKey = $this->scopeConfig->getValue('picqer_integration_options/webhook_settings/connection_key');

if (empty($subDomain) || empty($magentoKey)) {
throw new Exception("Module is not fully configured");
}

$orderData = [];
$orderData['increment_id'] = $order->getIncrementId();
$orderData['picqer_magento_key'] = $magentoKey;

$this->_curl->setHeaders([
'Content-Type' => 'application/json'
]);

$this->_curl->setOptions([
CURLOPT_TIMEOUT => 2 // in seconds
]);

$this->_curl->post(sprintf('https://%s.picqer.com/webshops/magento2/orderPush/%s', trim($subDomain), trim($magentoKey)), json_encode($orderData));

return $this->_curl->getBody();
}

}
55 changes: 55 additions & 0 deletions Model/PushOrderMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Picqer\Integration\Model;

use Magento\Framework\Model\AbstractModel;
use Picqer\Integration\Api\Data\PushOrderMetadataInterface;
use Picqer\Integration\Model\ResourceModel\PushOrderMetadata as Model;

class PushOrderMetadata extends AbstractModel implements PushOrderMetadataInterface
{
protected function _construct()
{
$this->_init(Model::class);
}

public function getId()
{
return $this->getData(PushOrderMetadataInterface::COLUMN_ID);
}

public function getOrderId()
{
return $this->getData(PushOrderMetadataInterface::COLUMN_ORDER_ID);
}

public function getCreatedAt()
{
return $this->getData(PushOrderMetadataInterface::COLUMN_CREATED_AT);
}

public function setOrderId($orderId)
{
$this->setData(PushOrderMetadataInterface::COLUMN_ORDER_ID, (int)$orderId);
return $this;
}

public function setOrderStatus($orderStatus)
{
$this->setData(PushOrderMetadataInterface::COLUMN_ORDER_STATUS, (string)$orderStatus);
return $this;
}

public function setStatus($status)
{
$this->setData(PushOrderMetadataInterface::COLUMN_STATUS, (int)$status);
return $this;
}

public function setMessage($message)
{
$this->setData(PushOrderMetadataInterface::COLUMN_MESSAGE, (string)$message);
return $this;
}

}
80 changes: 80 additions & 0 deletions Model/PushOrderMetadataRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Picqer\Integration\Model;

use Magento\Framework\Exception\LocalizedException;
use Picqer\Integration\Api\PushOrderMetadataRepositoryInterface;
use Picqer\Integration\Api\Data\PushOrderMetadataInterface;
use Picqer\Integration\Model\ResourceModel\PushOrderMetadata as ResourceModel;
use Picqer\Integration\Model\PushOrderMetadataFactory as PushOrderMetadataFactory;
use Picqer\Integration\Model\ResourceModel\PushOrderMetadata\CollectionFactory as CollectionFactory;
use Picqer\Integration\Model\ResourceModel\PushOrderMetadata\Collection;

class PushOrderMetadataRepository implements PushOrderMetadataRepositoryInterface
{
/**
* @var ResourceModel
*/
private $resourceModel;

/**
* @var CollectionFactory
*/
private $collectionFactory;
/**
* @var PushOrderMetadataFactory
*/
private $pushOrderMetadataFactory;

public function __construct(
ResourceModel $resourceModel,
PushOrderMetadataFactory $pushOrderMetadataFactory,
CollectionFactory $collectionFactory
)
{
$this->resourceModel = $resourceModel;
$this->collectionFactory = $collectionFactory;
$this->pushOrderMetadataFactory = $pushOrderMetadataFactory;
}

public function setProcessed(PushOrderMetadataInterface $pushOrderMetadata, $message)
{
$pushOrderMetadata->setMessage((string)$message);
$pushOrderMetadata->setStatus(PushOrderMetadataInterface::STATUS_PROCESSED);
return $this->save($pushOrderMetadata);
}

/**
* @param $id
* @return PushOrderMetadataInterface
* @throws LocalizedException
*/
public function getById($id)
{
$model = $this->pushOrderMetadataFactory->create();
$this->resourceModel->load($model, (int)$id, PushOrderMetadataInterface::COLUMN_ID);
if (!$model->getId()) {
throw new LocalizedException(__('Unable to find item'));
}
return $model;
}

function getUnprocessedList()
{
$list = [];
/** @var Collection $collection */
$collection = $this->collectionFactory->create();
$collection->addFieldToFilter(PushOrderMetadataInterface::COLUMN_STATUS, ['eq' => PushOrderMetadataInterface::STATUS_UNPROCESSED]);
foreach ($collection as $item) {
$model = $this->getById((int)$item->getId());
$list[$model->getId()] = $model;
}
return $list;
}

function save(PushOrderMetadataInterface $pushOrderMetadata)
{
$this->resourceModel->save($pushOrderMetadata);
return $this->getById((int)$pushOrderMetadata->getId());
}
}
14 changes: 14 additions & 0 deletions Model/ResourceModel/PushOrderMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Picqer\Integration\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class PushOrderMetadata extends AbstractDb
{
protected function _construct()
{
$this->_init('picqer_push_order_metadata', 'id');
}

}
18 changes: 18 additions & 0 deletions Model/ResourceModel/PushOrderMetadata/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Picqer\Integration\Model\ResourceModel\PushOrderMetadata;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Picqer\Integration\Model\PushOrderMetadata as Model;
use Picqer\Integration\Model\ResourceModel\PushOrderMetadata as ResourceModel;

class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(Model::class, ResourceModel::class);
}
}



64 changes: 64 additions & 0 deletions Observer/CollectOrderSaveEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Picqer\Integration\Observer;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Picqer\Integration\Api\PushOrderMetadataRepositoryInterface;
use Picqer\Integration\Cron\PushOrder;
use Picqer\Integration\Api\Data\PushOrderMetadataInterface;
use Picqer\Integration\Api\Data\PushOrderMetadataInterfaceFactory;
use Psr\Log\LoggerInterface;

class CollectOrderSaveEvent implements ObserverInterface
{

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var PushOrderMetadataInterfaceFactory
*/
private $pushOrderMetadataInterfaceFactory;
/**
* @var PushOrderMetadataRepositoryInterface
*/
private $pushOrderMetadataRepository;
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(
LoggerInterface $logger,
ScopeConfigInterface $scopeConfig,
PushOrderMetadataInterfaceFactory $pushOrderMetadataInterfaceFactory,
PushOrderMetadataRepositoryInterface $pushOrderMetadataRepository
) {
$this->scopeConfig = $scopeConfig;
$this->pushOrderMetadataInterfaceFactory = $pushOrderMetadataInterfaceFactory;
$this->pushOrderMetadataRepository = $pushOrderMetadataRepository;
$this->logger = $logger;
}

public function execute(Observer $observer)
{
if (!$this->scopeConfig->isSetFlag(PushOrder::XML_PATH_ENABLED)) {
return;
}

try {
$order = $observer->getEvent()->getOrder();
/** @var PushOrderMetadataInterface $pushOrderMetadata */
$pushOrderMetadata = $this->pushOrderMetadataInterfaceFactory->create();
$pushOrderMetadata->setOrderId($order->getId());
$pushOrderMetadata->setOrderStatus($order->getStatus());
$this->pushOrderMetadataRepository->save($pushOrderMetadata);
} catch (\Exception $e) {
$this->logger->error("Can not create PushOrderMetadata instance: " . $e->getMessage());
}

}
}
Loading