BraintreePayPalAccountGateway

Manages Braintree PayPalAccounts.

Defined (1)

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

/includes/lib/Braintree/lib/Braintree/PayPalAccountGateway.php  
  1. class PayPalAccountGateway 
  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->_config->assertHasAccessTokenOrKeys(); 
  10. $this->_http = new Http($gateway->config); 
  11.  
  12.  
  13. /** 
  14. * find a paypalAccount by token 
  15. * @access public 
  16. * @param string $token paypal accountunique id 
  17. * @return PayPalAccount 
  18. * @throws Exception\NotFound 
  19. */ 
  20. public function find($token) 
  21. $this->_validateId($token); 
  22. try { 
  23. $path = $this->_config->merchantPath() . '/payment_methods/paypal_account/' . $token; 
  24. $response = $this->_http->get($path); 
  25. return PayPalAccount::factory($response['paypalAccount']); 
  26. } catch (Exception\NotFound $e) { 
  27. throw new Exception\NotFound( 
  28. 'paypal account with token ' . $token . ' not found' 
  29. ); 
  30.  
  31.  
  32. /** 
  33. * updates the paypalAccount record 
  34. * if calling this method in context, $token 
  35. * is the 2nd attribute. $token is not sent in object context. 
  36. * @access public 
  37. * @param array $attributes 
  38. * @param string $token (optional) 
  39. * @return Result\Successful or Result\Error 
  40. */ 
  41. public function update($token, $attributes) 
  42. Util::verifyKeys(self::updateSignature(), $attributes); 
  43. $this->_validateId($token); 
  44. return $this->_doUpdate('put', '/payment_methods/paypal_account/' . $token, ['paypalAccount' => $attributes]); 
  45.  
  46. public function delete($token) 
  47. $this->_validateId($token); 
  48. $path = $this->_config->merchantPath() . '/payment_methods/paypal_account/' . $token; 
  49. $this->_http->delete($path); 
  50. return new Result\Successful(); 
  51.  
  52. /** 
  53. * create a new sale for the current PayPal account 
  54. * @param string $token 
  55. * @param array $transactionAttribs 
  56. * @return Result\Successful|Result\Error 
  57. * @see Transaction::sale() 
  58. */ 
  59. public function sale($token, $transactionAttribs) 
  60. $this->_validateId($token); 
  61. return Transaction::sale( 
  62. array_merge( 
  63. $transactionAttribs,  
  64. ['paymentMethodToken' => $token] 
  65. ); 
  66.  
  67. public static function updateSignature() 
  68. return [ 
  69. 'token',  
  70. ['options' => ['makeDefault']] 
  71. ]; 
  72.  
  73. /** 
  74. * sends the update request to the gateway 
  75. * @ignore 
  76. * @param string $subPath 
  77. * @param array $params 
  78. * @return mixed 
  79. */ 
  80. private function _doUpdate($httpVerb, $subPath, $params) 
  81. $fullPath = $this->_config->merchantPath() . $subPath; 
  82. $response = $this->_http->$httpVerb($fullPath, $params); 
  83. return $this->_verifyGatewayResponse($response); 
  84.  
  85. /** 
  86. * generic method for validating incoming gateway responses 
  87. * creates a new PayPalAccount object and encapsulates 
  88. * it inside a Result\Successful object, or 
  89. * encapsulates a Errors object inside a Result\Error 
  90. * alternatively, throws an Unexpected exception if the response is invalid. 
  91. * @ignore 
  92. * @param array $response gateway response values 
  93. * @return Result\Successful|Result\Error 
  94. * @throws Exception\Unexpected 
  95. */ 
  96. private function _verifyGatewayResponse($response) 
  97. if (isset($response['paypalAccount'])) { 
  98. // return a populated instance of PayPalAccount 
  99. return new Result\Successful( 
  100. PayPalAccount::factory($response['paypalAccount']) 
  101. ); 
  102. } else if (isset($response['apiErrorResponse'])) { 
  103. return new Result\Error($response['apiErrorResponse']); 
  104. } else { 
  105. throw new Exception\Unexpected( 
  106. 'Expected paypal account or apiErrorResponse' 
  107. ); 
  108.  
  109. /** 
  110. * verifies that a valid paypal account identifier is being used 
  111. * @ignore 
  112. * @param string $identifier 
  113. * @param Optional $string $identifierType type of identifier supplied, default 'token' 
  114. * @throws InvalidArgumentException 
  115. */ 
  116. private function _validateId($identifier = null, $identifierType = 'token') 
  117. if (empty($identifier)) { 
  118. throw new InvalidArgumentException( 
  119. 'expected paypal account id to be set' 
  120. ); 
  121. if (!preg_match('/^[0-9A-Za-z_-]+$/', $identifier)) { 
  122. throw new InvalidArgumentException( 
  123. $identifier . ' is an invalid paypal account ' . $identifierType . '.' 
  124. );