Files
Moodle/classes/local/protocol/CheckResponse.php
T

76 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace paygw_moneta\local\protocol;
use InvalidArgumentException;
use paygw_moneta\local\Money;
use paygw_moneta\local\receipt\JsonNumber;
use paygw_moneta\local\receipt\Receipt;
/**
* @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 CheckResponse
{
public function __construct(
private readonly string $accountId,
private readonly string $transactionId,
private readonly Money $amount,
private readonly ResultCode $resultCode,
private readonly Signature $signature,
private readonly string $cms,
) {}
public function getResultCode(): ResultCode
{
return $this->resultCode;
}
/**
* XML без атрибутов — для мерчанта без кассового сервиса.
*/
public function toXml(): CallbackResponse
{
return CallbackResponse::xml(
MntResponseXml::build(
accountId: $this->accountId,
transactionId: $this->transactionId,
resultCode: $this->resultCode,
amount: $this->amount,
signature: $this->sign(),
cms: $this->cms,
),
);
}
/**
* JSON с чеком — для мерчанта с подключённой кассой.
*/
public function toJson(Receipt $receipt): CallbackResponse
{
if (!$receipt->matchesAmount($this->amount)) {
throw new InvalidArgumentException('Сумма позиций чека не совпадает с суммой заказа.');
}
return CallbackResponse::json(JsonNumber::encode([
'id' => $this->accountId,
'transactionId' => $this->transactionId,
'amount' => JsonNumber::placeholder($this->amount),
'signature' => $this->sign(),
'resultCode' => (string) $this->resultCode->value,
'description' => $this->resultCode->description(),
'cms' => $this->cms,
'receipt' => $receipt->toCheckReceipt(),
]));
}
private function sign(): string
{
return $this->signature->forResponse($this->resultCode, $this->accountId, $this->transactionId);
}
}