Skip to content

Commit f5f97bd

Browse files
Merge pull request #496 from mercadopago/feature/add-idempotency-key-to-headers-v2
Add x-idempotency-key header to requests
2 parents edc06ce + 1929722 commit f5f97bd

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ First time using Mercado Pago? Create your [Mercado Pago account](https://www.me
1717
1. Download [Composer](https://getcomposer.org/doc/00-intro.md) if not already installed
1818

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

2222
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.
2323

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "library",
55
"homepage": "https://github.com/mercadopago/sdk-php",
66
"license": "MIT",
7-
"version": "2.6.1",
7+
"version": "2.6.2",
88
"config": {
99
"platform": {
1010
"php": "7.1"

src/MercadoPago/Manager.php

+30-19
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function execute($entity, $method = 'get', $options = [])
8484

8585
$this->_setDefaultHeaders($configuration->query);
8686
$this->_setCustomHeaders($entity, $configuration->query);
87-
//$this->_setIdempotencyHeader($configuration->query, $configuration, $method);
87+
$this->_setIdempotencyHeader($configuration->query, $method);
8888
$this->setQueryParams($entity);
8989

9090
return $this->_client->{$method}($configuration->url, $configuration->query);
@@ -409,32 +409,43 @@ protected function _setDefaultHeaders(&$query)
409409
* @param $configuration
410410
* @param string $method
411411
*/
412-
protected function _setIdempotencyHeader(&$query, $configuration, $method)
412+
protected function _setIdempotencyHeader(&$query, $method)
413413
{
414-
if (!isset($configuration->methods[$method])) {
415-
return;
416-
}
417-
$fields = '';
418-
if ($configuration->methods[$method]['idempotency']) {
419-
$fields = $this->_getIdempotencyAttributes($configuration->attributes);
420-
}
421-
if ($fields != '') {
422-
$query['headers']['x-idempotency-key'] = hash(self::$CIPHER, $fields);
414+
if ($method != 'get' && $method != 'delete') {
415+
if (array_key_exists('headers', array_change_key_case($query)) && !array_key_exists('x-idempotency-key', array_change_key_case($query['headers']))){
416+
$query['headers']['x-idempotency-key'] = $this->_generateUUID();
417+
}
423418
}
424419
}
425420
/**
426421
* @param $attributes
427422
*
428423
* @return string
429424
*/
430-
protected function _getIdempotencyAttributes($attributes)
425+
protected function _generateUUID()
431426
{
432-
$result = [];
433-
foreach ($attributes as $key => $value) {
434-
if ($value['idempotency']) {
435-
$result[] = $key;
436-
}
437-
}
438-
return implode('&', $result);
427+
return sprintf(
428+
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
429+
// 32 bits for "time_low"
430+
mt_rand(0, 0xffff),
431+
mt_rand(0, 0xffff),
432+
433+
// 16 bits for "time_mid"
434+
mt_rand(0, 0xffff),
435+
436+
// 16 bits for "time_hi_and_version",
437+
// four most significant bits holds version number 4
438+
mt_rand(0, 0x0fff) | 0x4000,
439+
440+
// 16 bits, 8 bits for "clk_seq_hi_res",
441+
// 8 bits for "clk_seq_low",
442+
// two most significant bits holds zero and one for variant DCE1.1
443+
mt_rand(0, 0x3fff) | 0x8000,
444+
445+
// 48 bits for "node"
446+
mt_rand(0, 0xffff),
447+
mt_rand(0, 0xffff),
448+
mt_rand(0, 0xffff)
449+
);
439450
}
440451
}

src/MercadoPago/Version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
class Version
55
{
66
public static
7-
$_VERSION = '2.6.1';
7+
$_VERSION = '2.6.2';
88
}

0 commit comments

Comments
 (0)