BraintreeOAuthGateway

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

Defined (1)

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

/includes/lib/Braintree/lib/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. public function revokeAccessToken($accessToken) 
  23. $params = ['token' => $accessToken]; 
  24. $response = $this->_http->post('/oauth/revoke_access_token', $params); 
  25. return $this->_verifyGatewayResponse($response); 
  26.  
  27. private function _createToken($params) 
  28. $params = ['credentials' => $params]; 
  29. $response = $this->_http->post('/oauth/access_tokens', $params); 
  30. return $this->_verifyGatewayResponse($response); 
  31.  
  32. private function _verifyGatewayResponse($response) 
  33. if (isset($response['credentials'])) { 
  34. $result = new Result\Successful( 
  35. OAuthCredentials::factory($response['credentials']) 
  36. ); 
  37. return $this->_mapSuccess($result); 
  38. } else if (isset($response['result'])) { 
  39. $result = new Result\Successful( 
  40. OAuthResult::factory($response['result']) 
  41. ); 
  42. return $this->_mapAccessTokenRevokeSuccess($result); 
  43. } else if (isset($response['apiErrorResponse'])) { 
  44. $result = new Result\Error($response['apiErrorResponse']); 
  45. return $this->_mapError($result); 
  46. } else { 
  47. throw new Exception\Unexpected( 
  48. "Expected credentials or apiErrorResponse" 
  49. ); 
  50.  
  51. public function _mapError($result) 
  52. $error = $result->errors->deepAll()[0]; 
  53.  
  54. if ($error->code == Error\Codes::OAUTH_INVALID_GRANT) { 
  55. $result->error = 'invalid_grant'; 
  56. } else if ($error->code == Error\Codes::OAUTH_INVALID_CREDENTIALS) { 
  57. $result->error = 'invalid_credentials'; 
  58. } else if ($error->code == Error\Codes::OAUTH_INVALID_SCOPE) { 
  59. $result->error = 'invalid_scope'; 
  60. $result->errorDescription = explode(': ', $error->message)[1]; 
  61. return $result; 
  62.  
  63. public function _mapAccessTokenRevokeSuccess($result) 
  64. $result->revocationResult = $result->success; 
  65. return $result; 
  66.  
  67. public function _mapSuccess($result) 
  68. $credentials = $result->credentials; 
  69. $result->accessToken = $credentials->accessToken; 
  70. $result->refreshToken = $credentials->refreshToken; 
  71. $result->tokenType = $credentials->tokenType; 
  72. $result->expiresAt = $credentials->expiresAt; 
  73. return $result; 
  74.  
  75. public function connectUrl($params = []) 
  76. $query = Util::camelCaseToDelimiterArray($params, '_'); 
  77. $query['client_id'] = $this->_config->getClientId(); 
  78. $queryString = preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($query)); 
  79. $url = $this->_config->baseUrl() . '/oauth/connect?' . $queryString; 
  80.  
  81. return $url . '&signature=' . $this->computeSignature($url) . '&algorithm=SHA256'; 
  82.  
  83. public function computeSignature($url) 
  84. $key = hash('sha256', $this->_config->getClientSecret(), true); 
  85. return hash_hmac('sha256', $url, $key);