Are you in Surge To do Transaction ? Then Learn How to Integrate Brain tree “An Payment Gateway API “ into our Swift Application Development

30 Mar 2016    Author :     Harnil Oza

Nowadays , In the world encircling with mobility and Smartphones all around us Performing transactions are the standard and the significant aspects any websites or the mobile application could think off. Probably almost each and every websites are affiliated with one or the other payment options in order to make feel to the customer rich user Experience.






As We can see the 80% Online trend is being channelized with E-commerce Websites and mobile apps and the more is gonna get explored in the upcoming few years . As per the statistics around 65-70% of Population highly depends on the online E-commerce platforms like those of E-bay , Amazon , Flip Kart and many other big-head companies for their style trends and shopping of their need as the current schedule has coupled them so tightly that they prefer online trading as the best option to be opted rather then personally visiting to the stores.




Looking at the above scenario Payment Gateway is the most inevitable option to be integrated in any of the Website developed . So , the let's have a deeper look inside Brain tree and know how it integrates with the Website.

Know What is Brain tree and its Working


It's an Full-fledged Platform that accepts payments from your application and websites and now with the coupling of mobile SDK it has become much more easier.


Consist of :

1) Client SDK : enables you to gather Payment Details from Paypal, credit card etc.

2) Server SDK : It maintains all the request to Brain tree Gateway.





1) An Request of client token is being send from your server side for client token.


2) Then, Server after generating client token sends back to the client along with server SDK.


3) Once Client SDK is created and all payment info of customer is submitted , communication takes place between SDK and Brain tree that returns payment method.


4) Then a payment nonce is being send to Server and once payment nonce is being received by server then it uses server SDK to create transaction and other Brain tree functions.


Now, let's start with Client Setup





There are number of ways client SDK can be setup

i) Using Drop-in UI

ii) Add buttons for Apple Pay , Paypal, Venmo , Android Pay.

iii) Credit Card Tokenization.

Instructions for setup :


Step 1: Engage Braintree to your podfile :

pod 'Braintree'

Then, run -> pod install .


Bridge Header For Swift

If your podfile does not include any kind of pods that needs framework , then your current versions of CocoaPods do not use framework till the time you make clear specification to use frameworks in your profile


If your app is being coded using Swift and that to does not contain any kind of frameworks , then use following steps to import header files of BrainTree.

1) Create bridge header file as : File -> new -> Header file and then Next . Create file using file name as of : MyAbc-Bridge-Header.h and make sure you add header to your application and check the appropriate box.

2) go to settings - > objective-C Bridging Header . Now set this path for the Bridging header. Eg. MyAbc / MyAbc-Bridge-Header.h . Also add all the other Braintree header files like eg. #import “BrainUI.h”.


Initializing Braintree


Just Declare the below property wherever you handle your order and payment details

var braintreeClient: BTAPIClient?

Get Client token

As we know Client tokens are not mandatory means if you are making use of tokenization key then you do not need to consider this section.


Your server is the one responsible to generate Client token that holds all your permission and configurations details that is needed by the client to generate client SDK.


Client Token is being requested by your app from your Server. Following Example demonstrates you the sample Server integration and if you want to adapt it then, you need to make use of your back end API.




Present Drop-in UI



It is the best way to collect payment details from your customer. You can also pick up the option for creating customized UI and then tokenize payment straightaway.


Initialize the Class BTDropInViewControllerDelegate to retrieve user's method of payment.




Now , to initialize Brain tree SDK create BTDropInViewController.



Now implement BTDropInViewControllerDelegate to receive the payment method nonce on victory, and reject the Drop In UI in any of the case:




Sending Payment method Nonce to Server

Now again Send the resulting Payment nonce to server back






Step 1: Create Your Sandbox Account.


Step 2: Now once, you have created your sandbox account login into that to acquire your Sandbox API details and for that you will need below mentioned concerns:

i) Sandbox Merchant ID

ii)Public Key

iii)Private Key

Install & configure

Include brain tree gem in your project's Gem-file as below :



gem "braintree"

and , to install run bundle install .


In your code, Set the environment and API credentials:




Generate Client token

Your server is answerable for creating a client token, which holds all permissions and configuration credentials that your client wants to commence the client SDK to commutate with Brain tree. That also includes a customer Id when creating the client token that returns customers selected from previously used payment method choices, that leads betterment in user experience over multiple payment checkouts.


Send a client token to your client

Here is an demonstration of how server would expose client token to you




Receive a payment method nonce from your client


Your server execution is then liable for receiving the payment_method_once and using it appropriately.



post "/checkout" do

nonce = params[:payment_method_nonce]

# Use payment method nonce here...


Creating transaction

Now you can create transactions with the help of payment_method_nonce and amount:



result =

:amount => "100.00",

:payment_method_nonce => nonce_from_the_client,

:options => {

:submit_for_settlement => true




Testing & Go live

Now the testing phase is conducted where with the help of different test values you can go with testing and then can go live.


So , hope now you are clear with Payment API integration and how the payment process is been scheduled. If still you are confused then no need to stress your mind just hire Android app developer from one of the  top app development companies i.e Hyperlink Infosystem and get your API integrated . Contact us now for further discussions.


Leave your comments

(Your email address will not be published. Required fields are marked *)

Get an instant Free, Non-Obligatory Quote NOW