Skip to content

Commit 9a09f9f

Browse files
Patch to mitigate Magento issue with billing/shipping address (#105)
1 parent afe1442 commit 9a09f9f

File tree

20 files changed

+655
-101
lines changed

20 files changed

+655
-101
lines changed

Api/Data/LogInterface.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Rvvup\Payments\Api\Data;
4+
5+
interface LogInterface
6+
{
7+
/**
8+
* String constants for property names
9+
*/
10+
public const ENTITY_ID = "entity_id";
11+
public const PAYLOAD = "payload";
12+
public const IS_PROCESSED = "is_processed";
13+
14+
/**
15+
* Getter for EntityId.
16+
*
17+
* @return int|null
18+
*/
19+
public function getEntityId(): ?int;
20+
21+
/**
22+
* Setter for EntityId.
23+
*
24+
* @param int|null $entityId
25+
*
26+
* @return void
27+
*/
28+
public function setEntityId(?int $entityId): void;
29+
30+
/**
31+
* Getter for Payload.
32+
*
33+
* @return string|null
34+
*/
35+
public function getPayload(): ?string;
36+
37+
/**
38+
* Setter for Payload.
39+
*
40+
* @param string|null $payload
41+
*
42+
* @return void
43+
*/
44+
public function setPayload(?string $payload): void;
45+
46+
/**
47+
* Getter for IsProcessed.
48+
*
49+
* @return bool|null
50+
*/
51+
public function getIsProcessed(): ?bool;
52+
53+
/**
54+
* Setter for IsProcessed.
55+
*
56+
* @param bool|null $isProcessed
57+
*
58+
* @return void
59+
*/
60+
public function setIsProcessed(?bool $isProcessed): void;
61+
}

Cron/Log.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Rvvup\Payments\Cron;
5+
6+
use Laminas\Http\Request;
7+
use Magento\Framework\Exception\AlreadyExistsException;
8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
use Magento\Framework\Serialize\Serializer\Json;
10+
use Magento\Store\Model\ScopeInterface;
11+
use Rvvup\Payments\Model\Config;
12+
use Rvvup\Payments\Model\Logger;
13+
use Rvvup\Payments\Model\ResourceModel\LogModel\LogCollection;
14+
use Rvvup\Payments\Model\ResourceModel\LogModel\LogCollectionFactory;
15+
use Rvvup\Payments\Sdk\Curl;
16+
use Rvvup\Payments\Model\ResourceModel\LogResource;
17+
18+
class Log
19+
{
20+
/** @var LogCollectionFactory */
21+
private $logCollectionFactory;
22+
23+
/** @var Json */
24+
private $json;
25+
26+
/** @var Curl */
27+
private $curl;
28+
29+
/** @var Config */
30+
private $config;
31+
32+
/** @var LogResource */
33+
private $resource;
34+
35+
/** @var Logger */
36+
private $logger;
37+
38+
/**
39+
* @param LogCollectionFactory $logCollectionFactory
40+
* @param Json $json
41+
* @param Curl $curl
42+
* @param Config $config
43+
* @param Logger $logger
44+
* @param LogResource $resource
45+
*/
46+
public function __construct(
47+
LogCollectionFactory $logCollectionFactory,
48+
Json $json,
49+
Curl $curl,
50+
Config $config,
51+
Logger $logger,
52+
LogResource $resource
53+
) {
54+
$this->logCollectionFactory = $logCollectionFactory;
55+
$this->json = $json;
56+
$this->curl = $curl;
57+
$this->config = $config;
58+
$this->resource = $resource;
59+
$this->logger = $logger;
60+
}
61+
62+
/**
63+
* @return void
64+
* @throws AlreadyExistsException|NoSuchEntityException
65+
*/
66+
public function execute(): void
67+
{
68+
/** @var LogCollection $collection */
69+
$collection = $this->logCollectionFactory->create();
70+
$collection->addFieldToSelect('*')
71+
->addFieldToFilter('is_processed', ['eq' => 'false']);
72+
/** Limit to 50 items per run */
73+
$collection->clear()->getSelect()->limit(50);
74+
75+
$this->processLogs($collection);
76+
}
77+
78+
/**
79+
* @param LogCollection $collection
80+
* @return void
81+
* @throws AlreadyExistsException|NoSuchEntityException
82+
*/
83+
private function processLogs(LogCollection $collection): void
84+
{
85+
$batch = [];
86+
foreach ($collection->getItems() as $item) {
87+
try {
88+
$payload = $item->getData('payload');
89+
$data = $this->json->unserialize($payload);
90+
$storeId = $data['metadata']['magento']['storeId'];
91+
92+
if (!isset($batch[$storeId])) {
93+
$batch[$storeId] = [];
94+
}
95+
96+
$batch[$storeId][] = $data;
97+
} catch (\Exception $e) {
98+
$this->logger->error('Rvvup Log Cron failed, exception', [$e->getMessage(), $item->getId()]);
99+
}
100+
101+
$item->setData('is_processed', true);
102+
$this->resource->save($item);
103+
}
104+
foreach ($batch as $key => $item) {
105+
$this->notifyRvvup((string) $key, $item);
106+
}
107+
}
108+
109+
/**
110+
* @param string $storeId
111+
* @param array $data
112+
* @return void
113+
* @throws NoSuchEntityException
114+
*/
115+
private function notifyRvvup(string $storeId, array $data): void
116+
{
117+
try {
118+
$token = $this->config->getJwtConfig(ScopeInterface::SCOPE_STORE, $storeId);
119+
$headers = [
120+
'Content-Type: application/json',
121+
'Accept: application/json',
122+
'Authorization: Bearer ' . $token
123+
];
124+
$baseUrl = $this->config->getEndpoint(ScopeInterface::SCOPE_STORE, $storeId);
125+
$url = str_replace('graphql', 'plugin/log', $baseUrl);
126+
$postData = ['headers' => $headers, 'json' => $data];
127+
$this->curl->request(Request::METHOD_POST, $url, $postData);
128+
} catch (\Exception $e) {
129+
$this->logger->error('Failed to notify Rvvup with logs: ', [$e->getMessage(), $storeId]);
130+
}
131+
}
132+
}

Model/Data/LogData.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Rvvup\Payments\Model\Data;
4+
5+
use Magento\Framework\DataObject;
6+
use Rvvup\Payments\Api\Data\LogInterface;
7+
8+
class LogData extends DataObject implements LogInterface
9+
{
10+
/**
11+
* Getter for EntityId.
12+
*
13+
* @return int|null
14+
*/
15+
public function getEntityId(): ?int
16+
{
17+
return $this->getData(self::ENTITY_ID) === null ? null
18+
: (int)$this->getData(self::ENTITY_ID);
19+
}
20+
21+
/**
22+
* Setter for EntityId.
23+
*
24+
* @param int|null $entityId
25+
*
26+
* @return void
27+
*/
28+
public function setEntityId(?int $entityId): void
29+
{
30+
$this->setData(self::ENTITY_ID, $entityId);
31+
}
32+
33+
/**
34+
* Getter for Payload.
35+
*
36+
* @return string|null
37+
*/
38+
public function getPayload(): ?string
39+
{
40+
return $this->getData(self::PAYLOAD);
41+
}
42+
43+
/**
44+
* Setter for Payload.
45+
*
46+
* @param string|null $payload
47+
*
48+
* @return void
49+
*/
50+
public function setPayload(?string $payload): void
51+
{
52+
$this->setData(self::PAYLOAD, $payload);
53+
}
54+
55+
/**
56+
* Getter for IsProcessed.
57+
*
58+
* @return bool|null
59+
*/
60+
public function getIsProcessed(): ?bool
61+
{
62+
return $this->getData(self::IS_PROCESSED) === null ? null
63+
: (bool)$this->getData(self::IS_PROCESSED);
64+
}
65+
66+
/**
67+
* Setter for IsProcessed.
68+
*
69+
* @param bool|null $isProcessed
70+
*
71+
* @return void
72+
*/
73+
public function setIsProcessed(?bool $isProcessed): void
74+
{
75+
$this->setData(self::IS_PROCESSED, $isProcessed);
76+
}
77+
}

Model/LogModel.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rvvup\Payments\Model;
4+
5+
use Magento\Framework\Model\AbstractModel;
6+
use Rvvup\Payments\Model\ResourceModel\LogResource;
7+
8+
class LogModel extends AbstractModel
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $_eventPrefix = 'log_model';
14+
15+
/**
16+
* Initialize magento model.
17+
*
18+
* @return void
19+
*/
20+
protected function _construct()
21+
{
22+
$this->_init(LogResource::class);
23+
}
24+
}

0 commit comments

Comments
 (0)