Skip to content

Commit

Permalink
Merge pull request #495 from mercadopago/feature/add-idempotency-key-…
Browse files Browse the repository at this point in the history
…to-headers

Add x-idempotency-key header to requests
  • Loading branch information
meliguilhermefernandes authored Feb 5, 2024
2 parents 4bb27cc + 1f45c4b commit 3230f45
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The SDK supports PHP 5.6 or major
#### Using Composer

1. Download [Composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos) if not already installed
2. Go to your project directory and run `composer require "mercadopago/dx-php:1.12.5"` on the command line.
2. Go to your project directory and run `composer require "mercadopago/dx-php:1.12.6"` on the command line.
3. This how your directory structure would look like.
4. Thats all, you have Mercado Pago SDK installed.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "library",
"homepage": "https://github.com/mercadopago/sdk-php",
"license": "MIT",
"version": "1.12.5",
"version": "1.12.6",
"config": {
"platform": {
"php": "5.6"
Expand Down
55 changes: 32 additions & 23 deletions src/MercadoPago/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public function execute($entity, $method = 'get', $options = [])

$this->_setDefaultHeaders($configuration->query);
$this->_setCustomHeaders($entity, $configuration->query);
//$this->_setIdempotencyHeader($configuration->query, $configuration, $method);
$this->_setIdempotencyHeader($configuration->query, $method);
$this->setQueryParams($entity);

return $this->_client->{$method}($configuration->url, $configuration->query);
}

Expand Down Expand Up @@ -409,32 +409,41 @@ protected function _setDefaultHeaders(&$query)
* @param $configuration
* @param string $method
*/
protected function _setIdempotencyHeader(&$query, $configuration, $method)
protected function _setIdempotencyHeader(&$query, $method)
{
if (!isset($configuration->methods[$method])) {
return;
}
$fields = '';
if ($configuration->methods[$method]['idempotency']) {
$fields = $this->_getIdempotencyAttributes($configuration->attributes);
}
if ($fields != '') {
$query['headers']['x-idempotency-key'] = hash(self::$CIPHER, $fields);
if ($method != 'get' && $method != 'delete') {
if (array_key_exists('headers', array_change_key_case($query)) && !array_key_exists('x-idempotency-key', array_change_key_case($query['headers']))){
$query['headers']['x-idempotency-key'] = $this->_generateUUID();
}
}
}
/**
* @param $attributes
*
* @return string
* _generateUUID()
*/
protected function _getIdempotencyAttributes($attributes)
protected function _generateUUID()
{
$result = [];
foreach ($attributes as $key => $value) {
if ($value['idempotency']) {
$result[] = $key;
}
}
return implode('&', $result);
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),

// 16 bits for "time_mid"
mt_rand(0, 0xffff),

// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,

// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,

// 48 bits for "node"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
}
2 changes: 1 addition & 1 deletion src/MercadoPago/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
class Version
{
public static
$_VERSION = '1.12.5';
$_VERSION = '1.12.6';
}

0 comments on commit 3230f45

Please sign in to comment.