1818use Sylius \Component \Core \Model \PaymentInterface ;
1919use Sylius \Component \Core \Repository \PaymentRepositoryInterface ;
2020use Sylius \PayPalPlugin \DependencyInjection \SyliusPayPalExtension ;
21+ use Sylius \PayPalPlugin \Exception \PaymentNotFoundException ;
2122
2223final 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 )
0 commit comments