Skip to content

Commit e332d7c

Browse files
committed
Added base value validation
1 parent aa303d0 commit e332d7c

8 files changed

+92
-3
lines changed

src/HTPayWayFactoryInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Locastic\SyliusHTPayWayPlugin;
4+
5+
interface HTPayWayFactoryInterface
6+
{
7+
public const LOCALES_AVAILABLE = ['en_US', 'hr_HR', 'de_CH', 'de_DE', 'fr_BE', 'fr_FR', 'it_IT', 'ru_RU'];
8+
9+
public const CURRENCIES_AVAILABLE = ['HRK'];
10+
}

src/HTPayWayOffsiteGatewayFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Payum\Core\Bridge\Spl\ArrayObject;
77
use Locastic\TcomPayWay\AuthorizeForm\Model\Payment as PaymentOffsite;
88

9-
final class HTPayWayOffsiteGatewayFactory extends GatewayFactory
9+
final class HTPayWayOffsiteGatewayFactory extends GatewayFactory implements HTPayWayFactoryInterface
1010
{
1111
const FACTORY_NAME = 'ht_payway_offsite';
1212

src/Resources/config/services.yml

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
services:
2+
locastic.sylius_ht_payway_plugin.validator.currency:
3+
class: Locastic\SyliusHTPayWayPlugin\Validator\Constraints\CurrencyValidator
4+
tags:
5+
- { name: validator.constraint_validator, alias: locastic_sylius_ht_payway_plugin_currency }
6+
27
locastic.sylius_ht_payway_plugin.form.type.ht_payway_gateway_configuration:
38
class: Locastic\SyliusHTPayWayPlugin\Form\Type\HTPayWayOffsiteGatewayConfigurationType
49
tags:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Sylius\Component\Core\Model\PaymentMethod:
2+
constraints:
3+
- Locastic\SyliusHTPayWayPlugin\Validator\Constraints\Currency:
4+
groups: ['sylius']
5+
message: 'locastic.sylius_ht_payway_plugin.channel.required_currency'

src/Resources/translations/validators.en.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ locastic:
22
sylius_ht_payway_plugin:
33
form:
44
shop_id_not_blank: Shop ID cannot be blank
5-
secret_key_not_blank: Secret key cannot be blank
5+
secret_key_not_blank: Secret key cannot be blank
6+
channel:
7+
required_currency: The base currency of the channel must be HRK.

src/Resources/translations/validators.hr.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ locastic:
22
sylius_ht_payway_plugin:
33
form:
44
shop_id_not_blank: Shop ID ne može biti prazan
5-
secret_key_not_blank: Tajni ključ ne može biti prazan
5+
secret_key_not_blank: Tajni ključ ne može biti prazan
6+
channel:
7+
required_currency: Osnovna vrijednost mora biti u HRK.
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Locastic\SyliusHTPayWayPlugin\Validator\Constraints;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
final class Currency extends Constraint
8+
{
9+
public $message;
10+
11+
public function validatedBy(): string
12+
{
13+
return 'locastic_sylius_ht_payway_plugin_currency';
14+
}
15+
16+
public function getTargets(): string
17+
{
18+
return self::CLASS_CONSTRAINT;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Locastic\SyliusHTPayWayPlugin\Validator\Constraints;
4+
5+
use Locastic\SyliusHTPayWayPlugin\HTPayWayFactoryInterface;
6+
use Locastic\SyliusHTPayWayPlugin\HTPayWayOffsiteGatewayFactory;
7+
use Sylius\Component\Core\Model\ChannelInterface;
8+
use Sylius\Component\Core\Model\PaymentMethodInterface;
9+
use Symfony\Component\Validator\Constraint;
10+
use Symfony\Component\Validator\ConstraintValidator;
11+
use Webmozart\Assert\Assert;
12+
13+
14+
final class CurrencyValidator extends ConstraintValidator
15+
{
16+
public function validate($paymentMethod, Constraint $constraint): void
17+
{
18+
Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class);
19+
Assert::isInstanceOf($constraint, Currency::class);
20+
21+
$gatewayConfig = $paymentMethod->getGatewayConfig();
22+
23+
if (null === $gatewayConfig || $gatewayConfig->getFactoryName(
24+
) !== HTPayWayOffsiteGatewayFactory::FACTORY_NAME) {
25+
return;
26+
}
27+
28+
/** @var ChannelInterface $channel */
29+
foreach ($paymentMethod->getChannels() as $channel) {
30+
if (
31+
null === $channel->getBaseCurrency() ||
32+
false === in_array(
33+
strtoupper($channel->getBaseCurrency()->getCode()),
34+
HTPayWayFactoryInterface::CURRENCIES_AVAILABLE
35+
)
36+
) {
37+
$message = isset($constraint->message) ? $constraint->message : null;
38+
39+
$this->context->buildViolation($message)->atPath('channels')->addViolation();
40+
41+
return;
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)