Customer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. namespace Braintree;
  3. /**
  4. * Braintree Customer module
  5. * Creates and manages Customers
  6. *
  7. * <b>== More information ==</b>
  8. *
  9. * For more detailed information on Customers, see {@link https://developers.braintreepayments.com/reference/response/customer/php https://developers.braintreepayments.com/reference/response/customer/php}
  10. *
  11. * @package Braintree
  12. * @category Resources
  13. *
  14. * @property-read array $addresses
  15. * @property-read array $paymentMethods
  16. * @property-read string $company
  17. * @property-read string $createdAt
  18. * @property-read array $creditCards
  19. * @property-read array $paypalAccounts
  20. * @property-read array $applePayCards
  21. * @property-read array $androidPayCards
  22. * @property-read array $amexExpressCheckoutCards
  23. * @property-read array $venmoAccounts
  24. * @property-read array $visaCheckoutCards
  25. * @property-read array $masterpassCards
  26. * @property-read array $samsungPayCards
  27. * @property-read array $coinbaseAccounts
  28. * @property-read array $customFields custom fields passed with the request
  29. * @property-read string $email
  30. * @property-read string $fax
  31. * @property-read string $firstName
  32. * @property-read string $id
  33. * @property-read string $lastName
  34. * @property-read string $phone
  35. * @property-read string $updatedAt
  36. * @property-read string $website
  37. */
  38. class Customer extends Base
  39. {
  40. /**
  41. *
  42. * @return Customer[]
  43. */
  44. public static function all()
  45. {
  46. return Configuration::gateway()->customer()->all();
  47. }
  48. /**
  49. *
  50. * @param string $query
  51. * @param int[] $ids
  52. * @return Customer|Customer[]
  53. */
  54. public static function fetch($query, $ids)
  55. {
  56. return Configuration::gateway()->customer()->fetch($query, $ids);
  57. }
  58. /**
  59. *
  60. * @param array $attribs
  61. * @return Result\Successful|Result\Error
  62. */
  63. public static function create($attribs = [])
  64. {
  65. return Configuration::gateway()->customer()->create($attribs);
  66. }
  67. /**
  68. *
  69. * @param array $attribs
  70. * @return Customer
  71. */
  72. public static function createNoValidate($attribs = [])
  73. {
  74. return Configuration::gateway()->customer()->createNoValidate($attribs);
  75. }
  76. /**
  77. * @deprecated since version 2.3.0
  78. * @param string $queryString
  79. * @return Result\Successful
  80. */
  81. public static function createFromTransparentRedirect($queryString)
  82. {
  83. return Configuration::gateway()->customer()->createFromTransparentRedirect($queryString);
  84. }
  85. /**
  86. * @deprecated since version 2.3.0
  87. * @return string
  88. */
  89. public static function createCustomerUrl()
  90. {
  91. return Configuration::gateway()->customer()->createCustomerUrl();
  92. }
  93. /**
  94. *
  95. * @throws Exception\NotFound
  96. * @param string $id customer id
  97. * @return Customer
  98. */
  99. public static function find($id, $associationFilterId = null)
  100. {
  101. return Configuration::gateway()->customer()->find($id, $associationFilterId);
  102. }
  103. /**
  104. *
  105. * @param int $customerId
  106. * @param array $transactionAttribs
  107. * @return Result\Successful|Result\Error
  108. */
  109. public static function credit($customerId, $transactionAttribs)
  110. {
  111. return Configuration::gateway()->customer()->credit($customerId, $transactionAttribs);
  112. }
  113. /**
  114. *
  115. * @throws Exception\ValidationError
  116. * @param type $customerId
  117. * @param type $transactionAttribs
  118. * @return Transaction
  119. */
  120. public static function creditNoValidate($customerId, $transactionAttribs)
  121. {
  122. return Configuration::gateway()->customer()->creditNoValidate($customerId, $transactionAttribs);
  123. }
  124. /**
  125. *
  126. * @throws Exception on invalid id or non-200 http response code
  127. * @param int $customerId
  128. * @return Result\Successful
  129. */
  130. public static function delete($customerId)
  131. {
  132. return Configuration::gateway()->customer()->delete($customerId);
  133. }
  134. /**
  135. *
  136. * @param int $customerId
  137. * @param array $transactionAttribs
  138. * @return Transaction
  139. */
  140. public static function sale($customerId, $transactionAttribs)
  141. {
  142. return Configuration::gateway()->customer()->sale($customerId, $transactionAttribs);
  143. }
  144. /**
  145. *
  146. * @param int $customerId
  147. * @param array $transactionAttribs
  148. * @return Transaction
  149. */
  150. public static function saleNoValidate($customerId, $transactionAttribs)
  151. {
  152. return Configuration::gateway()->customer()->saleNoValidate($customerId, $transactionAttribs);
  153. }
  154. /**
  155. *
  156. * @throws InvalidArgumentException
  157. * @param string $query
  158. * @return ResourceCollection
  159. */
  160. public static function search($query)
  161. {
  162. return Configuration::gateway()->customer()->search($query);
  163. }
  164. /**
  165. *
  166. * @throws Exception\Unexpected
  167. * @param int $customerId
  168. * @param array $attributes
  169. * @return Result\Successful|Result\Error
  170. */
  171. public static function update($customerId, $attributes)
  172. {
  173. return Configuration::gateway()->customer()->update($customerId, $attributes);
  174. }
  175. /**
  176. *
  177. * @throws Exception\Unexpected
  178. * @param int $customerId
  179. * @param array $attributes
  180. * @return CustomerGateway
  181. */
  182. public static function updateNoValidate($customerId, $attributes)
  183. {
  184. return Configuration::gateway()->customer()->updateNoValidate($customerId, $attributes);
  185. }
  186. /**
  187. *
  188. * @deprecated since version 2.3.0
  189. * @return string
  190. */
  191. public static function updateCustomerUrl()
  192. {
  193. return Configuration::gateway()->customer()->updateCustomerUrl();
  194. }
  195. /**
  196. *
  197. * @deprecated since version 2.3.0
  198. * @param string $queryString
  199. * @return Result\Successful|Result\Error
  200. */
  201. public static function updateFromTransparentRedirect($queryString)
  202. {
  203. return Configuration::gateway()->customer()->updateFromTransparentRedirect($queryString);
  204. }
  205. /* instance methods */
  206. /**
  207. * sets instance properties from an array of values
  208. *
  209. * @ignore
  210. * @access protected
  211. * @param array $customerAttribs array of customer data
  212. */
  213. protected function _initialize($customerAttribs)
  214. {
  215. $this->_attributes = $customerAttribs;
  216. $addressArray = [];
  217. if (isset($customerAttribs['addresses'])) {
  218. foreach ($customerAttribs['addresses'] AS $address) {
  219. $addressArray[] = Address::factory($address);
  220. }
  221. }
  222. $this->_set('addresses', $addressArray);
  223. $creditCardArray = [];
  224. if (isset($customerAttribs['creditCards'])) {
  225. foreach ($customerAttribs['creditCards'] AS $creditCard) {
  226. $creditCardArray[] = CreditCard::factory($creditCard);
  227. }
  228. }
  229. $this->_set('creditCards', $creditCardArray);
  230. $coinbaseAccountArray = [];
  231. if (isset($customerAttribs['coinbaseAccounts'])) {
  232. foreach ($customerAttribs['coinbaseAccounts'] AS $coinbaseAccount) {
  233. $coinbaseAccountArray[] = CoinbaseAccount::factory($coinbaseAccount);
  234. }
  235. }
  236. $this->_set('coinbaseAccounts', $coinbaseAccountArray);
  237. $paypalAccountArray = [];
  238. if (isset($customerAttribs['paypalAccounts'])) {
  239. foreach ($customerAttribs['paypalAccounts'] AS $paypalAccount) {
  240. $paypalAccountArray[] = PayPalAccount::factory($paypalAccount);
  241. }
  242. }
  243. $this->_set('paypalAccounts', $paypalAccountArray);
  244. $applePayCardArray = [];
  245. if (isset($customerAttribs['applePayCards'])) {
  246. foreach ($customerAttribs['applePayCards'] AS $applePayCard) {
  247. $applePayCardArray[] = ApplePayCard::factory($applePayCard);
  248. }
  249. }
  250. $this->_set('applePayCards', $applePayCardArray);
  251. $androidPayCardArray = [];
  252. if (isset($customerAttribs['androidPayCards'])) {
  253. foreach ($customerAttribs['androidPayCards'] AS $androidPayCard) {
  254. $androidPayCardArray[] = AndroidPayCard::factory($androidPayCard);
  255. }
  256. }
  257. $this->_set('androidPayCards', $androidPayCardArray);
  258. $amexExpressCheckoutCardArray = [];
  259. if (isset($customerAttribs['amexExpressCheckoutCards'])) {
  260. foreach ($customerAttribs['amexExpressCheckoutCards'] AS $amexExpressCheckoutCard) {
  261. $amexExpressCheckoutCardArray[] = AmexExpressCheckoutCard::factory($amexExpressCheckoutCard);
  262. }
  263. }
  264. $this->_set('amexExpressCheckoutCards', $amexExpressCheckoutCardArray);
  265. $venmoAccountArray = array();
  266. if (isset($customerAttribs['venmoAccounts'])) {
  267. foreach ($customerAttribs['venmoAccounts'] AS $venmoAccount) {
  268. $venmoAccountArray[] = VenmoAccount::factory($venmoAccount);
  269. }
  270. }
  271. $this->_set('venmoAccounts', $venmoAccountArray);
  272. $visaCheckoutCardArray = [];
  273. if (isset($customerAttribs['visaCheckoutCards'])) {
  274. foreach ($customerAttribs['visaCheckoutCards'] AS $visaCheckoutCard) {
  275. $visaCheckoutCardArray[] = VisaCheckoutCard::factory($visaCheckoutCard);
  276. }
  277. }
  278. $this->_set('visaCheckoutCards', $visaCheckoutCardArray);
  279. $masterpassCardArray = [];
  280. if (isset($customerAttribs['masterpassCards'])) {
  281. foreach ($customerAttribs['masterpassCards'] AS $masterpassCard) {
  282. $masterpassCardArray[] = MasterpassCard::factory($masterpassCard);
  283. }
  284. }
  285. $this->_set('masterpassCards', $masterpassCardArray);
  286. $samsungPayCardArray = [];
  287. if (isset($customerAttribs['samsungPayCards'])) {
  288. foreach ($customerAttribs['samsungPayCards'] AS $samsungPayCard) {
  289. $samsungPayCardArray[] = SamsungPayCard::factory($samsungPayCard);
  290. }
  291. }
  292. $this->_set('samsungPayCards', $samsungPayCardArray);
  293. $usBankAccountArray = array();
  294. if (isset($customerAttribs['usBankAccounts'])) {
  295. foreach ($customerAttribs['usBankAccounts'] AS $usBankAccount) {
  296. $usBankAccountArray[] = UsBankAccount::factory($usBankAccount);
  297. }
  298. }
  299. $this->_set('usBankAccounts', $usBankAccountArray);
  300. $this->_set('paymentMethods', array_merge(
  301. $this->creditCards,
  302. $this->paypalAccounts,
  303. $this->applePayCards,
  304. $this->coinbaseAccounts,
  305. $this->androidPayCards,
  306. $this->amexExpressCheckoutCards,
  307. $this->venmoAccounts,
  308. $this->visaCheckoutCards,
  309. $this->masterpassCards,
  310. $this->samsungPayCards,
  311. $this->usBankAccounts
  312. ));
  313. }
  314. /**
  315. * returns a string representation of the customer
  316. * @return string
  317. */
  318. public function __toString()
  319. {
  320. return __CLASS__ . '[' .
  321. Util::attributesToString($this->_attributes) .']';
  322. }
  323. /**
  324. * returns false if comparing object is not a Customer,
  325. * or is a Customer with a different id
  326. *
  327. * @param object $otherCust customer to compare against
  328. * @return boolean
  329. */
  330. public function isEqual($otherCust)
  331. {
  332. return !($otherCust instanceof Customer) ? false : $this->id === $otherCust->id;
  333. }
  334. /**
  335. * returns an array containt all of the customer's payment methods
  336. *
  337. * @deprecated since version 3.1.0 - use the paymentMethods property directly
  338. *
  339. * @return array
  340. */
  341. public function paymentMethods()
  342. {
  343. return $this->paymentMethods;
  344. }
  345. /**
  346. * returns the customer's default payment method
  347. *
  348. * @return CreditCard|PayPalAccount
  349. */
  350. public function defaultPaymentMethod()
  351. {
  352. $defaultPaymentMethods = array_filter($this->paymentMethods, 'Braintree\Customer::_defaultPaymentMethodFilter');
  353. return current($defaultPaymentMethods);
  354. }
  355. public static function _defaultPaymentMethodFilter($paymentMethod)
  356. {
  357. return $paymentMethod->isDefault();
  358. }
  359. /* private class properties */
  360. /**
  361. * @access protected
  362. * @var array registry of customer data
  363. */
  364. protected $_attributes = [
  365. 'addresses' => '',
  366. 'company' => '',
  367. 'creditCards' => '',
  368. 'email' => '',
  369. 'fax' => '',
  370. 'firstName' => '',
  371. 'id' => '',
  372. 'lastName' => '',
  373. 'phone' => '',
  374. 'createdAt' => '',
  375. 'updatedAt' => '',
  376. 'website' => '',
  377. ];
  378. /**
  379. * factory method: returns an instance of Customer
  380. * to the requesting method, with populated properties
  381. *
  382. * @ignore
  383. * @param array $attributes
  384. * @return Customer
  385. */
  386. public static function factory($attributes)
  387. {
  388. $instance = new Customer();
  389. $instance->_initialize($attributes);
  390. return $instance;
  391. }
  392. }
  393. class_alias('Braintree\Customer', 'Braintree_Customer');