Skip to content

Commit cff9249

Browse files
[Payment] Incorporate payment query into payment modification controllers
1 parent ecf560f commit cff9249

File tree

8 files changed

+81
-47
lines changed

8 files changed

+81
-47
lines changed

src/Controller/CancelPayPalCheckoutPaymentAction.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,31 @@
1515

1616
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
1717
use Sylius\PayPalPlugin\Provider\PaymentProviderInterface;
18+
use Sylius\PayPalPlugin\Repository\Query\PaypalPaymentQueryInterface;
1819
use Symfony\Component\HttpFoundation\Request;
1920
use Symfony\Component\HttpFoundation\Response;
2021
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
2122

2223
final class CancelPayPalCheckoutPaymentAction
2324
{
24-
private PaymentProviderInterface $paymentProvider;
25-
26-
private PaymentStateManagerInterface $paymentStateManager;
27-
2825
public function __construct(
29-
PaymentProviderInterface $paymentProvider,
30-
PaymentStateManagerInterface $paymentStateManager,
26+
private PaymentProviderInterface $paymentProvider,
27+
private PaymentStateManagerInterface $paymentStateManager,
28+
private ?PaypalPaymentQueryInterface $paypalPaymentQuery = null,
3129
) {
32-
$this->paymentProvider = $paymentProvider;
33-
$this->paymentStateManager = $paymentStateManager;
3430
}
3531

3632
public function __invoke(Request $request): Response
3733
{
38-
/**
39-
* @var string $content
40-
*/
34+
/** @var string $content */
4135
$content = $request->getContent();
42-
4336
$content = (array) json_decode($content, true);
4437

45-
$payment = $this->paymentProvider->getByPayPalOrderId((string) $content['payPalOrderId']);
38+
if (null !== $this->paypalPaymentQuery) {
39+
$payment = $this->paypalPaymentQuery->getForCancellationByOrderId((string) $content['payPalOrderId']);
40+
} else {
41+
$payment = $this->paymentProvider->getByPayPalOrderId((string) $content['payPalOrderId']);
42+
}
4643

4744
/** @var FlashBagInterface $flashBag */
4845
$flashBag = $request->getSession()->getBag('flashes');

src/Controller/CancelPayPalPaymentAction.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Sylius\Component\Payment\PaymentTransitions;
2323
use Sylius\PayPalPlugin\Provider\FlashBagProvider;
2424
use Sylius\PayPalPlugin\Provider\PaymentProviderInterface;
25+
use Sylius\PayPalPlugin\Repository\Query\PaypalPaymentQueryInterface;
2526
use Symfony\Component\HttpFoundation\Request;
2627
use Symfony\Component\HttpFoundation\RequestStack;
2728
use Symfony\Component\HttpFoundation\Response;
@@ -35,6 +36,7 @@ public function __construct(
3536
private readonly FlashBag|RequestStack $flashBagOrRequestStack,
3637
private readonly FactoryInterface|StateMachineInterface $stateMachineFactory,
3738
private readonly OrderProcessorInterface $orderPaymentProcessor,
39+
private readonly ?PaypalPaymentQueryInterface $paypalPaymentQuery = null,
3840
) {
3941
if ($flashBagOrRequestStack instanceof FlashBag) {
4042
trigger_deprecation('sylius/paypal-plugin', '1.5', sprintf('Passing an instance of %s as constructor argument for %s is deprecated as of PayPalPlugin 1.5 and will be removed in 2.0. Pass an instance of %s instead.', FlashBag::class, self::class, RequestStack::class));
@@ -55,13 +57,15 @@ public function __construct(
5557

5658
public function __invoke(Request $request): Response
5759
{
58-
/**
59-
* @var string $content
60-
*/
60+
/** @var string $content */
6161
$content = $request->getContent();
6262
$content = (array) json_decode($content, true);
6363

64-
$payment = $this->paymentProvider->getByPayPalOrderId((string) $content['payPalOrderId']);
64+
if (null !== $this->paypalPaymentQuery) {
65+
$payment = $this->paypalPaymentQuery->getForCancellationByOrderId((string) $content['payPalOrderId']);
66+
} else {
67+
$payment = $this->paymentProvider->getByPayPalOrderId((string) $content['payPalOrderId']);
68+
}
6569

6670
/** @var OrderInterface $order */
6771
$order = $payment->getOrder();

src/Controller/UpdatePayPalOrderAction.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Sylius\PayPalPlugin\Api\OrderDetailsApiInterface;
2424
use Sylius\PayPalPlugin\Api\UpdateOrderApiInterface;
2525
use Sylius\PayPalPlugin\Provider\PaymentProviderInterface;
26+
use Sylius\PayPalPlugin\Repository\Query\PaypalPaymentQueryInterface;
2627
use Symfony\Component\HttpFoundation\JsonResponse;
2728
use Symfony\Component\HttpFoundation\Request;
2829
use Symfony\Component\HttpFoundation\Response;
@@ -36,6 +37,7 @@ public function __construct(
3637
private readonly UpdateOrderApiInterface $updateOrderApi,
3738
private readonly AddressFactoryInterface $addressFactory,
3839
private readonly OrderProcessorInterface $orderProcessor,
40+
private readonly ?PaypalPaymentQueryInterface $paypalPaymentQuery = null,
3941
) {
4042
if (null !== $this->orderDetailsApi) {
4143
trigger_deprecation(
@@ -51,7 +53,11 @@ public function __construct(
5153

5254
public function __invoke(Request $request): Response
5355
{
54-
$payment = $this->paymentProvider->getByPayPalOrderId((string) $request->request->get('orderID'));
56+
if (null !== $this->paypalPaymentQuery) {
57+
$payment = $this->paypalPaymentQuery->getForUpdateByOrderId((string) $request->request->get('orderID'));
58+
} else {
59+
$payment = $this->paymentProvider->getByPayPalOrderId((string) $request->request->get('orderID'));
60+
}
5561
/** @var OrderInterface $order */
5662
$order = $payment->getOrder();
5763

src/Controller/Webhook/RefundOrderAction.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Sylius\PayPalPlugin\Exception\PayPalWrongDataException;
2323
use Sylius\PayPalPlugin\Provider\PaymentProviderInterface;
2424
use Sylius\PayPalPlugin\Provider\PayPalRefundDataProviderInterface;
25+
use Sylius\PayPalPlugin\Repository\Query\PaypalPaymentQueryInterface;
2526
use Symfony\Component\HttpFoundation\JsonResponse;
2627
use Symfony\Component\HttpFoundation\Request;
2728
use Symfony\Component\HttpFoundation\Response;
@@ -34,6 +35,7 @@ public function __construct(
3435
private readonly PaymentProviderInterface $paymentProvider,
3536
private readonly ObjectManager $paymentManager,
3637
private readonly PayPalRefundDataProviderInterface $payPalRefundDataProvider,
38+
private readonly ?PaypalPaymentQueryInterface $paypalPaymentQuery = null,
3739
) {
3840
if ($this->stateMachineFactory instanceof FactoryInterface) {
3941
trigger_deprecation(
@@ -53,7 +55,11 @@ public function __invoke(Request $request): Response
5355
$refundData = $this->payPalRefundDataProvider->provide($this->getPayPalPaymentUrl($request));
5456

5557
try {
56-
$payment = $this->paymentProvider->getByPayPalOrderId((string) $refundData['id']);
58+
if (null !== $this->paypalPaymentQuery) {
59+
$payment = $this->paypalPaymentQuery->getForRefundingByOrderId((string) $refundData['id']);
60+
} else {
61+
$payment = $this->paymentProvider->getByPayPalOrderId((string) $refundData['id']);
62+
}
5763
} catch (PaymentNotFoundException $exception) {
5864
return new JsonResponse(['error' => $exception->getMessage()], Response::HTTP_NOT_FOUND);
5965
}

src/Repository/Query/PaypalPaymentQuery.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
use Sylius\Component\Core\Model\PaymentInterface;
1919
use Sylius\Component\Core\Repository\PaymentRepositoryInterface;
2020
use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension;
21+
use Sylius\PayPalPlugin\Exception\PaymentNotFoundException;
2122

2223
final class PaypalPaymentQuery implements PaypalPaymentQueryInterface
2324
{
2425
public function __construct(
25-
private EntityManagerInterface $entityManager,
26-
private PaymentRepositoryInterface $paymentRepository,
27-
private array $updatableStates = ['cart', 'new', 'processing'],
28-
private array $cancellableStates = ['cart', 'new', 'processing', 'completed'],
29-
private array $refundableStates = ['completed'],
26+
private readonly EntityManagerInterface $entityManager,
27+
private readonly PaymentRepositoryInterface $paymentRepository,
28+
private readonly array $updatableStates = ['cart', 'new', 'processing'],
29+
private readonly array $cancellableStates = ['cart', 'new', 'processing', 'completed'],
30+
private readonly array $refundableStates = ['completed'],
3031
) {
3132
}
3233

3334
public function getForUpdateByOrderId(string $paypalOrderId): ?PaymentInterface
3435
{
35-
$queryBuilder = $this->getPaypalPaymentQueryBuilder('o')
36+
$queryBuilder = $this->getPaypalPaymentQueryBuilder()
3637
->andWhere('o.state IN (:states)')
3738
->setParameter('states', $this->updatableStates)
3839
->addOrderBy('o.createdAt', 'DESC')
@@ -43,7 +44,7 @@ public function getForUpdateByOrderId(string $paypalOrderId): ?PaymentInterface
4344

4445
public function getForCancellationByOrderId(string $paypalOrderId): ?PaymentInterface
4546
{
46-
$queryBuilder = $this->getPaypalPaymentQueryBuilder('o')
47+
$queryBuilder = $this->getPaypalPaymentQueryBuilder()
4748
->andWhere('o.state IN (:states)')
4849
->setParameter('states', $this->cancellableStates)
4950
->addOrderBy('o.createdAt', 'DESC')
@@ -54,7 +55,7 @@ public function getForCancellationByOrderId(string $paypalOrderId): ?PaymentInte
5455

5556
public function getForRefundingByOrderId(string $paypalOrderId): ?PaymentInterface
5657
{
57-
$queryBuilder = $this->getPaypalPaymentQueryBuilder('o')
58+
$queryBuilder = $this->getPaypalPaymentQueryBuilder()
5859
->andWhere('o.state IN (:states)')
5960
->setParameter('states', $this->refundableStates)
6061
->addOrderBy('o.updatedAt', 'DESC')
@@ -66,13 +67,18 @@ public function getForRefundingByOrderId(string $paypalOrderId): ?PaymentInterfa
6667
private function doGetPayment(QueryBuilder $queryBuilder, string $paypalOrderId): ?PaymentInterface
6768
{
6869
if ($this->isCastAvailable()) {
69-
return $queryBuilder
70+
$payment = $queryBuilder
7071
->andWhere('CAST(o.details AS text) LIKE :orderId')
7172
->setParameter('orderId', '%"paypal_order_id":"' . $paypalOrderId . '"%')
7273
->setMaxResults(1)
7374
->getQuery()
7475
->getOneOrNullResult()
7576
;
77+
if (null !== $payment) {
78+
return $payment;
79+
}
80+
81+
throw new PaymentNotFoundException();
7682
}
7783

7884
$payments = $queryBuilder
@@ -87,14 +93,14 @@ private function doGetPayment(QueryBuilder $queryBuilder, string $paypalOrderId)
8793
}
8894
}
8995

90-
return null;
96+
throw new PaymentNotFoundException();
9197
}
9298

93-
private function getPaypalPaymentQueryBuilder(string $alias): QueryBuilder
99+
private function getPaypalPaymentQueryBuilder(): QueryBuilder
94100
{
95101
return $this->paymentRepository
96-
->createQueryBuilder($alias)
97-
->innerJoin($alias . '.method', 'method')
102+
->createQueryBuilder('o')
103+
->innerJoin('o.method', 'method')
98104
->innerJoin('method.gatewayConfig', 'gatewayConfig')
99105
->andWhere('gatewayConfig.factoryName = :factoryName')
100106
->setParameter('factoryName', SyliusPayPalExtension::PAYPAL_FACTORY_NAME)

src/Repository/Query/PaypalPaymentQueryInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
namespace Sylius\PayPalPlugin\Repository\Query;
1515

1616
use Sylius\Component\Core\Model\PaymentInterface;
17+
use Sylius\PayPalPlugin\Exception\PaymentNotFoundException;
1718

1819
interface PaypalPaymentQueryInterface
1920
{
21+
/** @throws PaymentNotFoundException */
2022
public function getForUpdateByOrderId(string $paypalOrderId): ?PaymentInterface;
2123

24+
/** @throws PaymentNotFoundException */
2225
public function getForCancellationByOrderId(string $paypalOrderId): ?PaymentInterface;
2326

27+
/** @throws PaymentNotFoundException */
2428
public function getForRefundingByOrderId(string $paypalOrderId): ?PaymentInterface;
2529
}

src/Resources/config/services/controller.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<argument type="service" id="sylius_paypal.provider.payment" />
2525
<argument type="service" id="sylius.manager.payment" />
2626
<argument type="service" id="sylius_paypal.provider.paypal_refund_data" />
27+
<argument type="service" id="sylius_paypal.repository.query.paypal_payment" />
2728
</service>
2829
<service id="sylius_paypal.controller.webhook.refund_order" alias="Sylius\PayPalPlugin\Controller\Webhook\RefundOrderAction" />
2930

@@ -40,6 +41,7 @@
4041
<argument type="service" id="request_stack" />
4142
<argument type="service" id="sylius_abstraction.state_machine" />
4243
<argument type="service" id="sylius.order_processing.order_payment_processor.checkout" />
44+
<argument type="service" id="sylius_paypal.repository.query.paypal_payment" />
4345
</service>
4446
<service id="sylius_paypal.controller.cancel_paypal_payment" alias="Sylius\PayPalPlugin\Controller\CancelPayPalPaymentAction" />
4547

@@ -54,6 +56,7 @@
5456
<service id="Sylius\PayPalPlugin\Controller\CancelPayPalCheckoutPaymentAction">
5557
<argument type="service" id="sylius_paypal.provider.payment" />
5658
<argument type="service" id="sylius_paypal.manager.payment_state" />
59+
<argument type="service" id="sylius_paypal.repository.query.paypal_payment" />
5760
</service>
5861
<service id="sylius_paypal.controller.cancel_paypal_checkout_payment" alias="Sylius\PayPalPlugin\Controller\CancelPayPalCheckoutPaymentAction" />
5962

@@ -153,6 +156,7 @@
153156
<argument type="service" id="sylius_paypal.api.update_order" />
154157
<argument type="service" id="sylius.factory.address" />
155158
<argument type="service" id="sylius.order_processing.order_processor" />
159+
<argument type="service" id="sylius_paypal.repository.query.paypal_payment" />
156160
</service>
157161
<service id="sylius_paypal.controller.update_paypal_order" alias="Sylius\PayPalPlugin\Controller\UpdatePayPalOrderAction" />
158162

tests/Unit/Repository/Query/PaypalPaymentQueryTest.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Sylius\Component\Core\Model\PaymentInterface;
2424
use Sylius\Component\Core\Repository\PaymentRepositoryInterface;
2525
use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension;
26+
use Sylius\PayPalPlugin\Exception\PaymentNotFoundException;
2627
use Sylius\PayPalPlugin\Repository\Query\PaypalPaymentQuery;
2728

2829
final class PaypalPaymentQueryTest extends TestCase
@@ -101,7 +102,7 @@ public function testGetForUpdateByOrderIdReturnsPaymentWhenCastIsNotAvailable():
101102
$this->assertSame($payment, $result);
102103
}
103104

104-
public function testGetForUpdateByOrderIdReturnsNullWhenNoPaymentFound(): void
105+
public function testGetForUpdateByOrderIdThrowsExceptionWhenNoPaymentFound(): void
105106
{
106107
$paypalOrderId = 'PAYPAL123';
107108

@@ -121,12 +122,12 @@ public function testGetForUpdateByOrderIdReturnsNullWhenNoPaymentFound(): void
121122
$configuration->method('getCustomStringFunction')->with('CAST')->willReturn(null);
122123
$this->entityManager->method('getConfiguration')->willReturn($configuration);
123124

124-
$result = $this->query->getForUpdateByOrderId($paypalOrderId);
125+
$this->expectException(PaymentNotFoundException::class);
125126

126-
$this->assertNull($result);
127+
$this->query->getForUpdateByOrderId($paypalOrderId);
127128
}
128129

129-
public function testGetForUpdateByOrderIdReturnsNullWhenPaymentDoesNotMatchOrderId(): void
130+
public function testGetForUpdateByOrderIdThrowsExceptionWhenPaymentDoesNotMatchOrderId(): void
130131
{
131132
$paypalOrderId = 'PAYPAL123';
132133
$payment = $this->createMock(PaymentInterface::class);
@@ -148,12 +149,12 @@ public function testGetForUpdateByOrderIdReturnsNullWhenPaymentDoesNotMatchOrder
148149
$configuration->method('getCustomStringFunction')->with('CAST')->willReturn(null);
149150
$this->entityManager->method('getConfiguration')->willReturn($configuration);
150151

151-
$result = $this->query->getForUpdateByOrderId($paypalOrderId);
152+
$this->expectException(PaymentNotFoundException::class);
152153

153-
$this->assertNull($result);
154+
$this->query->getForUpdateByOrderId($paypalOrderId);
154155
}
155156

156-
public function testGetForUpdateByOrderIdReturnsNullWhenDetailsDoNotContainPaypalOrderId(): void
157+
public function testGetForUpdateByOrderIdThrowsExceptionWhenDetailsDoNotContainPaypalOrderId(): void
157158
{
158159
$paypalOrderId = 'PAYPAL123';
159160
$payment = $this->createMock(PaymentInterface::class);
@@ -175,9 +176,9 @@ public function testGetForUpdateByOrderIdReturnsNullWhenDetailsDoNotContainPaypa
175176
$configuration->method('getCustomStringFunction')->with('CAST')->willReturn(null);
176177
$this->entityManager->method('getConfiguration')->willReturn($configuration);
177178

178-
$result = $this->query->getForUpdateByOrderId($paypalOrderId);
179+
$this->expectException(PaymentNotFoundException::class);
179180

180-
$this->assertNull($result);
181+
$this->query->getForUpdateByOrderId($paypalOrderId);
181182
}
182183

183184
public function testGetForCancellationByOrderIdReturnsPaymentWithCancellableStates(): void
@@ -234,7 +235,7 @@ public function testGetForRefundingByOrderIdReturnsPaymentWithRefundableStates()
234235
$this->assertSame($payment, $result);
235236
}
236237

237-
public function testGetForCancellationByOrderIdReturnsNullWhenNoPaymentFound(): void
238+
public function testGetForCancellationByOrderIdThrowsExceptionWhenNoPaymentFound(): void
238239
{
239240
$paypalOrderId = 'PAYPAL123';
240241

@@ -254,12 +255,12 @@ public function testGetForCancellationByOrderIdReturnsNullWhenNoPaymentFound():
254255
$configuration->method('getCustomStringFunction')->with('CAST')->willReturn(null);
255256
$this->entityManager->method('getConfiguration')->willReturn($configuration);
256257

257-
$result = $this->query->getForCancellationByOrderId($paypalOrderId);
258+
$this->expectException(PaymentNotFoundException::class);
258259

259-
$this->assertNull($result);
260+
$this->query->getForCancellationByOrderId($paypalOrderId);
260261
}
261262

262-
public function testGetForRefundingByOrderIdReturnsNullWhenNoPaymentFound(): void
263+
public function testGetForRefundingByOrderIdThrowsExceptionWhenNoPaymentFound(): void
263264
{
264265
$paypalOrderId = 'PAYPAL123';
265266

@@ -279,9 +280,9 @@ public function testGetForRefundingByOrderIdReturnsNullWhenNoPaymentFound(): voi
279280
$configuration->method('getCustomStringFunction')->with('CAST')->willReturn(null);
280281
$this->entityManager->method('getConfiguration')->willReturn($configuration);
281282

282-
$result = $this->query->getForRefundingByOrderId($paypalOrderId);
283+
$this->expectException(PaymentNotFoundException::class);
283284

284-
$this->assertNull($result);
285+
$this->query->getForRefundingByOrderId($paypalOrderId);
285286
}
286287

287288
public function testGetForUpdateByOrderIdUsesCorrectStates(): void
@@ -317,6 +318,8 @@ public function testGetForUpdateByOrderIdUsesCorrectStates(): void
317318
$configuration->method('getCustomStringFunction')->with('CAST')->willReturn(null);
318319
$this->entityManager->method('getConfiguration')->willReturn($configuration);
319320

321+
$this->expectException(PaymentNotFoundException::class);
322+
320323
$this->query->getForUpdateByOrderId($paypalOrderId);
321324
}
322325

@@ -353,6 +356,8 @@ public function testGetForCancellationByOrderIdUsesCorrectStates(): void
353356
$configuration->method('getCustomStringFunction')->with('CAST')->willReturn(null);
354357
$this->entityManager->method('getConfiguration')->willReturn($configuration);
355358

359+
$this->expectException(PaymentNotFoundException::class);
360+
356361
$this->query->getForCancellationByOrderId($paypalOrderId);
357362
}
358363

@@ -386,6 +391,8 @@ public function testGetForRefundingByOrderIdUsesCorrectStates(): void
386391
$configuration->method('getCustomStringFunction')->with('CAST')->willReturn(null);
387392
$this->entityManager->method('getConfiguration')->willReturn($configuration);
388393

394+
$this->expectException(PaymentNotFoundException::class);
395+
389396
$this->query->getForRefundingByOrderId($paypalOrderId);
390397
}
391398

0 commit comments

Comments
 (0)