Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x-idempotency-key header to requests #496

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
);
}
}
Loading