Skip to content

Commit

Permalink
Merge pull request #232 from mercadopago/fix/numericAtributeToJSON
Browse files Browse the repository at this point in the history
Added OAuth functionality
  • Loading branch information
Pedro Gonçalves committed Nov 18, 2019
2 parents e8764a9 + d2406c3 commit 39e3c87
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
82 changes: 82 additions & 0 deletions src/MercadoPago/Entities/OAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
namespace MercadoPago;
use MercadoPago\Annotation\RestMethod;
use MercadoPago\Annotation\Attribute;

/**
* @RestMethod(resource="/oauth/token", method="create")
*/

class OAuth extends Entity
{
/**
* @Attribute()
*/
protected $client_secret;
/**
* @Attribute()
*/
protected $grant_type;
/**
* @Attribute()
*/
protected $code;
/**
* @Attribute()
*/
protected $redirect_uri;

/**
* @Attribute()
*/
protected $access_token;
/**
* @Attribute()
*/
protected $public_key;
/**
* @Attribute()
*/
protected $refresh_token;
/**
* @Attribute()
*/
protected $live_mode;
/**
* @Attribute()
*/
protected $user_id;
/**
* @Attribute()
*/
protected $token_type;
/**
* @Attribute()
*/
protected $expires_in;
/**
* @Attribute()
*/
protected $scope;

public function getAuthorizationURL($app_id, $redirect_uri){
return "https://auth.mercadopago.com.br/authorization?client_id=${app_id}&response_type=code&platform_id=mp&redirect_uri=${redirect_uri}";
}

public function getOAuthCredentials($authorization_code, $redirect_uri){
$this->client_secret = SDK::getAccessToken();
$this->grant_type = 'authorization_code';
$this->code = $authorization_code;
$this->redirect_uri = $redirect_uri;

return $this->save();
}

public function refreshOAuthCredentials($refresh_token){
$this->client_secret = SDK::getAccessToken();
$this->grant_type = 'refresh_token';
$this->refresh_token = $refresh_token;

return $this->save();
}
}
14 changes: 9 additions & 5 deletions src/MercadoPago/Generic/RecuperableError.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ public function add_cause($code, $description) {
}

public function proccess_causes($causes){
foreach ($causes as $cause){
if(is_array($cause) && (!isset($cause['code']) && !isset($cause['description']))){
$this->proccess_causes($cause);
}else{
$this->add_cause($cause['code'], $cause['description']);
if(isset($causes['code']) && isset($causes['description'])){
$this->add_cause($causes['code'], $causes['description']);
}else{
foreach ($causes as $cause){
if(is_array($cause) && (!isset($cause['code']) && !isset($cause['description']))){
$this->proccess_causes($cause);
}else{
$this->add_cause($cause['code'], $cause['description']);
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/MercadoPago/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ public function setQueryParams($entity, $urlParams = [])
protected function _attributesToJson($entity, &$result)
{
if (is_array($entity)) {
$attributes = array_filter($entity);
$attributes = array_filter($entity, function($entity) {
return ($entity !== null && $entity !== false && $entity !== '');
});
} else {
$attributes = $entity->toArray();
}
Expand All @@ -308,7 +310,7 @@ protected function _attributesToJson($entity, &$result)
if ($value instanceof Entity || is_array($value)) {
$this->_attributesToJson($value, $result[$key]);
} else {
if ($value != null || is_bool($value)){
if ($value != null || is_bool($value) || is_numeric($value)){
$result[$key] = $value;
}
}
Expand Down

0 comments on commit 39e3c87

Please sign in to comment.