1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusQuadPayPlugin\Validator\Constraints; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusQuadPayPlugin\QuadPayGatewayFactory; |
16
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
17
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
18
|
|
|
use Symfony\Component\Validator\Constraint; |
19
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
20
|
|
|
use Webmozart\Assert\Assert; |
21
|
|
|
|
22
|
|
|
final class OrderAmountValidator extends ConstraintValidator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param PaymentInterface $payment |
26
|
|
|
* @param Constraint|OrderAmount $constraint |
27
|
|
|
* |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function validate($payment, Constraint $constraint): void |
31
|
|
|
{ |
32
|
|
|
Assert::isInstanceOf($payment, PaymentInterface::class); |
33
|
|
|
|
34
|
|
|
Assert::isInstanceOf($constraint, OrderAmount::class); |
35
|
|
|
|
36
|
|
|
/** @var PaymentMethodInterface $paymentMethod */ |
37
|
|
|
$paymentMethod = $payment->getMethod(); |
38
|
|
|
|
39
|
|
|
if (null === $paymentMethod) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$gatewayConfig = $paymentMethod->getGatewayConfig(); |
44
|
|
|
|
45
|
|
|
if (null === $gatewayConfig || ($gatewayConfig->getFactoryName() !== QuadPayGatewayFactory::FACTORY_NAME)) { |
|
|
|
|
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$config = $gatewayConfig->getConfig(); |
50
|
|
|
|
51
|
|
|
$minimumAmount = $config['minimumAmount']; |
52
|
|
|
$maximumAmount = $config['maximumAmount']; |
53
|
|
|
|
54
|
|
|
if ($minimumAmount > $payment->getAmount()) { |
55
|
|
|
$this->context->buildViolation($constraint->minimumAmountMessage, [ |
56
|
|
|
'{{ minimumAmount }}' => number_format(abs($minimumAmount / 100), 2, '.', ','), |
57
|
|
|
])->atPath('method')->addViolation(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($maximumAmount < $payment->getAmount()) { |
61
|
|
|
$this->context->buildViolation($constraint->maximumAmountMessage, [ |
62
|
|
|
'{{ maximumAmount }}' => number_format(abs($maximumAmount / 100), 2, '.', ','), |
63
|
|
|
])->atPath('method')->addViolation(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.