Skip to content

Commit

Permalink
Merge pull request #25 from logeecom/4.1
Browse files Browse the repository at this point in the history
Release version 4.1.9
  • Loading branch information
logeecom authored Nov 9, 2021
2 parents a7f2757 + 6891ca9 commit 064e63e
Show file tree
Hide file tree
Showing 66 changed files with 3,662 additions and 272 deletions.
1 change: 0 additions & 1 deletion EventListener/OrderLineEntityListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
class OrderLineEntityListener
{

public static $MOLLIE_MAPPED_ATTRIBUTES = ['freeFormProduct', 'quantity', 'priceType'];
/**
* @var MollieDtoMapperInterface
Expand Down
1 change: 0 additions & 1 deletion Form/Type/PaymentMethodSettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Range;


/**
* Form type for Mollie integration payment methods settings
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public function reset()
}
}

/**
* Returns Authorization token
*
* @return string
*/
public function getAuthToken()
{
return $this->getConfigService()->getAuthorizationToken();
}

/**
* @return Configuration
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ public function validateToken(TokenInterface $token);
* Resets account
*/
public function reset();

/**
* Returns Authorization token
*
* @return string|null
*/
public function getAuthToken();
}
55 changes: 52 additions & 3 deletions IntegrationCore/BusinessLogic/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic;

use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Connect\DTO\AuthInfo;
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Http\DTO\WebsiteProfile;
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\Logger\Logger;

Expand All @@ -20,18 +21,21 @@ abstract class Configuration extends \Mollie\Bundle\PaymentBundle\IntegrationCor
* @return string Integration version.
*/
abstract public function getIntegrationVersion();

/**
* Retrieves extension (plugin) name (for example MollieMagento2).
*
* @return string Extension name.
*/
abstract public function getExtensionName();

/**
* Retrieves extension (plugin) version.
*
* @return string Extension version.
*/
abstract public function getExtensionVersion();

/**
* Returns URL for checking extension version
*
Expand Down Expand Up @@ -62,6 +66,31 @@ public function removeConfigValue($name)
return $entity ? $this->getRepository()->delete($entity) : true;
}

/**
* Returns authorization token.
*
* @return AuthInfo|null Authorization token if found; otherwise, NULL.
*/
public function getAuthorizationInfo()
{
$authInfo = json_decode($this->getConfigValue('authToken'), true);
if (empty($authInfo)) {
return null;
}

return AuthInfo::fromArray($authInfo);
}

/**
* Sets authorization token.
*
* @param AuthInfo $authInfo Authorization token.
*/
public function setAuthorizationInfo($authInfo)
{
$this->saveConfigValue('authToken', json_encode($authInfo->toArray()));
}

/**
* Returns authorization token.
*
Expand All @@ -75,11 +104,31 @@ public function getAuthorizationToken()
/**
* Sets authorization token.
*
* @param string $token Authorization token.
* @param string $authToken Authorization token.
*/
public function setAuthorizationToken($authToken)
{
$this->saveConfigValue('authToken', $authToken);
}

/**
* Returns state string.
*
* @return string|null State string if found; otherwise, NULL.
*/
public function getStateString()
{
return $this->getConfigValue('state') ?: null;
}

/**
* Sets authorization token.
*
* @param string $state State string.
*/
public function setAuthorizationToken($token)
public function setStateString($state)
{
$this->saveConfigValue('authToken', $token);
$this->saveConfigValue('state', $state);
}

/**
Expand Down
162 changes: 162 additions & 0 deletions IntegrationCore/BusinessLogic/Connect/AuthorizationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

namespace Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Connect;

use DateTime;
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Authorization\Interfaces\AuthorizationService as AuthorizationInterface;
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Configuration;
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Connect\DTO\AuthInfo;
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Http\Exceptions\UnprocessableEntityRequestException;
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\Http\Exceptions\HttpAuthenticationException;
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\Http\Exceptions\HttpCommunicationException;
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\Http\Exceptions\HttpRequestException;
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\ServiceRegister;

/**
* Class AuthorizationService
* @package Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Connect
*/
abstract class AuthorizationService implements AuthorizationInterface
{
const AUTHORIZE_URL = 'https://www.mollie.com/oauth2/authorize';

/**
* @var Configuration
*/
protected $configuration;
/**
* @var TokenService
*/
protected $tokenService;

/**
* AuthorizationService constructor.
*/
public function __construct()
{
$this->configuration = ServiceRegister::getService(Configuration::CLASS_NAME);
$this->tokenService = ServiceRegister::getService(TokenService::CLASS_NAME);
}


/**
* Gets string of Mollie permissions that are needed for application
*
* @return array
*/
abstract public function getApplicationPermissions();

/**
* Gets clients id
*
* @return string
*/
abstract public function getClientId();

/**
* Gets callback url
*
* @return string
*/
abstract public function getRedirectUrl();

/**
* Get client secret
*
* @return string
*/
abstract public function getClientSecret();

/**
* This function should generate Mollie authorize URL in the given language
*
* @param string $locale
*
* @return string
*/
public function getAuthorizeUrl($locale, $state = null)
{
$params = array(
'client_id' => $this->getClientId(),
'redirect_uri' => $this->getRedirectUrl(),
'state' => $state ?: $this->generateStateString(),
'scope' => $this->formatApplicationPermissions(),
'response_type' => 'code',
'approval_prompt' => 'force',
'locale' => $locale,
);

return static::AUTHORIZE_URL . '?' . http_build_query($params);
}

/**
* @return AuthInfo|null
* @throws UnprocessableEntityRequestException
* @throws HttpAuthenticationException
* @throws HttpCommunicationException
* @throws HttpRequestException
*/
public function getAuthInfo()
{
$authInfo = $this->configuration->getAuthorizationInfo();
if (!$authInfo) {
return null;
}

$currentTime = $this->getCurrentTimeInSeconds();
if ($authInfo->getAccessTokenDuration() < $currentTime) {
$authInfo = $this->tokenService->refreshToken($authInfo->getRefreshToken());
}

return $authInfo;
}

/**
* {@inheritdoc}
*
* @return string|null
* @throws HttpAuthenticationException
* @throws HttpCommunicationException
* @throws HttpRequestException
* @throws UnprocessableEntityRequestException
*/
abstract public function getAuthToken();

/**
* Generates state string
*
* @return string
*/
private function generateStateString()
{
$state = md5(uniqid('', true));
$this->configuration->setStateString($state);

return $state;
}

/**
* @return string
*/
private function formatApplicationPermissions()
{
$formattedPermission = '';
foreach ($this->getApplicationPermissions() as $permission) {
$formattedPermission .= $permission . ' ';
}

return trim($formattedPermission);
}

/**
* Returns current time in seconds
*
* @return int
*/
private function getCurrentTimeInSeconds()
{
$time = new DateTime('now');

return $time->getTimestamp();
}
}
Loading

0 comments on commit 064e63e

Please sign in to comment.