Skip to content

Commit

Permalink
Merge pull request #425 from mercadopago/feature/implement-payment-cl…
Browse files Browse the repository at this point in the history
…ient

Implement payment client
  • Loading branch information
rhames07 committed Aug 10, 2023
2 parents addedd7 + 010aeed commit c68c35b
Show file tree
Hide file tree
Showing 46 changed files with 2,800 additions and 48 deletions.
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
"type": "library",
"license": "MIT",
"homepage": "https://github.com/mercadopago/sdk-php",
"config": {
"platform": {
"php": "8.2"
}
},
"autoload": {
"psr-4": {
"MercadoPago\\": [
Expand Down
7 changes: 2 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions src/MercadoPago/Client/MercadoPagoClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace MercadoPago\Client;

use MercadoPago\Core\MPRequestOptions;
use MercadoPago\MercadoPagoConfig;
use MercadoPago\Net\MPHttpClient;
use MercadoPago\Net\MPRequest;
use MercadoPago\Net\MPResponse;

/** Mercado Pago client class. */
class MercadoPagoClient
{
/**
* MercadoPagoClient constructor.
* @param \MercadoPago\Net\MPHttpClient $http_client http client to be used.
*/
public function __construct(protected MPHttpClient $http_client)
{
}

/**
* Method used directly or by other methods to make requests with request options.
* @param string $uri path to be requested.
* @param string $method method to be used.
* @param mixed $payload payload to be sent.
* @param mixed $query_params query params to be sent.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Net\MPResponse response from the request.
*/
protected function send(string $uri, string $method, ?string $payload = null, ?array $query_params = [], ?MPRequestOptions $request_options = null): MPResponse
{
return $this->http_client->send($this->buildRequest($uri, $method, $payload, $query_params, $request_options));
}

private function buildRequest(
string $path,
string $method,
?string $payload = null,
?array $query_params = [],
?MPRequestOptions $request_options = null
): MPRequest {
$path = $this->formatUrlWithQueryParams($path, $query_params);
return new MPRequest($path, $method, $payload, $this->addHeaders($request_options), $this->addConnectionTimeout($request_options));
}

private function formatUrlWithQueryParams(string $url, ?array $query_params): string
{
if (!empty($query_params)) {
$query_string = http_build_query($query_params);

if (strpos($url, '?') !== false) {
$url .= '&' . $query_string;
} else {
$url .= '?' . $query_string;
}
}
return $url;
}

private function addHeaders(?MPRequestOptions $request_options = null): array
{
$headers = array();
$headers = $this->addCustomHeaders($headers, $request_options);
$headers = $this->addDefaultHeaders($headers, $request_options);
return $headers;
}

private function addCustomHeaders(array $headers, ?MPRequestOptions $request_options = null): array
{
if (!is_null($request_options) && !is_null($request_options->getCustomHeaders())) {
return array_merge($headers, $request_options->getCustomHeaders());
}
return $headers;
}

private function addDefaultHeaders(array $headers, ?MPRequestOptions $request_options = null): array
{
$default_headers = array(
'Accept: application/json',
'Content-Type: application/json; charset=UTF-8',
'Authorization: Bearer ' . $this->getAccessToken($request_options),
'X-Product-Id: ' . MercadoPagoConfig::$PRODUCT_ID,
'User-Agent: MercadoPago DX-PHP SDK/' . MercadoPagoConfig::$CURRENT_VERSION,
'X-Tracking-Id: platform:' . PHP_MAJOR_VERSION . '|' . PHP_VERSION . ',type:SDK' . MercadoPagoConfig::$CURRENT_VERSION . ',so;'
);

return array_merge($headers, $default_headers);
}

private function getAccessToken(?MPRequestOptions $request_options = null): string
{
return $request_options?->getAccessToken() ?? MercadoPagoConfig::getAccessToken();
}

private function addConnectionTimeout(?MPRequestOptions $request_options = null): int
{
return ($request_options?->getConnectionTimeout() ?? 0) > 0
? $request_options->getConnectionTimeout()
: MercadoPagoConfig::getConnectionTimeout();
}
}
10 changes: 10 additions & 0 deletions src/MercadoPago/Client/Payment/PaymentCancelRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MercadoPago\Client\Payment;

/** PaymentCancelRequest class. */
class PaymentCancelRequest
{
/** Status cancelled. */
public string $status = "cancelled";
}
13 changes: 13 additions & 0 deletions src/MercadoPago/Client/Payment/PaymentCaptureRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MercadoPago\Client\Payment;

/** PaymentCaptureRequest class. */
class PaymentCaptureRequest
{
/** Status cancelled. */
public bool $capture = true;

/** Transaction amount. */
public float $transaction_amount;
}
124 changes: 124 additions & 0 deletions src/MercadoPago/Client/Payment/PaymentClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace MercadoPago\Client\Payment;

use MercadoPago\Client\MercadoPagoClient;
use MercadoPago\Core\MPRequestOptions;
use MercadoPago\Exceptions\MPApiException;
use MercadoPago\MercadoPagoConfig;
use MercadoPago\Net\HttpMethod;
use MercadoPago\Net\MPSearchRequest;
use MercadoPago\Resources\Payment;
use MercadoPago\Resources\PaymentSearch;
use MercadoPago\Serialization\Serializer;

/** Client responsible for performing payment actions. */
class PaymentClient extends MercadoPagoClient
{
private static $URL = "/v1/payments";

private static $URL_WITH_ID = "/v1/payments/%s";

private static $URL_SEARCH = "/v1/payments/search";

/** Default constructor. Uses the default http client used by the SDK. */
public function __construct()
{
parent::__construct(MercadoPagoConfig::getHttpClient());
}

/**
* Method responsible for creating payment.
* @param array $request payment data.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\Payment payment created.
*/
public function create(array $request, ?MPRequestOptions $request_options = null): Payment
{
try {
$response = parent::send(self::$URL, HttpMethod::POST, json_encode($request), null, $request_options);
$result = Serializer::deserializeFromJson(Payment::class, $response->getContent());
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}

/**
* Method responsible for getting payment.
* @param int $id payment id.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\Payment payment found.
*/
public function get(int $id, ?MPRequestOptions $request_options = null): Payment
{
try {
$response = parent::send(sprintf(self::$URL_WITH_ID, strval($id)), HttpMethod::GET, null, null, $request_options);
$result = Serializer::deserializeFromJson(Payment::class, $response->getContent());
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}

/**
* Method responsible for cancel payment.
* @param int $id payment id.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\Payment payment canceled.
*/
public function cancel(int $id, ?MPRequestOptions $request_options = null): Payment
{
try {
$payload = new PaymentCancelRequest();
$response = parent::send(sprintf(self::$URL_WITH_ID, strval($id)), HttpMethod::PUT, json_encode($payload), null, $request_options);
$result = Serializer::deserializeFromJson(Payment::class, $response->getContent());
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}

/**
* Method responsible for capture payment.
* @param int $id payment id.
* @param mixed $amount amount to be captured.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\Payment payment captured.
*/
public function capture(int $id, ?float $amount, ?MPRequestOptions $request_options = null): Payment
{
try {
$payload = new PaymentCaptureRequest();
$payload->transaction_amount = $amount;
$response = parent::send(sprintf(self::$URL_WITH_ID, strval($id)), HttpMethod::PUT, json_encode($payload), null, $request_options);
$result = Serializer::deserializeFromJson(Payment::class, $response->getContent());
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}

/**
* Method responsible for search payments.
* @param \MercadoPago\Net\MPSearchRequest $request search request.
* @param mixed $requestOptions request options to be sent.
* @return \MercadoPago\Resources\PaymentSearch search results.
*/
public function search(MPSearchRequest $request, ?MPRequestOptions $requestOptions = null): PaymentSearch
{
try {
$queryParams = isset($request) ? $request->getParameters() : null;
$response = parent::send(self::$URL_SEARCH, HttpMethod::GET, null, $queryParams, $requestOptions);
$result = Serializer::deserializeFromJson(PaymentSearch::class, $response->getContent());
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}
}
62 changes: 48 additions & 14 deletions src/MercadoPago/Core/MPRequestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,73 @@
/** MPRequestOptions class. */
class MPRequestOptions
{
public $access_token;

public $connection_timeout;

public $custom_headers;
/**
* MPRequestOptions constructor.
* @param string|null $access_token access token to be used.
* @param int|null $connection_timeout connection timeout to be used.
* @param array|null $custom_headers custom headers to be used.
*/
public function __construct(
private ?string $access_token = null,
private ?int $connection_timeout = null,
private ?array $custom_headers = null
) {
}

public function getAccessToken(): string
/**
* Get access token.
* @return string|null access token.
*/
public function getAccessToken(): string | null
{
return $this->access_token;
}

public function setAccessToken($access_token): void
/**
* Set access token.
* @param string $access_token access token to be set.
* @return void access token.
*/
public function setAccessToken(string $access_token): void
{
$this->$access_token = $access_token;
$this->access_token = $access_token;
}

public function getConnectionTimeout(): int
/**
* Get connection timeout.
* @return int|null connection timeout.
*/
public function getConnectionTimeout(): int | null
{
return $this->connection_timeout;
}

public function setConnectionTimeout($connection_timeout): void
/**
* Set connection timeout.
* @param int $connection_timeout connection timeout to be set.
* @return void connection timeout.
*/
public function setConnectionTimeout(int $connection_timeout): void
{
$this->$connection_timeout = $connection_timeout;
$this->connection_timeout = $connection_timeout;
}

public function getCustomHeaders(): array
/**
* Get custom headers.
* @return array|null custom headers.
*/
public function getCustomHeaders(): array | null
{
return $this->custom_headers;
}

public function setCustomHeaders($custom_headers): void
/**
* Set custom headers.
* @param array $custom_headers custom headers to be set.
* @return void custom headers.
*/
public function setCustomHeaders(array $custom_headers): void
{
$this->$custom_headers = $custom_headers;
$this->custom_headers = $custom_headers;
}
}
4 changes: 2 additions & 2 deletions src/MercadoPago/Exceptions/MPApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
/** MPApiException class. */
class MPApiException extends Exception
{
private $status_code;
private int $status_code;

private $api_response;
private MPResponse $api_response;

/**
* MPApiException constructor.
Expand Down
5 changes: 1 addition & 4 deletions src/MercadoPago/Net/MPDefaultHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public function send(MPRequest $request): MPResponse

private function createHttpRequestOptions(MPRequest $request): array
{
$connection_timeout =
$request->getConnectionTimeout() != 0
? $request->getConnectionTimeout()
: MercadoPagoConfig::getConnectionTimeout();
$connection_timeout = $request->getConnectionTimeout() ?: MercadoPagoConfig::getConnectionTimeout();

return array(
CURLOPT_URL => MercadoPagoConfig::$BASE_URL . $request->getUri(),
Expand Down
Loading

0 comments on commit c68c35b

Please sign in to comment.