71 lines
2.5 KiB
PHP
71 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace paygw_moneta\local\callback;
|
|
|
|
use paygw_moneta\local\CmsInfo;
|
|
use paygw_moneta\local\GatewayConfig;
|
|
use paygw_moneta\local\order\Order;
|
|
use paygw_moneta\local\order\RejectionReason;
|
|
use paygw_moneta\local\order\TransactionRepository;
|
|
use paygw_moneta\local\order\TransactionStatus;
|
|
use paygw_moneta\local\protocol\CallbackNotification;
|
|
use paygw_moneta\local\protocol\CallbackResponse;
|
|
use paygw_moneta\local\protocol\CheckResponse;
|
|
use paygw_moneta\local\protocol\ResultCode;
|
|
|
|
/**
|
|
* @package paygw_moneta
|
|
* @copyright 2026 Moneta Labs {@link https://moneta.ru/}
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
final class CheckHandler implements NotificationHandler
|
|
{
|
|
public function __construct(
|
|
private readonly TransactionRepository $repository,
|
|
) {}
|
|
|
|
public function handle(CallbackNotification $notification, Order $order, GatewayConfig $config): CallbackResponse
|
|
{
|
|
if ($order->status->isOpen() && !$config->enabled) {
|
|
$order = $this->repository->markCanceled($order, RejectionReason::GatewayDisabled);
|
|
}
|
|
|
|
$code = match ($order->status) {
|
|
TransactionStatus::Paid => ResultCode::Paid,
|
|
// Failed — оплата у сервиса прошла, не удалась доставка: новую оплату
|
|
// по этому заказу не начинаем, повтор Pay URL её доставит.
|
|
TransactionStatus::Canceled, TransactionStatus::Failed => ResultCode::Rejected,
|
|
TransactionStatus::New, TransactionStatus::Pending => $notification->getAmount() === null
|
|
? ResultCode::WithAmount
|
|
: ResultCode::AwaitingPayment,
|
|
};
|
|
|
|
if ($order->status === TransactionStatus::New) {
|
|
$order = $this->repository->markPending($order);
|
|
}
|
|
|
|
$response = new CheckResponse(
|
|
accountId: $config->accountNumber,
|
|
transactionId: $notification->getTransactionId(),
|
|
amount: $order->amount,
|
|
resultCode: $code,
|
|
signature: $config->signature(),
|
|
cms: CmsInfo::getCmsModuleVersion(),
|
|
);
|
|
|
|
if (!$order->snapshot->fiscalization) {
|
|
return $response->toXml();
|
|
}
|
|
|
|
try {
|
|
return $response->toJson($order->snapshot->receipt);
|
|
} catch (\InvalidArgumentException) {
|
|
$this->repository->noteError($order, RejectionReason::Receipt);
|
|
|
|
return CallbackResponse::fail();
|
|
}
|
|
}
|
|
}
|