Skip to content

Commit 064e63e

Browse files
authored
Merge pull request #25 from logeecom/4.1
Release version 4.1.9
2 parents a7f2757 + 6891ca9 commit 064e63e

File tree

66 files changed

+3662
-272
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3662
-272
lines changed

EventListener/OrderLineEntityListener.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
*/
2222
class OrderLineEntityListener
2323
{
24-
2524
public static $MOLLIE_MAPPED_ATTRIBUTES = ['freeFormProduct', 'quantity', 'priceType'];
2625
/**
2726
* @var MollieDtoMapperInterface

Form/Type/PaymentMethodSettingsType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Contracts\Translation\TranslatorInterface;
2424
use Symfony\Component\Validator\Constraints\Range;
2525

26-
2726
/**
2827
* Form type for Mollie integration payment methods settings
2928
*/

IntegrationCore/BusinessLogic/Authorization/AuthorizationService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public function reset()
5353
}
5454
}
5555

56+
/**
57+
* Returns Authorization token
58+
*
59+
* @return string
60+
*/
61+
public function getAuthToken()
62+
{
63+
return $this->getConfigService()->getAuthorizationToken();
64+
}
65+
5666
/**
5767
* @return Configuration
5868
*/

IntegrationCore/BusinessLogic/Authorization/Interfaces/AuthorizationService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ public function validateToken(TokenInterface $token);
3030
* Resets account
3131
*/
3232
public function reset();
33+
34+
/**
35+
* Returns Authorization token
36+
*
37+
* @return string|null
38+
*/
39+
public function getAuthToken();
3340
}

IntegrationCore/BusinessLogic/Configuration.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic;
44

5+
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Connect\DTO\AuthInfo;
56
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Http\DTO\WebsiteProfile;
67
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\Logger\Logger;
78

@@ -20,18 +21,21 @@ abstract class Configuration extends \Mollie\Bundle\PaymentBundle\IntegrationCor
2021
* @return string Integration version.
2122
*/
2223
abstract public function getIntegrationVersion();
24+
2325
/**
2426
* Retrieves extension (plugin) name (for example MollieMagento2).
2527
*
2628
* @return string Extension name.
2729
*/
2830
abstract public function getExtensionName();
31+
2932
/**
3033
* Retrieves extension (plugin) version.
3134
*
3235
* @return string Extension version.
3336
*/
3437
abstract public function getExtensionVersion();
38+
3539
/**
3640
* Returns URL for checking extension version
3741
*
@@ -62,6 +66,31 @@ public function removeConfigValue($name)
6266
return $entity ? $this->getRepository()->delete($entity) : true;
6367
}
6468

69+
/**
70+
* Returns authorization token.
71+
*
72+
* @return AuthInfo|null Authorization token if found; otherwise, NULL.
73+
*/
74+
public function getAuthorizationInfo()
75+
{
76+
$authInfo = json_decode($this->getConfigValue('authToken'), true);
77+
if (empty($authInfo)) {
78+
return null;
79+
}
80+
81+
return AuthInfo::fromArray($authInfo);
82+
}
83+
84+
/**
85+
* Sets authorization token.
86+
*
87+
* @param AuthInfo $authInfo Authorization token.
88+
*/
89+
public function setAuthorizationInfo($authInfo)
90+
{
91+
$this->saveConfigValue('authToken', json_encode($authInfo->toArray()));
92+
}
93+
6594
/**
6695
* Returns authorization token.
6796
*
@@ -75,11 +104,31 @@ public function getAuthorizationToken()
75104
/**
76105
* Sets authorization token.
77106
*
78-
* @param string $token Authorization token.
107+
* @param string $authToken Authorization token.
108+
*/
109+
public function setAuthorizationToken($authToken)
110+
{
111+
$this->saveConfigValue('authToken', $authToken);
112+
}
113+
114+
/**
115+
* Returns state string.
116+
*
117+
* @return string|null State string if found; otherwise, NULL.
118+
*/
119+
public function getStateString()
120+
{
121+
return $this->getConfigValue('state') ?: null;
122+
}
123+
124+
/**
125+
* Sets authorization token.
126+
*
127+
* @param string $state State string.
79128
*/
80-
public function setAuthorizationToken($token)
129+
public function setStateString($state)
81130
{
82-
$this->saveConfigValue('authToken', $token);
131+
$this->saveConfigValue('state', $state);
83132
}
84133

85134
/**
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
3+
namespace Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Connect;
4+
5+
use DateTime;
6+
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Authorization\Interfaces\AuthorizationService as AuthorizationInterface;
7+
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Configuration;
8+
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Connect\DTO\AuthInfo;
9+
use Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Http\Exceptions\UnprocessableEntityRequestException;
10+
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\Http\Exceptions\HttpAuthenticationException;
11+
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\Http\Exceptions\HttpCommunicationException;
12+
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\Http\Exceptions\HttpRequestException;
13+
use Mollie\Bundle\PaymentBundle\IntegrationCore\Infrastructure\ServiceRegister;
14+
15+
/**
16+
* Class AuthorizationService
17+
* @package Mollie\Bundle\PaymentBundle\IntegrationCore\BusinessLogic\Connect
18+
*/
19+
abstract class AuthorizationService implements AuthorizationInterface
20+
{
21+
const AUTHORIZE_URL = 'https://www.mollie.com/oauth2/authorize';
22+
23+
/**
24+
* @var Configuration
25+
*/
26+
protected $configuration;
27+
/**
28+
* @var TokenService
29+
*/
30+
protected $tokenService;
31+
32+
/**
33+
* AuthorizationService constructor.
34+
*/
35+
public function __construct()
36+
{
37+
$this->configuration = ServiceRegister::getService(Configuration::CLASS_NAME);
38+
$this->tokenService = ServiceRegister::getService(TokenService::CLASS_NAME);
39+
}
40+
41+
42+
/**
43+
* Gets string of Mollie permissions that are needed for application
44+
*
45+
* @return array
46+
*/
47+
abstract public function getApplicationPermissions();
48+
49+
/**
50+
* Gets clients id
51+
*
52+
* @return string
53+
*/
54+
abstract public function getClientId();
55+
56+
/**
57+
* Gets callback url
58+
*
59+
* @return string
60+
*/
61+
abstract public function getRedirectUrl();
62+
63+
/**
64+
* Get client secret
65+
*
66+
* @return string
67+
*/
68+
abstract public function getClientSecret();
69+
70+
/**
71+
* This function should generate Mollie authorize URL in the given language
72+
*
73+
* @param string $locale
74+
*
75+
* @return string
76+
*/
77+
public function getAuthorizeUrl($locale, $state = null)
78+
{
79+
$params = array(
80+
'client_id' => $this->getClientId(),
81+
'redirect_uri' => $this->getRedirectUrl(),
82+
'state' => $state ?: $this->generateStateString(),
83+
'scope' => $this->formatApplicationPermissions(),
84+
'response_type' => 'code',
85+
'approval_prompt' => 'force',
86+
'locale' => $locale,
87+
);
88+
89+
return static::AUTHORIZE_URL . '?' . http_build_query($params);
90+
}
91+
92+
/**
93+
* @return AuthInfo|null
94+
* @throws UnprocessableEntityRequestException
95+
* @throws HttpAuthenticationException
96+
* @throws HttpCommunicationException
97+
* @throws HttpRequestException
98+
*/
99+
public function getAuthInfo()
100+
{
101+
$authInfo = $this->configuration->getAuthorizationInfo();
102+
if (!$authInfo) {
103+
return null;
104+
}
105+
106+
$currentTime = $this->getCurrentTimeInSeconds();
107+
if ($authInfo->getAccessTokenDuration() < $currentTime) {
108+
$authInfo = $this->tokenService->refreshToken($authInfo->getRefreshToken());
109+
}
110+
111+
return $authInfo;
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*
117+
* @return string|null
118+
* @throws HttpAuthenticationException
119+
* @throws HttpCommunicationException
120+
* @throws HttpRequestException
121+
* @throws UnprocessableEntityRequestException
122+
*/
123+
abstract public function getAuthToken();
124+
125+
/**
126+
* Generates state string
127+
*
128+
* @return string
129+
*/
130+
private function generateStateString()
131+
{
132+
$state = md5(uniqid('', true));
133+
$this->configuration->setStateString($state);
134+
135+
return $state;
136+
}
137+
138+
/**
139+
* @return string
140+
*/
141+
private function formatApplicationPermissions()
142+
{
143+
$formattedPermission = '';
144+
foreach ($this->getApplicationPermissions() as $permission) {
145+
$formattedPermission .= $permission . ' ';
146+
}
147+
148+
return trim($formattedPermission);
149+
}
150+
151+
/**
152+
* Returns current time in seconds
153+
*
154+
* @return int
155+
*/
156+
private function getCurrentTimeInSeconds()
157+
{
158+
$time = new DateTime('now');
159+
160+
return $time->getTimestamp();
161+
}
162+
}

0 commit comments

Comments
 (0)