Skip to content

Commit

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

Add x-idempotency-key header to requests
  • Loading branch information
meliguilhermefernandes authored Feb 5, 2024
2 parents edc06ce + 1929722 commit f5f97bd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ First time using Mercado Pago? Create your [Mercado Pago account](https://www.me
1. Download [Composer](https://getcomposer.org/doc/00-intro.md) if not already installed

2. On your project directory run on the command line
`composer require "mercadopago/dx-php:2.6.1"` for PHP7 or `composer require "mercadopago/dx-php:1.12.5"` for PHP5.6.
`composer require "mercadopago/dx-php:2.6.2"` for PHP7 or `composer require "mercadopago/dx-php:1.12.5"` for PHP5.6.

3. Copy the access_token in the [credentials](https://www.mercadopago.com/mlb/account/credentials) section of the page and replace YOUR_ACCESS_TOKEN with it.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"homepage": "https://github.com/mercadopago/sdk-php",
"license": "MIT",
"version": "2.6.1",
"version": "2.6.2",
"config": {
"platform": {
"php": "7.1"
Expand Down
49 changes: 30 additions & 19 deletions src/MercadoPago/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ 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,43 @@ 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
*/
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 = '2.6.1';
$_VERSION = '2.6.2';
}

0 comments on commit f5f97bd

Please sign in to comment.