BraintreeOAuthGateway

Braintree OAuthGateway module PHP Version 5 Creates and manages Braintree Addresses.

Defined (1)

The class is defined in the following location(s).

/classes/lib/Braintree/Braintree/OAuthGateway.php  
  1. class OAuthGateway 
  2. private $_gateway; 
  3. private $_config; 
  4. private $_http; 
  5.  
  6. public function __construct($gateway) 
  7. $this->_gateway = $gateway; 
  8. $this->_config = $gateway->config; 
  9. $this->_http = new Http($gateway->config); 
  10. $this->_http->useClientCredentials(); 
  11.  
  12. $this->_config->assertHasClientCredentials(); 
  13.  
  14. public function createTokenFromCode($params) 
  15. $params['grantType'] = "authorization_code"; 
  16. return $this->_createToken($params); 
  17.  
  18. public function createTokenFromRefreshToken($params) 
  19. $params['grantType'] = "refresh_token"; 
  20. return $this->_createToken($params); 
  21.  
  22. private function _createToken($params) 
  23. $params = ['credentials' => $params]; 
  24. $response = $this->_http->post('/oauth/access_tokens', $params); 
  25. return $this->_verifyGatewayResponse($response); 
  26.  
  27. private function _verifyGatewayResponse($response) 
  28. if (isset($response['credentials'])) { 
  29. $result = new Result\Successful( 
  30. OAuthCredentials::factory($response['credentials']) 
  31. ); 
  32. return $this->_mapSuccess($result); 
  33. } else if (isset($response['apiErrorResponse'])) { 
  34. $result = new Result\Error($response['apiErrorResponse']); 
  35. return $this->_mapError($result); 
  36. } else { 
  37. throw new Exception\Unexpected( 
  38. "Expected credentials or apiErrorResponse" 
  39. ); 
  40.  
  41. public function _mapError($result) 
  42. $error = $result->errors->deepAll()[0]; 
  43.  
  44. if ($error->code == Error\Codes::OAUTH_INVALID_GRANT) { 
  45. $result->error = 'invalid_grant'; 
  46. } else if ($error->code == Error\Codes::OAUTH_INVALID_CREDENTIALS) { 
  47. $result->error = 'invalid_credentials'; 
  48. } else if ($error->code == Error\Codes::OAUTH_INVALID_SCOPE) { 
  49. $result->error = 'invalid_scope'; 
  50. $result->errorDescription = explode(': ', $error->message)[1]; 
  51. return $result; 
  52.  
  53. public function _mapSuccess($result) 
  54. $credentials = $result->credentials; 
  55. $result->accessToken = $credentials->accessToken; 
  56. $result->refreshToken = $credentials->refreshToken; 
  57. $result->tokenType = $credentials->tokenType; 
  58. $result->expiresAt = $credentials->expiresAt; 
  59. return $result; 
  60.  
  61. public function connectUrl($params = []) 
  62. $query = Util::camelCaseToDelimiterArray($params, '_'); 
  63. $query['client_id'] = $this->_config->getClientId(); 
  64. $queryString = preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($query)); 
  65. $url = $this->_config->baseUrl() . '/oauth/connect?' . $queryString; 
  66.  
  67. return $url . '&signature=' . $this->computeSignature($url) . '&algorithm=SHA256'; 
  68.  
  69. public function computeSignature($url) 
  70. $key = hash('sha256', $this->_config->getClientSecret(), true); 
  71. return hash_hmac('sha256', $url, $key);