This tutorial is for those who are developing a mobile application and they want to charge a customer(one or more time) in future. In this tutorial I am going to explain backend functionality in details.
Most of the time we need charge to customer in future. Some developers may store details in the database but this is not the best way when it comes to security. Fortunately this feature is provided by braintree and stripe payment gateways. Following tutorial will explain you how we can use braintree API.
Before we start a tutorial make sure that you have a braintree(Sandbox/Production) account, if you don’t have an account you can open your braintree sandbox account from here Sandbox Account. Mobile developers need to install braintree provided sdk from here Mobile SDK
Once you installed SDK successfully you can see the above screen and fields like card Number, Expiration Date and CVV Number which is provided by braintree you don’t need to do any code for that.
If you using sandbox account enter following Card details for testing purpose.
Sandbox Card Number: 4242 4242 4242 4242
Card Exp: Any future expiry month/Year
CVV: 123
When user click “Buy Now” button braintree SDK returns you paymentMethodNonce . What you’ll do next step. Simply you’ll send paymentMethodNonce to your server through webservice.
Now the actual backend functionality start here.
Suppose you are sending the parameter paymentMethodNonce to the API YourServer.com/add_card_token.php
In add_card_token.php i will use the method provided by braintree (make sure you have downloaded PHP braintree library if not then click here)
<?php
require_once ‘lib/Braintree.php’; //installed braintree php library and include over here
Braintree_Configuration::environment(‘sandbox’);
Braintree_Configuration::merchantId(‘your_account_merchant_id’);
Braintree_Configuration::publicKey(‘public_key’);
Braintree_Configuration::privateKey(‘private_key’);
$paymentMethodNonce = $_POST[‘paymentMethodNonce ‘];//Getting parameters from mobile dev
$result = Braintree_Customer::create( array( ‘firstName’ => ‘abc’, ‘lastName’ => ‘efg’, ‘company’ => ‘Nanostuffs’, ‘paymentMethodNonce’ => $paymentMethodNonce ));
$customerid = $result->customer->id; //Store this info into the database
$card_token = $result->customer->paymentMethods[0]->token;//Store this info into the database
?>
The method (Braintree_Customer::create) will returns you all the basic information about the card like expiry, last four…
You will get customer id $result->customer->id and card_token in response $result->customer->paymentMethods[0]->token
You will store this information into the database for that particular customer.
Great, now you are ready to charge a customer in future anytime. Suppose i want to charge that customer now i will use the following method.
<?php
require_once ‘lib/Braintree.php’; //installed braintree php library and include over here
Braintree_Configuration::environment(‘sandbox’);
Braintree_Configuration::merchantId(‘your_account_merchant_id’);
Braintree_Configuration::publicKey(‘public_key’);
Braintree_Configuration::privateKey(‘private_key’);
try{
$result = Braintree_Transaction::sale(array( ‘amount’ => ‘100’,
‘paymentMethodToken’ => $customer_id,
‘options’ => [ ‘submitForSettlement’ => True ] )
);
if($result->success) {
$transactionId = $result->transaction->id;
}else{
$err = ”;
foreach($result->errors->deepAll() AS $error) { $err.= $error->message.” “; }
echo $err;
}
}catch(Exception $e)
{}
?>
Check and parse proper response returned by braintree. You have charged to the customer successfully. If you run the same method again you can see the charge successfully. Without entering user card details again.
You can achieve this using any sever side langauge. I am familiar with PHP only.
Let us know if this tutorial really help you 🙂