-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add encrypting a Tpay payment gateway config (#20)
- Loading branch information
Showing
17 changed files
with
309 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Config\PayumConfig; | ||
|
||
return function(PayumConfig $payum): void { | ||
$payum | ||
->dynamicGateways() | ||
->encryption() | ||
->defuseSecretKey('%env(PAYUM_CYPHER_KEY)%') | ||
; | ||
}; |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Fixture\Factory\PaymentMethodExampleFactory; | ||
|
||
return function(ContainerConfigurator $container): void { | ||
$services = $container->services(); | ||
|
||
$services->set('commerce_weavers_tpay.fixture.factory.payment_method_example', PaymentMethodExampleFactory::class) | ||
->decorate('sylius.fixture.example_factory.payment_method') | ||
->args([ | ||
service('.inner'), | ||
service('payum.dynamic_gateways.cypher'), | ||
]) | ||
; | ||
}; |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Fixture\Factory; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory; | ||
use Payum\Core\Security\CryptedInterface; | ||
use Payum\Core\Security\CypherInterface; | ||
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class PaymentMethodExampleFactory implements ExampleFactoryInterface | ||
{ | ||
public function __construct( | ||
private ExampleFactoryInterface $decorated, | ||
private CypherInterface $cypher, | ||
) { | ||
} | ||
|
||
public function create(array $options = []): PaymentMethodInterface | ||
{ | ||
/** @var PaymentMethodInterface|mixed $paymentMethod */ | ||
$paymentMethod = $this->decorated->create($options); | ||
|
||
Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class); | ||
|
||
$gatewayConfig = $paymentMethod->getGatewayConfig(); | ||
Assert::notNull($gatewayConfig); | ||
|
||
if ($gatewayConfig->getGatewayName() !== TpayGatewayFactory::NAME) { | ||
return $paymentMethod; | ||
} | ||
|
||
if ($gatewayConfig instanceof CryptedInterface) { | ||
$gatewayConfig->encrypt($this->cypher); | ||
} | ||
|
||
return $paymentMethod; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Form\EventListener; | ||
|
||
use Payum\Core\Security\CryptedInterface; | ||
use Payum\Core\Security\CypherInterface; | ||
use Symfony\Component\Form\Event\PreSetDataEvent; | ||
|
||
final class DecryptGatewayConfigListener implements DecryptGatewayConfigListenerInterface | ||
{ | ||
public function __construct( | ||
private CypherInterface $cypher, | ||
) { | ||
} | ||
|
||
public function __invoke(PreSetDataEvent $event): void | ||
{ | ||
$gatewayConfig = $event->getData(); | ||
|
||
if (!$gatewayConfig instanceof CryptedInterface) { | ||
return; | ||
} | ||
|
||
$gatewayConfig->decrypt($this->cypher); | ||
|
||
$event->setData($gatewayConfig); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Form/EventListener/DecryptGatewayConfigListenerInterface.php
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Form\EventListener; | ||
|
||
use Symfony\Component\Form\Event\PreSetDataEvent; | ||
|
||
interface DecryptGatewayConfigListenerInterface | ||
{ | ||
public function __invoke(PreSetDataEvent $event): void; | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Form\EventListener; | ||
|
||
use Payum\Core\Security\CryptedInterface; | ||
use Payum\Core\Security\CypherInterface; | ||
use Symfony\Component\Form\Event\PostSubmitEvent; | ||
|
||
final class EncryptGatewayConfigListener implements EncryptGatewayConfigListenerInterface | ||
{ | ||
public function __construct( | ||
private CypherInterface $cypher, | ||
) { | ||
} | ||
|
||
public function __invoke(PostSubmitEvent $event): void | ||
{ | ||
$gatewayConfig = $event->getData(); | ||
|
||
if (!$gatewayConfig instanceof CryptedInterface) { | ||
return; | ||
} | ||
|
||
$gatewayConfig->encrypt($this->cypher); | ||
|
||
$event->setData($gatewayConfig); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Form/EventListener/EncryptGatewayConfigListenerInterface.php
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Form\EventListener; | ||
|
||
use Symfony\Component\Form\Event\PostSubmitEvent; | ||
|
||
interface EncryptGatewayConfigListenerInterface | ||
{ | ||
public function __invoke(PostSubmitEvent $event): void; | ||
} |
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
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 @@ | ||
PAYUM_CYPHER_KEY=def00000626f0eff82cfc9ed8bd1f465a22a1ccd4d96e91585a6158975db8dda13c984280127fe8368d6cc7864851c3abc0af04e1fd2037086584e24d8abb6e39ae83583 |
55 changes: 55 additions & 0 deletions
55
tests/Unit/Form/EventListener/DecryptGatewayConfigListenerTest.php
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Form\EventListener; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\DecryptGatewayConfigListener; | ||
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\DecryptGatewayConfigListenerInterface; | ||
use Payum\Core\GatewayInterface; | ||
use Payum\Core\Model\GatewayConfigInterface; | ||
use Payum\Core\Security\CryptedInterface; | ||
use Payum\Core\Security\CypherInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Symfony\Component\Form\Event\PreSetDataEvent; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
final class DecryptGatewayConfigListenerTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private CypherInterface|ObjectProphecy $cypher; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->cypher = $this->prophesize(CypherInterface::class); | ||
} | ||
|
||
public function test_it_does_nothing_when_a_gateway_is_not_crypted(): void | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
|
||
$form = $this->prophesize(FormInterface::class); | ||
$gateway = $this->prophesize(GatewayInterface::class); | ||
|
||
$this->createTestSubject()->__invoke(new PreSetDataEvent($form->reveal(), $gateway->reveal())); | ||
} | ||
|
||
public function test_it_decrypts_a_gateway_config(): void | ||
{ | ||
$form = $this->prophesize(FormInterface::class); | ||
$gateway = $this->prophesize(GatewayConfigInterface::class); | ||
$gateway->willImplement(CryptedInterface::class); | ||
|
||
$gateway->decrypt($this->cypher)->shouldBeCalled(); | ||
|
||
$this->createTestSubject()->__invoke(new PreSetDataEvent($form->reveal(), $gateway->reveal())); | ||
} | ||
|
||
private function createTestSubject(): DecryptGatewayConfigListenerInterface | ||
{ | ||
return new DecryptGatewayConfigListener($this->cypher->reveal()); | ||
} | ||
} |
Oops, something went wrong.