Example #1
0
 function testAllWithManyResults()
 {
     $collection = Braintree_Customer::all();
     $this->assertTrue($collection->maximumCount() > 1);
     $customer = $collection->firstItem();
     $this->assertTrue(intval($customer->id) > 0);
     $this->assertTrue($customer instanceof Braintree_Customer);
 }
Example #2
0
 function testAllWithManyResults()
 {
     $collection = Braintree_Customer::all();
     $this->assertTrue($collection->maximumCount() > 1);
     $arr = array();
     foreach ($collection as $customer) {
         array_push($arr, $customer->id);
     }
     $unique_customer_ids = array_unique(array_values($arr));
     $this->assertEquals($collection->maximumCount(), count($unique_customer_ids));
 }
 /**
  * Reads from the API
  *
  * @param   object  $model
  * @param   array   $queryData
  * @return  array
  */
 public function read(Model $model, $queryData = array(), $recursive = NULL)
 {
     $queryData = array_merge(array('conditions' => null, 'fields' => null, 'joins' => array(), 'limit' => 1, 'offset' => null, 'order' => array(0 => null), 'page' => 1, 'group' => null, 'callbacks' => 1, 'contain' => false, 'recursive' => -1), $queryData);
     extract($queryData);
     if (!empty($fields) && is_string($fields) && $fields == 'count') {
         $is_count = true;
     } else {
         $is_count = false;
     }
     if (!$this->_readErrors($model, $queryData, array('is_count' => $is_count))) {
         return false;
     }
     if (!empty($conditions[$model->alias . '.' . $model->primaryKey]) && ($limit == 1 || empty($limit) && $is_count)) {
         $entity = $this->_getModelEntity($model);
         try {
             switch ($entity) {
                 case 'Customer':
                     $customer = Braintree_Customer::find($conditions[$model->alias . '.' . $model->primaryKey]);
                     $result = array(0 => array($model->alias => array('id' => $customer->id, 'first_name' => $customer->firstName, 'last_name' => $customer->lastName, 'company' => $customer->company, 'email' => $customer->email, 'phone' => $customer->phone, 'fax' => $customer->fax, 'website' => $customer->website, 'created' => $customer->createdAt->format('Y-m-d H:i:s'), 'modified' => $customer->updatedAt->format('Y-m-d H:i:s'))));
                     break;
                 case 'Transaction':
                     $exploded = explode('|', $conditions[$model->alias . '.' . $model->primaryKey]);
                     $braintree_transaction_id = isset($exploded[1]) ? $exploded[1] : $conditions[$model->alias . '.' . $model->primaryKey];
                     $transaction = Braintree_Transaction::find($braintree_transaction_id);
                     $result = array(0 => array($model->alias => array('id' => $transaction->customer['id'] . '|' . $transaction->id, 'customer_id' => $transaction->customer['id'], 'payment_method_token' => $transaction->creditCard['token'], 'type' => $transaction->type, 'amount' => $transaction->amount, 'status' => $transaction->status, 'created' => $transaction->createdAt->format('Y-m-d H:i:s'), 'modified' => $transaction->updatedAt->format('Y-m-d H:i:s'))));
                     $result[0][$model->alias . 'Status'] = array();
                     $count = 0;
                     foreach ($transaction->statusHistory as $status) {
                         $result[0][$model->alias . 'Status'][$count] = array('status' => $status->status, 'amount' => $status->amount, 'user' => $status->user, 'transaction_source' => $status->transactionSource, 'created' => $status->timestamp->format('Y-m-d H:i:s'));
                         $count++;
                     }
                     break;
                 case 'CreditCard':
                     $credit_card = Braintree_CreditCard::find($conditions[$model->alias . '.' . $model->primaryKey]);
                     $result = array(0 => array($model->alias => array('token' => $credit_card->token, 'customer_id' => $credit_card->customerId, 'cardholder_name' => $credit_card->cardholderName, 'card_type' => $credit_card->cardType, 'masked_number' => $credit_card->maskedNumber, 'expiration_date' => date('Y-m', strtotime($credit_card->expirationDate)) . '-01', 'is_default' => $credit_card->default, 'created' => $credit_card->createdAt->format('Y-m-d H:i:s'), 'modified' => $credit_card->updatedAt->format('Y-m-d H:i:s'))));
                     break;
                 case 'Address':
                     $exploded = explode('|', $conditions[$model->alias . '.' . $model->primaryKey]);
                     if (count($exploded) != 2) {
                         return false;
                     }
                     list($customer_id, $address_id) = $exploded;
                     $address = Braintree_Address::find($customer_id, $address_id);
                     $result = array(0 => array($model->alias => array('id' => $address->customerId . '|' . $address->id, 'first_name' => $address->firstName, 'last_name' => $address->lastName, 'company' => $address->company, 'street_address' => $address->streetAddress, 'extended_address' => $address->extendedAddress, 'locality' => $address->locality, 'region' => $address->region, 'postal_code' => $address->postalCode, 'country_code_alpha_2' => $address->countryCodeAlpha2, 'country_code_alpha_3' => $address->countryCodeAlpha3, 'country_code_numeric' => $address->countryCodeNumeric, 'country_name' => $address->countryName, 'created' => $address->createdAt->format('Y-m-d H:i:s'), 'modified' => $address->updatedAt->format('Y-m-d H:i:s'))));
                     break;
                 default:
                     $result = false;
                     break;
             }
         } catch (Exception $e) {
             $result = false;
         }
         if ($is_count) {
             return array(0 => array(0 => array('count' => $result ? 1 : 0)));
         }
         return $result;
     }
     if (empty($conditions)) {
         try {
             $all_customers = Braintree_Customer::all();
         } catch (Exception $e) {
             $this->showError(print_r($e, true));
             return array();
         }
         $return = array();
         $count = 0;
         foreach ($all_customers->_ids as $id) {
             $return[$count][$model->alias]['id'] = $id;
             $count++;
         }
         return $return;
     }
 }
 /**
  * Run script
  *
  */
 public function run()
 {
     $mode = $this->getArg('mode');
     $this->_verbose = $this->getArg('verbose') ? true : false;
     if ($mode != self::MODE_CREATED_AT) {
         $mode = self::MODE_ALL;
     }
     if ($mode == self::MODE_CREATED_AT) {
         $startDateArg = $this->getArg('start_date');
         if (!$startDateArg) {
             die('start_date is required for mode created_at');
         }
         if (!preg_match("/^\\d{4}-\\d{2}-\\d{2}\$/", $startDateArg)) {
             die('Invalid date format. Have to be YYYY-MM-DD');
         }
         try {
             $startDate = new DateTime($startDateArg);
         } catch (Exception $e) {
             die('Invalid date format. Have to be YYYY-MM-DD');
         }
     }
     $credentials = array();
     $websites = Mage::getResourceModel('core/website_collection')->setLoadDefault(true)->load();
     // get unique credentials for all websites
     foreach ($websites as $website) {
         $merchantId = $website->getConfig('payment/braintree/merchant_id');
         if (!array_key_exists($merchantId, $credentials)) {
             $credentials[$merchantId] = array('public_key' => $website->getConfig('payment/braintree/public_key'), 'private_key' => $website->getConfig('payment/braintree/private_key'), 'environment' => $website->getConfig('payment/braintree/environment'));
         }
     }
     if (!$credentials) {
         die('No credentials found');
     }
     $this->_braintree = Mage::getSingleton('braintree_payments/paymentmethod');
     $this->_customerResource = Mage::getResourceModel('customer/customer');
     foreach ($credentials as $merchantId => $additionalData) {
         Braintree_Configuration::environment($additionalData['environment']);
         Braintree_Configuration::merchantId($merchantId);
         Braintree_Configuration::publicKey($additionalData['public_key']);
         Braintree_Configuration::privateKey($additionalData['private_key']);
         $this->_output('Start processing for merchant account ' . $merchantId);
         if ($mode == self::MODE_ALL) {
             // load all Braintree customers for merchant account
             try {
                 $customers = Braintree_Customer::all();
             } catch (Exception $e) {
                 Mage::logException($e);
                 $this->_output('Cannot fetch customers for merchant account ' . $merchantId . '. Verify credentials', true);
                 continue;
             }
             if ($customers->maximumCount() == 0) {
                 $this->_output('No customers found for merchant account ' . $merchantId);
                 continue;
             }
             if ($customers->maximumCount() < self::BULK_MAX_SIZE) {
                 $this->_processBulk($customers);
             } else {
                 $mode = self::MODE_CREATED_AT;
                 // No Braintree customers before this moment
                 $startDate = new DateTime('2007-01-01');
             }
         }
         if ($mode == self::MODE_CREATED_AT) {
             $endDate = new DateTime();
             try {
                 $customers = Braintree_Customer::search(array(Braintree_CustomerSearch::createdAt()->between($startDate, $endDate)));
             } catch (Exception $e) {
                 Mage::logException($e);
             }
             if ($customers->maximumCount() == 0) {
                 $this->_output('No customers found for merchant account ' . $merchantId);
                 continue;
             }
             $this->_selectBulk($startDate);
         }
         $this->_output('Processed all customers for merchant account ' . $merchantId);
     }
     $this->_output('All customers for all websites processed');
 }
 /**
  * wrapper for getting customers from braintree
  * @param object - user
  * @param string - id of the customer (default null)
  *
  * @return object - the searched customers OR null if not found OR collection of all customers
  */
 private static function getCustomers($user, $customerId = null)
 {
     self::setBraintreeCredentials($user);
     $returnVariable = null;
     if ($customerId) {
         try {
             $returnVariable = Braintree_Customer::find($customerId);
         } catch (Exception $e) {
             return null;
         }
     } else {
         $returnVariable = Braintree_Customer::all();
     }
     return $returnVariable;
 }
 /**
  * getCustomers
  * --------------------------------------------------
  * Getting a list of customers.
  * @returns The stripe customers.
  * @throws StripeNotConnected
  * --------------------------------------------------
  */
 public function getCustomers()
 {
     // Return.
     return Braintree_Customer::all();
 }