Skip to content

Commit

Permalink
Patch to mitigate Magento issue with billing/shipping address (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-onufriichuk authored Apr 10, 2024
1 parent afe1442 commit 9a09f9f
Show file tree
Hide file tree
Showing 20 changed files with 655 additions and 101 deletions.
61 changes: 61 additions & 0 deletions Api/Data/LogInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Rvvup\Payments\Api\Data;

interface LogInterface
{
/**
* String constants for property names
*/
public const ENTITY_ID = "entity_id";
public const PAYLOAD = "payload";
public const IS_PROCESSED = "is_processed";

/**
* Getter for EntityId.
*
* @return int|null
*/
public function getEntityId(): ?int;

/**
* Setter for EntityId.
*
* @param int|null $entityId
*
* @return void
*/
public function setEntityId(?int $entityId): void;

/**
* Getter for Payload.
*
* @return string|null
*/
public function getPayload(): ?string;

/**
* Setter for Payload.
*
* @param string|null $payload
*
* @return void
*/
public function setPayload(?string $payload): void;

/**
* Getter for IsProcessed.
*
* @return bool|null
*/
public function getIsProcessed(): ?bool;

/**
* Setter for IsProcessed.
*
* @param bool|null $isProcessed
*
* @return void
*/
public function setIsProcessed(?bool $isProcessed): void;
}
132 changes: 132 additions & 0 deletions Cron/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
declare(strict_types=1);

namespace Rvvup\Payments\Cron;

use Laminas\Http\Request;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Store\Model\ScopeInterface;
use Rvvup\Payments\Model\Config;
use Rvvup\Payments\Model\Logger;
use Rvvup\Payments\Model\ResourceModel\LogModel\LogCollection;
use Rvvup\Payments\Model\ResourceModel\LogModel\LogCollectionFactory;
use Rvvup\Payments\Sdk\Curl;
use Rvvup\Payments\Model\ResourceModel\LogResource;

class Log
{
/** @var LogCollectionFactory */
private $logCollectionFactory;

/** @var Json */
private $json;

/** @var Curl */
private $curl;

/** @var Config */
private $config;

/** @var LogResource */
private $resource;

/** @var Logger */
private $logger;

/**
* @param LogCollectionFactory $logCollectionFactory
* @param Json $json
* @param Curl $curl
* @param Config $config
* @param Logger $logger
* @param LogResource $resource
*/
public function __construct(
LogCollectionFactory $logCollectionFactory,
Json $json,
Curl $curl,
Config $config,
Logger $logger,
LogResource $resource
) {
$this->logCollectionFactory = $logCollectionFactory;
$this->json = $json;
$this->curl = $curl;
$this->config = $config;
$this->resource = $resource;
$this->logger = $logger;
}

/**
* @return void
* @throws AlreadyExistsException|NoSuchEntityException
*/
public function execute(): void
{
/** @var LogCollection $collection */
$collection = $this->logCollectionFactory->create();
$collection->addFieldToSelect('*')
->addFieldToFilter('is_processed', ['eq' => 'false']);
/** Limit to 50 items per run */
$collection->clear()->getSelect()->limit(50);

$this->processLogs($collection);
}

/**
* @param LogCollection $collection
* @return void
* @throws AlreadyExistsException|NoSuchEntityException
*/
private function processLogs(LogCollection $collection): void
{
$batch = [];
foreach ($collection->getItems() as $item) {
try {
$payload = $item->getData('payload');
$data = $this->json->unserialize($payload);
$storeId = $data['metadata']['magento']['storeId'];

if (!isset($batch[$storeId])) {
$batch[$storeId] = [];
}

$batch[$storeId][] = $data;
} catch (\Exception $e) {
$this->logger->error('Rvvup Log Cron failed, exception', [$e->getMessage(), $item->getId()]);
}

$item->setData('is_processed', true);
$this->resource->save($item);
}
foreach ($batch as $key => $item) {
$this->notifyRvvup((string) $key, $item);
}
}

/**
* @param string $storeId
* @param array $data
* @return void
* @throws NoSuchEntityException
*/
private function notifyRvvup(string $storeId, array $data): void
{
try {
$token = $this->config->getJwtConfig(ScopeInterface::SCOPE_STORE, $storeId);
$headers = [
'Content-Type: application/json',
'Accept: application/json',
'Authorization: Bearer ' . $token
];
$baseUrl = $this->config->getEndpoint(ScopeInterface::SCOPE_STORE, $storeId);
$url = str_replace('graphql', 'plugin/log', $baseUrl);
$postData = ['headers' => $headers, 'json' => $data];
$this->curl->request(Request::METHOD_POST, $url, $postData);
} catch (\Exception $e) {
$this->logger->error('Failed to notify Rvvup with logs: ', [$e->getMessage(), $storeId]);
}
}
}
77 changes: 77 additions & 0 deletions Model/Data/LogData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Rvvup\Payments\Model\Data;

use Magento\Framework\DataObject;
use Rvvup\Payments\Api\Data\LogInterface;

class LogData extends DataObject implements LogInterface
{
/**
* Getter for EntityId.
*
* @return int|null
*/
public function getEntityId(): ?int
{
return $this->getData(self::ENTITY_ID) === null ? null
: (int)$this->getData(self::ENTITY_ID);
}

/**
* Setter for EntityId.
*
* @param int|null $entityId
*
* @return void
*/
public function setEntityId(?int $entityId): void
{
$this->setData(self::ENTITY_ID, $entityId);
}

/**
* Getter for Payload.
*
* @return string|null
*/
public function getPayload(): ?string
{
return $this->getData(self::PAYLOAD);
}

/**
* Setter for Payload.
*
* @param string|null $payload
*
* @return void
*/
public function setPayload(?string $payload): void
{
$this->setData(self::PAYLOAD, $payload);
}

/**
* Getter for IsProcessed.
*
* @return bool|null
*/
public function getIsProcessed(): ?bool
{
return $this->getData(self::IS_PROCESSED) === null ? null
: (bool)$this->getData(self::IS_PROCESSED);
}

/**
* Setter for IsProcessed.
*
* @param bool|null $isProcessed
*
* @return void
*/
public function setIsProcessed(?bool $isProcessed): void
{
$this->setData(self::IS_PROCESSED, $isProcessed);
}
}
24 changes: 24 additions & 0 deletions Model/LogModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rvvup\Payments\Model;

use Magento\Framework\Model\AbstractModel;
use Rvvup\Payments\Model\ResourceModel\LogResource;

class LogModel extends AbstractModel
{
/**
* @var string
*/
protected $_eventPrefix = 'log_model';

/**
* Initialize magento model.
*
* @return void
*/
protected function _construct()
{
$this->_init(LogResource::class);
}
}
Loading

0 comments on commit 9a09f9f

Please sign in to comment.