-
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.
- Loading branch information
Showing
15 changed files
with
400 additions
and
21 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
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,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
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 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; | ||
} | ||
} |
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,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() | ||
; | ||
} | ||
} |
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.