Files
alfa-bank-sdk/src/Sbp/Requests/Reversal/QrcReversalRequest.php

78 lines
3.9 KiB
PHP

<?php
/*
// MODULE_CONTRACT: Represents the SBP API call to initiate a QR Code reversal.
// RESPONSIBILITY: To define the POST request for reversing a payment made via QR code.
// INPUTS: QR Code reversal request data (TermNo, currency, qrcId, payrrn, etc.).
// OUTPUTS: QrcReversalResponseData (DTO).
*/
namespace Apoloz\AlfaBank\Sbp\Requests\Reversal;
// <SECTION name="module_imports" intent="Import DTOs and Saloon components for a JSON body request.">
// <BELIEF_STATE "This POST request requires specific DTOs for its JSON body and its response, along with Saloon's body-handling traits.">
use Apoloz\AlfaBank\Sbp\Data\Reversal\QrcReversalRequestData;
use Apoloz\AlfaBank\Sbp\Data\Reversal\QrcReversalResponseData;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;
// </SECTION>
// <SECTION name="class_definition" intent="Define the request to initiate a QR Code reversal.">
// <BELIEF_STATE "This class encapsulates the logic for initiating an SBP reversal, which is a critical operation in payment systems.">
class QrcReversalRequest extends Request implements HasBody
{
use HasJsonBody;
// <SECTION name="http_method" intent="Declare the HTTP verb.">
// <BELIEF_STATE "Initiating a new transaction or action that changes the system state (like a reversal) is correctly represented by a POST request.">
protected Method $method = Method::POST;
// </SECTION>
// <SECTION name="constructor" intent="Inject the DTO containing the QR Code reversal request data.">
// <BELIEF_STATE "Using a strongly-typed DTO (`QrcReversalRequestData`) ensures that all necessary information for the reversal is provided and correctly structured.">
public function __construct(
protected QrcReversalRequestData $data
) {}
// <SECTION name="endpoint_resolver" intent="Define the endpoint for SBP reversal requests.">
// <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="body_builder" intent="Construct the JSON body for the request.">
// <BELIEF_STATE "The `command` field specifies the operation to the SBP API. Other fields from the DTO are conditionally added, ensuring the payload precisely matches the API specification.">
protected function defaultBody(): array
{
$body = [
'command' => 'QRCreversal',
'TermNo' => $this->data->TermNo,
'currency' => $this->data->currency,
];
if ($this->data->qrcId !== null) $body['qrcId'] = $this->data->qrcId;
if ($this->data->payrrn !== null) $body['payrrn'] = $this->data->payrrn;
if ($this->data->trxId !== null) $body['trxId'] = $this->data->trxId;
if ($this->data->trxDT !== null) $body['trxDT'] = $this->data->trxDT;
if ($this->data->amount !== null) $body['amount'] = $this->data->amount;
if ($this->data->messageID !== null) $body['messageID'] = $this->data->messageID;
if ($this->data->notificationUrl !== null) $body['queryData'] = ['notificationUrl' => $this->data->notificationUrl];
if ($this->data->ReturnRestAmount !== null) $body['ReturnRestAmount'] = $this->data->ReturnRestAmount;
return $body;
}
// </SECTION>
// <SECTION name="response_caster" intent="Map the JSON response to a DTO.">
// <BELIEF_STATE "Casting the response to a `QrcReversalResponseData` DTO provides a type-safe and structured object for the result of the reversal.">
public function createDtoFromResponse(Response $response): QrcReversalResponseData
{
return QrcReversalResponseData::from($response->json());
}
// </SECTION>
}