Files
alfa-bank-sdk/src/Sbp/Requests/CashLink/ActivateCashQrRequest.php
Anatoliy Poloz b7b2a8fd61 refactor(Sbp)
2026-03-17 12:26:30 +03:00

90 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
/*
// MODULE_CONTRACT: Represents the SBP API call to activate a CashLink QR Code.
// RESPONSIBILITY: To define the POST request for making a previously registered CashLink QR Code active and scannable.
// INPUTS: ActivateCashQrRequestData (DTO) containing QR Code activation parameters.
// OUTPUTS: Raw array response from the API.
*/
namespace Apoloz\AlfaBank\Sbp\Requests\CashLink;
// <SECTION name="module_imports" intent="Import DTO and Saloon components for a JSON body request.">
// <BELIEF_STATE "This POST request requires the `ActivateCashQrRequestData` DTO for its JSON body and utilizes `HasBody` and `HasJsonBody` for proper payload serialization. It currently returns a raw array response.">
use Apoloz\AlfaBank\Sbp\Data\CashLink\ActivateCashQrRequestData;
use Apoloz\AlfaBank\Utils\CmsSigner;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasStringBody;
// </SECTION>
// <SECTION name="class_definition" intent="Define the request to activate a CashLink QR Code.">
// <BELIEF_STATE "This class encapsulates the logic for activating a CashLink QR Code, a key management operation in SBP payment systems.">
class ActivateCashQrRequest extends Request implements HasBody
{
use HasStringBody;
// <SECTION name="http_method" intent="Declare the HTTP verb.">
// <BELIEF_STATE "Activating a resource is an action that changes the system state, so POST is the correct verb.">
protected Method $method = Method::POST;
// </SECTION>
// <SECTION name="constructor" intent="Inject the DTO containing the QR Code activation data.">
// <BELIEF_STATE "Using a strongly-typed DTO (`ActivateCashQrRequestData`) ensures that all necessary information for QR Code activation is provided and correctly structured.">
public function __construct(
protected ActivateCashQrRequestData $data
) {}
// <SECTION name="endpoint_resolver" intent="Define the endpoint for SBP CashLink QR Code activation.">
// <BELIEF_STATE "The SBP API often uses generic endpoints with a 'command' field in the body, implying the endpoint itself might be empty or a base SBP URL.">
public function resolveEndpoint(): string
{
return ''; // Endpoint is determined by SbpConnector's base URL
}
// </SECTION>
// <SECTION name="response_caster" intent="Return the raw JSON response as an array.">
// <BELIEF_STATE "For this specific request, the response is currently returned as a raw array. A dedicated DTO could be created if a structured object is required.">
public function createDtoFromResponse(Response $response): array
{
return $response->json();
}
// </SECTION>
// <SECTION name="body_builder" intent="Construct the JSON body for the request.">
// <BELIEF_STATE "The `command` field specifies the operation to the SBP API. Fields from the DTO are mapped, including conditional parameters for order number, payment purpose, QR TTL, and notification URLs.">
protected function defaultBody(): string
{
$bodyArray = [
'command' => 'ActivateCashQRc',
'TermNo' => $this->data->TermNo,
'qrcId' => $this->data->qrcId,
'amount' => $this->data->amount,
'currency' => $this->data->currency,
];
if ($this->data->OrderNumber !== null) {
$bodyArray['OrderNumber'] = $this->data->OrderNumber;
}
if ($this->data->paymentPurpose !== null) {
$bodyArray['paymentPurpose'] = $this->data->paymentPurpose;
}
if ($this->data->qrTtl !== null) {
$bodyArray['qrTtl'] = $this->data->qrTtl;
}
if ($this->data->redirectUrl !== null) {
$bodyArray['redirectUrl'] = $this->data->redirectUrl;
}
if ($this->data->notificationUrl !== null) {
$bodyArray['queryData'] = ['notificationUrl' => $this->data->notificationUrl];
}
return CmsSigner::canonicalize($bodyArray);
}
// </SECTION>
}