Skip to content

Commit

Permalink
Merge pull request #17 from pagarme/release/1.1.1
Browse files Browse the repository at this point in the history
Release/1.1.1
  • Loading branch information
sfwill-dev authored Jul 22, 2021
2 parents 8f59e30 + 3e8a006 commit 7b5a390
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 47 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pagarme/ecommerce-module-core",
"description": "Core component for Pagar.me e-commerce platform modules.",
"license": "MIT",
"version": "1.1.0",
"version": "1.1.1",
"authors": [
{
"name":"Open Source Team"
Expand Down
22 changes: 15 additions & 7 deletions src/Hub/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use Pagarme\Core\Kernel\Interfaces\CommandInterface;
use Pagarme\Core\Kernel\Services\LogService;
use Pagarme\Core\Kernel\ValueObjects\Id\AccountId;
use Pagarme\Core\Kernel\ValueObjects\Id\MerchantId;
use Pagarme\Core\Kernel\ValueObjects\Id\GUID;
Expand All @@ -15,35 +16,42 @@ abstract class AbstractCommand implements CommandInterface
{
/**
*
* @var HubAccessTokenKey
* @var HubAccessTokenKey
*/
protected $accessToken;
/**
*
* @var AccountId
* @var AccountId
*/
protected $accountId;
/**
*
* @var PublicKey|TestPublicKey
* @var PublicKey|TestPublicKey
*/
protected $accountPublicKey;
/**
*
* @var GUID
* @var GUID
*/
protected $installId;
/**
*
* @var MerchantId
* @var MerchantId
*/
protected $merchantId;
/**
*
* @var CommandType
* @var CommandType
*/
protected $type;

private $logService;

public function __construct()
{
$this->logService = new LogService('Hub', true);
}

/**
*
* @return HubAccessTokenKey
Expand Down Expand Up @@ -163,4 +171,4 @@ public function setType($type)
$this->type = $type;
return $this;
}
}
}
6 changes: 4 additions & 2 deletions src/Hub/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public function execute()
$moduleConfig = MPSetup::getModuleConfiguration();

if ($moduleConfig->isHubEnabled()) {
throw new Exception("Hub already installed!");
$exception = new Exception("Hub already installed!");
$this->logService->exception($exception);
throw $exception;
}

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

$configRepo->save($moduleConfig);
}
}
}
12 changes: 8 additions & 4 deletions src/Hub/Commands/UninstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ public function execute()
$moduleConfig = MPSetup::getModuleConfiguration();

if (!$moduleConfig->isHubEnabled()) {
throw new Exception("Hub is not installed!");
$exception = new Exception("Hub is not installed!");
$this->logService->exception($exception);
throw $exception;
}

$hubKey = $moduleConfig->getSecretKey();
if (!$hubKey->equals($this->getAccessToken())) {
throw new Exception("Access Denied.");
$exception = new Exception("Access Denied.");
$this->logService->exception($exception);
throw $exception;
}

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

$cleanConfig->setId($moduleConfig->getId());
MPSetup::setModuleConfiguration($cleanConfig);

$configRepo = new ConfigurationRepository();

$configRepo->save($cleanConfig);
}
}
}
124 changes: 92 additions & 32 deletions src/Hub/Services/HubIntegrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Pagarme\Core\Hub\Services;

use Pagarme\Core\Hub\Aggregates\InstallToken;
use Pagarme\Core\Kernel\Services\LogService;
use Pagarme\Core\Hub\Factories\HubCommandFactory;
use Pagarme\Core\Hub\Factories\InstallTokenFactory;
use Pagarme\Core\Hub\Repositories\InstallTokenRepository;
Expand All @@ -12,6 +13,13 @@

final class HubIntegrationService
{
private $logService;

public function __construct()
{
$this->logService = new LogService('Hub', true);
}

/**
*
* @param $installSeed
Expand Down Expand Up @@ -49,43 +57,95 @@ public function endHubIntegration(

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

if (
is_a($installToken, InstallToken::class)
$isValidToken = is_a($installToken, InstallToken::class)
&& !$installToken->isExpired()
&& !$installToken->isUsed()
) {
$body = [
"code" => $authorizationCode
];

if ($hubCallbackUrl) {
$body['hub_callback_url'] = $hubCallbackUrl;
}

if ($webhookUrl) {
$body['webhook_url'] = $webhookUrl;
}

$url = 'https://hubapi.mundipagg.com/auth/apps/access-tokens';
$headers = [
'PublicAppKey' => MPSetup::getHubAppPublicAppKey(),
'Content-Type' => 'application/json'
];

$result = Request::post(
$url,
$headers,
json_encode($body)
&& !$installToken->isUsed();

if (!$isValidToken) {
$messageFormat = "Received an invalid installToken.
Is expired: %s | Is used: %s | Is a token: %s | Raw token: %s";

$message = sprintf(
$messageFormat,
$installToken->isExpired() ? "true" : "false",
$installToken->isUsed() ? "true" : "false",
is_a($installToken, InstallToken::class) ? "true" : "false",
$installToken->getToken()->getValue()
);
$exception = new \Exception($message);

if ($result->code === 201) {
$this->executeCommandFromPost($result->body);
$this->logService->exception($exception);
throw $exception;
}

$body = [
"code" => $authorizationCode
];

//if its ok
$installToken->setUsed(true);
$tokenRepo->save($installToken);
}
$this->logService->info(
sprintf(
'Valid install token received: %s',
$installToken->getToken()->getValue()
)
);

if ($hubCallbackUrl) {
$body['hub_callback_url'] = $hubCallbackUrl;
}

if ($webhookUrl) {
$body['webhook_url'] = $webhookUrl;
}

$url = 'https://hubapi.mundipagg.com/auth/apps/access-tokens';
$headers = [
'PublicAppKey' => MPSetup::getHubAppPublicAppKey(),
'Content-Type' => 'application/json'
];

$this->logService->info(
sprintf(
'Sending request to %s;',
$url
),
$body
);

$result = Request::post(
$url,
$headers,
json_encode($body)
);

if ($result->code === 201) {
$this->executeCommandFromPost($result->body);

//if its ok
$installToken->setUsed(true);
$tokenRepo->save($installToken);

$this->logService->info(
sprintf(
"Hub successfully installed for authorization code: %s",
$body["code"]
)
);
return;
}

$exception = new \Exception(
sprintf(
"Received unexpected response from hub. HTTP Code: %s",
$result->code
)
);

$this->logService->info(
$exception->getMessage(),
$result
);

throw $exception;
}

public function getHubStatus()
Expand Down
Loading

0 comments on commit 7b5a390

Please sign in to comment.