-
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.
Change resolved banks on pbl payments (#140)
- Loading branch information
Showing
24 changed files
with
707 additions
and
29 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
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Repository; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use Doctrine\ORM\Query\Expr\Join; | ||
use Sylius\Component\Channel\Model\ChannelInterface; | ||
|
||
/** | ||
* @mixin EntityRepository | ||
*/ | ||
trait FindByChannelWithGatewayConfigTrait | ||
{ | ||
public function findByChannelAndGatewayConfigNameWithGatewayConfig(ChannelInterface $channel, string $gatewayConfigName): array | ||
{ | ||
return $this->createQueryBuilder('o') | ||
->addSelect('gatewayConfig') | ||
->innerJoin('o.gatewayConfig', 'gatewayConfig', Join::WITH, 'gatewayConfig.gatewayName = :gatewayName') | ||
->where('o.enabled = :enabled') | ||
->andWhere(':channel MEMBER OF o.channels') | ||
->setParameter('enabled', true) | ||
->setParameter('channel', $channel) | ||
->setParameter('gatewayName', $gatewayConfigName) | ||
->getQuery() | ||
->getResult() | ||
; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Repository/FindByChannelWithGatewayConfigTraitInterface.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Repository; | ||
|
||
use Sylius\Component\Channel\Model\ChannelInterface; | ||
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface as BasePaymentMethodRepositoryInterface; | ||
|
||
interface FindByChannelWithGatewayConfigTraitInterface extends BasePaymentMethodRepositoryInterface | ||
{ | ||
public function findByChannelWithGatewayConfig(ChannelInterface $channel): array; | ||
} |
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,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider; | ||
|
||
use App\Repository\PaymentMethodRepositoryInterface; | ||
use CommerceWeavers\SyliusTpayPlugin\Payum\Exception\UnableToGetBankListException; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType; | ||
use Payum\Core\Security\CryptedInterface; | ||
use Payum\Core\Security\CypherInterface; | ||
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ValidTpayChannelListProvider implements ValidTpayChannelListProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly AvailableTpayChannelListProviderInterface $availableTpayApiBankListProvider, | ||
private readonly PaymentMethodRepositoryInterface $paymentMethodRepository, | ||
private readonly ChannelContextInterface $channelContext, | ||
private readonly CypherInterface $cypher, | ||
) { | ||
} | ||
|
||
public function provide(): array | ||
{ | ||
$availableChannels = $this->availableTpayApiBankListProvider->provide(); | ||
|
||
/** @var PaymentMethodInterface[] $paymentMethods */ | ||
$paymentMethods = $this->paymentMethodRepository->findByChannelAndGatewayConfigNameWithGatewayConfig( | ||
$this->channelContext->getChannel(), | ||
'tpay', | ||
); | ||
|
||
Assert::notEmpty($paymentMethods, 'There is no payment method of Tpay type available'); | ||
|
||
$paymentMethodsToRemoveByGroupId = []; | ||
$hasPblPaymentAvailable = false; | ||
foreach ($paymentMethods as $paymentMethod) { | ||
/** @var (GatewayConfigInterface&CryptedInterface)|null $tpayGatewayConfig */ | ||
$tpayGatewayConfig = $paymentMethod->getGatewayConfig(); | ||
|
||
if (null === $tpayGatewayConfig) { | ||
continue; | ||
} | ||
|
||
$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, | ||
PaymentType::CARD => $paymentMethodsToRemoveByGroupId[] = PayGroup::CARD, | ||
default => null, | ||
}; | ||
} | ||
|
||
if (!$hasPblPaymentAvailable) { | ||
throw new UnableToGetBankListException( | ||
'Bank list cannot be retrieved if there is no payment method with PayByLink type configured', | ||
); | ||
} | ||
|
||
return array_filter($availableChannels, static function (array $channel) use ($paymentMethodsToRemoveByGroupId): bool { | ||
$groupId = (int) $channel['groups'][0]['id']; | ||
|
||
return !in_array($groupId, $paymentMethodsToRemoveByGroupId, true); | ||
}); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Tpay/Provider/ValidTpayChannelListProviderInterface.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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider; | ||
|
||
interface ValidTpayChannelListProviderInterface | ||
{ | ||
public function provide(): array; | ||
} |
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 CommerceWeavers\SyliusTpayPlugin\Validator\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class ValidTpayChannel 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; | ||
} | ||
} |
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,40 @@ | ||
<?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; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ValidTpayChannelValidator extends ConstraintValidator | ||
{ | ||
public function __construct( | ||
private readonly ValidTpayChannelListProviderInterface $validatedTpayApiBankListProvider, | ||
) { | ||
} | ||
|
||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (null === $value) { | ||
return; | ||
} | ||
|
||
Assert::string($value); | ||
|
||
Assert::isInstanceOf($constraint, ValidTpayChannel::class); | ||
|
||
$validChannels = $this->validatedTpayApiBankListProvider->provide(); | ||
|
||
if (array_key_exists($value, $validChannels)) { | ||
return; | ||
} | ||
|
||
$this->context->buildViolation($constraint->message) | ||
->setCode($constraint::NOT_VALID_CHANNEL_ERROR) | ||
->addViolation() | ||
; | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
tests/Application/src/Repository/PaymentMethodRepository.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Repository; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Repository\FindByChannelWithGatewayConfigTrait; | ||
use Sylius\Bundle\CoreBundle\Doctrine\ORM\PaymentMethodRepository as BasePaymentMethodRepository; | ||
|
||
final class PaymentMethodRepository extends BasePaymentMethodRepository implements PaymentMethodRepositoryInterface | ||
{ | ||
use FindByChannelWithGatewayConfigTrait; | ||
} |
Oops, something went wrong.