Files
kupshop/bundles/KupShop/GraphQLBundle/ApiAdmin/Util/OrderPaymentUtil.php
2025-08-02 16:30:27 +02:00

108 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\GraphQLBundle\ApiAdmin\Util;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\OrderPaymentCollection;
use KupShop\GraphQLBundle\ApiAdmin\Types\OrderPayment\Input\OrderPaymentInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\OrderPayment\OrderPayment;
use KupShop\GraphQLBundle\ApiAdmin\Types\OrderPayment\Response\OrderPaymentMutateResponse;
use KupShop\GraphQLBundle\ApiAdmin\Types\Parameters;
use KupShop\GraphQLBundle\ApiShared\ApiUtil;
use KupShop\GraphQLBundle\Exception\GraphQLNotFoundException;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\Database\QueryHint;
use KupShop\KupShopBundle\Util\Entity\EntityUtil;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\OrderingBundle\Entity\OrderPaymentEntity;
use Query\Operator;
readonly class OrderPaymentUtil
{
public function __construct(
private readonly EntityUtil $entityUtil,
private readonly OrderUtil $orderUtil,
) {
}
public function getOrderPayment(int $paymentId): ?OrderPayment
{
$orderPaymentRow = sqlQueryBuilder()
->select('op.*, o.currency')
->addSelect('JSON_EXTRACT(o.note_admin, "$.payment_data") AS order_payment_data')
->from('order_payments', 'op')
->join('op', 'orders', 'o', 'o.id = op.id_order')
->where(Operator::equals(['op.id' => $paymentId]))
->execute()->fetchAssociative();
if (empty($orderPaymentRow)) {
return null;
}
$orderPayment = new OrderPayment($this->entityUtil->createEntity($this->prepareOrderPayment($orderPaymentRow), OrderPaymentEntity::class));
return $orderPayment;
}
public function getOrderPayments(?Parameters $filter = null): ?OrderPaymentCollection
{
$qb = sqlQueryBuilder()
->select('op.*, o.currency')
->addSelect('JSON_EXTRACT(o.note_admin, "$.payment_data") AS order_payment_data')
->from('order_payments', 'op')
->join('op', 'orders', 'o', 'o.id = op.id_order');
if ($filter?->getData()) {
$qb->andWhere(Operator::andX($this->orderUtil->getOrdersFilterSpecs($filter->getData())));
}
$orderPaymentRows = $qb->execute()->fetchAllAssociative();
if (empty($orderPaymentRows)) {
return null;
}
$orderPayments = ApiUtil::wrapItems(
array_map(fn ($paymentRow) => $this->entityUtil->createEntity($this->prepareOrderPayment($paymentRow), OrderPaymentEntity::class), $orderPaymentRows),
OrderPayment::class
);
return new OrderPaymentCollection($orderPayments);
}
private function prepareOrderPayment(array $orderPayment): array
{
$orderPayment['price'] = new Price(toDecimal($orderPayment['price']), Contexts::get(CurrencyContext::class)->getOrDefault($orderPayment['currency']), '0');
$orderPayment['date'] = new \DateTimeImmutable($orderPayment['date']);
$orderPayment['payment_data'] = json_decode($orderPayment['payment_data'] ?? '', true) ?: [];
$orderPayment['order_payment_data'] = json_decode($orderPayment['order_payment_data'] ?? '', true) ?: [];
return $orderPayment;
}
public function orderPaymentCreate(OrderPaymentInput $orderPayment): OrderPaymentMutateResponse
{
$order = new \Order();
if (!$order->createFromDB($orderPayment->orderId)) {
throw new GraphQLNotFoundException('Order not found.');
}
$paymentId = $order->insertPayment(
price: $orderPayment->price,
note: $orderPayment->note,
date: $orderPayment->date,
method: $orderPayment->method ? $orderPayment->method->value : 0,
payment_data: json_encode(['transactionID' => $orderPayment->transactionId])
);
if (!$paymentId) {
throw new GraphQLNotFoundException('Create payment failed.');
}
return QueryHint::withRouteToMaster(fn () => new OrderPaymentMutateResponse(true, $this->getOrderPayment((int) $paymentId)));
}
}