Skip to content

Commit

Permalink
Bank List Provider change
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Oct 30, 2024
1 parent c9d0e6d commit 2a21040
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 21 deletions.
1 change: 1 addition & 0 deletions config/config/sylius_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Symfony\Config\SyliusFixturesConfig;

Expand Down
2 changes: 1 addition & 1 deletion config/services/context_provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

$services->set(BankListContextProvider::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.provider.tpay_api_bank_list'),
service('commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list'),
])
->tag('sylius.ui.template_event.context_provider')
;
Expand Down
3 changes: 1 addition & 2 deletions config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\CardTypeDataTransformer;
use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\VisaMobilePhoneDataTransformer;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\DecryptGatewayConfigListener;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\EncryptGatewayConfigListener;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyPasswordFieldsListener;
Expand All @@ -17,7 +16,7 @@
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayPaymentDetailsType;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;

return function(ContainerConfigurator $container): void {
return static function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set(CompleteTypeExtension::class)
Expand Down
20 changes: 15 additions & 5 deletions config/services/tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateRedirectBasedPaymentPayloadFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateVisaMobilePaymentPayloadFactory;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateVisaMobilePaymentPayloadFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProvider;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProviderInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\AvailableTpayChannelListProvider;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\AvailableTpayChannelListProviderInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProvider;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver\CachedTpayTransactionChannelResolver;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver\TpayTransactionChannelResolver;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Security\Notification\Factory\BasicPaymentFactory;
Expand Down Expand Up @@ -149,18 +150,27 @@
->alias(SignatureVerifierInterface::class, 'commerce_weavers_sylius_tpay.tpay.security.notification.verifier.signature')
;

$services->set('commerce_weavers_sylius_tpay.tpay.provider.tpay_api_bank_list', TpayApiBankListProvider::class)
$services->set('commerce_weavers_sylius_tpay.tpay.provider.available_tpay_api_bank_list', AvailableTpayChannelListProvider::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.resolver.tpay_transaction_channel_resolver'),
])
->alias(TpayApiBankListProviderInterface::class, 'commerce_weavers_sylius_tpay.tpay.provider.tpay_api_bank_list')
->alias(AvailableTpayChannelListProviderInterface::class, 'commerce_weavers_sylius_tpay.tpay.provider.available_tpay_api_bank_list')
;

$services->set('commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list', ValidTpayChannelListProvider::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.provider.available_tpay_api_bank_list'),
service('sylius.repository.gateway_config'),
service('payum.dynamic_gateways.cypher')
])
->alias(AvailableTpayChannelListProviderInterface::class, 'commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list')
;

$services->set('commerce_weavers_sylius_tpay.tpay.resolver.tpay_transaction_channel_resolver', TpayTransactionChannelResolver::class)
->args([
service('payum'),
])
->alias(TpayApiBankListProviderInterface::class, 'commerce_weavers_sylius_tpay.tpay.resolver.tpay_transaction_channel_resolver')
->alias(AvailableTpayChannelListProviderInterface::class, 'commerce_weavers_sylius_tpay.tpay.resolver.tpay_transaction_channel_resolver')
;

$services->set('commerce_weavers_sylius_tpay.tpay.resolver.cached_tpay_transaction_channel_resolver', CachedTpayTransactionChannelResolver::class)
Expand Down
8 changes: 5 additions & 3 deletions src/ContextProvider/BankListContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

namespace CommerceWeavers\SyliusTpayPlugin\ContextProvider;

use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProviderInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProviderInterface;
use Sylius\Bundle\UiBundle\ContextProvider\ContextProviderInterface;
use Sylius\Bundle\UiBundle\Registry\TemplateBlock;
use Sylius\Component\Core\Model\OrderInterface;

final class BankListContextProvider implements ContextProviderInterface
{
public function __construct(
private readonly TpayApiBankListProviderInterface $bankListProvider,
private readonly ValidTpayChannelListProviderInterface $validatedTpayApiBankListProvider,
) {
}

public function provide(array $templateContext, TemplateBlock $templateBlock): array
{
// TODO this is runned few many times when on checkout/complete/choice-item
// propably there it should not be runned totaly but on summary step only
if (isset($templateContext['order'])) {
/** @var OrderInterface $order */
$order = $templateContext['order'];
Expand All @@ -27,7 +29,7 @@ public function provide(array $templateContext, TemplateBlock $templateBlock): a
}
}

$templateContext['banks'] = $this->bankListProvider->provide();
$templateContext['banks'] = $this->validatedTpayApiBankListProvider->provide();

return $templateContext;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Form/Type/TpayPaymentDetailsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CommerceWeavers\SyliusTpayPlugin\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayToken;
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ValidTpayApiChannel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
Expand Down Expand Up @@ -70,9 +71,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
)
->add(
'tpay_channel_id',

HiddenType::class,
[
'property_path' => '[tpay_channel_id]',
'validation_groups' => ['sylius_checkout_complete'],
'constraints' => [
new ValidTpayApiChannel(groups: ['sylius_checkout_complete']),
],
],
)
->add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver\TpayTransactionChannelResolverInterface;

final class TpayApiBankListProvider implements TpayApiBankListProviderInterface
final class AvailableTpayChannelListProvider implements AvailableTpayChannelListProviderInterface
{
public function __construct(
private readonly TpayTransactionChannelResolverInterface $channelResolver,
Expand All @@ -30,11 +30,7 @@ public function provide(): array
* }
*/
return array_filter($result, static function (array $channel) {
return
($channel['instantRedirection'] ?? false) === true &&
($channel['onlinePayment'] ?? false) === true &&
($channel['available'] ?? false) === true
;
return ($channel['available'] ?? false) === true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider;

interface TpayApiBankListProviderInterface
interface AvailableTpayChannelListProviderInterface
{
/** @return array{
* id: string,
Expand Down
78 changes: 78 additions & 0 deletions src/Tpay/Provider/ValidTpayChannelListProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider;

use CommerceWeavers\SyliusTpayPlugin\Payum\Exception\UnableToGetBankListException;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Payum\Core\Model\GatewayConfigInterface;
use Payum\Core\Security\CypherInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Webmozart\Assert\Assert;

final class ValidTpayChannelListProvider implements ValidTpayChannelListProviderInterface
{
public function __construct(
private readonly AvailableTpayChannelListProviderInterface $availableTpayApiBankListProvider,
private readonly RepositoryInterface $gatewayRepository,
private readonly CypherInterface $cypher,
) {
}

public function provide(): array
{
$availableChannels = $this->availableTpayApiBankListProvider->provide();

/** @var GatewayConfigInterface[] $tpayGatewayConfigs */
$tpayGatewayConfigs = $this->gatewayRepository->findBy(['gatewayName' => 'tpay']);

Assert::notEmpty($tpayGatewayConfigs, 'There is no gateway config of Tpay type available');

if (count($tpayGatewayConfigs) === 1 &&
$tpayGatewayConfigs[0]->getConfig()['type'] === PaymentType::PAY_BY_LINK
) {
return $availableChannels;
}

$paymentMethodsToRemoveByGroupId = [];
$hasPblPaymentAvailable = false;
foreach ($tpayGatewayConfigs as $tpayGatewayConfig) {
// cached doctrine values are encrypted hence need for decrypt
$tpayGatewayConfig->decrypt($this->cypher);
$config = $tpayGatewayConfig->getConfig();

if (!array_key_exists('type', $config)) {
continue;
}

if ($hasPblPaymentAvailable === false && $config['type'] === PaymentType::PAY_BY_LINK) {
$hasPblPaymentAvailable = true;
}

match ($config['type']) {
PaymentType::VISA_MOBILE => $paymentMethodsToRemoveByGroupId[] = PayGroup::VISA_MOBILE,
PaymentType::APPLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::APPLE_PAY,
PaymentType::GOOGLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::GOOGLE_PAY,
PaymentType::BLIK => $paymentMethodsToRemoveByGroupId[] = PayGroup::BLIK,
default => null,
};
}

if (!$hasPblPaymentAvailable) {
throw new UnableToGetBankListException(
'Bank list cannot be retrieved if there is no payment method with PayByLink type configured'
);
}

foreach ($availableChannels as $availableChannel) {
$channelId = (int) $availableChannel['id'];
if (in_array($channelId, $paymentMethodsToRemoveByGroupId, true)) {
unset($availableChannels[$channelId]);
}
}

return $availableChannels;
}
}
10 changes: 10 additions & 0 deletions src/Tpay/Provider/ValidTpayChannelListProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider;

interface ValidTpayChannelListProviderInterface
{
public function provide(): array;
}
19 changes: 19 additions & 0 deletions src/Validator/Constraint/ValidTpayApiChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Validator\Constraint;

use Symfony\Component\Validator\Constraint;

final class ValidTpayApiChannel extends Constraint
{
public string $message = 'commerce_weavers_sylius_tpay.shop.pay.tpay_channel.not_valid';

public const NOT_VALID_CHANNEL_ERROR = '632f97f3-c302-409b-a321-ec078194302d';

public function getTargets(): string
{
return self::PROPERTY_CONSTRAINT;
}
}
31 changes: 31 additions & 0 deletions src/Validator/Constraint/ValidTpayApiChannelValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Validator\Constraint;

use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProviderInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

final class ValidTpayApiChannelValidator extends ConstraintValidator
{
public function __construct(
private readonly ValidTpayChannelListProviderInterface $validatedTpayApiBankListProvider,
) {
}

public function validate($value, Constraint $constraint): void
{
$validChannels = $this->validatedTpayApiBankListProvider->provide();

if (array_key_exists($value, $validChannels)) {
return;
}

$this->context->buildViolation($constraint->message)
->setCode($constraint::NOT_VALID_CHANNEL_ERROR)
->addViolation()
;
}
}
6 changes: 3 additions & 3 deletions tests/Unit/ContextProvider/BankListContextProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\ContextProvider;

use CommerceWeavers\SyliusTpayPlugin\ContextProvider\BankListContextProvider;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProviderInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\AvailableTpayChannelListProviderInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
Expand All @@ -17,10 +17,10 @@ class BankListContextProviderTest extends TestCase
{
use ProphecyTrait;

private TpayApiBankListProviderInterface|ObjectProphecy $bankListProvider;
private AvailableTpayChannelListProviderInterface|ObjectProphecy $bankListProvider;
protected function setUp(): void
{
$this->bankListProvider = $this->prophesize(TpayApiBankListProviderInterface::class);
$this->bankListProvider = $this->prophesize(AvailableTpayChannelListProviderInterface::class);
}

public function test_it_does_not_support_some_template_event_and_pay_by_link_template_name(): void
Expand Down
Loading

0 comments on commit 2a21040

Please sign in to comment.