|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\PayPalPlugin\Repository\Query; |
| 15 | + |
| 16 | +use Doctrine\ORM\EntityManagerInterface; |
| 17 | +use Doctrine\ORM\QueryBuilder; |
| 18 | +use Sylius\Component\Core\Model\PaymentInterface; |
| 19 | +use Sylius\Component\Core\Repository\PaymentRepositoryInterface; |
| 20 | +use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension; |
| 21 | +use Sylius\PayPalPlugin\Exception\PaymentNotFoundException; |
| 22 | + |
| 23 | +final class PaypalPaymentQuery implements PaypalPaymentQueryInterface |
| 24 | +{ |
| 25 | + public function __construct( |
| 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'], |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function getForUpdateByOrderId(string $paypalOrderId): ?PaymentInterface |
| 35 | + { |
| 36 | + $queryBuilder = $this->getPaypalPaymentQueryBuilder() |
| 37 | + ->andWhere('o.state IN (:states)') |
| 38 | + ->setParameter('states', $this->updatableStates) |
| 39 | + ->addOrderBy('o.createdAt', 'DESC') |
| 40 | + ; |
| 41 | + |
| 42 | + return $this->doGetPayment($queryBuilder, $paypalOrderId); |
| 43 | + } |
| 44 | + |
| 45 | + public function getForCancellationByOrderId(string $paypalOrderId): ?PaymentInterface |
| 46 | + { |
| 47 | + $queryBuilder = $this->getPaypalPaymentQueryBuilder() |
| 48 | + ->andWhere('o.state IN (:states)') |
| 49 | + ->setParameter('states', $this->cancellableStates) |
| 50 | + ->addOrderBy('o.createdAt', 'DESC') |
| 51 | + ; |
| 52 | + |
| 53 | + return $this->doGetPayment($queryBuilder, $paypalOrderId); |
| 54 | + } |
| 55 | + |
| 56 | + public function getForRefundingByOrderId(string $paypalOrderId): ?PaymentInterface |
| 57 | + { |
| 58 | + $queryBuilder = $this->getPaypalPaymentQueryBuilder() |
| 59 | + ->andWhere('o.state IN (:states)') |
| 60 | + ->setParameter('states', $this->refundableStates) |
| 61 | + ->addOrderBy('o.updatedAt', 'DESC') |
| 62 | + ; |
| 63 | + |
| 64 | + return $this->doGetPayment($queryBuilder, $paypalOrderId); |
| 65 | + } |
| 66 | + |
| 67 | + private function doGetPayment(QueryBuilder $queryBuilder, string $paypalOrderId): ?PaymentInterface |
| 68 | + { |
| 69 | + if ($this->isCastAvailable()) { |
| 70 | + $payment = $queryBuilder |
| 71 | + ->andWhere('CAST(o.details AS text) LIKE :orderId') |
| 72 | + ->setParameter('orderId', '%"' . $paypalOrderId . '"%') |
| 73 | + ->setMaxResults(1) |
| 74 | + ->getQuery() |
| 75 | + ->getOneOrNullResult() |
| 76 | + ; |
| 77 | + if (null !== $payment) { |
| 78 | + return $payment; |
| 79 | + } |
| 80 | + |
| 81 | + throw new PaymentNotFoundException(); |
| 82 | + } |
| 83 | + |
| 84 | + $payments = $queryBuilder |
| 85 | + ->getQuery() |
| 86 | + ->toIterable() |
| 87 | + ; |
| 88 | + |
| 89 | + foreach ($payments as $payment) { |
| 90 | + $details = $payment->getDetails(); |
| 91 | + if (isset($details['paypal_order_id']) && $details['paypal_order_id'] === $paypalOrderId) { |
| 92 | + return $payment; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + throw new PaymentNotFoundException(); |
| 97 | + } |
| 98 | + |
| 99 | + private function getPaypalPaymentQueryBuilder(): QueryBuilder |
| 100 | + { |
| 101 | + return $this->paymentRepository |
| 102 | + ->createQueryBuilder('o') |
| 103 | + ->innerJoin('o.method', 'method') |
| 104 | + ->innerJoin('method.gatewayConfig', 'gatewayConfig') |
| 105 | + ->andWhere('gatewayConfig.factoryName = :factoryName') |
| 106 | + ->setParameter('factoryName', SyliusPayPalExtension::PAYPAL_FACTORY_NAME) |
| 107 | + ; |
| 108 | + } |
| 109 | + |
| 110 | + private function isCastAvailable(): bool |
| 111 | + { |
| 112 | + return null !== $this->entityManager->getConfiguration()->getCustomStringFunction('CAST'); |
| 113 | + } |
| 114 | +} |
0 commit comments