Skip to content

Commit 5354ebc

Browse files
authored
[UPMERGE] 1.6 -> 1.7 (Sylius#356)
This PR has been generated automatically. For more details see [upmerge_pr.yaml](/Sylius/PayPalPlugin/blob/1.7/.github/workflows/upmerge_pr.yaml). **Remember!** The upmerge should always be merged with using `Merge pull request` button. In case of conflicts, please resolve them manually with usign the following commands: ``` git fetch upstream gh pr checkout <this-pr-number> git merge upstream/1.7 -m "Resolve conflicts between 1.6 and 1.7" ``` If you use other name for the upstream remote, please replace `upstream` with the name of your remote pointing to the `Sylius/PayPalPlugin` repository. Once the conflicts are resolved, please run `git merge --continue` and push the changes to this PR.
2 parents 4b1347a + 6c68cd3 commit 5354ebc

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
@@ -18,15 +18,24 @@
1818
use Sylius\Component\Core\Model\PaymentInterface;
1919
use Sylius\Component\Core\Model\PaymentMethodInterface;
2020
use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension;
21+
use Sylius\PayPalPlugin\Exception\PaymentAmountMismatchException;
2122
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
23+
use Sylius\PayPalPlugin\Verifier\PaymentAmountVerifierInterface;
2224

2325
final class PayPalOrderCompleteProcessor
2426
{
25-
private PaymentStateManagerInterface $paymentStateManager;
26-
27-
public function __construct(PaymentStateManagerInterface $paymentStateManager)
28-
{
29-
$this->paymentStateManager = $paymentStateManager;
27+
public function __construct(
28+
private PaymentStateManagerInterface $paymentStateManager,
29+
private ?PaymentAmountVerifierInterface $paymentAmountVerifier = null,
30+
) {
31+
if (null === $this->paymentAmountVerifier) {
32+
trigger_deprecation(
33+
'sylius/paypal-plugin',
34+
'1.6',
35+
'Not passing an instance of "%s" as the second argument is deprecated and will be prohibited in 3.0.',
36+
PaymentAmountVerifierInterface::class,
37+
);
38+
}
3039
}
3140

3241
public function completePayPalOrder(OrderInterface $order): void
@@ -45,6 +54,34 @@ public function completePayPalOrder(OrderInterface $order): void
4554
return;
4655
}
4756

57+
try {
58+
if (null !== $this->paymentAmountVerifier) {
59+
$this->paymentAmountVerifier->verify($payment);
60+
} else {
61+
$this->verify($payment);
62+
}
63+
} catch (PaymentAmountMismatchException) {
64+
$this->paymentStateManager->cancel($payment);
65+
66+
return;
67+
}
68+
4869
$this->paymentStateManager->complete($payment);
4970
}
71+
72+
private function verify(PaymentInterface $payment): void
73+
{
74+
$totalAmount = $this->getTotalPaymentAmountFromPaypal($payment);
75+
76+
if ($payment->getOrder()->getTotal() !== $totalAmount) {
77+
throw new PaymentAmountMismatchException();
78+
}
79+
}
80+
81+
private function getTotalPaymentAmountFromPaypal(PaymentInterface $payment): int
82+
{
83+
$details = $payment->getDetails();
84+
85+
return $details['payment_amount'] ?? 0;
86+
}
5087
}

src/Resources/config/services.xml

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

159159
<service id="Sylius\PayPalPlugin\Processor\PayPalOrderCompleteProcessor" public="true">
160160
<argument type="service" id="sylius_paypal.manager.payment_state" />
161+
<argument type="service" id="sylius_paypal.verifier.payment_amount" />
161162
</service>
162163
<service id="sylius_paypal.processor.paypal_order_complete" alias="Sylius\PayPalPlugin\Processor\PayPalOrderCompleteProcessor" />
163164

0 commit comments

Comments
 (0)