Thursday, 25 June 2015

Braintree Questions and Answers - Javascript+PHP

Braintree Questions and Answers - Javascript+PHP


Question: What is use of custom fields? How to use custom fields to save the customer details?
When we create/update the customer details, you might need to add extra fields which are not supported by braintree APIs. In that case, you can use braintree custom fields, with use of custom fields you can save extra customer details like nick name, personal meeting date etc.
When you start save custom fields you will get following error:
Customer ID has already been taken.

Then means, you must have to set the custom fields name in cpanel of your account.
Following are simple instruction to set the custom fields in cpanel of merchant account.
01) Log into the Control Panel in braintree.
02) Go to Settings > Processing > Custom Fields.
03) Click on "New".
04) Fill the form. (Use API Name variable in custom field ).
05) Click Save.


Question: How to get the client token?
"Client token" are generated by Server, use following function to do the same.
$clientToken = Braintree_ClientToken::generate();
This token is used by braintree js and compulsory to process the transaction.
   braintree.setup(clientToken, "custom", {
        . 
        .
        .
      });



Question: How to get "noune payment method"?
When customer fill the credit card details and submit the form, Request goes to "Braintree".
All details are saved in "Braintree" Server.
and you get an unique string that is know as "noune payment method".
With use of this "noune payment method", you can charge the customer.

$result = Braintree_Transaction::sale(array(
    'amount' => '1.00',
    'paymentMethodNonce' => '699dc252-6388-464a-9712-5dd8fa2bb656',
    'options' => array(
      'submitForSettlement' => True
    )
  ));
$transactionId = $result->transaction->id;

 noune payment method will expired in 24 Hours.


Question: I am getting error "Unknown payment_method_nonce.". Why?
If you are getting above error message, It might have any of below Reason.
a) paymentMethodNonce is invalid.
b) paymentMethodNonce is already expired OR used.


Question: Give some test credit card numbers for braintree?
378282246310005
371449635398431
6011111111111117
3530111333300000
4111111111111111
4500600000000061

Use any CVV Number and Expiry date (Must future date)


Question: How to get Client Token from server using for Customer.
/** Include Library and set configuration**/
require_once '/braintree-php301/lib/Braintree.php';
Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('xxxxxxxx');//update merchantId
Braintree_Configuration::publicKey('xxxxxxxxxx');//update public key
Braintree_Configuration::privateKey('xxxxxxxxxxxxxxxxxxxxxx'); //Private key
 
//Get the Client Token
$clientToken = Braintree_ClientToken::generate();

//Get the Client Token for Customer 
$clientToken = Braintree_ClientToken::generate(array('customerId'=>464654654));



Question: How to get Custom details using customerId?
/** Include Library and set configuration**/
.
.
.       
/** Include Library and set configuration**/
$customerId = 67222186;  
   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;
  }


Question: How to connect a customer with nonce (payment method)?
$customer = 66082493; //CustomerId of customer
$result = Braintree_PaymentMethod::create(array(
    'customerId' => $customer,
    'paymentMethodNonce' => 'be2d6271-c71f-46ae-96c4-3b1e471cc575'
));


Question: How to charge from Customer with customer_id?
$result = Braintree_Transaction::sale(
  array(
    'customerId' => 'the_customer_id',
    'amount' => '100.00'
  )
);

Question: How to charge from Customer with nonce token?
$result = Braintree_Transaction::sale(array(
  'amount' => '100.00',
  'paymentMethodNonce' => nonceFromTheClient
]));





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 **/