1313
1414namespace Sylius \PayPalPlugin \Repository \Query ;
1515
16+ use Doctrine \DBAL \Platforms \PostgreSQLPlatform ;
1617use Doctrine \ORM \EntityManagerInterface ;
1718use Doctrine \ORM \QueryBuilder ;
1819use Sylius \Component \Core \Model \PaymentInterface ;
1920use Sylius \Component \Core \Repository \PaymentRepositoryInterface ;
2021use Sylius \PayPalPlugin \DependencyInjection \SyliusPayPalExtension ;
22+ use Sylius \PayPalPlugin \Exception \PaymentNotFoundException ;
2123
2224final class PaypalPaymentQuery implements PaypalPaymentQueryInterface
2325{
2426 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 ' ],
27+ private readonly EntityManagerInterface $ entityManager ,
28+ private readonly PaymentRepositoryInterface $ paymentRepository ,
29+ private readonly array $ updatableStates = ['cart ' , 'new ' , 'processing ' ],
30+ private readonly array $ cancellableStates = ['cart ' , 'new ' , 'processing ' , 'completed ' ],
31+ private readonly array $ refundableStates = ['completed ' ],
3032 ) {
3133 }
3234
3335 public function getForUpdateByOrderId (string $ paypalOrderId ): ?PaymentInterface
3436 {
35- $ queryBuilder = $ this ->getPaypalPaymentQueryBuilder (' o ' )
37+ $ queryBuilder = $ this ->getPaypalPaymentQueryBuilder ()
3638 ->andWhere ('o.state IN (:states) ' )
3739 ->setParameter ('states ' , $ this ->updatableStates )
3840 ->addOrderBy ('o.createdAt ' , 'DESC ' )
@@ -43,7 +45,7 @@ public function getForUpdateByOrderId(string $paypalOrderId): ?PaymentInterface
4345
4446 public function getForCancellationByOrderId (string $ paypalOrderId ): ?PaymentInterface
4547 {
46- $ queryBuilder = $ this ->getPaypalPaymentQueryBuilder (' o ' )
48+ $ queryBuilder = $ this ->getPaypalPaymentQueryBuilder ()
4749 ->andWhere ('o.state IN (:states) ' )
4850 ->setParameter ('states ' , $ this ->cancellableStates )
4951 ->addOrderBy ('o.createdAt ' , 'DESC ' )
@@ -54,7 +56,7 @@ public function getForCancellationByOrderId(string $paypalOrderId): ?PaymentInte
5456
5557 public function getForRefundingByOrderId (string $ paypalOrderId ): ?PaymentInterface
5658 {
57- $ queryBuilder = $ this ->getPaypalPaymentQueryBuilder (' o ' )
59+ $ queryBuilder = $ this ->getPaypalPaymentQueryBuilder ()
5860 ->andWhere ('o.state IN (:states) ' )
5961 ->setParameter ('states ' , $ this ->refundableStates )
6062 ->addOrderBy ('o.updatedAt ' , 'DESC ' )
@@ -66,13 +68,18 @@ public function getForRefundingByOrderId(string $paypalOrderId): ?PaymentInterfa
6668 private function doGetPayment (QueryBuilder $ queryBuilder , string $ paypalOrderId ): ?PaymentInterface
6769 {
6870 if ($ this ->isCastAvailable ()) {
69- return $ queryBuilder
71+ $ payment = $ queryBuilder
7072 ->andWhere ('CAST(o.details AS text) LIKE :orderId ' )
71- ->setParameter ('orderId ' , '%"paypal_order_id":" ' . $ paypalOrderId . '"% ' )
73+ ->setParameter ('orderId ' , '%" ' . $ paypalOrderId . '"% ' )
7274 ->setMaxResults (1 )
7375 ->getQuery ()
7476 ->getOneOrNullResult ()
7577 ;
78+ if (null !== $ payment ) {
79+ return $ payment ;
80+ }
81+
82+ throw new PaymentNotFoundException ();
7683 }
7784
7885 $ payments = $ queryBuilder
@@ -87,14 +94,14 @@ private function doGetPayment(QueryBuilder $queryBuilder, string $paypalOrderId)
8794 }
8895 }
8996
90- return null ;
97+ throw new PaymentNotFoundException () ;
9198 }
9299
93- private function getPaypalPaymentQueryBuilder (string $ alias ): QueryBuilder
100+ private function getPaypalPaymentQueryBuilder (): QueryBuilder
94101 {
95102 return $ this ->paymentRepository
96- ->createQueryBuilder ($ alias )
97- ->innerJoin ($ alias . ' .method ' , 'method ' )
103+ ->createQueryBuilder (' o ' )
104+ ->innerJoin (' o .method ' , 'method ' )
98105 ->innerJoin ('method.gatewayConfig ' , 'gatewayConfig ' )
99106 ->andWhere ('gatewayConfig.factoryName = :factoryName ' )
100107 ->setParameter ('factoryName ' , SyliusPayPalExtension::PAYPAL_FACTORY_NAME )
0 commit comments