PayPalAccountGateway.php 5.0 KB

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