Skip to content

Commit

Permalink
Merge pull request #161 from vippsas/publication-2.4.43
Browse files Browse the repository at this point in the history
Publication 2.4.43
  • Loading branch information
voleye authored Mar 22, 2023
2 parents fb3c218 + f714bdb commit da56b9f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
74 changes: 36 additions & 38 deletions Model/TokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
namespace Vipps\Payment\Model;

use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;
use Magento\Payment\Gateway\ConfigInterface;
use Laminas\Http\Response;
use Magento\Framework\HTTP\Adapter\Curl as MagentoCurl;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\HTTP\ZendClientFactory;
use Magento\Framework\HTTP\ZendClient;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\HTTP\Adapter\CurlFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Payment\Gateway\ConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;
use Vipps\Payment\Gateway\Exception\AuthenticationException;
use Vipps\Payment\Gateway\Http\Client\Curl;

Expand All @@ -45,11 +46,6 @@ class TokenProvider implements TokenProviderInterface
*/
private static $endpointUrl = '/accessToken/get';

/**
* @var ZendClientFactory
*/
private $httpClientFactory;

/**
* @var ResourceConnection
*/
Expand Down Expand Up @@ -86,27 +82,30 @@ class TokenProvider implements TokenProviderInterface
private $jwtRecord = [];

/**
* TokenProvider constructor.
*
* @var CurlFactory
*/
private $adapterFactory;

/**
* @param ResourceConnection $resourceConnection
* @param ZendClientFactory $httpClientFactory
* @param CurlFactory $adapterFactory
* @param ConfigInterface $config
* @param Json $serializer,
* @param Json $serializer
* @param LoggerInterface $logger
* @param UrlResolver $urlResolver
* @param ScopeResolverInterface $scopeResolver
*/
public function __construct(
ResourceConnection $resourceConnection,
ZendClientFactory $httpClientFactory,
CurlFactory $adapterFactory,
ConfigInterface $config,
Json $serializer,
LoggerInterface $logger,
UrlResolver $urlResolver,
ScopeResolverInterface $scopeResolver
) {
$this->resourceConnection = $resourceConnection;
$this->httpClientFactory = $httpClientFactory;
$this->adapterFactory = $adapterFactory;
$this->config = $config;
$this->serializer = $serializer;
$this->logger = $logger;
Expand Down Expand Up @@ -171,36 +170,35 @@ public function regenerate($scopeId = null)
*/
private function readJwt($scopeId)
{
/** Configuring headers for Vipps authentication method */
$headers = [
Curl::HEADER_PARAM_CLIENT_ID => $this->config->getValue('client_id', $this->getScopeId($scopeId)),
Curl::HEADER_PARAM_CLIENT_SECRET => $this->config->getValue('client_secret', $this->getScopeId($scopeId)),
Curl::HEADER_PARAM_SUBSCRIPTION_KEY =>
$this->config->getValue('subscription_key1', $this->getScopeId($scopeId)),
];
/** @var ZendClient $client */
$client = $this->httpClientFactory->create();
try {
$client->setConfig(['strict' => false]);
$client->setUri($this->urlResolver->getUrl(self::$endpointUrl));
$client->setMethod(ZendClient::POST);
$client->setHeaders($headers);

/** Making request to Vipps
* @var $response \Zend_Http_Response
*/
$response = $client->request();
$adapter = null;
/** @var MagentoCurl $adapter */
$adapter = $this->adapterFactory->create();
$headers = [
Curl::HEADER_PARAM_CLIENT_ID . ': '
. $this->config->getValue('client_id', $this->getScopeId($scopeId)),
Curl::HEADER_PARAM_CLIENT_SECRET . ': '
. $this->config->getValue('client_secret', $this->getScopeId($scopeId)),
Curl::HEADER_PARAM_SUBSCRIPTION_KEY . ': '
. $this->config->getValue('subscription_key1', $this->getScopeId($scopeId)),
'Content-Type: application/json',
'Content-Length: 0'
];
// send request
$adapter->write('post', $this->urlResolver->getUrl(self::$endpointUrl), '1.1', $headers);
$response = Response::fromString($adapter->read());
$jwt = $this->serializer->unserialize($response->getBody());
if (!$response->isSuccessful()) {
if (!$response->isSuccess()) {
throw new \Exception($response->getBody()); //@codingStandardsIgnoreLine
}
if (!$this->isJwtValid($jwt)) {
throw new \Exception('Not valid JWT data returned from Vipps. Response: '. $response); //@codingStandardsIgnoreLine
throw new \Exception('Not valid JWT data returned from Vipps. Response: '. $response->toString()); //@codingStandardsIgnoreLine
}
$this->logger->debug('Token fetched from Vipps');
} catch (\Exception $e) { //@codingStandardsIgnoreLine
$this->logger->critical($e->getMessage());
throw new AuthenticationException(__('Can\'t retrieve access token from Vipps.'), $e);
} finally {
$adapter ? $adapter->close() : null;
}

return $jwt;
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
~ IN THE SOFTWARE.
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vipps_Payment" setup_version="2.4.42">
<module name="Vipps_Payment" setup_version="2.4.43">
<sequence>
<module name="Magento_Sales" />
<module name="Magento_Payment" />
Expand Down

0 comments on commit da56b9f

Please sign in to comment.