UsBankAccountGateway.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Braintree;
  3. use InvalidArgumentException;
  4. /**
  5. * Braintree UsBankAccountGateway module
  6. *
  7. * @package Braintree
  8. * @category Resources
  9. */
  10. /**
  11. * Manages Braintree UsBankAccounts
  12. *
  13. * <b>== More information ==</b>
  14. *
  15. *
  16. * @package Braintree
  17. * @category Resources
  18. */
  19. class UsBankAccountGateway
  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 usBankAccount by token
  33. *
  34. * @access public
  35. * @param string $token paypal accountunique id
  36. * @return UsBankAccount
  37. * @throws Exception\NotFound
  38. */
  39. public function find($token)
  40. {
  41. try {
  42. $path = $this->_config->merchantPath() . '/payment_methods/us_bank_account/' . $token;
  43. $response = $this->_http->get($path);
  44. return UsBankAccount::factory($response['usBankAccount']);
  45. } catch (Exception\NotFound $e) {
  46. throw new Exception\NotFound(
  47. 'US bank account with token ' . $token . ' not found'
  48. );
  49. }
  50. }
  51. /**
  52. * create a new sale for the current UsBank account
  53. *
  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. {
  61. return Transaction::sale(
  62. array_merge(
  63. $transactionAttribs,
  64. ['paymentMethodToken' => $token]
  65. )
  66. );
  67. }
  68. /**
  69. * generic method for validating incoming gateway responses
  70. *
  71. * creates a new UsBankAccount object and encapsulates
  72. * it inside a Result\Successful object, or
  73. * encapsulates a Errors object inside a Result\Error
  74. * alternatively, throws an Unexpected exception if the response is invalid.
  75. *
  76. * @ignore
  77. * @param array $response gateway response values
  78. * @return Result\Successful|Result\Error
  79. * @throws Exception\Unexpected
  80. */
  81. private function _verifyGatewayResponse($response)
  82. {
  83. if (isset($response['usBankAccount'])) {
  84. // return a populated instance of UsBankAccount
  85. return new Result\Successful(
  86. UsBankAccount::factory($response['usBankAccount'])
  87. );
  88. } else if (isset($response['apiErrorResponse'])) {
  89. return new Result\Error($response['apiErrorResponse']);
  90. } else {
  91. throw new Exception\Unexpected(
  92. 'Expected US bank account or apiErrorResponse'
  93. );
  94. }
  95. }
  96. }
  97. class_alias('Braintree\UsBankAccountGateway', 'Braintree_UsBankAccountGateway');