Integrating BrainTree Payment Gateway With PHP


BrainTree Payment Gateway API is cool enough for you to understand, but you need to setup the prerequisites first. This article assumes that you have installed the dependencies on your server. BrainTree API expect the followings:

  1. A Developer Sandbox Account – Create one here
  2. BrainTree Client Framework within your application – Download from here

Now, lets create a php file called _environment.php and place the following code in it

BrainTree Control Panel API Key

Now, login to your Sandbox account. You will see your merchant Id, Public Key and Private Key. Replace those values in this code.

BrainTree Transparent Redirect

We will use BrainTree Transparent Redirect to accept one time transactions. Create a file called single_transactions.php and place the following code in it.

Code Explanation

Above code is simple to understand. First we are showing a form to customers, taking their information and passing to BrainTree without actually redirecting our customers to BrainTree Payment Gateway. You can use these test credit card information:
Credit Card #: 5105105105105100
Expiry: 05/2015
CVV: 123

This is the most important piece of code, notice the transaction array in which we are passing amount and type to BrainTree PG.

$result holds the processor responses from BrainTree.

Now, we have the full information in $transaction and you can display the transaction information like

This is how you can use BrainTree Payment Gateway’s Transparent Redirect Method to make one time transactions. In the next part we are going to explain how to subscribe customers for recurring payments.

Creating Transactions from the Vault

BrainTree Payment Gateway expects Secure Credit Card Storage before actually initiating recurring billing to any customers. This is how it is expected.

  1. Secure Credit Card Storage
  2. Creating Transactions from the Vault

To develop recurring billing option first we will setup Secure Credit Cart Storage. This is fairly easy just use the following code.

Above code is straightforward, all commented code is optional but you can specify additional information you want to attach to a customer. First we can taking customers information on a form and then passing all the information to this function via AJAX or POST. The function calls BrainTree Payment Gateway and creates a record in BrainTree vault.

While creating a transaction we will need this token to tell BrainTree which credit card is going to be used for recurring billing. Finally, you will see your customer record in your sandbox like this

BrainTree Customer Record in Vault

If you can see your newly created customer in BrainTree Sandbox Vault, you are good to create Transactions (recurring in our case).

Creating BrainTree Recurring Billing Using PHP

To create a recurring billing option first, you need to create recurring billing plan under your sandbox account. Login to your sandbox account => Recurring Billing => Plans => Add New Fill the form and create a valid billing plan. Once you plan is created note the Plan ID as this is what we are going to associate our customers to. Following is the function to subscribe customers already in BrainTree vault to recurring billing plan we have just created.

Congratulations, you have just created your first recurring billing transaction. Should you need to cancel a subscription, you will need the $subscriptionID, simply use the following code

BrainTree on CVV

BrainTree’s stand on storing CVV

We can’t store the CVV according to PCI guidelines, but you can send it in with the Vault tokens. CVV rules will be applied to these transactions if the CVV is sent in with the tokens. However, CVV is not required when creating a transaction from Vault tokens. If the CVV is not passed CVV rules will not be applied.

I hope these methods will help you create BrainTree Payment Gateway one time or recurring billing transactions. I would love to hear your own experiences of using BrainTree Payment Gateway with PHP.

9 I like it
0 I don't like it