Skip to content

Commit

Permalink
Vipps Payment tracking headers support (#86)
Browse files Browse the repository at this point in the history
* Vipps Payment tracking headers support

* Update ModuleMetadata.php
  • Loading branch information
ed007m authored Jan 12, 2021
1 parent 3e229b2 commit c10e124
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 23 deletions.
5 changes: 2 additions & 3 deletions Cron/CancelQuoteByAttempts.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CancelQuoteByAttempts
/**
* Order collection page size
*/
const COLLECTION_PAGE_SIZE = 100;
const COLLECTION_PAGE_SIZE = 250;

/**
* @var LoggerInterface
Expand Down Expand Up @@ -148,8 +148,7 @@ private function createCollection($currentPage)
['in' => [
QuoteStatusInterface::STATUS_NEW,
QuoteStatusInterface::STATUS_PENDING,
QuoteStatusInterface::STATUS_RESERVE_FAILED,
QuoteStatusInterface::STATUS_REVERT_FAILED
QuoteStatusInterface::STATUS_RESERVE_FAILED
]]
);

Expand Down
2 changes: 1 addition & 1 deletion Cron/FetchOrderFromVipps.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FetchOrderFromVipps
/**
* Order collection page size
*/
const COLLECTION_PAGE_SIZE = 100;
const COLLECTION_PAGE_SIZE = 250;

/**
* @var TransactionProcessor
Expand Down
5 changes: 3 additions & 2 deletions Gateway/Http/Client/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,12 @@ private function place(TransferInterface $transfer)
}
$adapter->setOptions($options);
$headers = $this->getHeaders($transfer->getHeaders());
$requestHeaders = $this->productMetadata->addOptionalHeaders($headers);
// send request
$adapter->write(
$transfer->getMethod(),
$transfer->getUri(),
'1.1',
$requestHeaders,
$headers,
$this->jsonEncoder->encode($transfer->getBody())
);

Expand Down Expand Up @@ -173,6 +172,8 @@ private function getHeaders($headers)
$headers
);

$headers = $this->productMetadata->addOptionalHeaders($headers);

$result = [];
foreach ($headers as $key => $value) {
$result[] = sprintf('%s: %s', $key, $value);
Expand Down
67 changes: 53 additions & 14 deletions Model/ModuleMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

namespace Vipps\Payment\Model;

use Vipps\Payment\Model\ModuleMetadataInterface;
use \Magento\Framework\App\Config;
use \Magento\Framework\Module\ResourceInterface;
use \Magento\Framework\App\CacheInterface;
use \Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Component\ComponentRegistrarInterface;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Filesystem\Directory\ReadFactory;
use Magento\Framework\Serialize\SerializerInterface;
use Psr\Log\LoggerInterface;

/**
* Class Metadata
Expand All @@ -42,9 +45,14 @@ class ModuleMetadata implements ModuleMetadataInterface
private $version;

/**
* ResourceInterface
* @var ComponentRegistrar
*/
private $resource;
private $componentRegistrar;

/**
* @var ReadFactory
*/
private $readFactory;

/**
* ProductMetadataInterface
Expand All @@ -56,6 +64,16 @@ class ModuleMetadata implements ModuleMetadataInterface
*/
private $cache;

/**
* @var SerializerInterface
*/
private $serializer;

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

/**
* Metadata constructor.
*
Expand All @@ -64,13 +82,19 @@ class ModuleMetadata implements ModuleMetadataInterface
* @param CacheInterface $cache
*/
public function __construct(
ResourceInterface $resource,
ComponentRegistrarInterface $componentRegistrar,
ReadFactory $readFactory,
SerializerInterface $serializer,
ProductMetadataInterface $systemMetadata,
CacheInterface $cache
CacheInterface $cache,
LoggerInterface $logger
) {
$this->resource = $resource;
$this->componentRegistrar = $componentRegistrar;
$this->readFactory = $readFactory;
$this->systemMetadata = $systemMetadata;
$this->cache = $cache;
$this->serializer = $serializer;
$this->logger = $logger;
}

/**
Expand All @@ -84,6 +108,7 @@ public function addOptionalHeaders(array $headers): array
'Vipps-System-Plugin-Name' => $this->getModuleName(),
'Vipps-System-Plugin-Version' => $this->getModuleVersion(),
];

return array_merge($headers, $additionalHeaders);
}

Expand All @@ -94,13 +119,11 @@ public function addOptionalHeaders(array $headers): array
*/
private function getSystemName(): string
{
$systemName = sprintf(
return sprintf(
'%s 2 %s',
$this->systemMetadata->getName(),
$this->systemMetadata->getEdition()
);

return $systemName;
}

/**
Expand Down Expand Up @@ -133,12 +156,28 @@ private function getModuleVersion(): string
if ($this->version) {
return (string) $this->version;
}

$this->version = (string) $this->cache->load(self::VERSION_CACHE_KEY);
if ($this->version) {
return $this->version;
}
$this->version = (string) $this->resource->getDbVersion('Vipps_Payment') ?: 'UNKNOWN';

$path = $this->componentRegistrar->getPath(
ComponentRegistrar::MODULE,
'Vipps_Payment'
);

try {
$directoryRead = $this->readFactory->create($path);
$composerJsonData = $directoryRead->readFile('composer.json');
$data = $this->serializer->unserialize($composerJsonData);
$this->version = $data['version'] ?? 'UNKNOWN';
} catch (\Throwable $t) {
$this->logger->error($t);
$this->version = 'UNKNOWN';
}
$this->cache->save($this->version, self::VERSION_CACHE_KEY, [Config::CACHE_TAG]);

return $this->version;
}
}
13 changes: 11 additions & 2 deletions Model/Quote/CancelFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@

namespace Vipps\Payment\Model\Quote;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Model\Order;
use Psr\Log\LoggerInterface;
use Vipps\Payment\Api\CommandManagerInterface;
use Vipps\Payment\Api\Data\QuoteInterface;
use Vipps\Payment\Api\Data\QuoteStatusInterface;
use Vipps\Payment\Api\Quote\CancelFacadeInterface;
use Vipps\Payment\Model\Quote;
use Vipps\Payment\Model\QuoteRepository;
use Magento\Framework\Exception\CouldNotSaveException;

/**
* Quote Cancellation Facade.
Expand Down Expand Up @@ -64,6 +65,11 @@ class CancelFacade implements CancelFacadeInterface
*/
private $orderRepository;

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

/**
* CancelFacade constructor.
*
Expand All @@ -80,14 +86,16 @@ public function __construct(
QuoteRepository $quoteRepository,
AttemptManagement $attemptManagement,
CartRepositoryInterface $cartRepository,
OrderRepositoryInterface $orderRepository
OrderRepositoryInterface $orderRepository,
LoggerInterface $logger
) {
$this->commandManager = $commandManager;
$this->orderManagement = $orderManagement;
$this->quoteRepository = $quoteRepository;
$this->attemptManagement = $attemptManagement;
$this->cartRepository = $cartRepository;
$this->orderRepository = $orderRepository;
$this->logger = $logger;
}

/**
Expand All @@ -112,6 +120,7 @@ public function cancel(QuoteInterface $vippsQuote)
$vippsQuote->setStatus(QuoteStatusInterface::STATUS_REVERTED);
$this->quoteRepository->save($vippsQuote);
} catch (\Throwable $t) {
$this->logger->critical($t);
$vippsQuote->setStatus(QuoteStatusInterface::STATUS_REVERT_FAILED);
$this->quoteRepository->save($vippsQuote);

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "magento2-module",
"description": "Vipps Payment Method",
"license": "proprietary",
"version": "2.4.5",
"version": "2.4.6",
"require": {
"magento/framework": "103.0.*",
"magento/module-sales": "103.0.*",
Expand Down
10 changes: 10 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,14 @@
<argument name="logger" xsi:type="object">Vipps\Payment\Model\Logger</argument>
</arguments>
</type>
<type name="Vipps\Payment\Model\Quote\CancelFacade">
<arguments>
<argument name="logger" xsi:type="object">Vipps\Payment\Model\Logger</argument>
</arguments>
</type>
<type name="Vipps\Payment\Model\ModuleMetadata">
<arguments>
<argument name="logger" xsi:type="object">Vipps\Payment\Model\Logger</argument>
</arguments>
</type>
</config>

0 comments on commit c10e124

Please sign in to comment.