Account.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace Stripe;
  3. /**
  4. * Class Account
  5. *
  6. * @property string $id
  7. * @property string $object
  8. * @property string $business_logo
  9. * @property string $business_name
  10. * @property string $business_primary_color
  11. * @property string $business_url
  12. * @property bool $charges_enabled
  13. * @property string $country
  14. * @property int $created
  15. * @property bool $debit_negative_balances
  16. * @property mixed $decline_charge_on
  17. * @property string $default_currency
  18. * @property bool $details_submitted
  19. * @property string $display_name
  20. * @property string $email
  21. * @property Collection $external_accounts
  22. * @property mixed $legal_entity
  23. * @property StripeObject $metadata
  24. * @property mixed $payout_schedule
  25. * @property string $payout_statement_descriptor
  26. * @property bool $payouts_enabled
  27. * @property string $product_description
  28. * @property string $statement_descriptor
  29. * @property mixed $support_address
  30. * @property string $support_email
  31. * @property string $support_phone
  32. * @property string $support_url
  33. * @property string $timezone
  34. * @property mixed $tos_acceptance
  35. * @property string $type
  36. * @property mixed $verification
  37. *
  38. * @package Stripe
  39. */
  40. class Account extends ApiResource
  41. {
  42. const OBJECT_NAME = "account";
  43. use ApiOperations\All;
  44. use ApiOperations\Create;
  45. use ApiOperations\Delete;
  46. use ApiOperations\NestedResource;
  47. use ApiOperations\Retrieve {
  48. retrieve as protected _retrieve;
  49. }
  50. use ApiOperations\Update;
  51. public static function getSavedNestedResources()
  52. {
  53. static $savedNestedResources = null;
  54. if ($savedNestedResources === null) {
  55. $savedNestedResources = new Util\Set([
  56. 'external_account',
  57. 'bank_account',
  58. ]);
  59. }
  60. return $savedNestedResources;
  61. }
  62. const PATH_EXTERNAL_ACCOUNTS = '/external_accounts';
  63. const PATH_LOGIN_LINKS = '/login_links';
  64. public function instanceUrl()
  65. {
  66. if ($this['id'] === null) {
  67. return '/v1/account';
  68. } else {
  69. return parent::instanceUrl();
  70. }
  71. }
  72. /**
  73. * @param array|string|null $id The ID of the account to retrieve, or an
  74. * options array containing an `id` key.
  75. * @param array|string|null $opts
  76. *
  77. * @return Account
  78. */
  79. public static function retrieve($id = null, $opts = null)
  80. {
  81. if (!$opts && is_string($id) && substr($id, 0, 3) === 'sk_') {
  82. $opts = $id;
  83. $id = null;
  84. }
  85. return self::_retrieve($id, $opts);
  86. }
  87. /**
  88. * @param array|null $params
  89. * @param array|string|null $opts
  90. *
  91. * @return Account The rejected account.
  92. */
  93. public function reject($params = null, $opts = null)
  94. {
  95. $url = $this->instanceUrl() . '/reject';
  96. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  97. $this->refreshFrom($response, $opts);
  98. return $this;
  99. }
  100. /**
  101. * @param array|null $clientId
  102. * @param array|string|null $opts
  103. *
  104. * @return StripeObject Object containing the response from the API.
  105. */
  106. public function deauthorize($clientId = null, $opts = null)
  107. {
  108. $params = [
  109. 'client_id' => $clientId,
  110. 'stripe_user_id' => $this->id,
  111. ];
  112. return OAuth::deauthorize($params, $opts);
  113. }
  114. /**
  115. * @param array|null $id The ID of the account on which to create the external account.
  116. * @param array|null $params
  117. * @param array|string|null $opts
  118. *
  119. * @return BankAccount|Card
  120. */
  121. public static function createExternalAccount($id, $params = null, $opts = null)
  122. {
  123. return self::_createNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $params, $opts);
  124. }
  125. /**
  126. * @param array|null $id The ID of the account to which the external account belongs.
  127. * @param array|null $externalAccountId The ID of the external account to retrieve.
  128. * @param array|null $params
  129. * @param array|string|null $opts
  130. *
  131. * @return BankAccount|Card
  132. */
  133. public static function retrieveExternalAccount($id, $externalAccountId, $params = null, $opts = null)
  134. {
  135. return self::_retrieveNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
  136. }
  137. /**
  138. * @param array|null $id The ID of the account to which the external account belongs.
  139. * @param array|null $externalAccountId The ID of the external account to update.
  140. * @param array|null $params
  141. * @param array|string|null $opts
  142. *
  143. * @return BankAccount|Card
  144. */
  145. public static function updateExternalAccount($id, $externalAccountId, $params = null, $opts = null)
  146. {
  147. return self::_updateNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
  148. }
  149. /**
  150. * @param array|null $id The ID of the account to which the external account belongs.
  151. * @param array|null $externalAccountId The ID of the external account to delete.
  152. * @param array|null $params
  153. * @param array|string|null $opts
  154. *
  155. * @return BankAccount|Card
  156. */
  157. public static function deleteExternalAccount($id, $externalAccountId, $params = null, $opts = null)
  158. {
  159. return self::_deleteNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
  160. }
  161. /**
  162. * @param array|null $id The ID of the account on which to retrieve the external accounts.
  163. * @param array|null $params
  164. * @param array|string|null $opts
  165. *
  166. * @return BankAccount|Card
  167. */
  168. public static function allExternalAccounts($id, $params = null, $opts = null)
  169. {
  170. return self::_allNestedResources($id, static::PATH_EXTERNAL_ACCOUNTS, $params, $opts);
  171. }
  172. /**
  173. * @param array|null $id The ID of the account on which to create the login link.
  174. * @param array|null $params
  175. * @param array|string|null $opts
  176. *
  177. * @return LoginLink
  178. */
  179. public static function createLoginLink($id, $params = null, $opts = null)
  180. {
  181. return self::_createNestedResource($id, static::PATH_LOGIN_LINKS, $params, $opts);
  182. }
  183. public function serializeParameters($force = false)
  184. {
  185. $update = parent::serializeParameters($force);
  186. if (isset($this->_values['legal_entity'])) {
  187. $entity = $this['legal_entity'];
  188. if (isset($entity->_values['additional_owners'])) {
  189. $owners = $entity['additional_owners'];
  190. $entityUpdate = isset($update['legal_entity']) ? $update['legal_entity'] : [];
  191. $entityUpdate['additional_owners'] = $this->serializeAdditionalOwners($entity, $owners);
  192. $update['legal_entity'] = $entityUpdate;
  193. }
  194. }
  195. return $update;
  196. }
  197. private function serializeAdditionalOwners($legalEntity, $additionalOwners)
  198. {
  199. if (isset($legalEntity->_originalValues['additional_owners'])) {
  200. $originalValue = $legalEntity->_originalValues['additional_owners'];
  201. } else {
  202. $originalValue = [];
  203. }
  204. if (($originalValue) && (count($originalValue) > count($additionalOwners))) {
  205. throw new \InvalidArgumentException(
  206. "You cannot delete an item from an array, you must instead set a new array"
  207. );
  208. }
  209. $updateArr = [];
  210. foreach ($additionalOwners as $i => $v) {
  211. $update = ($v instanceof StripeObject) ? $v->serializeParameters() : $v;
  212. if ($update !== []) {
  213. if (!$originalValue || ($update != $legalEntity->serializeParamsValue($originalValue[$i], null, false, true))) {
  214. $updateArr[$i] = $update;
  215. }
  216. }
  217. }
  218. return $updateArr;
  219. }
  220. }