MerchantAccountGateway.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Braintree;
  3. class MerchantAccountGateway
  4. {
  5. private $_gateway;
  6. private $_config;
  7. private $_http;
  8. public function __construct($gateway)
  9. {
  10. $this->_gateway = $gateway;
  11. $this->_config = $gateway->config;
  12. $this->_config->assertHasAccessTokenOrKeys();
  13. $this->_http = new Http($gateway->config);
  14. }
  15. public function create($attribs)
  16. {
  17. Util::verifyKeys(self::detectSignature($attribs), $attribs);
  18. return $this->_doCreate('/merchant_accounts/create_via_api', ['merchant_account' => $attribs]);
  19. }
  20. public function find($merchant_account_id)
  21. {
  22. try {
  23. $path = $this->_config->merchantPath() . '/merchant_accounts/' . $merchant_account_id;
  24. $response = $this->_http->get($path);
  25. return MerchantAccount::factory($response['merchantAccount']);
  26. } catch (Exception\NotFound $e) {
  27. throw new Exception\NotFound('merchant account with id ' . $merchant_account_id . ' not found');
  28. }
  29. }
  30. public function update($merchant_account_id, $attributes)
  31. {
  32. Util::verifyKeys(self::updateSignature(), $attributes);
  33. return $this->_doUpdate('/merchant_accounts/' . $merchant_account_id . '/update_via_api', ['merchant_account' => $attributes]);
  34. }
  35. public static function detectSignature($attribs)
  36. {
  37. if (isset($attribs['applicantDetails'])) {
  38. trigger_error("DEPRECATED: Passing applicantDetails to create is deprecated. Please use individual, business, and funding", E_USER_NOTICE);
  39. return self::createDeprecatedSignature();
  40. } else {
  41. return self::createSignature();
  42. }
  43. }
  44. public static function updateSignature()
  45. {
  46. $signature = self::createSignature();
  47. unset($signature['tosAccepted']);
  48. return $signature;
  49. }
  50. public function createForCurrency($attribs)
  51. {
  52. $response = $this->_http->post($this->_config->merchantPath() . '/merchant_accounts/create_for_currency', ['merchant_account' => $attribs]);
  53. return $this->_verifyGatewayResponse($response);
  54. }
  55. public function all()
  56. {
  57. $pager = [
  58. 'object' => $this,
  59. 'method' => 'fetchMerchantAccounts',
  60. ];
  61. return new PaginatedCollection($pager);
  62. }
  63. public function fetchMerchantAccounts($page)
  64. {
  65. $response = $this->_http->get($this->_config->merchantPath() . '/merchant_accounts?page=' . $page);
  66. $body = $response['merchantAccounts'];
  67. $merchantAccounts = Util::extractattributeasarray($body, 'merchantAccount');
  68. $totalItems = $body['totalItems'][0];
  69. $pageSize = $body['pageSize'][0];
  70. return new PaginatedResult($totalItems, $pageSize, $merchantAccounts);
  71. }
  72. public static function createSignature()
  73. {
  74. $addressSignature = ['streetAddress', 'postalCode', 'locality', 'region'];
  75. $individualSignature = [
  76. 'firstName',
  77. 'lastName',
  78. 'email',
  79. 'phone',
  80. 'dateOfBirth',
  81. 'ssn',
  82. ['address' => $addressSignature]
  83. ];
  84. $businessSignature = [
  85. 'dbaName',
  86. 'legalName',
  87. 'taxId',
  88. ['address' => $addressSignature]
  89. ];
  90. $fundingSignature = [
  91. 'routingNumber',
  92. 'accountNumber',
  93. 'destination',
  94. 'email',
  95. 'mobilePhone',
  96. 'descriptor',
  97. ];
  98. return [
  99. 'id',
  100. 'tosAccepted',
  101. 'masterMerchantAccountId',
  102. ['individual' => $individualSignature],
  103. ['funding' => $fundingSignature],
  104. ['business' => $businessSignature]
  105. ];
  106. }
  107. public static function createDeprecatedSignature()
  108. {
  109. $applicantDetailsAddressSignature = ['streetAddress', 'postalCode', 'locality', 'region'];
  110. $applicantDetailsSignature = [
  111. 'companyName',
  112. 'firstName',
  113. 'lastName',
  114. 'email',
  115. 'phone',
  116. 'dateOfBirth',
  117. 'ssn',
  118. 'taxId',
  119. 'routingNumber',
  120. 'accountNumber',
  121. ['address' => $applicantDetailsAddressSignature]
  122. ];
  123. return [
  124. ['applicantDetails' => $applicantDetailsSignature],
  125. 'id',
  126. 'tosAccepted',
  127. 'masterMerchantAccountId'
  128. ];
  129. }
  130. public function _doCreate($subPath, $params)
  131. {
  132. $fullPath = $this->_config->merchantPath() . $subPath;
  133. $response = $this->_http->post($fullPath, $params);
  134. return $this->_verifyGatewayResponse($response);
  135. }
  136. private function _doUpdate($subPath, $params)
  137. {
  138. $fullPath = $this->_config->merchantPath() . $subPath;
  139. $response = $this->_http->put($fullPath, $params);
  140. return $this->_verifyGatewayResponse($response);
  141. }
  142. private function _verifyGatewayResponse($response)
  143. {
  144. if (isset($response['response'])) {
  145. $response = $response['response'];
  146. }
  147. if (isset($response['merchantAccount'])) {
  148. // return a populated instance of merchantAccount
  149. return new Result\Successful(
  150. MerchantAccount::factory($response['merchantAccount'])
  151. );
  152. } else if (isset($response['apiErrorResponse'])) {
  153. return new Result\Error($response['apiErrorResponse']);
  154. } else {
  155. throw new Exception\Unexpected(
  156. "Expected merchant account or apiErrorResponse"
  157. );
  158. }
  159. }
  160. }
  161. class_alias('Braintree\MerchantAccountGateway', 'Braintree_MerchantAccountGateway');