Skip to content

Commit 7b5a390

Browse files
authored
Merge pull request #17 from pagarme/release/1.1.1
Release/1.1.1
2 parents 8f59e30 + 3e8a006 commit 7b5a390

File tree

6 files changed

+121
-47
lines changed

6 files changed

+121
-47
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "pagarme/ecommerce-module-core",
33
"description": "Core component for Pagar.me e-commerce platform modules.",
44
"license": "MIT",
5-
"version": "1.1.0",
5+
"version": "1.1.1",
66
"authors": [
77
{
88
"name":"Open Source Team"

src/Hub/Commands/AbstractCommand.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
use Pagarme\Core\Kernel\Interfaces\CommandInterface;
7+
use Pagarme\Core\Kernel\Services\LogService;
78
use Pagarme\Core\Kernel\ValueObjects\Id\AccountId;
89
use Pagarme\Core\Kernel\ValueObjects\Id\MerchantId;
910
use Pagarme\Core\Kernel\ValueObjects\Id\GUID;
@@ -15,35 +16,42 @@ abstract class AbstractCommand implements CommandInterface
1516
{
1617
/**
1718
*
18-
* @var HubAccessTokenKey
19+
* @var HubAccessTokenKey
1920
*/
2021
protected $accessToken;
2122
/**
2223
*
23-
* @var AccountId
24+
* @var AccountId
2425
*/
2526
protected $accountId;
2627
/**
2728
*
28-
* @var PublicKey|TestPublicKey
29+
* @var PublicKey|TestPublicKey
2930
*/
3031
protected $accountPublicKey;
3132
/**
3233
*
33-
* @var GUID
34+
* @var GUID
3435
*/
3536
protected $installId;
3637
/**
3738
*
38-
* @var MerchantId
39+
* @var MerchantId
3940
*/
4041
protected $merchantId;
4142
/**
4243
*
43-
* @var CommandType
44+
* @var CommandType
4445
*/
4546
protected $type;
4647

48+
private $logService;
49+
50+
public function __construct()
51+
{
52+
$this->logService = new LogService('Hub', true);
53+
}
54+
4755
/**
4856
*
4957
* @return HubAccessTokenKey
@@ -163,4 +171,4 @@ public function setType($type)
163171
$this->type = $type;
164172
return $this;
165173
}
166-
}
174+
}

src/Hub/Commands/InstallCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public function execute()
1313
$moduleConfig = MPSetup::getModuleConfiguration();
1414

1515
if ($moduleConfig->isHubEnabled()) {
16-
throw new Exception("Hub already installed!");
16+
$exception = new Exception("Hub already installed!");
17+
$this->logService->exception($exception);
18+
throw $exception;
1719
}
1820

1921
$moduleConfig->setHubInstallId($this->getInstallId());
@@ -32,4 +34,4 @@ public function execute()
3234

3335
$configRepo->save($moduleConfig);
3436
}
35-
}
37+
}

src/Hub/Commands/UninstallCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ public function execute()
1515
$moduleConfig = MPSetup::getModuleConfiguration();
1616

1717
if (!$moduleConfig->isHubEnabled()) {
18-
throw new Exception("Hub is not installed!");
18+
$exception = new Exception("Hub is not installed!");
19+
$this->logService->exception($exception);
20+
throw $exception;
1921
}
2022

2123
$hubKey = $moduleConfig->getSecretKey();
2224
if (!$hubKey->equals($this->getAccessToken())) {
23-
throw new Exception("Access Denied.");
25+
$exception = new Exception("Access Denied.");
26+
$this->logService->exception($exception);
27+
throw $exception;
2428
}
2529

2630
$cleanConfig = json_decode(json_encode($moduleConfig));
@@ -44,9 +48,9 @@ public function execute()
4448

4549
$cleanConfig->setId($moduleConfig->getId());
4650
MPSetup::setModuleConfiguration($cleanConfig);
47-
51+
4852
$configRepo = new ConfigurationRepository();
4953

5054
$configRepo->save($cleanConfig);
5155
}
52-
}
56+
}

src/Hub/Services/HubIntegrationService.php

Lines changed: 92 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pagarme\Core\Hub\Services;
44

55
use Pagarme\Core\Hub\Aggregates\InstallToken;
6+
use Pagarme\Core\Kernel\Services\LogService;
67
use Pagarme\Core\Hub\Factories\HubCommandFactory;
78
use Pagarme\Core\Hub\Factories\InstallTokenFactory;
89
use Pagarme\Core\Hub\Repositories\InstallTokenRepository;
@@ -12,6 +13,13 @@
1213

1314
final class HubIntegrationService
1415
{
16+
private $logService;
17+
18+
public function __construct()
19+
{
20+
$this->logService = new LogService('Hub', true);
21+
}
22+
1523
/**
1624
*
1725
* @param $installSeed
@@ -49,43 +57,95 @@ public function endHubIntegration(
4957

5058
$installToken = $tokenRepo->findByPagarmeId(new HubInstallToken($installToken));
5159

52-
if (
53-
is_a($installToken, InstallToken::class)
60+
$isValidToken = is_a($installToken, InstallToken::class)
5461
&& !$installToken->isExpired()
55-
&& !$installToken->isUsed()
56-
) {
57-
$body = [
58-
"code" => $authorizationCode
59-
];
60-
61-
if ($hubCallbackUrl) {
62-
$body['hub_callback_url'] = $hubCallbackUrl;
63-
}
64-
65-
if ($webhookUrl) {
66-
$body['webhook_url'] = $webhookUrl;
67-
}
68-
69-
$url = 'https://hubapi.mundipagg.com/auth/apps/access-tokens';
70-
$headers = [
71-
'PublicAppKey' => MPSetup::getHubAppPublicAppKey(),
72-
'Content-Type' => 'application/json'
73-
];
74-
75-
$result = Request::post(
76-
$url,
77-
$headers,
78-
json_encode($body)
62+
&& !$installToken->isUsed();
63+
64+
if (!$isValidToken) {
65+
$messageFormat = "Received an invalid installToken.
66+
Is expired: %s | Is used: %s | Is a token: %s | Raw token: %s";
67+
68+
$message = sprintf(
69+
$messageFormat,
70+
$installToken->isExpired() ? "true" : "false",
71+
$installToken->isUsed() ? "true" : "false",
72+
is_a($installToken, InstallToken::class) ? "true" : "false",
73+
$installToken->getToken()->getValue()
7974
);
75+
$exception = new \Exception($message);
8076

81-
if ($result->code === 201) {
82-
$this->executeCommandFromPost($result->body);
77+
$this->logService->exception($exception);
78+
throw $exception;
79+
}
80+
81+
$body = [
82+
"code" => $authorizationCode
83+
];
8384

84-
//if its ok
85-
$installToken->setUsed(true);
86-
$tokenRepo->save($installToken);
87-
}
85+
$this->logService->info(
86+
sprintf(
87+
'Valid install token received: %s',
88+
$installToken->getToken()->getValue()
89+
)
90+
);
91+
92+
if ($hubCallbackUrl) {
93+
$body['hub_callback_url'] = $hubCallbackUrl;
8894
}
95+
96+
if ($webhookUrl) {
97+
$body['webhook_url'] = $webhookUrl;
98+
}
99+
100+
$url = 'https://hubapi.mundipagg.com/auth/apps/access-tokens';
101+
$headers = [
102+
'PublicAppKey' => MPSetup::getHubAppPublicAppKey(),
103+
'Content-Type' => 'application/json'
104+
];
105+
106+
$this->logService->info(
107+
sprintf(
108+
'Sending request to %s;',
109+
$url
110+
),
111+
$body
112+
);
113+
114+
$result = Request::post(
115+
$url,
116+
$headers,
117+
json_encode($body)
118+
);
119+
120+
if ($result->code === 201) {
121+
$this->executeCommandFromPost($result->body);
122+
123+
//if its ok
124+
$installToken->setUsed(true);
125+
$tokenRepo->save($installToken);
126+
127+
$this->logService->info(
128+
sprintf(
129+
"Hub successfully installed for authorization code: %s",
130+
$body["code"]
131+
)
132+
);
133+
return;
134+
}
135+
136+
$exception = new \Exception(
137+
sprintf(
138+
"Received unexpected response from hub. HTTP Code: %s",
139+
$result->code
140+
)
141+
);
142+
143+
$this->logService->info(
144+
$exception->getMessage(),
145+
$result
146+
);
147+
148+
throw $exception;
89149
}
90150

91151
public function getHubStatus()

0 commit comments

Comments
 (0)