Wednesday 24 June 2015

Manage Customer Details in Braintree




Following are code snippet that are useful to mange the customer details in braintree. you must have an account in braintree to use below code.


Add Customer
You can ADD customer details in braintree server, after saving the customer detail you will get customerId which can use latter on.  You can also add custom fields.

Note: Each customer is bound to specific merchant.

/** Include Library and set configuration**/
require_once '/braintree-php301/lib/Braintree.php';
Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('xxxxxxxxxxxxxxxx');
Braintree_Configuration::publicKey('xxxxxxxxxxxxxxxxx');
Braintree_Configuration::privateKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxx');
/** Include Library and set configuration**/

$result = Braintree_Customer::create(array(
    'firstName' => 'Ram   ',
    'lastName' => 'Kumar',
    'company' => 'web-technology-experts-notes',
    'email' => 'email@web-technology-experts-notes.in',
    'phone' => '281.330.8004',
    'fax' => '419.555.1235',
    'website' => 'http://www.web-technology-experts-notes.in',
    'customFields' => array(
            'custom_field_one' => 'custom value',
            'custom_field_two' => 'another custom value'
        )
));

if($result->success){
    $customerId = $result->customer->id; //New Customer Id
}



Update Customer
Using the customerId, You can UPDATE customer details in braintree server.

/** Include Library and set configuration**/
.
.
.
/** Include Library and set configuration**/


$customerId = 14983213;
$result = Braintree_Customer::update($customerId,array(
            'firstName' => 'Ram   ',
            'lastName' => 'Singh',
            'company' => 'web-technology-experts-notes',
            'email' => 'email@web-technology-experts-notes.in',
            'phone' => '281.330.8004',
            'fax' => '419.555.1235',
            'website' => 'http://www.web-technology-experts-notes.in'
));  
if($result->success){
    die("Customer Detail Updated Successfuly");
}



Find Customer
Using the customerId, You can SEARCH the customer detail from braintree server.

/** Include Library and set configuration**/
.
.
.
/** Include Library and set configuration**/

/** find customer detail **/
$customerId = 14983213;
try{
      $result = Braintree_Customer::find($customerId); 
     echo $result->id; echo "\n";
     echo $result->firstName; echo "\n";
     echo $result->lastName; echo "\n";
     echo $result->email; echo "\n";
     echo $result->phone; echo "\n";
  }  catch (Exception $e){

      echo $e->getMessage();die;
  }
/** find customer detail **/


Delete Customer
Using the customerId, You can DELETE customer detail from braintree server.

/** Include Library and set configuration**/
.
.
.
/** Include Library and set configuration**/

/** Delete a customer **/
$customerId = 14983213;
try{
      $result = Braintree_Customer::delete($customerId); 
      if($result->success){
        echo "Customer deleted successfully";
        }
      
  }  catch (Exception $e){

      echo $e->getMessage();die;
  }
/** Delete a customer **/