BraintreeClientTokenGateway

The PayPal for WooCommerce Braintree ClientTokenGateway class.

Defined (1)

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

/classes/lib/Braintree/Braintree/ClientTokenGateway.php  
  1. class ClientTokenGateway 
  2. /** 
  3. * @var Gateway 
  4. */ 
  5. private $_gateway; 
  6.  
  7. /** 
  8. * @var Configuration 
  9. */ 
  10. private $_config; 
  11.  
  12. /** 
  13. * @var Http 
  14. */ 
  15. private $_http; 
  16.  
  17. /** 
  18. * @param Gateway $gateway 
  19. */ 
  20. public function __construct($gateway) 
  21. $this->_gateway = $gateway; 
  22. $this->_config = $gateway->config; 
  23. $this->_config->assertHasAccessTokenOrKeys(); 
  24. $this->_http = new Http($gateway->config); 
  25.  
  26. public function generate($params=[]) 
  27. if (!array_key_exists("version", $params)) { 
  28. $params["version"] = ClientToken::DEFAULT_VERSION; 
  29.  
  30. $this->conditionallyVerifyKeys($params); 
  31. $generateParams = ["client_token" => $params]; 
  32.  
  33. return $this->_doGenerate('/client_token', $generateParams); 
  34.  
  35. /** 
  36. * sends the generate request to the gateway 
  37. * @ignore 
  38. * @param var $url 
  39. * @param array $params 
  40. * @return mixed 
  41. */ 
  42. public function _doGenerate($subPath, $params) 
  43. $fullPath = $this->_config->merchantPath() . $subPath; 
  44. $response = $this->_http->post($fullPath, $params); 
  45.  
  46. return $this->_verifyGatewayResponse($response); 
  47.  
  48. /** 
  49. * @param array $params 
  50. * @throws InvalidArgumentException 
  51. */ 
  52. public function conditionallyVerifyKeys($params) 
  53. if (array_key_exists("customerId", $params)) { 
  54. Util::verifyKeys($this->generateWithCustomerIdSignature(), $params); 
  55. } else { 
  56. Util::verifyKeys($this->generateWithoutCustomerIdSignature(), $params); 
  57.  
  58. /** 
  59. * @return mixed[] 
  60. */ 
  61. public function generateWithCustomerIdSignature() 
  62. return [ 
  63. "version", "customerId", "proxyMerchantId",  
  64. ["options" => ["makeDefault", "verifyCard", "failOnDuplicatePaymentMethod"]],  
  65. "merchantAccountId", "sepaMandateType", "sepaMandateAcceptanceLocation"]; 
  66.  
  67. /** 
  68. * @return string[] 
  69. */ 
  70. public function generateWithoutCustomerIdSignature() 
  71. return ["version", "proxyMerchantId", "merchantAccountId"]; 
  72.  
  73. /** 
  74. * generic method for validating incoming gateway responses 
  75. * If the request is successful, returns a client token string. 
  76. * Otherwise, throws an InvalidArgumentException with the error 
  77. * response from the Gateway or an HTTP status code exception. 
  78. * @ignore 
  79. * @param array $response gateway response values 
  80. * @return string client token 
  81. * @throws InvalidArgumentException | HTTP status code exception 
  82. */ 
  83. private function _verifyGatewayResponse($response) 
  84. if (isset($response['clientToken'])) { 
  85. return $response['clientToken']['value']; 
  86. } elseif (isset($response['apiErrorResponse'])) { 
  87. throw new InvalidArgumentException( 
  88. $response['apiErrorResponse']['message'] 
  89. ); 
  90. } else { 
  91. throw new Exception\Unexpected( 
  92. "Expected clientToken or apiErrorResponse" 
  93. ); 
  94.