109 lines
2.9 KiB
PHP
109 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MonetaPhpShop\protocol;
|
|
|
|
use MonetaPhpShop\Money;
|
|
|
|
/**
|
|
* Îòâåò íà ïðîâåðî÷íûé çàïðîñ (Check URL).
|
|
*
|
|
* Ôîðìàò âûáèðàåò íàñòðîéêà «Ôèñêàëèçàöèÿ ÷åêîâ»:
|
|
* âûêëþ÷åíà — XML `MNT_RESPONSE` ñ `MNT_ATTRIBUTES`, âêëþ÷åíà — JSON ïî `cmsspecification.pdf` ñ ÷åêîì.
|
|
*/
|
|
final class CheckResponse
|
|
{
|
|
/** @var string */
|
|
private $accountId;
|
|
|
|
/** @var string */
|
|
private $transactionId;
|
|
|
|
/** @var Money|null */
|
|
private $amount;
|
|
|
|
/** @var ResultCode */
|
|
private $resultCode;
|
|
|
|
/** @var Signature */
|
|
private $signature;
|
|
|
|
/** @var string */
|
|
private $cms;
|
|
|
|
/**
|
|
* @param string $transactionId ðîâíî òî, ÷òî ïðèñëàë ñåðâèñ
|
|
* @param Money|null $amount ñóììà çàêàçà; null — çàêàç íå íàéäåí
|
|
*/
|
|
public function __construct(
|
|
string $accountId,
|
|
string $transactionId,
|
|
?Money $amount,
|
|
ResultCode $resultCode,
|
|
Signature $signature,
|
|
string $cms
|
|
) {
|
|
$this->accountId = $accountId;
|
|
$this->transactionId = $transactionId;
|
|
$this->amount = $amount;
|
|
$this->resultCode = $resultCode;
|
|
$this->signature = $signature;
|
|
$this->cms = $cms;
|
|
}
|
|
|
|
public function getResultCode(): ResultCode
|
|
{
|
|
return $this->resultCode;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string> $attributes `INVENTORY`, `CLIENT`, `CUSTOMER`, `PHONE` — êàê ó Pay URL
|
|
*/
|
|
public function toXml(array $attributes = []): CallbackResponse
|
|
{
|
|
return CallbackResponse::xml(
|
|
MntResponseXml::build(
|
|
$this->accountId,
|
|
$this->transactionId,
|
|
$this->resultCode,
|
|
$this->amount,
|
|
$this->sign(),
|
|
$this->cms,
|
|
$attributes,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $receipt îáúåêò `receipt` ñõåìû (`client`, `items`); äåíåæíûå
|
|
* ïîëÿ — ÷åðåç {@see JsonNumber::placeholder()}, ñòðîêè â UTF-8
|
|
*/
|
|
public function toJson(array $receipt): CallbackResponse
|
|
{
|
|
$payload = [
|
|
'id' => $this->accountId,
|
|
'transactionId' => $this->transactionId,
|
|
];
|
|
|
|
if ($this->amount !== null) {
|
|
$payload['amount'] = JsonNumber::placeholder($this->amount);
|
|
}
|
|
|
|
$payload += [
|
|
'signature' => $this->sign(),
|
|
'resultCode' => (string) $this->resultCode->getValue(),
|
|
'description' => $this->resultCode->description(),
|
|
'cms' => $this->cms,
|
|
'receipt' => $receipt,
|
|
];
|
|
|
|
return CallbackResponse::json(JsonNumber::encode($payload));
|
|
}
|
|
|
|
private function sign(): string
|
|
{
|
|
return $this->signature->forResponse($this->resultCode, $this->accountId, $this->transactionId);
|
|
}
|
|
}
|