UsBankAccountVerificationGateway.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Braintree;
  3. use InvalidArgumentException;
  4. /**
  5. * Braintree UsBankAccountVerificationGateway module
  6. *
  7. * @package Braintree
  8. * @category Resources
  9. */
  10. /**
  11. * Manages Braintree UsBankAccountVerifications
  12. *
  13. * <b>== More information ==</b>
  14. *
  15. *
  16. * @package Braintree
  17. * @category Resources
  18. */
  19. class UsBankAccountVerificationGateway
  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 usBankAccountVerification by token
  33. *
  34. * @access public
  35. * @param string $token unique id
  36. * @return UsBankAccountVerification
  37. * @throws Exception\NotFound
  38. */
  39. public function find($token)
  40. {
  41. try {
  42. $path = $this->_config->merchantPath() . '/us_bank_account_verifications/' . $token;
  43. $response = $this->_http->get($path);
  44. return UsBankAccountVerification::factory($response['usBankAccountVerification']);
  45. } catch (Exception\NotFound $e) {
  46. throw new Exception\NotFound(
  47. 'US bank account with token ' . $token . ' not found'
  48. );
  49. }
  50. }
  51. public function search($query)
  52. {
  53. $criteria = [];
  54. foreach ($query as $term) {
  55. $criteria[$term->name] = $term->toparam();
  56. }
  57. $path = $this->_config->merchantPath() . '/us_bank_account_verifications/advanced_search_ids';
  58. $response = $this->_http->post($path, ['search' => $criteria]);
  59. $pager = [
  60. 'object' => $this,
  61. 'method' => 'fetch',
  62. 'methodArgs' => [$query]
  63. ];
  64. return new ResourceCollection($response, $pager);
  65. }
  66. /**
  67. * complete micro transfer verification by confirming the transfer amounts
  68. *
  69. * @access public
  70. * @param string $token unique id
  71. * @param array $amounts amounts deposited in micro transfer
  72. * @return UsBankAccountVerification
  73. * @throws Exception\Unexpected
  74. */
  75. public function confirmMicroTransferAmounts($token, $amounts)
  76. {
  77. try {
  78. $path = $this->_config->merchantPath() . '/us_bank_account_verifications/' . $token . '/confirm_micro_transfer_amounts';
  79. $response = $this->_http->put($path, [
  80. "us_bank_account_verification" => ["deposit_amounts" => $amounts]
  81. ]);
  82. return $this->_verifyGatewayResponse($response);
  83. } catch (Exception\Unexpected $e) {
  84. throw new Exception\Unexpected(
  85. 'Unexpected exception.'
  86. );
  87. }
  88. }
  89. /**
  90. * generic method for validating incoming gateway responses
  91. *
  92. * creates a new UsBankAccountVerification object and encapsulates
  93. * it inside a Result\Successful object, or
  94. * encapsulates a Errors object inside a Result\Error
  95. * alternatively, throws an Unexpected exception if the response is invalid.
  96. *
  97. * @ignore
  98. * @param array $response gateway response values
  99. * @return Result\Successful|Result\Error
  100. * @throws Exception\Unexpected
  101. */
  102. private function _verifyGatewayResponse($response)
  103. {
  104. if (isset($response['apiErrorResponse'])) {
  105. return new Result\Error($response['apiErrorResponse']);
  106. } else if (isset($response['usBankAccountVerification'])) {
  107. // return a populated instance of UsBankAccountVerification
  108. return new Result\Successful(
  109. UsBankAccountVerification::factory($response['usBankAccountVerification'])
  110. );
  111. } else {
  112. throw new Exception\Unexpected(
  113. 'Expected US bank account or apiErrorResponse'
  114. );
  115. }
  116. }
  117. }
  118. class_alias('Braintree\UsBankAccountVerificationGateway', 'Braintree_UsBankAccountVerificationGateway');