123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- namespace Braintree;
- /**
- * Braintree Customer module
- * Creates and manages Customers
- *
- * <b>== More information ==</b>
- *
- * For more detailed information on Customers, see {@link https://developers.braintreepayments.com/reference/response/customer/php https://developers.braintreepayments.com/reference/response/customer/php}
- *
- * @package Braintree
- * @category Resources
- *
- * @property-read array $addresses
- * @property-read array $paymentMethods
- * @property-read string $company
- * @property-read string $createdAt
- * @property-read array $creditCards
- * @property-read array $paypalAccounts
- * @property-read array $applePayCards
- * @property-read array $androidPayCards
- * @property-read array $amexExpressCheckoutCards
- * @property-read array $venmoAccounts
- * @property-read array $visaCheckoutCards
- * @property-read array $masterpassCards
- * @property-read array $samsungPayCards
- * @property-read array $coinbaseAccounts
- * @property-read array $customFields custom fields passed with the request
- * @property-read string $email
- * @property-read string $fax
- * @property-read string $firstName
- * @property-read string $id
- * @property-read string $lastName
- * @property-read string $phone
- * @property-read string $updatedAt
- * @property-read string $website
- */
- class Customer extends Base
- {
- /**
- *
- * @return Customer[]
- */
- public static function all()
- {
- return Configuration::gateway()->customer()->all();
- }
- /**
- *
- * @param string $query
- * @param int[] $ids
- * @return Customer|Customer[]
- */
- public static function fetch($query, $ids)
- {
- return Configuration::gateway()->customer()->fetch($query, $ids);
- }
- /**
- *
- * @param array $attribs
- * @return Result\Successful|Result\Error
- */
- public static function create($attribs = [])
- {
- return Configuration::gateway()->customer()->create($attribs);
- }
- /**
- *
- * @param array $attribs
- * @return Customer
- */
- public static function createNoValidate($attribs = [])
- {
- return Configuration::gateway()->customer()->createNoValidate($attribs);
- }
- /**
- * @deprecated since version 2.3.0
- * @param string $queryString
- * @return Result\Successful
- */
- public static function createFromTransparentRedirect($queryString)
- {
- return Configuration::gateway()->customer()->createFromTransparentRedirect($queryString);
- }
- /**
- * @deprecated since version 2.3.0
- * @return string
- */
- public static function createCustomerUrl()
- {
- return Configuration::gateway()->customer()->createCustomerUrl();
- }
- /**
- *
- * @throws Exception\NotFound
- * @param string $id customer id
- * @return Customer
- */
- public static function find($id, $associationFilterId = null)
- {
- return Configuration::gateway()->customer()->find($id, $associationFilterId);
- }
- /**
- *
- * @param int $customerId
- * @param array $transactionAttribs
- * @return Result\Successful|Result\Error
- */
- public static function credit($customerId, $transactionAttribs)
- {
- return Configuration::gateway()->customer()->credit($customerId, $transactionAttribs);
- }
- /**
- *
- * @throws Exception\ValidationError
- * @param type $customerId
- * @param type $transactionAttribs
- * @return Transaction
- */
- public static function creditNoValidate($customerId, $transactionAttribs)
- {
- return Configuration::gateway()->customer()->creditNoValidate($customerId, $transactionAttribs);
- }
- /**
- *
- * @throws Exception on invalid id or non-200 http response code
- * @param int $customerId
- * @return Result\Successful
- */
- public static function delete($customerId)
- {
- return Configuration::gateway()->customer()->delete($customerId);
- }
- /**
- *
- * @param int $customerId
- * @param array $transactionAttribs
- * @return Transaction
- */
- public static function sale($customerId, $transactionAttribs)
- {
- return Configuration::gateway()->customer()->sale($customerId, $transactionAttribs);
- }
- /**
- *
- * @param int $customerId
- * @param array $transactionAttribs
- * @return Transaction
- */
- public static function saleNoValidate($customerId, $transactionAttribs)
- {
- return Configuration::gateway()->customer()->saleNoValidate($customerId, $transactionAttribs);
- }
- /**
- *
- * @throws InvalidArgumentException
- * @param string $query
- * @return ResourceCollection
- */
- public static function search($query)
- {
- return Configuration::gateway()->customer()->search($query);
- }
- /**
- *
- * @throws Exception\Unexpected
- * @param int $customerId
- * @param array $attributes
- * @return Result\Successful|Result\Error
- */
- public static function update($customerId, $attributes)
- {
- return Configuration::gateway()->customer()->update($customerId, $attributes);
- }
- /**
- *
- * @throws Exception\Unexpected
- * @param int $customerId
- * @param array $attributes
- * @return CustomerGateway
- */
- public static function updateNoValidate($customerId, $attributes)
- {
- return Configuration::gateway()->customer()->updateNoValidate($customerId, $attributes);
- }
- /**
- *
- * @deprecated since version 2.3.0
- * @return string
- */
- public static function updateCustomerUrl()
- {
- return Configuration::gateway()->customer()->updateCustomerUrl();
- }
- /**
- *
- * @deprecated since version 2.3.0
- * @param string $queryString
- * @return Result\Successful|Result\Error
- */
- public static function updateFromTransparentRedirect($queryString)
- {
- return Configuration::gateway()->customer()->updateFromTransparentRedirect($queryString);
- }
- /* instance methods */
- /**
- * sets instance properties from an array of values
- *
- * @ignore
- * @access protected
- * @param array $customerAttribs array of customer data
- */
- protected function _initialize($customerAttribs)
- {
- $this->_attributes = $customerAttribs;
- $addressArray = [];
- if (isset($customerAttribs['addresses'])) {
- foreach ($customerAttribs['addresses'] AS $address) {
- $addressArray[] = Address::factory($address);
- }
- }
- $this->_set('addresses', $addressArray);
- $creditCardArray = [];
- if (isset($customerAttribs['creditCards'])) {
- foreach ($customerAttribs['creditCards'] AS $creditCard) {
- $creditCardArray[] = CreditCard::factory($creditCard);
- }
- }
- $this->_set('creditCards', $creditCardArray);
- $coinbaseAccountArray = [];
- if (isset($customerAttribs['coinbaseAccounts'])) {
- foreach ($customerAttribs['coinbaseAccounts'] AS $coinbaseAccount) {
- $coinbaseAccountArray[] = CoinbaseAccount::factory($coinbaseAccount);
- }
- }
- $this->_set('coinbaseAccounts', $coinbaseAccountArray);
- $paypalAccountArray = [];
- if (isset($customerAttribs['paypalAccounts'])) {
- foreach ($customerAttribs['paypalAccounts'] AS $paypalAccount) {
- $paypalAccountArray[] = PayPalAccount::factory($paypalAccount);
- }
- }
- $this->_set('paypalAccounts', $paypalAccountArray);
- $applePayCardArray = [];
- if (isset($customerAttribs['applePayCards'])) {
- foreach ($customerAttribs['applePayCards'] AS $applePayCard) {
- $applePayCardArray[] = ApplePayCard::factory($applePayCard);
- }
- }
- $this->_set('applePayCards', $applePayCardArray);
- $androidPayCardArray = [];
- if (isset($customerAttribs['androidPayCards'])) {
- foreach ($customerAttribs['androidPayCards'] AS $androidPayCard) {
- $androidPayCardArray[] = AndroidPayCard::factory($androidPayCard);
- }
- }
- $this->_set('androidPayCards', $androidPayCardArray);
- $amexExpressCheckoutCardArray = [];
- if (isset($customerAttribs['amexExpressCheckoutCards'])) {
- foreach ($customerAttribs['amexExpressCheckoutCards'] AS $amexExpressCheckoutCard) {
- $amexExpressCheckoutCardArray[] = AmexExpressCheckoutCard::factory($amexExpressCheckoutCard);
- }
- }
- $this->_set('amexExpressCheckoutCards', $amexExpressCheckoutCardArray);
- $venmoAccountArray = array();
- if (isset($customerAttribs['venmoAccounts'])) {
- foreach ($customerAttribs['venmoAccounts'] AS $venmoAccount) {
- $venmoAccountArray[] = VenmoAccount::factory($venmoAccount);
- }
- }
- $this->_set('venmoAccounts', $venmoAccountArray);
- $visaCheckoutCardArray = [];
- if (isset($customerAttribs['visaCheckoutCards'])) {
- foreach ($customerAttribs['visaCheckoutCards'] AS $visaCheckoutCard) {
- $visaCheckoutCardArray[] = VisaCheckoutCard::factory($visaCheckoutCard);
- }
- }
- $this->_set('visaCheckoutCards', $visaCheckoutCardArray);
- $masterpassCardArray = [];
- if (isset($customerAttribs['masterpassCards'])) {
- foreach ($customerAttribs['masterpassCards'] AS $masterpassCard) {
- $masterpassCardArray[] = MasterpassCard::factory($masterpassCard);
- }
- }
- $this->_set('masterpassCards', $masterpassCardArray);
- $samsungPayCardArray = [];
- if (isset($customerAttribs['samsungPayCards'])) {
- foreach ($customerAttribs['samsungPayCards'] AS $samsungPayCard) {
- $samsungPayCardArray[] = SamsungPayCard::factory($samsungPayCard);
- }
- }
- $this->_set('samsungPayCards', $samsungPayCardArray);
- $usBankAccountArray = array();
- if (isset($customerAttribs['usBankAccounts'])) {
- foreach ($customerAttribs['usBankAccounts'] AS $usBankAccount) {
- $usBankAccountArray[] = UsBankAccount::factory($usBankAccount);
- }
- }
- $this->_set('usBankAccounts', $usBankAccountArray);
- $this->_set('paymentMethods', array_merge(
- $this->creditCards,
- $this->paypalAccounts,
- $this->applePayCards,
- $this->coinbaseAccounts,
- $this->androidPayCards,
- $this->amexExpressCheckoutCards,
- $this->venmoAccounts,
- $this->visaCheckoutCards,
- $this->masterpassCards,
- $this->samsungPayCards,
- $this->usBankAccounts
- ));
- }
- /**
- * returns a string representation of the customer
- * @return string
- */
- public function __toString()
- {
- return __CLASS__ . '[' .
- Util::attributesToString($this->_attributes) .']';
- }
- /**
- * returns false if comparing object is not a Customer,
- * or is a Customer with a different id
- *
- * @param object $otherCust customer to compare against
- * @return boolean
- */
- public function isEqual($otherCust)
- {
- return !($otherCust instanceof Customer) ? false : $this->id === $otherCust->id;
- }
- /**
- * returns an array containt all of the customer's payment methods
- *
- * @deprecated since version 3.1.0 - use the paymentMethods property directly
- *
- * @return array
- */
- public function paymentMethods()
- {
- return $this->paymentMethods;
- }
- /**
- * returns the customer's default payment method
- *
- * @return CreditCard|PayPalAccount
- */
- public function defaultPaymentMethod()
- {
- $defaultPaymentMethods = array_filter($this->paymentMethods, 'Braintree\Customer::_defaultPaymentMethodFilter');
- return current($defaultPaymentMethods);
- }
- public static function _defaultPaymentMethodFilter($paymentMethod)
- {
- return $paymentMethod->isDefault();
- }
- /* private class properties */
- /**
- * @access protected
- * @var array registry of customer data
- */
- protected $_attributes = [
- 'addresses' => '',
- 'company' => '',
- 'creditCards' => '',
- 'email' => '',
- 'fax' => '',
- 'firstName' => '',
- 'id' => '',
- 'lastName' => '',
- 'phone' => '',
- 'createdAt' => '',
- 'updatedAt' => '',
- 'website' => '',
- ];
- /**
- * factory method: returns an instance of Customer
- * to the requesting method, with populated properties
- *
- * @ignore
- * @param array $attributes
- * @return Customer
- */
- public static function factory($attributes)
- {
- $instance = new Customer();
- $instance->_initialize($attributes);
- return $instance;
- }
- }
- class_alias('Braintree\Customer', 'Braintree_Customer');
|