1717use Sylius \Component \Core \Model \OrderInterface ;
1818use Sylius \Component \Core \Model \PaymentInterface ;
1919use Sylius \Component \Core \Model \PaymentMethodInterface ;
20+ use Sylius \PayPalPlugin \Exception \PaymentAmountMismatchException ;
2021use Sylius \PayPalPlugin \Manager \PaymentStateManagerInterface ;
22+ use Sylius \PayPalPlugin \Verifier \PaymentAmountVerifierInterface ;
2123
2224final class PayPalOrderCompleteProcessor
2325{
24- private PaymentStateManagerInterface $ paymentStateManager ;
25-
26- public function __construct (PaymentStateManagerInterface $ paymentStateManager )
27- {
28- $ this ->paymentStateManager = $ paymentStateManager ;
26+ public function __construct (
27+ private PaymentStateManagerInterface $ paymentStateManager ,
28+ private ?PaymentAmountVerifierInterface $ paymentAmountVerifier = null ,
29+ ) {
30+ if (null === $ this ->paymentAmountVerifier ) {
31+ trigger_deprecation (
32+ 'sylius/paypal-plugin ' ,
33+ '1.6 ' ,
34+ 'Not passing an instance of "%s" as the second argument is deprecated and will be prohibited in 3.0. ' ,
35+ PaymentAmountVerifierInterface::class,
36+ );
37+ }
2938 }
3039
3140 public function completePayPalOrder (OrderInterface $ order ): void
@@ -44,6 +53,34 @@ public function completePayPalOrder(OrderInterface $order): void
4453 return ;
4554 }
4655
56+ try {
57+ if (null !== $ this ->paymentAmountVerifier ) {
58+ $ this ->paymentAmountVerifier ->verify ($ payment );
59+ } else {
60+ $ this ->verify ($ payment );
61+ }
62+ } catch (PaymentAmountMismatchException ) {
63+ $ this ->paymentStateManager ->cancel ($ payment );
64+
65+ return ;
66+ }
67+
4768 $ this ->paymentStateManager ->complete ($ payment );
4869 }
70+
71+ private function verify (PaymentInterface $ payment ): void
72+ {
73+ $ totalAmount = $ this ->getTotalPaymentAmountFromPaypal ($ payment );
74+
75+ if ($ payment ->getOrder ()->getTotal () !== $ totalAmount ) {
76+ throw new PaymentAmountMismatchException ();
77+ }
78+ }
79+
80+ private function getTotalPaymentAmountFromPaypal (PaymentInterface $ payment ): int
81+ {
82+ $ details = $ payment ->getDetails ();
83+
84+ return $ details ['payment_amount ' ] ?? 0 ;
85+ }
4986}
0 commit comments