-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #84 Authorization token caching (Zales0123)
This PR was merged into the 1.0-dev branch. Discussion ---------- Commits ------- 2e92d39 New PayPalCredentials entity, for token caching de9929e Service for caching authorization token 92568b6 Use cached access token everywhere 43c7ac5 Adjust Travis configuration 6ee9c39 CS fixes
- Loading branch information
Showing
20 changed files
with
481 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20200907102535 extends AbstractMigration | ||
{ | ||
public function getDescription() : string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function up(Schema $schema) : void | ||
{ | ||
// this up() migration is auto-generated, please modify it to your needs | ||
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); | ||
|
||
$this->addSql('CREATE TABLE sylius_paypal_plugin_pay_pal_credentials (id VARCHAR(255) NOT NULL, payment_method_id INT DEFAULT NULL, access_token VARCHAR(255) NOT NULL, creation_time DATETIME NOT NULL, expiration_time DATETIME NOT NULL, INDEX IDX_C56F54AD5AA1164F (payment_method_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB'); | ||
$this->addSql('ALTER TABLE sylius_paypal_plugin_pay_pal_credentials ADD CONSTRAINT FK_C56F54AD5AA1164F FOREIGN KEY (payment_method_id) REFERENCES sylius_payment_method (id)'); | ||
} | ||
|
||
public function down(Schema $schema) : void | ||
{ | ||
// this down() migration is auto-generated, please modify it to your needs | ||
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); | ||
|
||
$this->addSql('DROP TABLE sylius_paypal_plugin_pay_pal_credentials'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\PayPalPlugin\Api; | ||
|
||
use Doctrine\Persistence\ObjectManager; | ||
use Doctrine\Persistence\ObjectRepository; | ||
use Payum\Core\Model\GatewayConfigInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface; | ||
use Sylius\PayPalPlugin\Api\CacheAuthorizeClientApiInterface; | ||
use Sylius\PayPalPlugin\Entity\PayPalCredentialsInterface; | ||
|
||
final class CacheAuthorizeClientApiSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
ObjectManager $payPalCredentialsManager, | ||
ObjectRepository $payPalCredentialsRepository, | ||
AuthorizeClientApiInterface $authorizeClientApi | ||
): void { | ||
$this->beConstructedWith($payPalCredentialsManager, $payPalCredentialsRepository, $authorizeClientApi); | ||
} | ||
|
||
function it_implements_cache_authorize_client_api_interface(): void | ||
{ | ||
$this->shouldImplement(CacheAuthorizeClientApiInterface::class); | ||
} | ||
|
||
function it_returns_cached_access_token_if_it_is_not_expired( | ||
ObjectRepository $payPalCredentialsRepository, | ||
PayPalCredentialsInterface $payPalCredentials, | ||
PaymentMethodInterface $paymentMethod | ||
): void { | ||
$payPalCredentialsRepository->findOneBy(['paymentMethod' => $paymentMethod])->willReturn($payPalCredentials); | ||
|
||
$payPalCredentials->isExpired()->willReturn(false); | ||
$payPalCredentials->accessToken()->willReturn('TOKEN'); | ||
|
||
$this->authorize($paymentMethod)->shouldReturn('TOKEN'); | ||
} | ||
|
||
function it_gets_access_token_from_api_caches_and_returns_it( | ||
ObjectManager $payPalCredentialsManager, | ||
ObjectRepository $payPalCredentialsRepository, | ||
AuthorizeClientApiInterface $authorizeClientApi, | ||
PaymentMethodInterface $paymentMethod, | ||
GatewayConfigInterface $gatewayConfig | ||
): void { | ||
$payPalCredentialsRepository->findOneBy(['paymentMethod' => $paymentMethod])->willReturn(null); | ||
|
||
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); | ||
$gatewayConfig->getConfig()->willReturn(['client_id' => 'CLIENT_ID', 'client_secret' => '$ECRET']); | ||
|
||
$authorizeClientApi->authorize('CLIENT_ID', '$ECRET')->willReturn('TOKEN'); | ||
|
||
$payPalCredentialsManager | ||
->persist(Argument::that(function (PayPalCredentialsInterface $payPalCredentials) use ($paymentMethod): bool { | ||
return | ||
$payPalCredentials->accessToken() === 'TOKEN' && | ||
$payPalCredentials->creationTime()->format('d-m-Y H:i') === (new \DateTime())->format('d-m-Y H:i') && | ||
$payPalCredentials->expirationTime()->format('d-m-Y H:i') === (new \DateTime())->modify('+3600 seconds')->format('d-m-Y H:i') && | ||
$payPalCredentials->paymentMethod() === $paymentMethod->getWrappedObject() | ||
; | ||
})) | ||
->shouldBeCalled() | ||
; | ||
$payPalCredentialsManager->flush()->shouldBeCalled(); | ||
|
||
$this->authorize($paymentMethod)->shouldReturn('TOKEN'); | ||
} | ||
|
||
function it_returns_expired_token_and_ask_for_a_new_one( | ||
ObjectManager $payPalCredentialsManager, | ||
ObjectRepository $payPalCredentialsRepository, | ||
AuthorizeClientApiInterface $authorizeClientApi, | ||
PaymentMethodInterface $paymentMethod, | ||
GatewayConfigInterface $gatewayConfig, | ||
PayPalCredentialsInterface $payPalCredentials | ||
): void { | ||
$payPalCredentialsRepository->findOneBy(['paymentMethod' => $paymentMethod])->willReturn($payPalCredentials); | ||
$payPalCredentials->isExpired()->willReturn(true); | ||
|
||
$payPalCredentialsManager->remove($payPalCredentials)->shouldBeCalled(); | ||
$payPalCredentialsManager->flush()->shouldBeCalledTimes(2); | ||
|
||
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); | ||
$gatewayConfig->getConfig()->willReturn(['client_id' => 'CLIENT_ID', 'client_secret' => '$ECRET']); | ||
|
||
$authorizeClientApi->authorize('CLIENT_ID', '$ECRET')->willReturn('TOKEN'); | ||
|
||
$payPalCredentialsManager | ||
->persist(Argument::that(function (PayPalCredentialsInterface $payPalCredentials) use ($paymentMethod): bool { | ||
return | ||
$payPalCredentials->accessToken() === 'TOKEN' && | ||
$payPalCredentials->creationTime()->format('d-m-Y H:i') == (new \DateTime())->format('d-m-Y H:i') && | ||
$payPalCredentials->expirationTime()->format('d-m-Y H:i') == (new \DateTime())->modify('+3600 seconds')->format('d-m-Y H:i') && | ||
$payPalCredentials->paymentMethod() === $paymentMethod->getWrappedObject() | ||
; | ||
})) | ||
->shouldBeCalled() | ||
; | ||
$payPalCredentialsManager->flush()->shouldBeCalled(); | ||
|
||
$this->authorize($paymentMethod)->shouldReturn('TOKEN'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\PayPalPlugin\Entity; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\PayPalPlugin\Entity\PayPalCredentialsInterface; | ||
|
||
final class PayPalCredentialsSpec extends ObjectBehavior | ||
{ | ||
function let(PaymentMethodInterface $paymentMethod): void | ||
{ | ||
$this->beConstructedWith('123ASD123', $paymentMethod, 'TOKEN', new \DateTime('2020-01-01 10:00:00'), 3600); | ||
} | ||
|
||
function it_implements_pay_pal_credentials_interface(): void | ||
{ | ||
$this->shouldImplement(PayPalCredentialsInterface::class); | ||
} | ||
|
||
function it_has_a_payment_method(PaymentMethodInterface $paymentMethod): void | ||
{ | ||
$this->paymentMethod()->shouldReturn($paymentMethod); | ||
} | ||
|
||
function it_has_a_access_token(): void | ||
{ | ||
$this->accessToken()->shouldReturn('TOKEN'); | ||
} | ||
|
||
function it_has_a_creation_time(): void | ||
{ | ||
$this->creationTime()->shouldBeLike(new \DateTime('2020-01-01 10:00:00')); | ||
} | ||
|
||
function it_has_a_expiration_time(): void | ||
{ | ||
$this->expirationTime()->shouldBeLike(new \DateTime('2020-01-01 11:00:00')); | ||
} | ||
|
||
function it_can_be_expired(): void | ||
{ | ||
$this->isExpired()->shouldReturn(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.