diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b9083e99..b2c22c30 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: [7.4] + php-versions: [8.2] steps: - name: Checkout repository uses: actions/checkout@v2 diff --git a/README.md b/README.md index e7772cda..ffe5e286 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ## Requisitos: -- PHP 7.0+ +- PHP 8.2+ ## Dependencias Para utilizar este SDK, debes tener las siguientes extensiones de PHP instaladas: diff --git a/composer.json b/composer.json index bf4ebcdd..4868bdea 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Transbank SDK", "keywords": ["payments", "transbank", "api", "sdk"], "require": { - "php": ">=7.0", + "php": ">=8.2", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", diff --git a/src/Contracts/HttpClientInterface.php b/src/Contracts/HttpClientInterface.php index 5723dc01..5c9a7eef 100644 --- a/src/Contracts/HttpClientInterface.php +++ b/src/Contracts/HttpClientInterface.php @@ -2,7 +2,25 @@ namespace Transbank\Contracts; +use Psr\Http\Message\ResponseInterface; +use GuzzleHttp\Exception\GuzzleException; + interface HttpClientInterface { - public function request($method, $url, $payload = [], $options = null); + /** + * @param string $method + * @param string $url + * @param array|null $payload + * @param array|null $options + * + * @throws GuzzleException + * + * @return ResponseInterface + */ + public function request( + string $method, + string $url, + array|null $payload = [], + array|null $options = null + ): ResponseInterface; } diff --git a/src/Contracts/RequestService.php b/src/Contracts/RequestService.php index 0b0485f5..0a06a226 100644 --- a/src/Contracts/RequestService.php +++ b/src/Contracts/RequestService.php @@ -3,18 +3,28 @@ namespace Transbank\Contracts; use Transbank\Webpay\Options; +use Transbank\Webpay\Exceptions\WebpayRequestException; +use Psr\Http\Message\ResponseInterface; +use Transbank\Webpay\Exceptions\TransbankApiRequest; interface RequestService { /** - * @param $method - * @param $endpoint - * @param $payload + * @param string $method + * @param string $endpoint + * @param array $payload * @param Options $options * - * @throws Transbank\Webpay\Exceptions\WebpayRequestException + * @throws WebpayRequestException * * @return array Response from the API as json. */ - public function request($method, $endpoint, $payload, Options $options); + public function request( + string $method, + string $endpoint, + array $payload, + Options $options + ): array; + public function getLastResponse(): ResponseInterface|null; + public function getLastRequest(): TransbankApiRequest|null; } diff --git a/src/PatpassComercio/Inscription.php b/src/PatpassComercio/Inscription.php index 6284c30a..bea2c1df 100644 --- a/src/PatpassComercio/Inscription.php +++ b/src/PatpassComercio/Inscription.php @@ -17,6 +17,7 @@ use Transbank\Utils\RequestServiceTrait; use Transbank\Contracts\RequestService; use Transbank\PatpassComercio\Options; +use GuzzleHttp\Exception\GuzzleException; class Inscription { @@ -27,63 +28,62 @@ class Inscription /** * @var Options */ - protected $options; + protected Options $options; /** * Transaction constructor. * - * @param Options $options - * @param RequestService |null $requestService + * @param Options $options + * @param RequestService|null $requestService */ public function __construct( Options $options, - RequestService $requestService = null + RequestService|null $requestService = null ) { $this->options = $options; - $this->setRequestService($requestService !== null ? $requestService : new HttpClientRequestService()); } /** - * @param $url - * @param $name - * @param $lastName - * @param $secondLastName - * @param $rut - * @param $serviceId - * @param $finalUrl - * @param $maxAmount - * @param $phone - * @param $cellPhone - * @param $patpassName - * @param $personEmail - * @param $commerceEmail - * @param $address - * @param $city + * @param string $url + * @param string $name + * @param string $lastName + * @param string $secondLastName + * @param string $rut + * @param string $serviceId + * @param string $finalUrl + * @param string $maxAmount + * @param string $phone + * @param string $cellPhone + * @param string $patpassName + * @param string $personEmail + * @param string $commerceEmail + * @param string $address + * @param string $city * * @throws InscriptionStartException - * @throws GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return InscriptionStartResponse */ public function start( - $url, - $name, - $lastName, - $secondLastName, - $rut, - $serviceId, - $finalUrl, - $maxAmount, - $phone, - $cellPhone, - $patpassName, - $personEmail, - $commerceEmail, - $address, - $city - ) { + string $url, + string $name, + string $lastName, + string $secondLastName, + string $rut, + string $serviceId, + string $finalUrl, + string $maxAmount, + string $phone, + string $cellPhone, + string $patpassName, + string $personEmail, + string $commerceEmail, + string $address, + string $city + ): InscriptionStartResponse { $payload = [ 'url' => $url, 'nombre' => $name, @@ -120,15 +120,14 @@ public function start( } /** - * @param $token - * @param null $options + * @param string $token * * @throws InscriptionStatusException - * @throws GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return InscriptionStatusResponse */ - public function status($token) + public function status(string $token): InscriptionStatusResponse { $payload = [ 'token' => $token, @@ -154,15 +153,17 @@ public function status($token) /** * @return Options */ - public function getOptions() + public function getOptions(): Options { return $this->options; } /** * @param Options $options + * + * @return void */ - public function setOptions(Options $options) + public function setOptions(Options $options): void { $this->options = $options; } @@ -170,30 +171,30 @@ public function setOptions(Options $options) /** * @return string */ - protected function getBaseUrl() + protected function getBaseUrl(): string { return $this->getOptions()->getApiBaseUrl(); } /** - * @param $commerceCode - * @param $apiKey + * @param string $commerceCode + * @param string $apiKey * - * @return $this + * @return self */ - public static function buildForIntegration($commerceCode, $apiKey) + public static function buildForIntegration(string $commerceCode, string $apiKey): self { - return new static(new Options($apiKey, $commerceCode, Options::ENVIRONMENT_INTEGRATION)); + return new self(new Options($apiKey, $commerceCode, Options::ENVIRONMENT_INTEGRATION)); } /** - * @param $commerceCode - * @param $apiKey + * @param string $commerceCode + * @param string $apiKey * - * @return $this + * @return self */ - public static function buildForProduction($commerceCode, $apiKey) + public static function buildForProduction(string $commerceCode, string $apiKey): self { - return new static(new Options($apiKey, $commerceCode, Options::ENVIRONMENT_PRODUCTION)); + return new self(new Options($apiKey, $commerceCode, Options::ENVIRONMENT_PRODUCTION)); } } diff --git a/src/PatpassComercio/Options.php b/src/PatpassComercio/Options.php index c3818842..20a33bb8 100644 --- a/src/PatpassComercio/Options.php +++ b/src/PatpassComercio/Options.php @@ -2,10 +2,12 @@ namespace Transbank\PatpassComercio; +use Transbank\Webpay\Options as WebpayOptions; + /** * Class Options. */ -class Options extends \Transbank\Webpay\Options +class Options extends WebpayOptions { const BASE_URL_PRODUCTION = 'https://www.pagoautomaticocontarjetas.cl/'; const BASE_URL_INTEGRATION = 'https://pagoautomaticocontarjetasint.transbank.cl/'; @@ -13,7 +15,7 @@ class Options extends \Transbank\Webpay\Options /** * @return array */ - public function getHeaders() + public function getHeaders(): array { return [ 'commercecode' => $this->getCommerceCode(), diff --git a/src/PatpassComercio/PatpassComercio.php b/src/PatpassComercio/PatpassComercio.php index 7e4e5c1b..e44e19c0 100644 --- a/src/PatpassComercio/PatpassComercio.php +++ b/src/PatpassComercio/PatpassComercio.php @@ -6,11 +6,11 @@ * @category */ - namespace Transbank\PatpassComercio; +namespace Transbank\PatpassComercio; class PatpassComercio { - const INTEGRATION_API_KEY = 'cxxXQgGD9vrVe4M41FIt'; - const INTEGRATION_COMMERCE_CODE = '28299257'; + public const INTEGRATION_API_KEY = 'cxxXQgGD9vrVe4M41FIt'; + public const INTEGRATION_COMMERCE_CODE = '28299257'; } diff --git a/src/PatpassComercio/Responses/InscriptionFinishResponse.php b/src/PatpassComercio/Responses/InscriptionFinishResponse.php index 4e99b6c0..0e172615 100644 --- a/src/PatpassComercio/Responses/InscriptionFinishResponse.php +++ b/src/PatpassComercio/Responses/InscriptionFinishResponse.php @@ -10,12 +10,13 @@ class InscriptionFinishResponse { - public $status; - public $code; + public string|null $status; + public int $code; - public function __construct($httpCode) + public function __construct(int $httpCode) { $this->code = $httpCode; + $this->status = null; if ($httpCode == 204) { $this->status = 'OK'; @@ -26,19 +27,18 @@ public function __construct($httpCode) } /** - * @return mixed + * @return string|null */ - public function getStatus() + public function getStatus(): string|null { return $this->status; } /** - * @return mixed + * @return int */ - public function getCode() + public function getCode(): int { return $this->code; } - } diff --git a/src/PatpassComercio/Responses/InscriptionStartResponse.php b/src/PatpassComercio/Responses/InscriptionStartResponse.php index 1681c781..7fbd5631 100644 --- a/src/PatpassComercio/Responses/InscriptionStartResponse.php +++ b/src/PatpassComercio/Responses/InscriptionStartResponse.php @@ -12,34 +12,33 @@ class InscriptionStartResponse { - public $token; - public $urlWebpay; + public string|null $token; + public string|null $urlWebpay; /** * InscriptionStartResponse constructor. * - * @param $token + * @param array $json */ - public function __construct($json) + public function __construct(array $json) { $this->token = Utils::returnValueIfExists($json, 'token'); $this->urlWebpay = Utils::returnValueIfExists($json, 'url'); } /** - * @return mixed + * @return string|null */ - public function getToken() + public function getToken(): string|null { return $this->token; } /** - * @return mixed + * @return string|null */ - public function getUrlWebpay() + public function getUrlWebpay(): string|null { return $this->urlWebpay; } - } diff --git a/src/PatpassComercio/Responses/InscriptionStatusResponse.php b/src/PatpassComercio/Responses/InscriptionStatusResponse.php index 7d7bb9ba..516e2047 100644 --- a/src/PatpassComercio/Responses/InscriptionStatusResponse.php +++ b/src/PatpassComercio/Responses/InscriptionStatusResponse.php @@ -12,29 +12,34 @@ class InscriptionStatusResponse { - public $status; - public $urlVoucher; + public string|null $status; + public string|null $urlVoucher; - public function __construct($json) + /** + * InscriptionStatusResponse constructor. + * + * @param array $json + */ + + public function __construct(array $json) { $this->status = Utils::returnValueIfExists($json, 'authorized'); $this->urlVoucher = Utils::returnValueIfExists($json, 'voucherUrl'); } /** - * @return mixed + * @return string|null */ - public function getStatus() + public function getStatus(): string|null { return $this->status; } /** - * @return mixed + * @return string|null */ - public function getUrlVoucher() + public function getUrlVoucher(): string|null { return $this->urlVoucher; } - } diff --git a/src/Utils/HasTransactionStatus.php b/src/Utils/HasTransactionStatus.php index 4a5a0973..626c6d43 100644 --- a/src/Utils/HasTransactionStatus.php +++ b/src/Utils/HasTransactionStatus.php @@ -4,81 +4,81 @@ trait HasTransactionStatus { - public $status; - public $responseCode; - public $amount; - public $authorizationCode; - public $paymentTypeCode; - public $accountingDate; - public $installmentsNumber; - public $installmentsAmount; - public $sessionId; - public $buyOrder; - public $cardNumber; - public $cardDetail; - public $transactionDate; - public $balance; + public string|null $status; + public int|null $responseCode; + public int|float|null $amount; + public string|null $authorizationCode; + public string|null $paymentTypeCode; + public string|null $accountingDate; + public int|null $installmentsNumber; + public int|float|null $installmentsAmount; + public string|null $sessionId; + public string|null $buyOrder; + public string|null $cardNumber; + public array|null $cardDetail; + public string|null $transactionDate; + public int|float|null $balance; /** - * @return mixed + * @return int|float|null */ - public function getBalance() + public function getBalance(): int|float|null { return $this->balance; } /** - * @return mixed + * @return string|null */ - public function getStatus() + public function getStatus(): string|null { return $this->status; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationCode() + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } /** - * @return mixed + * @return int|null */ - public function getInstallmentsNumber() + public function getInstallmentsNumber(): int|null { return $this->installmentsNumber; } /** - * @return mixed + * @return int|null */ - public function getResponseCode() + public function getResponseCode(): int|null { return $this->responseCode; } /** - * @return mixed + * @return string|null */ - public function getBuyOrder() + public function getBuyOrder(): string|null { return $this->buyOrder; } /** - * @return mixed + * @return string|null */ - public function getAccountingDate() + public function getAccountingDate(): string|null { return $this->accountingDate; } /** - * @param $json + * @param array $json */ - public function setTransactionStatusFields($json) + public function setTransactionStatusFields(array $json): void { $this->amount = Utils::returnValueIfExists($json, 'amount'); $this->status = Utils::returnValueIfExists($json, 'status'); @@ -94,63 +94,61 @@ public function setTransactionStatusFields($json) $this->installmentsAmount = Utils::returnValueIfExists($json, 'installments_amount'); $this->installmentsNumber = Utils::returnValueIfExists($json, 'installments_number'); $this->balance = Utils::returnValueIfExists($json, 'balance'); - } /** - * @return mixed|null + * @return array|null */ - public function getCardDetail() + public function getCardDetail(): array|null { return $this->cardDetail; } /** - * @return mixed + * @return string|null */ - public function getCardNumber() + public function getCardNumber(): string|null { return $this->cardNumber; } /** - * @return mixed + * @return string|null */ - public function getPaymentTypeCode() + public function getPaymentTypeCode(): string|null { return $this->paymentTypeCode; } /** - * @return mixed + * @return int|float|null */ - public function getInstallmentsAmount() + public function getInstallmentsAmount(): int|float|null { return $this->installmentsAmount; } /** - * @return mixed + * @return string|null */ - public function getSessionId() + public function getSessionId(): string|null { return $this->sessionId; } /** - * @return mixed + * @return int|float|null */ - public function getAmount() + public function getAmount(): int|float|null { return $this->amount; } /** - * @return mixed + * @return string|null */ - public function getTransactionDate() + public function getTransactionDate(): string|null { return $this->transactionDate; } - } diff --git a/src/Utils/HttpClient.php b/src/Utils/HttpClient.php index 0b0010ca..8947fc72 100644 --- a/src/Utils/HttpClient.php +++ b/src/Utils/HttpClient.php @@ -5,23 +5,29 @@ use Composer\InstalledVersions; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Exception\GuzzleException; use Transbank\Contracts\HttpClientInterface; +use Psr\Http\Message\ResponseInterface; + class HttpClient implements HttpClientInterface { /** - * @param $url - * @param $path - * @param $options - * @param $method - * @param $payload + * @param string $method + * @param string $url + * @param array|null $payload + * @param array|null $options * - *@throws \GuzzleHttp\Exception\GuzzleException + *@throws GuzzleException * - * @return \Psr\Http\Message\ResponseInterface + * @return ResponseInterface */ - public function request($method, $url, $payload = [], $options = null) - { + public function request( + string $method, + string $url, + array|null $payload = [], + array|null $options = null + ): ResponseInterface { $installedVersion = 'unknown'; if (class_exists('\Composer\InstalledVersions')) { @@ -33,10 +39,10 @@ public function request($method, $url, $payload = [], $options = null) $baseHeaders = [ 'Content-Type' => 'application/json', - 'User-Agent' => 'SDK-PHP/'.$installedVersion, + 'User-Agent' => 'SDK-PHP/' . $installedVersion, ]; - $givenHeaders = isset($options['headers']) ? $options['headers'] : []; + $givenHeaders = $options['headers'] ?? []; $headers = array_merge($baseHeaders, $givenHeaders); if (!$payload) { $payload = null; @@ -55,20 +61,25 @@ public function request($method, $url, $payload = [], $options = null) } /** - * @param $method - * @param $url - * @param array $headers - * @param array $payload + * Sends a Guzzle 5 request. * - * @throws \GuzzleHttp\Exception\GuzzleException + * @param string $method + * @param string $url + * @param array $headers + * @param array|null $payload + * @param int $timeout * - * @return \Psr\Http\Message\ResponseInterface + * @throws GuzzleException + * + * @return ResponseInterface */ - protected function sendGuzzle5Request($method, $url, array $headers, $payload, $timeout) + protected function sendGuzzle5Request($method, $url, array $headers, $payload, int $timeout): ResponseInterface { - $client = new Client(['timeout' => $timeout, - 'read_timeout' => $timeout, - 'connect_timeout' => $timeout]); + $client = new Client([ + 'timeout' => $timeout, + 'read_timeout' => $timeout, + 'connect_timeout' => $timeout, + ]); $request = $client->createRequest($method, $url, [ 'headers' => $headers, @@ -79,24 +90,33 @@ protected function sendGuzzle5Request($method, $url, array $headers, $payload, $ } /** - * @param $method - * @param $url - * @param array $headers + * Sends a Guzzle request. + * + * @param string $method + * @param string $url + * @param array $headers * @param string|null $payload + * @param int $timeout * - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * - * @return \Psr\Http\Message\ResponseInterface + * @return ResponseInterface */ - protected function sendGuzzleRequest($method, $url, array $headers, $payload, $timeout) - { + protected function sendGuzzleRequest( + string $method, + string $url, + array $headers, + string|null $payload, + int $timeout + ): ResponseInterface { $request = new Request($method, $url, $headers, $payload); - $client = new Client(['http_errors' => false, - 'timeout' => $timeout, - 'read_timeout' => $timeout, - 'connect_timeout' => $timeout], - ); + $client = new Client([ + 'http_errors' => false, + 'timeout' => $timeout, + 'read_timeout' => $timeout, + 'connect_timeout' => $timeout, + ]); return $client->send($request); } diff --git a/src/Utils/HttpClientRequestService.php b/src/Utils/HttpClientRequestService.php index 2b48620d..5565d1e8 100644 --- a/src/Utils/HttpClientRequestService.php +++ b/src/Utils/HttpClientRequestService.php @@ -7,61 +7,66 @@ use Transbank\Webpay\Exceptions\TransbankApiRequest; use Transbank\Webpay\Exceptions\WebpayRequestException; use Transbank\Webpay\Options; +use GuzzleHttp\Exception\GuzzleException; +use Psr\Http\Message\ResponseInterface; class HttpClientRequestService implements RequestService { /** - * @var Psr\Http\Message\ResponseInterface|null + * @var ResponseInterface|null */ - protected $lastResponse = null; + protected ResponseInterface|null $lastResponse = null; /** * @var TransbankApiRequest|null */ - protected $lastRequest = null; + protected TransbankApiRequest|null $lastRequest = null; + /** - * @var HttpClientInterface + * @var HttpClientInterface|null */ - protected $httpClient; + protected HttpClientInterface|null $httpClient; - public function __construct(HttpClientInterface $httpClient = null) + public function __construct(HttpClientInterface|null $httpClient = null) { - $this->setHttpClient($httpClient !== null ? $httpClient : new HttpClient()); + $this->setHttpClient($httpClient ?? new HttpClient()); } /** * @return HttpClientInterface */ - public function getHttpClient() + public function getHttpClient(): HttpClientInterface { return $this->httpClient; } /** * @param HttpClientInterface $httpClient + * + * @return void */ - public function setHttpClient($httpClient) + public function setHttpClient(HttpClientInterface $httpClient): void { $this->httpClient = $httpClient; } /** - * @param $method - * @param $endpoint - * @param $payload + * @param string $method + * @param string $endpoint + * @param array $payload * @param Options $options * - * @throws GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * @throws WebpayRequestException * * @return array */ public function request( - $method, - $endpoint, - $payload, + string $method, + string $endpoint, + array $payload, Options $options - ) { + ): array { $headers = $options->getHeaders(); $client = $this->httpClient; if ($client == null) { @@ -72,8 +77,10 @@ public function request( $request = new TransbankApiRequest($method, $baseUrl, $endpoint, $payload, $headers); $this->setLastRequest($request); - $response = $client->request($method, $baseUrl.$endpoint, $payload, ['headers' => $headers, - 'timeout' => $options->getTimeout()]); + $response = $client->request($method, $baseUrl . $endpoint, $payload, [ + 'headers' => $headers, + 'timeout' => $options->getTimeout() + ]); $this->setLastResponse($response); $responseStatusCode = $response->getStatusCode(); @@ -82,11 +89,7 @@ public function request( $reason = $response->getReasonPhrase(); $message = "Could not obtain a response from Transbank API: $reason (HTTP code $responseStatusCode)"; $body = json_decode($response->getBody(), true); - $tbkErrorMessage = null; - if (isset($body['error_message'])) { - $tbkErrorMessage = $body['error_message']; - $message = "Transbank API REST Error: $tbkErrorMessage | $message"; - } + $tbkErrorMessage = $body['error_message'] ?? null; throw new WebpayRequestException($message, $tbkErrorMessage, $responseStatusCode, $request); } @@ -95,17 +98,17 @@ public function request( } /** - * @return Psr\Http\Message\ResponseInterface|null + * @return ResponseInterface|null */ - public function getLastResponse() + public function getLastResponse(): ResponseInterface|null { return $this->lastResponse; } /** - * @param Psr\Http\Message\ResponseInterface|null $lastResponse + * @param ResponseInterface|null $lastResponse */ - protected function setLastResponse($lastResponse) + protected function setLastResponse(ResponseInterface|null $lastResponse): void { $this->lastResponse = $lastResponse; } @@ -113,7 +116,7 @@ protected function setLastResponse($lastResponse) /** * @return TransbankApiRequest|null */ - public function getLastRequest() + public function getLastRequest(): TransbankApiRequest|null { return $this->lastRequest; } @@ -121,7 +124,7 @@ public function getLastRequest() /** * @param TransbankApiRequest|null $lastRequest */ - protected function setLastRequest($lastRequest) + protected function setLastRequest(TransbankApiRequest|null $lastRequest): void { $this->lastRequest = $lastRequest; } diff --git a/src/Utils/InteractsWithWebpayApi.php b/src/Utils/InteractsWithWebpayApi.php index 8fc95acd..74e8ee96 100644 --- a/src/Utils/InteractsWithWebpayApi.php +++ b/src/Utils/InteractsWithWebpayApi.php @@ -15,17 +15,17 @@ trait InteractsWithWebpayApi /** * @var Options */ - protected $options; + protected Options $options; /** * Transaction constructor. * - * @param Options $options - * @param RequestService |null $requestService + * @param Options $options + * @param RequestService|null $requestService */ public function __construct( Options $options, - RequestService $requestService = null + RequestService|null $requestService = null ) { $this->options = $options; @@ -36,7 +36,7 @@ public function __construct( /** * @return Options */ - public function getOptions() + public function getOptions(): Options { return $this->options; } @@ -44,7 +44,7 @@ public function getOptions() /** * @param Options $options */ - public function setOptions(Options $options) + public function setOptions(Options $options): void { $this->options = $options; } @@ -52,29 +52,31 @@ public function setOptions(Options $options) /** * @return string */ - protected function getBaseUrl() + protected function getBaseUrl(): string { return $this->getOptions()->getApiBaseUrl(); } /** - * @param $commerceCode - * @param $apiKey + * Build an instance configured for integration environment. + * @param string $commerceCode + * @param string $apiKey * - * @return $this + * @return static */ - public static function buildForIntegration($commerceCode, $apiKey) + public static function buildForIntegration(string $commerceCode, string $apiKey): self { return new static(new Options($apiKey, $commerceCode, Options::ENVIRONMENT_INTEGRATION)); } /** - * @param $commerceCode - * @param $apiKey + * Build an instance configured for integration environment. + * @param string $commerceCode + * @param string $apiKey * - * @return $this + * @return static */ - public static function buildForProduction($commerceCode, $apiKey) + public static function buildForProduction(string $commerceCode, string $apiKey): self { return new static(new Options($apiKey, $commerceCode, Options::ENVIRONMENT_PRODUCTION)); } diff --git a/src/Utils/RequestServiceTrait.php b/src/Utils/RequestServiceTrait.php index 3a531501..a58bb488 100644 --- a/src/Utils/RequestServiceTrait.php +++ b/src/Utils/RequestServiceTrait.php @@ -3,6 +3,7 @@ namespace Transbank\Utils; use Transbank\Contracts\RequestService; +use Transbank\Webpay\Exceptions\WebpayRequestException; /** * Trait RequestServiceTrait . @@ -11,20 +12,20 @@ trait RequestServiceTrait { /** - * @var RequestService |null + * @var RequestService|null */ - protected $requestService; + protected RequestService|null $requestService = null; /** - * @param $method - * @param $endpoint - * @param array|null $payload + * @param string $method + * @param string $endpoint + * @param array $payload * - * @throws \Transbank\Webpay\Exceptions\WebpayRequestException + * @throws WebpayRequestException * - * @return mixed + * @return array */ - public function sendRequest($method, $endpoint, $payload = []) + public function sendRequest(string $method, string $endpoint, array $payload = []): array { return $this->getRequestService()->request( $method, @@ -35,17 +36,17 @@ public function sendRequest($method, $endpoint, $payload = []) } /** - * @return RequestService |null + * @return RequestService|null */ - public function getRequestService() + public function getRequestService(): RequestService|null { return $this->requestService; } /** - * @param RequestService |null $requestService + * @param RequestService|null $requestService */ - public function setRequestService(RequestService $requestService = null) + public function setRequestService(RequestService|null $requestService = null): void { $this->requestService = $requestService; } diff --git a/src/Utils/ResponseCodesEnum.php b/src/Utils/ResponseCodesEnum.php index c9ac8cfc..19502094 100644 --- a/src/Utils/ResponseCodesEnum.php +++ b/src/Utils/ResponseCodesEnum.php @@ -4,5 +4,5 @@ class ResponseCodesEnum { - const RESPONSE_CODE_APPROVED = 0; + public const RESPONSE_CODE_APPROVED = 0; } diff --git a/src/Utils/TransactionStatusEnum.php b/src/Utils/TransactionStatusEnum.php index 0a568ceb..c5978f0b 100644 --- a/src/Utils/TransactionStatusEnum.php +++ b/src/Utils/TransactionStatusEnum.php @@ -2,12 +2,17 @@ namespace Transbank\Utils; +/** + * Class TransactionStatusEnum + * + * @package Transbank\Utils + */ class TransactionStatusEnum { - const STATUS_AUTHORIZED = 'AUTHORIZED'; - const STATUS_NULLIFIED = 'NULLIFIED'; - const STATUS_REVERSED = 'REVERSED'; - const STATUS_PARTIALLY_NULLIFIED = 'PARTIALLY_NULLIFIED'; - const STATUS_CAPTURED = 'CAPTURED'; - const STATUS_FAILED = 'FAILED'; + public const STATUS_AUTHORIZED = 'AUTHORIZED'; + public const STATUS_NULLIFIED = 'NULLIFIED'; + public const STATUS_REVERSED = 'REVERSED'; + public const STATUS_PARTIALLY_NULLIFIED = 'PARTIALLY_NULLIFIED'; + public const STATUS_CAPTURED = 'CAPTURED'; + public const STATUS_FAILED = 'FAILED'; } diff --git a/src/Utils/Utils.php b/src/Utils/Utils.php index 7a81fcd0..16cd8a7d 100644 --- a/src/Utils/Utils.php +++ b/src/Utils/Utils.php @@ -10,7 +10,12 @@ class Utils { - public static function returnValueIfExists($json, $key) + /** + * @param array $json + * @param string $key + * @return mixed + */ + public static function returnValueIfExists(array $json, string $key): mixed { return isset($json[$key]) ? $json[$key] : null; } diff --git a/src/Webpay/Exceptions/TransbankApiRequest.php b/src/Webpay/Exceptions/TransbankApiRequest.php index 9fc94ed1..43b41701 100644 --- a/src/Webpay/Exceptions/TransbankApiRequest.php +++ b/src/Webpay/Exceptions/TransbankApiRequest.php @@ -4,23 +4,28 @@ class TransbankApiRequest { - public $method; - public $baseUrl; - public $endpoint; - public $payload; - public $headers; + public string $method; + public string $baseUrl; + public string $endpoint; + public array $payload; + public array $headers; /** * FailedRequestCapturedData constructor. * - * @param $method - * @param $baseUrl - * @param $endpoint + * @param string $method + * @param string $baseUrl + * @param string $endpoint * @param array $payload * @param array $headers */ - public function __construct($method, $baseUrl, $endpoint, $payload = [], $headers = []) - { + public function __construct( + string $method, + string $baseUrl, + string $endpoint, + array $payload = [], + array $headers = [] + ) { $this->method = $method; $this->baseUrl = $baseUrl; $this->endpoint = $endpoint; @@ -29,25 +34,25 @@ public function __construct($method, $baseUrl, $endpoint, $payload = [], $header } /** - * @return mixed + * @return string */ - public function getMethod() + public function getMethod(): string { return $this->method; } /** - * @return mixed + * @return string */ - public function getBaseUrl() + public function getBaseUrl(): string { return $this->baseUrl; } /** - * @return mixed + * @return string */ - public function getEndpoint() + public function getEndpoint(): string { return $this->endpoint; } @@ -55,7 +60,7 @@ public function getEndpoint() /** * @return array */ - public function getPayload() + public function getPayload(): array { return $this->payload; } @@ -63,7 +68,7 @@ public function getPayload() /** * @return array */ - public function getHeaders() + public function getHeaders(): array { return $this->headers; } diff --git a/src/Webpay/Exceptions/TransbankException.php b/src/Webpay/Exceptions/TransbankException.php index 4ab01797..17aeec51 100644 --- a/src/Webpay/Exceptions/TransbankException.php +++ b/src/Webpay/Exceptions/TransbankException.php @@ -6,7 +6,14 @@ class TransbankException extends \Exception { const DEFAULT_MESSAGE = 'An error has happened, verify given parameters and try again.'; - public function __construct($message = self::DEFAULT_MESSAGE, $code = 0, $previous = null) + /** + * TransbankException constructor. + * + * @param string $message + * @param int $code + * @param \Throwable|null $previous + */ + public function __construct(string $message = self::DEFAULT_MESSAGE, int $code = 0, \Throwable|null $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Webpay/Exceptions/WebpayException.php b/src/Webpay/Exceptions/WebpayException.php index 9fa92f3c..f5351e3a 100644 --- a/src/Webpay/Exceptions/WebpayException.php +++ b/src/Webpay/Exceptions/WebpayException.php @@ -11,7 +11,14 @@ class WebpayException extends \Exception { const DEFAULT_MESSAGE = 'An error has happened, verify given parameters and try again.'; - public function __construct($message = self::DEFAULT_MESSAGE, $code = 0, $previous = null) + /** + * WebpayException constructor. + * + * @param string $message + * @param int $code + * @param \Throwable|null $previous + */ + public function __construct(string $message = self::DEFAULT_MESSAGE, int $code = 0, \Throwable|null $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Webpay/Exceptions/WebpayRequestException.php b/src/Webpay/Exceptions/WebpayRequestException.php index 71bfe2d2..525d69f4 100644 --- a/src/Webpay/Exceptions/WebpayRequestException.php +++ b/src/Webpay/Exceptions/WebpayRequestException.php @@ -10,43 +10,45 @@ class WebpayRequestException extends WebpayException /** * @var string */ - protected static $defaultMessage = 'An error has happened on the request'; + protected static string $defaultMessage = 'An error has happened on the request'; /** - * @var mixed + * @var string|null */ - protected $transbankErrorMessage; + protected string|null $transbankErrorMessage; /** - * @var int + * @var int|null */ protected $httpCode; /** - * @var + * @var mixed */ protected $response; /** * @var TransbankApiRequest|null */ - protected $failedRequest; + protected TransbankApiRequest|null $failedRequest; /** * WebpayRequestException constructor. * - * @param string $message - * @param mixed|string $tbkErrorMessage - * @param int $httpCode - * @param TransbankApiRequest|null $failedRequest + * @param string $message + * @param string|null $tbkErrorMessage + * @param int|null $httpCode + * @param TransbankApiRequest|null $failedRequest + * @param \Exception|null $previous */ public function __construct( - $message, - $tbkErrorMessage = null, - $httpCode = null, - TransbankApiRequest $failedRequest = null, - \Exception $previous = null + string $message, + string|null $tbkErrorMessage = null, + int|null $httpCode = null, + TransbankApiRequest|null $failedRequest = null, + \Exception|null $previous = null ) { - $theMessage = isset($tbkErrorMessage) ? $tbkErrorMessage : $message; + $theMessage = $tbkErrorMessage ?? $message; + if ($failedRequest !== null) { $theMessage = $this->getExceptionMessage($message, $tbkErrorMessage, $httpCode); } @@ -56,13 +58,13 @@ public function __construct( $this->httpCode = $httpCode; $this->failedRequest = $failedRequest; - parent::__construct($theMessage, $httpCode, $previous); + parent::__construct($theMessage, $httpCode ?? 0, $previous); } /** - * @return mixed + * @return string|null */ - public function getTransbankErrorMessage() + public function getTransbankErrorMessage(): string|null { return $this->transbankErrorMessage; } @@ -72,47 +74,54 @@ public function getTransbankErrorMessage() * * @return static */ - public static function raise(WebpayRequestException $exception) + public static function raise(WebpayRequestException $exception): self { - return new static($exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), - $exception->getFailedRequest(), $exception); + return new static( + $exception->getMessage(), + $exception->getTransbankErrorMessage(), + $exception->getHttpCode(), + $exception->getFailedRequest(), + $exception + ); } /** * @return int */ - public function getHttpCode() + public function getHttpCode(): int { - return $this->httpCode; + return $this->httpCode ?? 0; } /** * @return TransbankApiRequest|null */ - public function getFailedRequest() + public function getFailedRequest(): TransbankApiRequest|null { return $this->failedRequest; } /** - * @param $message - * @param $tbkErrorMessage - * @param $httpCode + * @param string $message + * @param string|null $tbkErrorMessage + * @param int|null $httpCode * * @return string */ protected function getExceptionMessage( - $message, - $tbkErrorMessage, - $httpCode - ) { + string $message, + string|null $tbkErrorMessage, + int|null $httpCode + ): string { if (!$tbkErrorMessage) { - $theMessage = $message; - } else { - $theMessage = 'API Response: "'.$tbkErrorMessage.'" ['.$httpCode.'] - '.static::$defaultMessage; + return $message; } - return $theMessage; + return sprintf( + 'API Response: "%s" [%d] - %s', + $tbkErrorMessage, + $httpCode ?? 0, + static::$defaultMessage + ); } - } diff --git a/src/Webpay/Oneclick/MallInscription.php b/src/Webpay/Oneclick/MallInscription.php index 66de83d0..a0a49bb3 100644 --- a/src/Webpay/Oneclick/MallInscription.php +++ b/src/Webpay/Oneclick/MallInscription.php @@ -4,14 +4,13 @@ use Transbank\Utils\InteractsWithWebpayApi; use Transbank\Webpay\Exceptions\WebpayRequestException; -use Transbank\Webpay\Oneclick; use Transbank\Webpay\Oneclick\Exceptions\InscriptionDeleteException; use Transbank\Webpay\Oneclick\Exceptions\InscriptionFinishException; use Transbank\Webpay\Oneclick\Exceptions\InscriptionStartException; use Transbank\Webpay\Oneclick\Responses\InscriptionDeleteResponse; use Transbank\Webpay\Oneclick\Responses\InscriptionFinishResponse; use Transbank\Webpay\Oneclick\Responses\InscriptionStartResponse; -use Transbank\Webpay\Options; +use GuzzleHttp\Exception\GuzzleException; class MallInscription { @@ -22,17 +21,16 @@ class MallInscription const INSCRIPTION_DELETE_ENDPOINT = 'rswebpaytransaction/api/oneclick/v1.2/inscriptions'; /** - * @param $username - * @param $email - * @param $responseUrl - * @param null $options - * - * @throws InscriptionStartException - * @throws \GuzzleHttp\Exception\GuzzleException + * @param string $username + * @param string $email + * @param string $responseUrl * * @return InscriptionStartResponse + * + * @throws InscriptionStartException + * @throws GuzzleException */ - public function start($username, $email, $responseUrl) + public function start(string $username, string $email, string $responseUrl): InscriptionStartResponse { $payload = [ 'username' => $username, @@ -47,7 +45,8 @@ public function start($username, $email, $responseUrl) $payload ); } catch (WebpayRequestException $exception) { - throw new InscriptionStartException($exception->getMessage(), + throw new InscriptionStartException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -58,16 +57,25 @@ public function start($username, $email, $responseUrl) return new InscriptionStartResponse($response); } - public function finish($token) + /** + * @param string $token + * + * @return InscriptionFinishResponse + * + * @throws InscriptionFinishException + * @throws GuzzleException + */ + public function finish(string $token): InscriptionFinishResponse { try { $response = $this->sendRequest( 'PUT', str_replace('{token}', $token, static::INSCRIPTION_FINISH_ENDPOINT), - null + [] ); } catch (WebpayRequestException $exception) { - throw new InscriptionFinishException($exception->getMessage(), + throw new InscriptionFinishException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -78,7 +86,16 @@ public function finish($token) return new InscriptionFinishResponse($response); } - public function delete($tbkUser, $username) + /** + * @param string $tbkUser + * @param string $username + * + * @return InscriptionDeleteResponse + * + * @throws InscriptionDeleteException + * @throws GuzzleException + */ + public function delete(string $tbkUser, string $username): InscriptionDeleteResponse { $payload = [ 'tbk_user' => $tbkUser, @@ -96,7 +113,8 @@ public function delete($tbkUser, $username) return new InscriptionDeleteResponse(false, $exception->getHttpCode()); } - throw new InscriptionDeleteException($exception->getMessage(), + throw new InscriptionDeleteException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), diff --git a/src/Webpay/Oneclick/MallTransaction.php b/src/Webpay/Oneclick/MallTransaction.php index 31f54e06..a5ead788 100644 --- a/src/Webpay/Oneclick/MallTransaction.php +++ b/src/Webpay/Oneclick/MallTransaction.php @@ -12,6 +12,7 @@ use Transbank\Webpay\Oneclick\Responses\MallTransactionCaptureResponse; use Transbank\Webpay\Oneclick\Responses\MallTransactionRefundResponse; use Transbank\Webpay\Oneclick\Responses\MallTransactionStatusResponse; +use GuzzleHttp\Exception\GuzzleException; class MallTransaction { @@ -21,12 +22,23 @@ class MallTransaction const TRANSACTION_REFUND_ENDPOINT = 'rswebpaytransaction/api/oneclick/v1.2/transactions/{buy_order}/refunds'; const TRANSACTION_CAPTURE_ENDPOINT = 'rswebpaytransaction/api/oneclick/v1.2/transactions/capture'; + /** + * @param string $userName + * @param string $tbkUser + * @param string $parentBuyOrder + * @param array $details + * + * @return MallTransactionAuthorizeResponse + * + * @throws MallTransactionAuthorizeException + * @throws GuzzleException + */ public function authorize( - $userName, - $tbkUser, - $parentBuyOrder, - $details - ) { + string $userName, + string $tbkUser, + string $parentBuyOrder, + array $details + ): MallTransactionAuthorizeResponse { $payload = [ 'username' => $userName, 'tbk_user' => $tbkUser, @@ -41,7 +53,8 @@ public function authorize( $payload ); } catch (WebpayRequestException $exception) { - throw new MallTransactionAuthorizeException($exception->getMessage(), + throw new MallTransactionAuthorizeException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -52,8 +65,23 @@ public function authorize( return new MallTransactionAuthorizeResponse($response); } - public function capture($childCommerceCode, $childBuyOrder, $authorizationCode, $amount) - { + /** + * @param string $childCommerceCode + * @param string $childBuyOrder + * @param string $authorizationCode + * @param int|float $amount + * + * @return MallTransactionCaptureResponse + * + * @throws MallTransactionCaptureException + * @throws GuzzleException + */ + public function capture( + string $childCommerceCode, + string $childBuyOrder, + string $authorizationCode, + int|float $amount + ): MallTransactionCaptureResponse { $payload = [ 'commerce_code' => $childCommerceCode, 'buy_order' => $childBuyOrder, @@ -68,7 +96,8 @@ public function capture($childCommerceCode, $childBuyOrder, $authorizationCode, $payload ); } catch (WebpayRequestException $exception) { - throw new MallTransactionCaptureException($exception->getMessage(), + throw new MallTransactionCaptureException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -79,16 +108,25 @@ public function capture($childCommerceCode, $childBuyOrder, $authorizationCode, return new MallTransactionCaptureResponse($response); } - public function status($buyOrder) + /** + * @param string $buyOrder + * + * @return MallTransactionStatusResponse + * + * @throws MallTransactionStatusException + * @throws GuzzleException + */ + public function status(string $buyOrder): MallTransactionStatusResponse { try { $response = $this->sendRequest( 'GET', str_replace('{buy_order}', $buyOrder, static::TRANSACTION_STATUS_ENDPOINT), - null + [] ); } catch (WebpayRequestException $exception) { - throw new MallTransactionStatusException($exception->getMessage(), + throw new MallTransactionStatusException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -99,8 +137,23 @@ public function status($buyOrder) return new MallTransactionStatusResponse($response); } - public function refund($buyOrder, $childCommerceCode, $childBuyOrder, $amount) - { + /** + * @param string $buyOrder + * @param string $childCommerceCode + * @param string $childBuyOrder + * @param int|float $amount + * + * @return MallTransactionRefundResponse + * + * @throws MallRefundTransactionException + * @throws GuzzleException + */ + public function refund( + string $buyOrder, + string $childCommerceCode, + string $childBuyOrder, + int|float $amount + ): MallTransactionRefundResponse { $payload = [ 'detail_buy_order' => $childBuyOrder, 'commerce_code' => $childCommerceCode, @@ -114,7 +167,8 @@ public function refund($buyOrder, $childCommerceCode, $childBuyOrder, $amount) $payload ); } catch (WebpayRequestException $exception) { - throw new MallRefundTransactionException($exception->getMessage(), + throw new MallRefundTransactionException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), diff --git a/src/Webpay/Oneclick/Responses/InscriptionDeleteResponse.php b/src/Webpay/Oneclick/Responses/InscriptionDeleteResponse.php index 629349dd..a8b505b8 100644 --- a/src/Webpay/Oneclick/Responses/InscriptionDeleteResponse.php +++ b/src/Webpay/Oneclick/Responses/InscriptionDeleteResponse.php @@ -4,24 +4,24 @@ class InscriptionDeleteResponse { - public $success = false; - public $code; + public bool $success = false; + public int|null $code; - public function __construct($success, $httpCode = null) + public function __construct(bool $success, int|null $httpCode = null) { $this->success = $success; $this->code = $httpCode; } - public function wasSuccessfull() + public function wasSuccessfull(): bool { return $this->success === true; } /** - * @return mixed|null + * @return int|null */ - public function getCode() + public function getCode(): int|null { return $this->code; } diff --git a/src/Webpay/Oneclick/Responses/InscriptionFinishResponse.php b/src/Webpay/Oneclick/Responses/InscriptionFinishResponse.php index ec4f21fb..6d213c03 100644 --- a/src/Webpay/Oneclick/Responses/InscriptionFinishResponse.php +++ b/src/Webpay/Oneclick/Responses/InscriptionFinishResponse.php @@ -7,13 +7,13 @@ class InscriptionFinishResponse { - public $responseCode; - public $tbkUser; - public $authorizationCode; - public $cardType; - public $cardNumber; + public int|null $responseCode; + public string|null $tbkUser; + public string|null $authorizationCode; + public string|null $cardType; + public string|null $cardNumber; - public function __construct($json) + public function __construct(array $json) { $this->responseCode = Utils::returnValueIfExists($json, 'response_code'); $this->tbkUser = Utils::returnValueIfExists($json, 'tbk_user'); @@ -22,49 +22,48 @@ public function __construct($json) $this->cardNumber = Utils::returnValueIfExists($json, 'card_number'); } - public function isApproved() + public function isApproved(): bool { return $this->getResponseCode() === ResponseCodesEnum::RESPONSE_CODE_APPROVED; } /** - * @return mixed + * @return int|null */ - public function getResponseCode() + public function getResponseCode(): int|null { - return (int) $this->responseCode; + return $this->responseCode; } /** - * @return mixed + * @return string|null */ - public function getTbkUser() + public function getTbkUser(): string|null { return $this->tbkUser; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationCode() + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } /** - * @return mixed + * @return string|null */ - public function getCardType() + public function getCardType(): string|null { return $this->cardType; } /** - * @return mixed + * @return string|null */ - public function getCardNumber() + public function getCardNumber(): string|null { return $this->cardNumber; } - } diff --git a/src/Webpay/Oneclick/Responses/InscriptionStartResponse.php b/src/Webpay/Oneclick/Responses/InscriptionStartResponse.php index 221dfa22..dd75fd9b 100644 --- a/src/Webpay/Oneclick/Responses/InscriptionStartResponse.php +++ b/src/Webpay/Oneclick/Responses/InscriptionStartResponse.php @@ -6,34 +6,33 @@ class InscriptionStartResponse { - public $token; - public $urlWebpay; + public string|null $token; + public string|null $urlWebpay; - public function __construct($json) + public function __construct(array $json) { $this->token = Utils::returnValueIfExists($json, 'token'); $this->urlWebpay = Utils::returnValueIfExists($json, 'url_webpay'); } - public function getRedirectUrl() + public function getRedirectUrl(): string { - return $this->getUrlWebpay().'?TBK_TOKEN='.$this->getToken(); + return $this->getUrlWebpay() . '?TBK_TOKEN=' . $this->getToken(); } /** - * @return mixed + * @return string|null */ - public function getToken() + public function getToken(): string|null { return $this->token; } /** - * @return mixed + * @return string|null */ - public function getUrlWebpay() + public function getUrlWebpay(): string|null { return $this->urlWebpay; } - } diff --git a/src/Webpay/Oneclick/Responses/MallTransactionAuthorizeResponse.php b/src/Webpay/Oneclick/Responses/MallTransactionAuthorizeResponse.php index a361a206..e28cfd36 100644 --- a/src/Webpay/Oneclick/Responses/MallTransactionAuthorizeResponse.php +++ b/src/Webpay/Oneclick/Responses/MallTransactionAuthorizeResponse.php @@ -4,11 +4,11 @@ class MallTransactionAuthorizeResponse extends MallTransactionStatusResponse { - public function __construct($json) - { - parent::__construct($json); - foreach ($this->details as $index => $detail) { - unset($this->details[$index]->balance); - } + public function __construct(array $json) + { + parent::__construct($json); + foreach ($this->details as &$detail) { + unset($detail->balance); } + } } diff --git a/src/Webpay/Oneclick/Responses/MallTransactionCaptureResponse.php b/src/Webpay/Oneclick/Responses/MallTransactionCaptureResponse.php index ea2e5f35..0f42bfb0 100644 --- a/src/Webpay/Oneclick/Responses/MallTransactionCaptureResponse.php +++ b/src/Webpay/Oneclick/Responses/MallTransactionCaptureResponse.php @@ -6,12 +6,12 @@ class MallTransactionCaptureResponse { - public $authorizationCode; - public $authorizationDate; - public $capturedAmount; - public $responseCode; + public string|null $authorizationCode; + public string|null $authorizationDate; + public int|float|null $capturedAmount; + public int|null $responseCode; - public function __construct($json) + public function __construct(array $json) { $this->authorizationCode = Utils::returnValueIfExists($json, 'authorization_code'); $this->authorizationDate = Utils::returnValueIfExists($json, 'authorization_date'); @@ -19,24 +19,35 @@ public function __construct($json) $this->responseCode = Utils::returnValueIfExists($json, 'response_code'); } - public function getAuthorizationCode() + /** + * @return string|null + */ + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } - public function getAuthorizationDate() + /** + * @return string|null + */ + public function getAuthorizationDate(): string|null { return $this->authorizationDate; } - public function getCapturedAmount() + /** + * @return int|float|null + */ + public function getCapturedAmount(): int|float|null { return $this->capturedAmount; } - public function getResponseCode() + /** + * @return int|null + */ + public function getResponseCode(): int|null { return $this->responseCode; } - } diff --git a/src/Webpay/Oneclick/Responses/MallTransactionRefundResponse.php b/src/Webpay/Oneclick/Responses/MallTransactionRefundResponse.php index 7c6ccef3..9026120a 100644 --- a/src/Webpay/Oneclick/Responses/MallTransactionRefundResponse.php +++ b/src/Webpay/Oneclick/Responses/MallTransactionRefundResponse.php @@ -6,14 +6,14 @@ class MallTransactionRefundResponse { - public $type; - public $authorizationCode; - public $authorizationDate; - public $nullifiedAmount; - public $balance; - public $responseCode; + public string|null $type; + public string|null $authorizationCode; + public string|null $authorizationDate; + public int|float|null $nullifiedAmount; + public int|float|null $balance; + public int|null $responseCode; - public function __construct($json) + public function __construct(array $json) { $this->type = Utils::returnValueIfExists($json, 'type'); $this->authorizationCode = Utils::returnValueIfExists($json, 'authorization_code'); @@ -24,51 +24,50 @@ public function __construct($json) } /** - * @return mixed + * @return string|null */ - public function getType() + public function getType(): string|null { return $this->type; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationCode() + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationDate() + public function getAuthorizationDate(): string|null { return $this->authorizationDate; } /** - * @return mixed + * @return int|float|null */ - public function getNullifiedAmount() + public function getNullifiedAmount(): int|float|null { return $this->nullifiedAmount; } /** - * @return mixed + * @return int|float|null */ - public function getBalance() + public function getBalance(): int|float|null { return $this->balance; } /** - * @return mixed + * @return int|null */ - public function getResponseCode() + public function getResponseCode(): int|null { return $this->responseCode; } - } diff --git a/src/Webpay/Oneclick/Responses/MallTransactionStatusResponse.php b/src/Webpay/Oneclick/Responses/MallTransactionStatusResponse.php index 07010ce3..113fd6ee 100644 --- a/src/Webpay/Oneclick/Responses/MallTransactionStatusResponse.php +++ b/src/Webpay/Oneclick/Responses/MallTransactionStatusResponse.php @@ -6,32 +6,27 @@ class MallTransactionStatusResponse { - public $buyOrder; - public $cardNumber; - public $accountingDate; - public $transactionDate; + public string|null $buyOrder; + public array|null $cardDetail; + public string|null $cardNumber; + public string|null $accountingDate; + public string|null $transactionDate; + public array|null $details; - /** - * @var TransactionDetail[] - */ - public $details; - - public function __construct($json) + public function __construct(array $json) { $this->buyOrder = Utils::returnValueIfExists($json, 'buy_order'); $this->accountingDate = Utils::returnValueIfExists($json, 'accounting_date'); $this->transactionDate = Utils::returnValueIfExists($json, 'transaction_date'); - $cardDetail = Utils::returnValueIfExists($json, 'card_detail'); - $this->cardNumber = Utils::returnValueIfExists($cardDetail, 'card_number'); + $this->cardDetail = Utils::returnValueIfExists($json, 'card_detail'); + $this->cardNumber = Utils::returnValueIfExists($this->cardDetail, 'card_number'); - $details = Utils::returnValueIfExists($json, 'details'); - $detailsObjectArray = []; - if (is_array($details)) { - foreach ($details as $detail) { - $detailsObjectArray[] = TransactionDetail::createFromArray($detail); + $this->details = []; + if (is_array($json['details'])) { + foreach ($json['details'] as $detail) { + $this->details[] = TransactionDetail::createFromArray($detail); } } - $this->details = $detailsObjectArray; } /** @@ -39,7 +34,7 @@ public function __construct($json) * * @return bool */ - public function isApproved() + public function isApproved(): bool { if (!$details = $this->getDetails()) { return false; @@ -55,43 +50,42 @@ public function isApproved() } /** - * @return mixed + * @return string|null */ - public function getTransactionDate() + public function getTransactionDate(): string|null { return $this->transactionDate; } /** - * @return mixed + * @return string|null */ - public function getCardNumber() + public function getCardNumber(): string|null { return $this->cardNumber; } /** - * @return mixed + * @return string|null */ - public function getAccountingDate() + public function getAccountingDate(): string|null { return $this->accountingDate; } /** - * @return TransactionDetail[] + * @return TransactionDetail[]|null */ - public function getDetails() + public function getDetails(): array|null { return $this->details; } /** - * @return mixed + * @return string|null */ - public function getBuyOrder() + public function getBuyOrder(): string|null { return $this->buyOrder; } - } diff --git a/src/Webpay/Options.php b/src/Webpay/Options.php index b1bdb722..c4dbefd2 100644 --- a/src/Webpay/Options.php +++ b/src/Webpay/Options.php @@ -4,6 +4,12 @@ /** * Class Options. + * @var string ENVIRONMENT_PRODUCTION + * @var string ENVIRONMENT_INTEGRATION + * @var string BASE_URL_PRODUCTION + * @var string BASE_URL_INTEGRATION + * @var string INTEGRATION_API_KEY + * @var int DEFAULT_TIMEOUT */ class Options { @@ -19,41 +25,48 @@ class Options /** * @var int Timeout for requests in seconds */ - protected $timeout; + protected int $timeout; /** - * @var string Your api key, given by Transbank.Sent as a header when + * @var string|null Your api key, given by Transbank.Sent as a header when * making requests to Transbank on a field called "Tbk-Api-Key-Secret" */ - public $apiKey = null; + public string|null $apiKey = null; /** - * @var string Your commerce code, given by Transbank. Sent as + * @var string|null Your commerce code, given by Transbank. Sent as * a header when making requests to Transbank on a field called "Tbk-Api-Key-Id" */ - public $commerceCode = null; + public string|null $commerceCode = null; /** * @var string Sets the environment that the SDK is going * to point to (eg. TEST, LIVE, etc). */ - public $integrationType = self::ENVIRONMENT_INTEGRATION; - - public function __construct($apiKey, $commerceCode, $integrationType, $timeout = self::DEFAULT_TIMEOUT) - { + public string|null $integrationType = null; + + public function __construct( + string $apiKey, + string $commerceCode, + string $integrationType, + int $timeout = self::DEFAULT_TIMEOUT + ) { $this->apiKey = $apiKey; $this->commerceCode = $commerceCode; $this->integrationType = $integrationType; $this->timeout = $timeout; } - public function isProduction() + /** + * @return bool + */ + public function isProduction(): bool { return $this->getIntegrationType() === static::ENVIRONMENT_PRODUCTION; } /** - * @return string + * @return string|null */ - public function getIntegrationType() + public function getIntegrationType(): string|null { return $this->integrationType; } @@ -63,7 +76,7 @@ public function getIntegrationType() * * @return Options */ - public function setIntegrationType($integrationType) + public function setIntegrationType($integrationType): Options { $this->integrationType = $integrationType; @@ -71,9 +84,9 @@ public function setIntegrationType($integrationType) } /** - * @return mixed + * @return string|null */ - public function getApiKey() + public function getApiKey(): string|null { return $this->apiKey; } @@ -83,7 +96,7 @@ public function getApiKey() * * @return Options */ - public function setApiKey($apiKey) + public function setApiKey(string $apiKey): Options { $this->apiKey = $apiKey; @@ -91,19 +104,19 @@ public function setApiKey($apiKey) } /** - * @return mixed + * @return string|null */ - public function getCommerceCode() + public function getCommerceCode(): string|null { return $this->commerceCode; } /** - * @param mixed $commerceCode + * @param string $commerceCode * * @return Options */ - public function setCommerceCode($commerceCode) + public function setCommerceCode(string $commerceCode): Options { $this->commerceCode = $commerceCode; @@ -114,19 +127,18 @@ public function setCommerceCode($commerceCode) * @return string Returns the base URL used for making requests, depending on which * integration types */ - public function getApiBaseUrl() + public function getApiBaseUrl(): string { if ($this->isProduction()) { return static::BASE_URL_PRODUCTION; } - return static::BASE_URL_INTEGRATION; } /** * @return array */ - public function getHeaders() + public function getHeaders(): array { return [ 'Tbk-Api-Key-Id' => $this->getCommerceCode(), @@ -137,7 +149,7 @@ public function getHeaders() /** * @return int */ - public function getTimeout() + public function getTimeout(): int { return $this->timeout; } @@ -147,7 +159,7 @@ public function getTimeout() * * @return Options */ - public function setTimeout($timeout) + public function setTimeout($timeout): Options { $this->timeout = $timeout; diff --git a/src/Webpay/TransaccionCompleta.php b/src/Webpay/TransaccionCompleta.php index 7ca73d26..2b0ab269 100644 --- a/src/Webpay/TransaccionCompleta.php +++ b/src/Webpay/TransaccionCompleta.php @@ -1,15 +1,29 @@ $buyOrder, @@ -56,7 +70,8 @@ public function create( try { $response = $this->sendRequest('POST', static::ENDPOINT_CREATE, $payload); } catch (WebpayRequestException $exception) { - throw new MallTransactionCreateException($exception->getMessage(), + throw new MallTransactionCreateException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -67,11 +82,19 @@ public function create( return new MallTransactionCreateResponse($response); } + /** + * @param string $token + * @param array $details + * + * @throws TransactionInstallmentsException + * + * @return MallTransactionInstallmentsResponse[] + */ public function installments( - $token, - $details + string $token, + array $details ) { - $endpoint = str_replace('{token}', $token, static::ENDPOINT_INSTALLMENTS); + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_INSTALLMENTS); try { return array_map(function ($detail) use ($endpoint) { @@ -85,7 +108,8 @@ public function installments( return new MallTransactionInstallmentsResponse($response); }, $details); } catch (WebpayRequestException $exception) { - throw new TransactionInstallmentsException($exception->getMessage(), + throw new TransactionInstallmentsException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -94,6 +118,14 @@ public function installments( } } + /** + * @param string $token + * @param array $details + * + * @throws MallTransactionCommitException + * + * @return MallTransactionCommitResponse + */ public function commit( $token, $details @@ -102,12 +134,13 @@ public function commit( 'details' => $details, ]; - $endpoint = str_replace('{token}', $token, static::ENDPOINT_COMMIT); + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_COMMIT); try { $response = $this->sendRequest('PUT', $endpoint, $payload); } catch (WebpayRequestException $exception) { - throw new MallTransactionCommitException($exception->getMessage(), + throw new MallTransactionCommitException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -118,11 +151,21 @@ public function commit( return new MallTransactionCommitResponse($response); } + /** + * @param string $token + * @param string $buyOrder + * @param string $commerceCodeChild + * @param int|float $amount + * + * @throws MallTransactionRefundException + * + * @return MallTransactionRefundResponse + */ public function refund( - $token, - $buyOrder, - $commerceCodeChild, - $amount + string $token, + string $buyOrder, + string $commerceCodeChild, + int|float $amount ) { $payload = [ 'buy_order' => $buyOrder, @@ -130,12 +173,13 @@ public function refund( 'amount' => $amount, ]; - $endpoint = str_replace('{token}', $token, static::ENDPOINT_REFUND); + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_REFUND); try { $response = $this->sendRequest('POST', $endpoint, $payload); } catch (WebpayRequestException $exception) { - throw new MallTransactionRefundException($exception->getMessage(), + throw new MallTransactionRefundException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -146,14 +190,22 @@ public function refund( return new MallTransactionRefundResponse($response); } - public function status($token) + /** + * @param string $token + * + * @throws MallTransactionStatusException + * + * @return MallTransactionStatusResponse + */ + public function status(string $token) { - $endpoint = str_replace('{token}', $token, static::ENDPOINT_STATUS); + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_STATUS); try { - $response = $this->sendRequest('GET', $endpoint, null); + $response = $this->sendRequest('GET', $endpoint, []); } catch (WebpayRequestException $exception) { - throw new MallTransactionStatusException($exception->getMessage(), + throw new MallTransactionStatusException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -165,32 +217,38 @@ public function status($token) } /** - * @param $token - * @param $commerceCode - * @param $buyOrder - * @param $authorizationCode - * @param $captureAmount + * @param string $token + * @param string $commerceCode + * @param string $buyOrder + * @param string $authorizationCode + * @param int|float $captureAmount * * @throws MallTransactionCaptureException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * - * @return Responses\MallTransactionCaptureResponse + * @return MallTransactionCaptureResponse */ - public function capture($token, $commerceCode, $buyOrder, $authorizationCode, $captureAmount) - { - $endpoint = str_replace('{token}', $token, static::ENDPOINT_CAPTURE); + public function capture( + string $token, + string $commerceCode, + string $buyOrder, + string $authorizationCode, + int|float $captureAmount + ) { + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_CAPTURE); $payload = [ 'buy_order' => $buyOrder, 'commerce_code' => $commerceCode, 'authorization_code' => $authorizationCode, - 'capture_amount' => (int) $captureAmount, + 'capture_amount' => $captureAmount, ]; try { $response = $this->sendRequest('PUT', $endpoint, $payload); } catch (WebpayRequestException $exception) { - throw new MallTransactionCaptureException($exception->getMessage(), + throw new MallTransactionCaptureException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -198,6 +256,6 @@ public function capture($token, $commerceCode, $buyOrder, $authorizationCode, $c ); } - return new Responses\MallTransactionCaptureResponse($response); + return new MallTransactionCaptureResponse($response); } } diff --git a/src/Webpay/TransaccionCompleta/Responses/MallTransactionCommitResponse.php b/src/Webpay/TransaccionCompleta/Responses/MallTransactionCommitResponse.php index ff046141..2073cc02 100644 --- a/src/Webpay/TransaccionCompleta/Responses/MallTransactionCommitResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/MallTransactionCommitResponse.php @@ -4,11 +4,11 @@ class MallTransactionCommitResponse extends MallTransactionStatusResponse { - public function __construct($json) - { - parent::__construct($json); - foreach ($this->details as $index => $detail) { - unset($this->details[$index]->balance); - } + public function __construct(array $json) + { + parent::__construct($json); + foreach ($this->details as &$detail) { + unset($detail->balance); } + } } diff --git a/src/Webpay/TransaccionCompleta/Responses/MallTransactionCreateResponse.php b/src/Webpay/TransaccionCompleta/Responses/MallTransactionCreateResponse.php index eee96b54..42b10fa6 100644 --- a/src/Webpay/TransaccionCompleta/Responses/MallTransactionCreateResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/MallTransactionCreateResponse.php @@ -6,19 +6,18 @@ class MallTransactionCreateResponse { - public $token; + public string|null $token; - public function __construct($json) + public function __construct(array $json) { $this->token = Utils::returnValueIfExists($json, 'token'); } /** - * @return mixed + * @return string|null */ - public function getToken() + public function getToken(): string|null { return $this->token; } - } diff --git a/src/Webpay/TransaccionCompleta/Responses/MallTransactionInstallmentsResponse.php b/src/Webpay/TransaccionCompleta/Responses/MallTransactionInstallmentsResponse.php index 4750488a..797bc987 100644 --- a/src/Webpay/TransaccionCompleta/Responses/MallTransactionInstallmentsResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/MallTransactionInstallmentsResponse.php @@ -6,11 +6,11 @@ class MallTransactionInstallmentsResponse { - public $installmentsAmount; - public $idQueryInstallments; - public $deferredPeriods; + public int|float|null $installmentsAmount; + public string|null $idQueryInstallments; + public array|null $deferredPeriods; - public function __construct($json) + public function __construct(array $json) { $this->installmentsAmount = Utils::returnValueIfExists($json, 'installments_amount'); $this->idQueryInstallments = Utils::returnValueIfExists($json, 'id_query_installments'); @@ -18,27 +18,26 @@ public function __construct($json) } /** - * @return mixed + * @return int|float|null */ - public function getInstallmentsAmount() + public function getInstallmentsAmount(): int|float|null { return $this->installmentsAmount; } /** - * @return mixed + * @return string|null */ - public function getIdQueryInstallments() + public function getIdQueryInstallments(): string|null { return $this->idQueryInstallments; } /** - * @return mixed + * @return array|null */ - public function getDeferredPeriods() + public function getDeferredPeriods(): array|null { return $this->deferredPeriods; } - } diff --git a/src/Webpay/TransaccionCompleta/Responses/MallTransactionRefundResponse.php b/src/Webpay/TransaccionCompleta/Responses/MallTransactionRefundResponse.php index 6f719e42..bf010918 100644 --- a/src/Webpay/TransaccionCompleta/Responses/MallTransactionRefundResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/MallTransactionRefundResponse.php @@ -6,14 +6,14 @@ class MallTransactionRefundResponse { - public $type; - public $authorizationCode; - public $authorizationDate; - public $nullifiedAmount; - public $balance; - public $responseCode; + public string|null $type; + public string|null $authorizationCode; + public string|null $authorizationDate; + public int|float|null $nullifiedAmount; + public int|float|null $balance; + public int|null $responseCode; - public function __construct($json) + public function __construct(array $json) { $this->type = Utils::returnValueIfExists($json, 'type'); $this->authorizationCode = Utils::returnValueIfExists($json, 'authorization_code'); @@ -24,51 +24,50 @@ public function __construct($json) } /** - * @return mixed + * @return string|null */ - public function getType() + public function getType(): string|null { return $this->type; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationCode() + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationDate() + public function getAuthorizationDate(): string|null { return $this->authorizationDate; } /** - * @return mixed + * @return int|float|null */ - public function getNullifiedAmount() + public function getNullifiedAmount(): int|float|null { return $this->nullifiedAmount; } /** - * @return mixed + * @return int|float|null */ - public function getBalance() + public function getBalance(): int|float|null { return $this->balance; } /** - * @return mixed + * @return int|null */ - public function getResponseCode() + public function getResponseCode(): int|null { - return (int) $this->responseCode; + return $this->responseCode; } - } diff --git a/src/Webpay/TransaccionCompleta/Responses/MallTransactionStatusResponse.php b/src/Webpay/TransaccionCompleta/Responses/MallTransactionStatusResponse.php index 73dd2eab..5b5adea0 100644 --- a/src/Webpay/TransaccionCompleta/Responses/MallTransactionStatusResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/MallTransactionStatusResponse.php @@ -6,75 +6,73 @@ class MallTransactionStatusResponse { - protected $buyOrder; - protected $cardDetail; - protected $cardNumber; - protected $accountingDate; - protected $transactionDate; - protected $details; + protected string|null $buyOrder; + protected array|null $cardDetail; + protected string|null $cardNumber; + protected string|null $accountingDate; + protected string|null $transactionDate; + protected array|null $details; - public function __construct($json) + public function __construct(array $json) { - $this->buyOrder = Utils::returnValueIfExists($json, 'buyOrder'); + $this->buyOrder = Utils::returnValueIfExists($json, 'buy_order'); $this->accountingDate = Utils::returnValueIfExists($json, 'accounting_date'); $this->transactionDate = Utils::returnValueIfExists($json, 'transaction_date'); $this->cardDetail = Utils::returnValueIfExists($json, 'card_detail'); $this->cardNumber = Utils::returnValueIfExists($this->cardDetail, 'card_number'); - $details = Utils::returnValueIfExists($json, 'details'); - $this->details = null; - if (is_array($details)) { - $this->details = []; - foreach ($details as $detail) { + $this->details = []; + if (is_array($json['details'])) { + foreach ($json['details'] as $detail) { $this->details[] = TransactionDetail::createFromArray($detail); } } } /** - * @return mixed + * @return string|null */ - public function getBuyOrder() + public function getBuyOrder(): string|null { return $this->buyOrder; } /** - * @return mixed + * @return array|null */ - public function getCardDetail() + public function getCardDetail(): array|null { return $this->cardDetail; } /** - * @return mixed + * @return string|null */ - public function getAccountingDate() + public function getAccountingDate(): string|null { return $this->accountingDate; } /** - * @return mixed + * @return string|null */ - public function getTransactionDate() + public function getTransactionDate(): string|null { return $this->transactionDate; } /** - * @return mixed + * @return array|null */ - public function getDetails() + public function getDetails(): array|null { return $this->details; } /** - * @return mixed|null + * @return string|null */ - public function getCardNumber() + public function getCardNumber(): string|null { return $this->cardNumber; } diff --git a/src/Webpay/TransaccionCompleta/Responses/TransactionCommitResponse.php b/src/Webpay/TransaccionCompleta/Responses/TransactionCommitResponse.php index ad5fe876..ef4c4a41 100644 --- a/src/Webpay/TransaccionCompleta/Responses/TransactionCommitResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/TransactionCommitResponse.php @@ -4,7 +4,7 @@ class TransactionCommitResponse extends TransactionStatusResponse { - public function __construct($json) + public function __construct(array $json) { parent::__construct($json); unset($this->balance); diff --git a/src/Webpay/TransaccionCompleta/Responses/TransactionCreateResponse.php b/src/Webpay/TransaccionCompleta/Responses/TransactionCreateResponse.php index d7332afa..558cab1f 100644 --- a/src/Webpay/TransaccionCompleta/Responses/TransactionCreateResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/TransactionCreateResponse.php @@ -6,19 +6,18 @@ class TransactionCreateResponse { - public $token; + public string|null $token; - public function __construct($json) + public function __construct(array $json) { $this->token = Utils::returnValueIfExists($json, 'token'); } /** - * @return mixed + * @return string|null */ - public function getToken() + public function getToken(): string|null { return $this->token; } - } diff --git a/src/Webpay/TransaccionCompleta/Responses/TransactionDetail.php b/src/Webpay/TransaccionCompleta/Responses/TransactionDetail.php index 9ee50059..8f93ddd8 100644 --- a/src/Webpay/TransaccionCompleta/Responses/TransactionDetail.php +++ b/src/Webpay/TransaccionCompleta/Responses/TransactionDetail.php @@ -7,8 +7,15 @@ class TransactionDetail extends BaseTransactionDetail { - public $prepaidBalance; + public int|float|null $prepaidBalance; + /** + * Creates an instance of TransactionDetail from an array + * + * @param array $array + * + * @return TransactionDetail + */ public static function createFromArray(array $array) { $result = new TransactionDetail(); @@ -28,11 +35,10 @@ public static function createFromArray(array $array) } /** - * @return mixed + * @return int|float|null */ - public function getPrepaidBalance() + public function getPrepaidBalance(): int|float|null { return $this->prepaidBalance; } - } diff --git a/src/Webpay/TransaccionCompleta/Responses/TransactionInstallmentsResponse.php b/src/Webpay/TransaccionCompleta/Responses/TransactionInstallmentsResponse.php index 3894b3ea..6752be33 100644 --- a/src/Webpay/TransaccionCompleta/Responses/TransactionInstallmentsResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/TransactionInstallmentsResponse.php @@ -6,11 +6,11 @@ class TransactionInstallmentsResponse { - public $installmentsAmount; - public $idQueryInstallments; - public $deferredPeriods; + public int|float|null $installmentsAmount; + public string|null $idQueryInstallments; + public array|null $deferredPeriods; - public function __construct($json) + public function __construct(array $json) { $this->installmentsAmount = Utils::returnValueIfExists($json, 'installments_amount'); $this->idQueryInstallments = Utils::returnValueIfExists($json, 'id_query_installments'); @@ -18,27 +18,26 @@ public function __construct($json) } /** - * @return mixed + * @return int|float|null */ - public function getInstallmentsAmount() + public function getInstallmentsAmount(): int|float|null { return $this->installmentsAmount; } /** - * @return mixed + * @return string|null */ - public function getIdQueryInstallments() + public function getIdQueryInstallments(): string|null { return $this->idQueryInstallments; } /** - * @return mixed + * @return array|null */ - public function getDeferredPeriods() + public function getDeferredPeriods(): array|null { return $this->deferredPeriods; } - } diff --git a/src/Webpay/TransaccionCompleta/Responses/TransactionRefundResponse.php b/src/Webpay/TransaccionCompleta/Responses/TransactionRefundResponse.php index e263dbfb..46d21dae 100644 --- a/src/Webpay/TransaccionCompleta/Responses/TransactionRefundResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/TransactionRefundResponse.php @@ -6,14 +6,14 @@ class TransactionRefundResponse { - public $type; - public $authorizationCode; - public $authorizationDate; - public $nullifiedAmount; - public $balance; - public $responseCode; + public string|null $type; + public string|null $authorizationCode; + public string|null $authorizationDate; + public int|float|null $nullifiedAmount; + public int|float|null $balance; + public int|null $responseCode; - public function __construct($json) + public function __construct(array $json) { $this->type = Utils::returnValueIfExists($json, 'type'); $this->authorizationCode = Utils::returnValueIfExists($json, 'authorization_code'); @@ -24,51 +24,50 @@ public function __construct($json) } /** - * @return mixed + * @return string|null */ - public function getType() + public function getType(): string|null { return $this->type; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationCode() + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationDate() + public function getAuthorizationDate(): string|null { return $this->authorizationDate; } /** - * @return mixed + * @return int|float|null */ - public function getNullifiedAmount() + public function getNullifiedAmount(): int|float|null { return $this->nullifiedAmount; } /** - * @return mixed + * @return int|float|null */ - public function getBalance() + public function getBalance(): int|float|null { return $this->balance; } /** - * @return mixed + * @return int|null */ - public function getResponseCode() + public function getResponseCode(): int|null { - return (int) $this->responseCode; + return $this->responseCode; } - } diff --git a/src/Webpay/TransaccionCompleta/Responses/TransactionStatusResponse.php b/src/Webpay/TransaccionCompleta/Responses/TransactionStatusResponse.php index 2661ba5d..02b60dd7 100644 --- a/src/Webpay/TransaccionCompleta/Responses/TransactionStatusResponse.php +++ b/src/Webpay/TransaccionCompleta/Responses/TransactionStatusResponse.php @@ -9,10 +9,10 @@ class TransactionStatusResponse { use HasTransactionStatus; - public $vci; - public $prepaidBalance; + public string|null $vci; + public int|float|null $prepaidBalance; - public function __construct($json) + public function __construct(array $json) { $this->vci = Utils::returnValueIfExists($json, 'vci'); $this->setTransactionStatusFields($json); @@ -20,19 +20,18 @@ public function __construct($json) } /** - * @return mixed + * @return string|null */ - public function getVci() + public function getVci(): string|null { return $this->vci; } - /** - * @return mixed + /** + * @return int|float|null */ - public function getPrepaidBalance() + public function getPrepaidBalance(): int|float|null { return $this->prepaidBalance; } - } diff --git a/src/Webpay/TransaccionCompleta/Transaction.php b/src/Webpay/TransaccionCompleta/Transaction.php index ceabedc4..2721317b 100644 --- a/src/Webpay/TransaccionCompleta/Transaction.php +++ b/src/Webpay/TransaccionCompleta/Transaction.php @@ -16,6 +16,7 @@ use Transbank\Webpay\TransaccionCompleta\Responses\TransactionCaptureResponse; use Transbank\Utils\InteractsWithWebpayApi; use Transbank\Webpay\Exceptions\WebpayRequestException; +use GuzzleHttp\Exception\GuzzleException; /** * Class Transaction. @@ -30,27 +31,28 @@ class Transaction const ENDPOINT_REFUND = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}/refunds'; const ENDPOINT_STATUS = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}'; const ENDPOINT_CAPTURE = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}/capture'; + const SEARCH_STRING = '{token}'; /** - * @param $buyOrder - * @param $sessionId - * @param $amount - * @param $cvv - * @param $cardNumber - * @param $cardExpirationDate + * @param string $buyOrder + * @param string $sessionId + * @param int|float $amount + * @param string $cardNumber + * @param string $cardExpirationDate + * @param string|null $cvv * * @throws TransactionCreateException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionCreateResponse */ public function create( - $buyOrder, - $sessionId, - $amount, - $cvv, - $cardNumber, - $cardExpirationDate + string $buyOrder, + string $sessionId, + int|float $amount, + string $cardNumber, + string $cardExpirationDate, + string|null $cvv = null, ) { $payload = [ 'buy_order' => $buyOrder, @@ -64,9 +66,10 @@ public function create( try { $response = $this->sendRequest('POST', static::ENDPOINT_CREATE, $payload); } catch (WebpayRequestException $exception) { - throw new TransactionCreateException($exception->getMessage(), + throw new TransactionCreateException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), - $exception->getHttpCode(), + $exception->getHttpCode() ?? 0, $exception->getFailedRequest(), $exception ); @@ -76,28 +79,29 @@ public function create( } /** - * @param $token - * @param $installmentsNumber + * @param string $token + * @param int $installmentsNumber * * @throws TransactionInstallmentsException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionInstallmentsResponse */ public function installments( - $token, - $installmentsNumber + string $token, + int $installmentsNumber ) { $payload = [ 'installments_number' => $installmentsNumber, ]; - $endpoint = str_replace('{token}', $token, static::ENDPOINT_INSTALLMENTS); + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_INSTALLMENTS); try { $response = $this->sendRequest('POST', $endpoint, $payload); } catch (WebpayRequestException $exception) { - throw new TransactionInstallmentsException($exception->getMessage(), + throw new TransactionInstallmentsException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -109,21 +113,21 @@ public function installments( } /** - * @param $token - * @param $idQueryInstallments - * @param $deferredPeriodIndex - * @param $gracePeriod + * @param string $token + * @param int|null $idQueryInstallments + * @param int|null $deferredPeriodIndex + * @param bool|null $gracePeriod * * @throws TransactionCommitException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionCommitResponse */ public function commit( - $token, - $idQueryInstallments = null, - $deferredPeriodIndex = null, - $gracePeriod = null + string $token, + int|null $idQueryInstallments = null, + int|null $deferredPeriodIndex = null, + bool|null $gracePeriod = null ) { $payload = [ 'id_query_installments' => $idQueryInstallments, @@ -131,12 +135,13 @@ public function commit( 'grace_period' => $gracePeriod, ]; - $endpoint = str_replace('{token}', $token, static::ENDPOINT_COMMIT); + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_COMMIT); try { $response = $this->sendRequest('PUT', $endpoint, $payload); } catch (WebpayRequestException $exception) { - throw new TransactionCommitException($exception->getMessage(), + throw new TransactionCommitException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -148,26 +153,27 @@ public function commit( } /** - * @param $token - * @param $amount + * @param string $token + * @param int|float $amount * * @throws TransactionRefundException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionRefundResponse */ - public function refund($token, $amount) + public function refund(string $token, int|float $amount) { $payload = [ 'amount' => $amount, ]; - $endpoint = str_replace('{token}', $token, static::ENDPOINT_REFUND); + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_REFUND); try { $response = $this->sendRequest('POST', $endpoint, $payload); } catch (WebpayRequestException $exception) { - throw new TransactionRefundException($exception->getMessage(), + throw new TransactionRefundException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -179,21 +185,22 @@ public function refund($token, $amount) } /** - * @param $token + * @param string $token * * @throws TransactionStatusException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionStatusResponse */ public function status($token) { - $endpoint = str_replace('{token}', $token, static::ENDPOINT_STATUS); + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_STATUS); try { - $response = $this->sendRequest('GET', $endpoint, null); + $response = $this->sendRequest('GET', $endpoint, []); } catch (WebpayRequestException $exception) { - throw new TransactionStatusException($exception->getMessage(), + throw new TransactionStatusException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -205,30 +212,35 @@ public function status($token) } /** - * @param $token - * @param $buyOrder - * @param $authorizationCode - * @param $captureAmount + * @param string $token + * @param string $buyOrder + * @param string $authorizationCode + * @param int|float $captureAmount * * @throws TransactionCaptureException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionCaptureResponse */ - public function capture($token, $buyOrder, $authorizationCode, $captureAmount) - { - $endpoint = str_replace('{token}', $token, static::ENDPOINT_CAPTURE); + public function capture( + string $token, + string $buyOrder, + string $authorizationCode, + int|float $captureAmount + ) { + $endpoint = str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_CAPTURE); $payload = [ 'buy_order' => $buyOrder, 'authorization_code' => $authorizationCode, - 'capture_amount' => (int) $captureAmount, + 'capture_amount' => $captureAmount, ]; try { $response = $this->sendRequest('PUT', $endpoint, $payload); } catch (WebpayRequestException $exception) { - throw new TransactionCaptureException($exception->getMessage(), + throw new TransactionCaptureException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), diff --git a/src/Webpay/WebpayPlus.php b/src/Webpay/WebpayPlus.php index 746199f6..4970edac 100644 --- a/src/Webpay/WebpayPlus.php +++ b/src/Webpay/WebpayPlus.php @@ -2,12 +2,17 @@ namespace Transbank\Webpay; -use Transbank\Contracts\RequestService; -use Transbank\Webpay\WebpayPlus\MallTransaction; -use Transbank\Webpay\WebpayPlus\Transaction; - /** * Class WebpayPlus. + * @var string INTEGRATION_COMMERCE_CODE + * @var string INTEGRATION_DEFERRED_COMMERCE_CODE + * @var string INTEGRATION_MALL_COMMERCE_CODE + * @var string INTEGRATION_MALL_CHILD_COMMERCE_CODE_1 + * @var string INTEGRATION_MALL_CHILD_COMMERCE_CODE_2 + * @var string INTEGRATION_MALL_DEFERRED_COMMERCE_CODE + * @var string INTEGRATION_MALL_DEFERRED_CHILD_COMMERCE_CODE_1 + * @var string INTEGRATION_MALL_DEFERRED_CHILD_COMMERCE_CODE_2 + * @var string INTEGRATION_API_KEY */ class WebpayPlus { diff --git a/src/Webpay/WebpayPlus/Exceptions/TransactionCommitException.php b/src/Webpay/WebpayPlus/Exceptions/TransactionCommitException.php index 7e77db10..a68ce09b 100644 --- a/src/Webpay/WebpayPlus/Exceptions/TransactionCommitException.php +++ b/src/Webpay/WebpayPlus/Exceptions/TransactionCommitException.php @@ -6,5 +6,5 @@ class TransactionCommitException extends WebpayRequestException { - protected static $defaultMessage = 'The transaction could not be confirmed'; + protected static string $defaultMessage = 'The transaction could not be confirmed'; } diff --git a/src/Webpay/WebpayPlus/Exceptions/TransactionCreateException.php b/src/Webpay/WebpayPlus/Exceptions/TransactionCreateException.php index e74e0445..52efa616 100644 --- a/src/Webpay/WebpayPlus/Exceptions/TransactionCreateException.php +++ b/src/Webpay/WebpayPlus/Exceptions/TransactionCreateException.php @@ -6,5 +6,5 @@ class TransactionCreateException extends WebpayRequestException { - protected static $defaultMessage = 'The transaction could not be created.'; + protected static string $defaultMessage = 'The transaction could not be created.'; } diff --git a/src/Webpay/WebpayPlus/Exceptions/TransactionRefundException.php b/src/Webpay/WebpayPlus/Exceptions/TransactionRefundException.php index 89ca5ca8..e86ec718 100644 --- a/src/Webpay/WebpayPlus/Exceptions/TransactionRefundException.php +++ b/src/Webpay/WebpayPlus/Exceptions/TransactionRefundException.php @@ -6,5 +6,5 @@ class TransactionRefundException extends WebpayRequestException { - protected static $defaultMessage = 'The transaction could not be refunded. '; + protected static string $defaultMessage = 'The transaction could not be refunded.'; } diff --git a/src/Webpay/WebpayPlus/Exceptions/TransactionStatusException.php b/src/Webpay/WebpayPlus/Exceptions/TransactionStatusException.php index 322736e0..860eaea7 100644 --- a/src/Webpay/WebpayPlus/Exceptions/TransactionStatusException.php +++ b/src/Webpay/WebpayPlus/Exceptions/TransactionStatusException.php @@ -6,5 +6,5 @@ class TransactionStatusException extends WebpayRequestException { - protected static $defaultMessage = 'Transaction Status could not be confirmed. Please verify given parameters'; + protected static string $defaultMessage = 'Transaction Status could not be confirmed. Please verify given parameters'; } diff --git a/src/Webpay/WebpayPlus/MallTransaction.php b/src/Webpay/WebpayPlus/MallTransaction.php index acccfee5..6efd7d7b 100644 --- a/src/Webpay/WebpayPlus/MallTransaction.php +++ b/src/Webpay/WebpayPlus/MallTransaction.php @@ -16,6 +16,7 @@ use Transbank\Webpay\WebpayPlus\Responses\MallTransactionCreateResponse; use Transbank\Webpay\WebpayPlus\Responses\MallTransactionRefundResponse; use Transbank\Webpay\WebpayPlus\Responses\MallTransactionStatusResponse; +use GuzzleHttp\Exception\GuzzleException; class MallTransaction { @@ -26,19 +27,20 @@ class MallTransaction const ENDPOINT_REFUND = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}/refunds'; const ENDPOINT_STATUS = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}'; const ENDPOINT_CAPTURE = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}/capture'; + const SEARCH_STRING = '{token}'; /** - * @param $buyOrder - * @param $sessionId - * @param $returnUrl - * @param $details + * @param string $buyOrder + * @param string $sessionId + * @param string $returnUrl + * @param array $details * * @throws MallTransactionCreateException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return MallTransactionCreateResponse */ - public function create($buyOrder, $sessionId, $returnUrl, $details) + public function create(string $buyOrder, string $sessionId, string $returnUrl, array $details) { $payload = [ 'buy_order' => $buyOrder, @@ -54,7 +56,8 @@ public function create($buyOrder, $sessionId, $returnUrl, $details) $payload ); } catch (WebpayRequestException $exception) { - throw new MallTransactionCreateException($exception->getMessage(), + throw new MallTransactionCreateException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -66,14 +69,14 @@ public function create($buyOrder, $sessionId, $returnUrl, $details) } /** - * @param $token + * @param string $token * * @throws MallTransactionCommitException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return MallTransactionCommitResponse */ - public function commit($token) + public function commit(string $token) { if (!is_string($token)) { throw new \InvalidArgumentException('Token parameter given is not string.'); @@ -85,11 +88,12 @@ public function commit($token) try { $response = $this->sendRequest( 'PUT', - str_replace('{token}', $token, static::ENDPOINT_COMMIT), - null + str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_COMMIT), + [] ); } catch (WebpayRequestException $exception) { - throw new MallTransactionCommitException($exception->getMessage(), + throw new MallTransactionCommitException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -101,17 +105,17 @@ public function commit($token) } /** - * @param $token - * @param $buyOrder - * @param $childCommerceCode - * @param $amount + * @param string $token + * @param string $buyOrder + * @param string $childCommerceCode + * @param int|float $amount * * @throws MallTransactionRefundException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return MallTransactionRefundResponse */ - public function refund($token, $buyOrder, $childCommerceCode, $amount) + public function refund(string $token, string $buyOrder, string $childCommerceCode, int|float $amount) { $payload = [ 'buy_order' => $buyOrder, @@ -122,11 +126,12 @@ public function refund($token, $buyOrder, $childCommerceCode, $amount) try { $response = $this->sendRequest( 'POST', - str_replace('{token}', $token, static::ENDPOINT_REFUND), + str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_REFUND), $payload ); } catch (WebpayRequestException $exception) { - throw new MallTransactionRefundException($exception->getMessage(), + throw new MallTransactionRefundException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -138,23 +143,24 @@ public function refund($token, $buyOrder, $childCommerceCode, $amount) } /** - * @param $token + * @param string $token * * @throws MallTransactionStatusException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return MallTransactionStatusResponse */ - public function status($token) + public function status(string $token) { try { $response = $this->sendRequest( 'GET', - str_replace('{token}', $token, static::ENDPOINT_STATUS), - null + str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_STATUS), + [] ); } catch (WebpayRequestException $exception) { - throw new MallTransactionStatusException($exception->getMessage(), + throw new MallTransactionStatusException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -166,23 +172,23 @@ public function status($token) } /** - * @param $childCommerceCode - * @param $token - * @param $buyOrder - * @param $authorizationCode - * @param null $captureAmount + * @param string $childCommerceCode + * @param string $token + * @param string $buyOrder + * @param string $authorizationCode + * @param int|float $captureAmount * * @throws MallTransactionCaptureException - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return MallTransactionCaptureResponse */ public function capture( - $childCommerceCode, - $token, - $buyOrder, - $authorizationCode, - $captureAmount + string $childCommerceCode, + string $token, + string $buyOrder, + string $authorizationCode, + int|float $captureAmount ) { $payload = [ 'commerce_code' => $childCommerceCode, @@ -194,11 +200,12 @@ public function capture( try { $response = $this->sendRequest( 'PUT', - str_replace('{token}', $token, static::ENDPOINT_CAPTURE), + str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_CAPTURE), $payload ); } catch (WebpayRequestException $exception) { - throw new MallTransactionCaptureException($exception->getMessage(), + throw new MallTransactionCaptureException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), diff --git a/src/Webpay/WebpayPlus/Responses/MallTransactionStatusResponse.php b/src/Webpay/WebpayPlus/Responses/MallTransactionStatusResponse.php index abef71b6..3fc18c44 100644 --- a/src/Webpay/WebpayPlus/Responses/MallTransactionStatusResponse.php +++ b/src/Webpay/WebpayPlus/Responses/MallTransactionStatusResponse.php @@ -9,21 +9,20 @@ */ class MallTransactionStatusResponse { - public $vci; - public $buyOrder; - public $sessionId; - public $cardNumber; - public $cardDetail; - public $expirationDate; - public $accountingDate; - public $transactionDate; + public string|null $vci; + public string|null $buyOrder; + public string|null $sessionId; + public string|null $cardNumber; + public array|null $cardDetail; + public string|null $expirationDate; + public string|null $accountingDate; + public string|null $transactionDate; + public array|null $details; /** * @var TransactionDetail[] */ - public $details; - - public function __construct($json) + public function __construct(array $json) { $this->vci = Utils::returnValueIfExists($json, 'vci'); $this->buyOrder = Utils::returnValueIfExists($json, 'buy_order'); @@ -33,12 +32,10 @@ public function __construct($json) $this->expirationDate = Utils::returnValueIfExists($json, 'expiration_date'); $this->accountingDate = Utils::returnValueIfExists($json, 'accounting_date'); $this->transactionDate = Utils::returnValueIfExists($json, 'transaction_date'); - $details = Utils::returnValueIfExists($json, 'details'); - $this->details = null; - if (is_array($details)) { - $this->details = []; - foreach ($details as $detail) { + $this->details = []; + if (is_array($json['details'])) { + foreach ($json['details'] as $detail) { $this->details[] = TransactionDetail::createFromArray($detail); } } @@ -49,7 +46,7 @@ public function __construct($json) * * @return bool */ - public function isApproved() + public function isApproved(): bool { if (!$details = $this->getDetails()) { return false; @@ -65,49 +62,49 @@ public function isApproved() } /** - * @return mixed + * @return string|null */ - public function getBuyOrder() + public function getBuyOrder(): string|null { return $this->buyOrder; } /** - * @return mixed + * @return string|null */ - public function getSessionId() + public function getSessionId(): string|null { return $this->sessionId; } /** - * @return mixed + * @return string|null */ - public function getCardNumber() + public function getCardNumber(): string|null { return $this->cardNumber; } /** - * @return mixed + * @return string|null */ - public function getExpirationDate() + public function getExpirationDate(): string|null { return $this->expirationDate; } /** - * @return mixed + * @return string|null */ - public function getAccountingDate() + public function getAccountingDate(): string|null { return $this->accountingDate; } /** - * @return mixed + * @return string|null */ - public function getTransactionDate() + public function getTransactionDate(): string|null { return $this->transactionDate; } @@ -115,25 +112,24 @@ public function getTransactionDate() /** * @return TransactionDetail[]|null */ - public function getDetails() + public function getDetails(): array|null { return $this->details; } /** - * @return mixed + * @return string|null */ - public function getVci() + public function getVci(): string|null { return $this->vci; } /** - * @return mixed|null + * @return array|null */ - public function getCardDetail() + public function getCardDetail(): array|null { return $this->cardDetail; } - } diff --git a/src/Webpay/WebpayPlus/Responses/TransactionCaptureResponse.php b/src/Webpay/WebpayPlus/Responses/TransactionCaptureResponse.php index ba6acaaa..de0ab968 100644 --- a/src/Webpay/WebpayPlus/Responses/TransactionCaptureResponse.php +++ b/src/Webpay/WebpayPlus/Responses/TransactionCaptureResponse.php @@ -10,14 +10,14 @@ class TransactionCaptureResponse /** * TransactionCaptureResponse constructor. * - * @param mixed $responseJson + * @param array $json */ - public $authorizationCode; - public $authorizationDate; - public $capturedAmount; - public $responseCode; + public string|null $authorizationCode; + public string|null $authorizationDate; + public int|float|null $capturedAmount; + public int|null $responseCode; - public function __construct($json) + public function __construct(array $json) { $this->authorizationCode = Utils::returnValueIfExists($json, 'authorization_code'); $this->authorizationDate = Utils::returnValueIfExists($json, 'authorization_date'); @@ -25,41 +25,43 @@ public function __construct($json) $this->responseCode = Utils::returnValueIfExists($json, 'response_code'); } - public function isApproved() + /** + * @return bool + */ + public function isApproved(): bool { return $this->responseCode === ResponseCodesEnum::RESPONSE_CODE_APPROVED; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationCode() + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationDate() + public function getAuthorizationDate(): string|null { return $this->authorizationDate; } /** - * @return mixed + * @return int|float|null */ - public function getCapturedAmount() + public function getCapturedAmount(): int|float|null { return $this->capturedAmount; } /** - * @return mixed + * @return int|null */ - public function getResponseCode() + public function getResponseCode(): int|null { return $this->responseCode; } - } diff --git a/src/Webpay/WebpayPlus/Responses/TransactionCreateResponse.php b/src/Webpay/WebpayPlus/Responses/TransactionCreateResponse.php index 4ad95def..ef7e6d46 100644 --- a/src/Webpay/WebpayPlus/Responses/TransactionCreateResponse.php +++ b/src/Webpay/WebpayPlus/Responses/TransactionCreateResponse.php @@ -9,12 +9,12 @@ class TransactionCreateResponse /** * @var string|null */ - public $token; + public string|null $token; /** * @var string|null */ - public $url; + public string|null $url; /** * TransactionCreateResponse constructor. @@ -25,7 +25,7 @@ class TransactionCreateResponse * + url (string, required) - the url returned from a successful call to * 'create' */ - public function __construct($json) + public function __construct(array $json) { $this->token = Utils::returnValueIfExists($json, 'token'); $this->url = Utils::returnValueIfExists($json, 'url'); @@ -40,7 +40,7 @@ public function __construct($json) * * @return $this */ - public function fromJSON($json) + public function fromJSON(array $json) { $this->token = Utils::returnValueIfExists($json, 'token'); $this->url = Utils::returnValueIfExists($json, 'url'); @@ -51,7 +51,7 @@ public function fromJSON($json) /** * @return string|null */ - public function getToken() + public function getToken(): string|null { return $this->token; } @@ -59,9 +59,8 @@ public function getToken() /** * @return string|null */ - public function getUrl() + public function getUrl(): string|null { return $this->url; } - } diff --git a/src/Webpay/WebpayPlus/Responses/TransactionDetail.php b/src/Webpay/WebpayPlus/Responses/TransactionDetail.php index 6caa90ca..94b2a8e1 100644 --- a/src/Webpay/WebpayPlus/Responses/TransactionDetail.php +++ b/src/Webpay/WebpayPlus/Responses/TransactionDetail.php @@ -8,16 +8,16 @@ class TransactionDetail { - public $amount; - public $status; - public $authorizationCode; - public $paymentTypeCode; - public $responseCode; - public $installmentsNumber; - public $installmentsAmount; - public $commerceCode; - public $buyOrder; - public $balance; + public int|float|null $amount; + public string|null $status; + public string|null $authorizationCode; + public string|null $paymentTypeCode; + public int|null $responseCode; + public int|null $installmentsNumber; + public int|float|null $installmentsAmount; + public string|null $commerceCode; + public string|null $buyOrder; + public int|float|null $balance; public static function createFromArray(array $array) { @@ -36,102 +36,104 @@ public static function createFromArray(array $array) return $result; } - public function isApproved() + /** + * @return bool + */ + public function isApproved(): bool { - if($this->getResponseCode() !== ResponseCodesEnum::RESPONSE_CODE_APPROVED) { + if ($this->getResponseCode() !== ResponseCodesEnum::RESPONSE_CODE_APPROVED) { return false; } - switch($this->getStatus()) { + switch ($this->getStatus()) { case TransactionStatusEnum::STATUS_CAPTURED: case TransactionStatusEnum::STATUS_REVERSED: case TransactionStatusEnum::STATUS_NULLIFIED: case TransactionStatusEnum::STATUS_AUTHORIZED: case TransactionStatusEnum::STATUS_PARTIALLY_NULLIFIED: return true; - default : + default: return false; } } /** - * @return mixed + * @return int|float|null */ - public function getAmount() + public function getAmount(): int|float|null { return $this->amount; } /** - * @return mixed + * @return string|null */ - public function getStatus() + public function getStatus(): string|null { return $this->status; } /** - * @return mixed + * @return string|null */ - public function getAuthorizationCode() + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } /** - * @return mixed + * @return string|null */ - public function getPaymentTypeCode() + public function getPaymentTypeCode(): string|null { return $this->paymentTypeCode; } /** - * @return mixed + * @return int|null */ - public function getResponseCode() + public function getResponseCode(): int|null { return $this->responseCode; } /** - * @return mixed + * @return int|null */ - public function getInstallmentsNumber() + public function getInstallmentsNumber(): int|null { return $this->installmentsNumber; } /** - * @return mixed + * @return string|null */ - public function getCommerceCode() + public function getCommerceCode(): string|null { return $this->commerceCode; } /** - * @return mixed + * @return string|null */ - public function getBuyOrder() + public function getBuyOrder(): string|null { return $this->buyOrder; } /** - * @return mixed + * @return int|float|null */ - public function getInstallmentsAmount() + public function getInstallmentsAmount(): int|float|null { return $this->installmentsAmount; } /** - * @return mixed + * @return int|float|null */ - public function getBalance() + public function getBalance(): int|float|null { return $this->balance; } - } diff --git a/src/Webpay/WebpayPlus/Responses/TransactionRefundResponse.php b/src/Webpay/WebpayPlus/Responses/TransactionRefundResponse.php index 5432d725..5ca250a5 100644 --- a/src/Webpay/WebpayPlus/Responses/TransactionRefundResponse.php +++ b/src/Webpay/WebpayPlus/Responses/TransactionRefundResponse.php @@ -8,36 +8,36 @@ class TransactionRefundResponse { /** - * @var mixed|null + * @var string */ - public $type; + public string|null $type; /** - * @var mixed|null + * @var string|null */ - public $authorizationCode; + public string|null $authorizationCode; /** - * @var mixed|null + * @var string|null */ - public $authorizationDate; + public string|null $authorizationDate; /** - * @var mixed|null + * @var int|float|null */ - public $nullifiedAmount; + public int|float|null $nullifiedAmount; /** - * @var mixed|null + * @var int|float|null */ - public $balance; + public int|float|null $balance; /** - * @var mixed|null + * @var int|null */ - public $responseCode; + public int|null $responseCode; /** * TransactionRefundResponse constructor. * - * @param $json + * @param array $json */ - public function __construct($json) + public function __construct(array $json) { $this->type = Utils::returnValueIfExists($json, 'type'); $this->authorizationCode = Utils::returnValueIfExists($json, 'authorization_code'); @@ -47,58 +47,60 @@ public function __construct($json) $this->responseCode = Utils::returnValueIfExists($json, 'response_code'); } - public function success() + /** + * @return bool + */ + public function success(): bool { return $this->getType() === TransactionStatusEnum::STATUS_REVERSED || ($this->getType() === TransactionStatusEnum::STATUS_NULLIFIED && $this->getResponseCode() === 0); } /** - * @return mixed|null + * @return int|float|null */ - public function getNullifiedAmount() + public function getNullifiedAmount(): int|float|null { return $this->nullifiedAmount; } /** - * @return mixed|null + * @return int|float|null */ - public function getBalance() + public function getBalance(): int|float|null { return $this->balance; } /** - * @return mixed|null + * @return int|null */ - public function getResponseCode() + public function getResponseCode(): int|null { - return (int) $this->responseCode; + return $this->responseCode; } /** - * @return mixed|null + * @return string|null */ - public function getType() + public function getType(): string|null { return $this->type; } /** - * @return mixed|null + * @return string|null */ - public function getAuthorizationCode() + public function getAuthorizationCode(): string|null { return $this->authorizationCode; } /** - * @return mixed|null + * @return string|null */ - public function getAuthorizationDate() + public function getAuthorizationDate(): string|null { return $this->authorizationDate; } - } diff --git a/src/Webpay/WebpayPlus/Responses/TransactionStatusResponse.php b/src/Webpay/WebpayPlus/Responses/TransactionStatusResponse.php index 35c3b861..d689b5bb 100644 --- a/src/Webpay/WebpayPlus/Responses/TransactionStatusResponse.php +++ b/src/Webpay/WebpayPlus/Responses/TransactionStatusResponse.php @@ -10,9 +10,9 @@ class TransactionStatusResponse { use HasTransactionStatus; - public $vci; + public string|null $vci; - public function __construct($json) + public function __construct(array $json) { $this->vci = Utils::returnValueIfExists($json, 'vci'); $this->setTransactionStatusFields($json); @@ -23,38 +23,38 @@ public function __construct($json) * * @return bool */ - public function isApproved() + public function isApproved(): bool { - if($this->getResponseCode() !== ResponseCodesEnum::RESPONSE_CODE_APPROVED) { + if ($this->getResponseCode() !== ResponseCodesEnum::RESPONSE_CODE_APPROVED) { return false; } - switch($this->getStatus()) { + switch ($this->getStatus()) { case TransactionStatusEnum::STATUS_CAPTURED: case TransactionStatusEnum::STATUS_REVERSED: case TransactionStatusEnum::STATUS_NULLIFIED: case TransactionStatusEnum::STATUS_AUTHORIZED: case TransactionStatusEnum::STATUS_PARTIALLY_NULLIFIED: return true; - default : + default: return false; } } /** - * @return mixed + * @return string|null */ - public function getVci() + public function getVci(): string|null { return $this->vci; } /** - * @param mixed $vci + * @param string $vci * * @return TransactionStatusResponse */ - public function setVci($vci) + public function setVci(string $vci): TransactionStatusResponse { $this->vci = $vci; diff --git a/src/Webpay/WebpayPlus/Transaction.php b/src/Webpay/WebpayPlus/Transaction.php index 853c597a..568c5df6 100644 --- a/src/Webpay/WebpayPlus/Transaction.php +++ b/src/Webpay/WebpayPlus/Transaction.php @@ -3,6 +3,7 @@ namespace Transbank\Webpay\WebpayPlus; use Transbank\Utils\InteractsWithWebpayApi; +use GuzzleHttp\Exception\GuzzleException; use Transbank\Webpay\Exceptions\WebpayRequestException; use Transbank\Webpay\WebpayPlus\Exceptions\TransactionCaptureException; use Transbank\Webpay\WebpayPlus\Exceptions\TransactionCommitException; @@ -17,6 +18,12 @@ /** * Class Transaction. + * @var string ENDPOINT_CREATE + * @var string ENDPOINT_COMMIT + * @var string ENDPOINT_REFUND + * @var string ENDPOINT_STATUS + * @var string ENDPOINT_CAPTURE + * @var string SEARCH_STRING */ class Transaction { @@ -27,20 +34,25 @@ class Transaction const ENDPOINT_REFUND = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}/refunds'; const ENDPOINT_STATUS = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}'; const ENDPOINT_CAPTURE = 'rswebpaytransaction/api/webpay/v1.2/transactions/{token}/capture'; + const SEARCH_STRING = '{token}'; /** - * @param string $buyOrder - * @param string $sessionId - * @param int $amount - * @param string $returnUrl + * @param string $buyOrder + * @param string $sessionId + * @param int|float $amount + * @param string $returnUrl * * @throws TransactionCreateException - * @throws GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionCreateResponse */ - public function create($buyOrder, $sessionId, $amount, $returnUrl) - { + public function create( + string $buyOrder, + string $sessionId, + int|float $amount, + string $returnUrl + ): TransactionCreateResponse { $payload = [ 'buy_order' => $buyOrder, 'session_id' => $sessionId, @@ -51,7 +63,8 @@ public function create($buyOrder, $sessionId, $amount, $returnUrl) try { $response = $this->sendRequest('POST', static::ENDPOINT_CREATE, $payload); } catch (WebpayRequestException $exception) { - throw new TransactionCreateException($exception->getMessage(), + throw new TransactionCreateException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -63,14 +76,14 @@ public function create($buyOrder, $sessionId, $amount, $returnUrl) } /** - * @param $token + * @param string $token * * @throws TransactionCommitException - * @throws GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionCommitResponse */ - public function commit($token) + public function commit(string $token): TransactionCommitResponse { if (!is_string($token)) { throw new \InvalidArgumentException('Token parameter given is not string.'); @@ -82,11 +95,12 @@ public function commit($token) try { $response = $this->sendRequest( 'PUT', - str_replace('{token}', $token, static::ENDPOINT_COMMIT), - null + str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_COMMIT), + [] ); } catch (WebpayRequestException $exception) { - throw new TransactionCommitException($exception->getMessage(), + throw new TransactionCommitException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -98,24 +112,25 @@ public function commit($token) } /** - * @param $token - * @param $amount + * @param string $token + * @param int|float $amount * * @throws TransactionRefundException - * @throws GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionRefundResponse */ - public function refund($token, $amount) + public function refund(string $token, int|float $amount): TransactionRefundResponse { try { $response = $this->sendRequest( 'POST', - str_replace('{token}', $token, static::ENDPOINT_REFUND), + str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_REFUND), ['amount' => $amount] ); } catch (WebpayRequestException $exception) { - throw new TransactionRefundException($exception->getMessage(), + throw new TransactionRefundException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -127,23 +142,24 @@ public function refund($token, $amount) } /** - * @param $token + * @param string $token * * @throws TransactionStatusException - * @throws GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionStatusResponse */ - public function status($token) + public function status(string $token): TransactionStatusResponse { try { $response = $this->sendRequest( 'GET', - str_replace('{token}', $token, static::ENDPOINT_STATUS), - null + str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_STATUS), + [] ); } catch (WebpayRequestException $exception) { - throw new TransactionStatusException($exception->getMessage(), + throw new TransactionStatusException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -155,18 +171,22 @@ public function status($token) } /** - * @param $token - * @param $buyOrder - * @param $authorizationCode - * @param $captureAmount + * @param string $token + * @param string $buyOrder + * @param string $authorizationCode + * @param int|float $captureAmount * * @throws TransactionCaptureException - * @throws GuzzleHttp\Exception\GuzzleException + * @throws GuzzleException * * @return TransactionCaptureResponse */ - public function capture($token, $buyOrder, $authorizationCode, $captureAmount) - { + public function capture( + string $token, + string $buyOrder, + string $authorizationCode, + int|float $captureAmount + ): TransactionCaptureResponse { $payload = [ 'buy_order' => $buyOrder, 'authorization_code' => $authorizationCode, @@ -176,11 +196,12 @@ public function capture($token, $buyOrder, $authorizationCode, $captureAmount) try { $response = $this->sendRequest( 'PUT', - str_replace('{token}', $token, static::ENDPOINT_CAPTURE), + str_replace(self::SEARCH_STRING, $token, static::ENDPOINT_CAPTURE), $payload ); } catch (WebpayRequestException $exception) { - throw new TransactionCaptureException($exception->getMessage(), + throw new TransactionCaptureException( + $exception->getMessage(), $exception->getTransbankErrorMessage(), $exception->getHttpCode(), $exception->getFailedRequest(), @@ -190,5 +211,4 @@ public function capture($token, $buyOrder, $authorizationCode, $captureAmount) return new TransactionCaptureResponse($response); } - }