-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #425 from mercadopago/feature/implement-payment-cl…
…ient Implement payment client
- Loading branch information
Showing
46 changed files
with
2,800 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.