File size: 20,494 Bytes
352fe2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | diff --git a/UPGRADE.md b/UPGRADE.md
index 96868b4f4..3b1ef5a21 100644
--- a/UPGRADE.md
+++ b/UPGRADE.md
@@ -36,6 +36,36 @@
)
```
+ `Sylius\PayPalPlugin\Controller\ProcessPayPalOrderAction`:
+ ```diff
+ public function __construct(
+ private readonly OrderRepositoryInterface $orderRepository,
+ private readonly CustomerRepositoryInterface $customerRepository,
+ private readonly FactoryInterface $customerFactory,
+ private readonly AddressFactoryInterface $addressFactory,
+ private readonly ObjectManager $orderManager,
+ private readonly StateMachineFactoryInterface|StateMachineInterface $stateMachineFactory,
+ private readonly PaymentStateManagerInterface $paymentStateManager,
+ private readonly CacheAuthorizeClientApiInterface $authorizeClientApi,
+ private readonly OrderDetailsApiInterface $orderDetailsApi,
+ private readonly OrderProviderInterface $orderProvider,
+ + private readonly ?PaymentAmountVerifierInterface $paymentAmountVerifier = null,
+ )
+ ```
+
+ `Sylius\PayPalPlugin\Controller\CompletePayPalOrderFromPaymentPageAction`:
+ ```diff
+ public function __construct(
+ private readonly PaymentStateManagerInterface $paymentStateManager,
+ private readonly UrlGeneratorInterface $router,
+ private readonly OrderProviderInterface $orderProvider,
+ private readonly FactoryInterface|StateMachineInterface $stateMachine,
+ private readonly ObjectManager $orderManager,
+ + private readonly ?PaymentAmountVerifierInterface $paymentAmountVerifier = null,
+ + private readonly ?OrderProcessorInterface $orderProcessor = null,
+ )
+ ```
+
### UPGRADE FROM 1.5.1 to 1.6.0
1. Support for Sylius 1.13 has been added, it is now the recommended Sylius version to use.
diff --git a/spec/Payum/Action/CaptureActionSpec.php b/spec/Payum/Action/CaptureActionSpec.php
index 7abe4040a..831bd20bb 100644
--- a/spec/Payum/Action/CaptureActionSpec.php
+++ b/spec/Payum/Action/CaptureActionSpec.php
@@ -66,6 +66,7 @@ function it_authorizes_seller_send_create_order_request_and_sets_order_response_
'status' => StatusAction::STATUS_CAPTURED,
'paypal_order_id' => '123123',
'reference_id' => 'UUID',
+ 'payment_amount' => 1000,
])->shouldBeCalled();
$this->execute($request);
diff --git a/src/Controller/CompletePayPalOrderFromPaymentPageAction.php b/src/Controller/CompletePayPalOrderFromPaymentPageAction.php
index 693dad1cd..7a02a24c6 100644
--- a/src/Controller/CompletePayPalOrderFromPaymentPageAction.php
+++ b/src/Controller/CompletePayPalOrderFromPaymentPageAction.php
@@ -19,8 +19,11 @@
use Sylius\Abstraction\StateMachine\WinzouStateMachineAdapter;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
+use Sylius\Component\Order\Processor\OrderProcessorInterface;
+use Sylius\PayPalPlugin\Exception\PaymentAmountMismatchException;
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
use Sylius\PayPalPlugin\Provider\OrderProviderInterface;
+use Sylius\PayPalPlugin\Verifier\PaymentAmountVerifierInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -34,6 +37,8 @@ public function __construct(
private readonly OrderProviderInterface $orderProvider,
private readonly FactoryInterface|StateMachineInterface $stateMachine,
private readonly ObjectManager $orderManager,
+ private readonly ?PaymentAmountVerifierInterface $paymentAmountVerifier = null,
+ private readonly ?OrderProcessorInterface $orderProcessor = null,
) {
if ($this->stateMachine instanceof FactoryInterface) {
trigger_deprecation(
@@ -46,6 +51,22 @@ public function __construct(
),
);
}
+ if (null === $this->paymentAmountVerifier) {
+ trigger_deprecation(
+ 'sylius/paypal-plugin',
+ '1.6',
+ 'Not passing an instance of "%s" as the fifth argument is deprecated and will be prohibited in 3.0.',
+ PaymentAmountVerifierInterface::class,
+ );
+ }
+ if (null === $this->orderProcessor) {
+ trigger_deprecation(
+ 'sylius/paypal-plugin',
+ '1.6',
+ 'Not passing an instance of "%s" as the sixth argument is deprecated and will be prohibited in 3.0.',
+ OrderProcessorInterface::class,
+ );
+ }
}
public function __invoke(Request $request): Response
@@ -56,6 +77,26 @@ public function __invoke(Request $request): Response
/** @var PaymentInterface $payment */
$payment = $order->getLastPayment(PaymentInterface::STATE_PROCESSING);
+ try {
+ if ($this->paymentAmountVerifier !== null) {
+ $this->paymentAmountVerifier->verify($payment);
+ } else {
+ $this->verify($payment);
+ }
+ } catch (PaymentAmountMismatchException) {
+ $this->paymentStateManager->cancel($payment);
+ $order->removePayment($payment);
+
+ if (null === $this->orderProcessor) {
+ throw new \RuntimeException('Order processor is required to process the order.');
+ }
+ $this->orderProcessor->process($order);
+
+ return new JsonResponse([
+ 'return_url' => $this->router->generate('sylius_shop_checkout_complete', [], UrlGeneratorInterface::ABSOLUTE_URL),
+ ]);
+ }
+
$this->paymentStateManager->complete($payment);
$this->getStateMachine()->apply($order, OrderCheckoutTransitions::GRAPH, OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
@@ -78,4 +119,20 @@ private function getStateMachine(): StateMachineInterface
return $this->stateMachine;
}
+
+ private function verify(PaymentInterface $payment): void
+ {
+ $totalAmount = $this->getTotalPaymentAmountFromPaypal($payment);
+
+ if ($payment->getOrder()->getTotal() !== $totalAmount) {
+ throw new PaymentAmountMismatchException();
+ }
+ }
+
+ private function getTotalPaymentAmountFromPaypal(PaymentInterface $payment): int
+ {
+ $details = $payment->getDetails();
+
+ return $details['payment_amount'] ?? 0;
+ }
}
diff --git a/src/Controller/ProcessPayPalOrderAction.php b/src/Controller/ProcessPayPalOrderAction.php
index 1945fa88a..47797486c 100644
--- a/src/Controller/ProcessPayPalOrderAction.php
+++ b/src/Controller/ProcessPayPalOrderAction.php
@@ -27,8 +27,10 @@
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\PayPalPlugin\Api\CacheAuthorizeClientApiInterface;
use Sylius\PayPalPlugin\Api\OrderDetailsApiInterface;
+use Sylius\PayPalPlugin\Exception\PaymentAmountMismatchException;
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
use Sylius\PayPalPlugin\Provider\OrderProviderInterface;
+use Sylius\PayPalPlugin\Verifier\PaymentAmountVerifierInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -46,6 +48,7 @@ public function __construct(
private readonly CacheAuthorizeClientApiInterface $authorizeClientApi,
private readonly OrderDetailsApiInterface $orderDetailsApi,
private readonly OrderProviderInterface $orderProvider,
+ private readonly ?PaymentAmountVerifierInterface $paymentAmountVerifier = null,
) {
if ($this->stateMachineFactory instanceof StateMachineFactoryInterface) {
trigger_deprecation(
@@ -58,6 +61,16 @@ public function __construct(
),
);
}
+ if (null === $this->paymentAmountVerifier) {
+ trigger_deprecation(
+ 'sylius/paypal-plugin',
+ '1.6',
+ message: sprintf(
+ 'Not passing $paymentAmountVerifier to "%s" constructor is deprecated and will be prohibited in 3.0',
+ self::class,
+ ),
+ );
+ }
}
public function __invoke(Request $request): Response
@@ -112,6 +125,18 @@ public function __invoke(Request $request): Response
$this->orderManager->flush();
+ try {
+ if ($this->paymentAmountVerifier !== null) {
+ $this->paymentAmountVerifier->verify($payment, $data);
+ } else {
+ $this->verify($payment, $data);
+ }
+ } catch (PaymentAmountMismatchException) {
+ $this->paymentStateManager->cancel($payment);
+
+ return new JsonResponse(['orderID' => $order->getId()]);
+ }
+
$this->paymentStateManager->create($payment);
$this->paymentStateManager->process($payment);
@@ -152,4 +177,30 @@ private function getStateMachine(): StateMachineInterface
return $this->stateMachineFactory;
}
+
+ private function verify(PaymentInterface $payment, array $paypalOrderDetails): void
+ {
+ $totalAmount = $this->getTotalPaymentAmountFromPaypal($paypalOrderDetails);
+
+ if ($payment->getAmount() !== $totalAmount) {
+ throw new PaymentAmountMismatchException();
+ }
+ }
+
+ private function getTotalPaymentAmountFromPaypal(array $paypalOrderDetails): int
+ {
+ if (!isset($paypalOrderDetails['purchase_units']) || !is_array($paypalOrderDetails['purchase_units'])) {
+ return 0;
+ }
+
+ $totalAmount = 0;
+
+ foreach ($paypalOrderDetails['purchase_units'] as $unit) {
+ $stringAmount = $unit['amount']['value'] ?? '0';
+
+ $totalAmount += (int) ($stringAmount * 100);
+ }
+
+ return $totalAmount;
+ }
}
diff --git a/src/Exception/PaymentAmountMismatchException.php b/src/Exception/PaymentAmountMismatchException.php
new file mode 100644
index 000000000..e36f14cb4
--- /dev/null
+++ b/src/Exception/PaymentAmountMismatchException.php
@@ -0,0 +1,22 @@
+<?php
+
+/*
+ * This file is part of the Sylius package.
+ *
+ * (c) Sylius Sp. z o.o.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+declare(strict_types=1);
+
+namespace Sylius\PayPalPlugin\Exception;
+
+final class PaymentAmountMismatchException extends \Exception
+{
+ public function __construct()
+ {
+ parent::__construct('Order payment amount does not match the total amount from PayPal payment.');
+ }
+}
diff --git a/src/Payum/Action/CaptureAction.php b/src/Payum/Action/CaptureAction.php
index f4f2cb685..47806d27b 100644
--- a/src/Payum/Action/CaptureAction.php
+++ b/src/Payum/Action/CaptureAction.php
@@ -60,6 +60,7 @@ public function execute($request): void
'status' => StatusAction::STATUS_CAPTURED,
'paypal_order_id' => $content['id'],
'reference_id' => $referenceId,
+ 'payment_amount' => $payment->getAmount(),
]);
}
}
diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml
index 8385cf0b8..b32c7a7d3 100644
--- a/src/Resources/config/services.xml
+++ b/src/Resources/config/services.xml
@@ -270,5 +270,8 @@
<service id="Sylius\PayPalPlugin\Twig\OrderAddressExtension">
<tag name="twig.extension" />
</service>
+
+ <service id="sylius_paypal.verifier.payment_amount" class="Sylius\PayPalPlugin\Verifier\PaymentAmountVerifier" />
+ <service id="Sylius\PayPalPlugin\Verifier\PaymentAmountVerifierInterface" alias="sylius_paypal.verifier.payment_amount" />
</services>
</container>
diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml
index fe4d94fff..9d0c03acb 100644
--- a/src/Resources/config/services/controller.xml
+++ b/src/Resources/config/services/controller.xml
@@ -114,6 +114,7 @@
<argument type="service" id="Sylius\PayPalPlugin\Api\CacheAuthorizeClientApiInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\OrderDetailsApiInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
+ <argument type="service" id="sylius_paypal.verifier.payment_amount" />
</service>
<service id="Sylius\PayPalPlugin\Controller\UpdatePayPalOrderAction">
@@ -131,6 +132,8 @@
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
<argument type="service" id="sylius_abstraction.state_machine" />
<argument type="service" id="sylius.manager.order" />
+ <argument type="service" id="sylius_paypal.verifier.payment_amount" />
+ <argument type="service" id="sylius.order_processing.order_processor" />
</service>
<service id="Sylius\PayPalPlugin\Controller\PayPalPaymentOnErrorAction">
diff --git a/src/Verifier/PaymentAmountVerifier.php b/src/Verifier/PaymentAmountVerifier.php
new file mode 100644
index 000000000..1d0795c6b
--- /dev/null
+++ b/src/Verifier/PaymentAmountVerifier.php
@@ -0,0 +1,56 @@
+<?php
+
+/*
+ * This file is part of the Sylius package.
+ *
+ * (c) Sylius Sp. z o.o.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+declare(strict_types=1);
+
+namespace Sylius\PayPalPlugin\Verifier;
+
+use Sylius\Component\Core\Model\PaymentInterface;
+use Sylius\PayPalPlugin\Exception\PaymentAmountMismatchException;
+
+final class PaymentAmountVerifier implements PaymentAmountVerifierInterface
+{
+ public function verify(PaymentInterface $payment, array $paypalOrderDetails = []): void
+ {
+ $totalAmount = $this->getTotalPaymentAmountFromPaypal($payment, $paypalOrderDetails);
+
+ if ($payment->getOrder()->getTotal() !== $totalAmount) {
+ throw new PaymentAmountMismatchException();
+ }
+ }
+
+ private function getTotalPaymentAmountFromPaypal(PaymentInterface $payment, array $paypalOrderDetails = []): int
+ {
+ if (empty($paypalOrderDetails)) {
+ return $this->getPaymentAmountFromDetails($payment);
+ }
+
+ if (!isset($paypalOrderDetails['purchase_units']) || !is_array($paypalOrderDetails['purchase_units'])) {
+ return 0;
+ }
+
+ $totalAmount = 0;
+
+ foreach ($paypalOrderDetails['purchase_units'] as $unit) {
+ $stringAmount = $unit['amount']['value'] ?? '0';
+ $totalAmount += (int) ($stringAmount * 100);
+ }
+
+ return $totalAmount;
+ }
+
+ private function getPaymentAmountFromDetails(PaymentInterface $payment): int
+ {
+ $details = $payment->getDetails();
+
+ return $details['payment_amount'] ?? 0;
+ }
+}
diff --git a/src/Verifier/PaymentAmountVerifierInterface.php b/src/Verifier/PaymentAmountVerifierInterface.php
new file mode 100644
index 000000000..31327f241
--- /dev/null
+++ b/src/Verifier/PaymentAmountVerifierInterface.php
@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Sylius package.
+ *
+ * (c) Sylius Sp. z o.o.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+declare(strict_types=1);
+
+namespace Sylius\PayPalPlugin\Verifier;
+
+use Sylius\Component\Core\Model\PaymentInterface;
+
+interface PaymentAmountVerifierInterface
+{
+ public function verify(PaymentInterface $payment, array $paypalOrderDetails = []): void;
+}
diff --git a/tests/Unit/PaymentAmountVerifierTest.php b/tests/Unit/PaymentAmountVerifierTest.php
new file mode 100644
index 000000000..240ff3693
--- /dev/null
+++ b/tests/Unit/PaymentAmountVerifierTest.php
@@ -0,0 +1,133 @@
+<?php
+
+/*
+ * This file is part of the Sylius package.
+ *
+ * (c) Sylius Sp. z o.o.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Sylius\PayPalPlugin\Unit;
+
+use PHPUnit\Framework\TestCase;
+use Sylius\Component\Core\Model\OrderInterface;
+use Sylius\Component\Core\Model\PaymentInterface;
+use Sylius\PayPalPlugin\Exception\PaymentAmountMismatchException;
+use Sylius\PayPalPlugin\Verifier\PaymentAmountVerifier;
+
+final class PaymentAmountVerifierTest extends TestCase
+{
+ private PaymentAmountVerifier $verifier;
+
+ protected function setUp(): void
+ {
+ $this->verifier = new PaymentAmountVerifier();
+ }
+
+ public function testVerifySucceedsWhenAmountsMatch(): void
+ {
+ $payment = $this->createMock(PaymentInterface::class);
+ $order = $this->createMock(OrderInterface::class);
+ $payment->method('getOrder')->willReturn($order);
+ $order->method('getTotal')->willReturn(1500);
+
+ $paypalOrderDetails = [
+ 'purchase_units' => [
+ ['amount' => ['value' => '10.00']],
+ ['amount' => ['value' => '5.00']],
+ ],
+ ];
+
+ $this->verifier->verify($payment, $paypalOrderDetails);
+
+ $this->addToAssertionCount(1);
+ }
+
+ public function testVerifyThrowsExceptionWhenAmountsDoNotMatch(): void
+ {
+ $payment = $this->createMock(PaymentInterface::class);
+ $order = $this->createMock(OrderInterface::class);
+ $payment->method('getOrder')->willReturn($order);
+ $order->method('getTotal')->willReturn(1000);
+
+ $paypalOrderDetails = [
+ 'purchase_units' => [
+ ['amount' => ['value' => '10.00']],
+ ['amount' => ['value' => '5.00']],
+ ],
+ ];
+
+ $this->expectException(PaymentAmountMismatchException::class);
+
+ $this->verifier->verify($payment, $paypalOrderDetails);
+ }
+
+ public function testVerifyWithEmptyPurchaseUnitsShouldCompareToZero(): void
+ {
+ $payment = $this->createMock(PaymentInterface::class);
+ $order = $this->createMock(OrderInterface::class);
+ $payment->method('getOrder')->willReturn($order);
+ $order->method('getTotal')->willReturn(0);
+
+ $paypalOrderDetails = [
+ 'purchase_units' => [],
+ ];
+
+ $this->verifier->verify($payment, $paypalOrderDetails);
+
+ $this->addToAssertionCount(1);
+ }
+
+ public function testVerifyWithMissingPurchaseUnitsShouldCompareToPaymentDetailsAmount(): void
+ {
+ $payment = $this->createMock(PaymentInterface::class);
+ $order = $this->createMock(OrderInterface::class);
+ $payment->method('getOrder')->willReturn($order);
+ $order->method('getTotal')->willReturn(2000);
+ $payment->method('getDetails')->willReturn(['payment_amount' => 2000]);
+
+ $paypalOrderDetails = [];
+
+ $this->verifier->verify($payment, $paypalOrderDetails);
+
+ $this->addToAssertionCount(1);
+ }
+
+ public function testVerifyWithMissingPaymentAmountInDetailsShouldDefaultToZero(): void
+ {
+ $payment = $this->createMock(PaymentInterface::class);
+ $order = $this->createMock(OrderInterface::class);
+ $payment->method('getOrder')->willReturn($order);
+ $order->method('getTotal')->willReturn(0);
+ $payment->method('getDetails')->willReturn([]);
+
+ $paypalOrderDetails = [];
+
+ $this->verifier->verify($payment, $paypalOrderDetails);
+
+ $this->addToAssertionCount(1);
+ }
+
+ public function testVerifyWithMissingAmountInPurchaseUnitShouldDefaultToZero(): void
+ {
+ $payment = $this->createMock(PaymentInterface::class);
+ $order = $this->createMock(OrderInterface::class);
+ $payment->method('getOrder')->willReturn($order);
+ $order->method('getTotal')->willReturn(1000);
+
+ $paypalOrderDetails = [
+ 'purchase_units' => [
+ ['amount' => ['value' => '10.00']],
+ ['amount' => []],
+ ],
+ ];
+
+ $this->verifier->verify($payment, $paypalOrderDetails);
+
+ $this->addToAssertionCount(1);
+ }
+}
|