Skip to content

Commit 5613df8

Browse files
authored
[Bug] Fix issue after paypal checkout (#355)
| Q | A | --------------- | ----- | Branch? | 1.6 | Bug fix? | yes | New feature? | no | Related tickets | n/a
2 parents 31e71b0 + 6205237 commit 5613df8

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

spec/Processor/PayPalOrderCompleteProcessorSpec.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
use Sylius\Component\Core\Model\PaymentInterface;
2121
use Sylius\Component\Core\Model\PaymentMethodInterface;
2222
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
23+
use Sylius\PayPalPlugin\Verifier\PaymentAmountVerifierInterface;
2324

2425
final class PayPalOrderCompleteProcessorSpec extends ObjectBehavior
2526
{
26-
function let(PaymentStateManagerInterface $paymentStateManager): void
27-
{
28-
$this->beConstructedWith($paymentStateManager);
27+
function let(
28+
PaymentStateManagerInterface $paymentStateManager,
29+
PaymentAmountVerifierInterface $paymentAmountVerifier,
30+
): void {
31+
$this->beConstructedWith($paymentStateManager, $paymentAmountVerifier);
2932
}
3033

3134
function it_completes_pay_pal_order(
@@ -34,12 +37,14 @@ function it_completes_pay_pal_order(
3437
PaymentInterface $payment,
3538
PaymentMethodInterface $paymentMethod,
3639
GatewayConfigInterface $gatewayConfig,
40+
PaymentAmountVerifierInterface $paymentAmountVerifier,
3741
): void {
3842
$order->getLastPayment(PaymentInterface::STATE_PROCESSING)->willReturn($payment);
3943

4044
$payment->getMethod()->willReturn($paymentMethod);
4145
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
4246
$gatewayConfig->getFactoryName()->willReturn('sylius.pay_pal');
47+
$paymentAmountVerifier->verify($payment)->shouldBeCalled();
4348

4449
$paymentStateManager->complete($payment)->shouldBeCalled();
4550

src/Processor/PayPalOrderCompleteProcessor.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,24 @@
1717
use Sylius\Component\Core\Model\OrderInterface;
1818
use Sylius\Component\Core\Model\PaymentInterface;
1919
use Sylius\Component\Core\Model\PaymentMethodInterface;
20+
use Sylius\PayPalPlugin\Exception\PaymentAmountMismatchException;
2021
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
22+
use Sylius\PayPalPlugin\Verifier\PaymentAmountVerifierInterface;
2123

2224
final 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
}

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132

133133
<service id="Sylius\PayPalPlugin\Processor\PayPalOrderCompleteProcessor" public="true">
134134
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
135+
<argument type="service" id="sylius_paypal.verifier.payment_amount" />
135136
</service>
136137

137138
<service

0 commit comments

Comments
 (0)