How to implement Braintree client for card payment in iOS Swift

Posted By : Varun Wadhwa | 13-Mar-2016



First of all you need to add Braintree framework into your project. add pod 'Braintree/Card' in podfile of your project.

now import Braintree in file in which  you want to collect card details.

 

  import Braintree

 

declare the BTAPIClient instance as :

 

 var braintreeAPIClient: BTAPIClient?

 

now you need to obtain a client Token from your backend server, your backend server is responsible for genrating a clientToken by Braintree server side SDK.

so before initialization of  BTAPIClient make sure you have obtained  the clientToken.

 

now intialize the BTAPIClient instance as:

 

  braintreeAPIClient = BTAPIClient(authorization: clientToken)! 
 //clientToken must obtain from backend server before initialization

 

now create a BTCardClient instance

 

let cardClient = BTCardClient(APIClient: braintreeAPIClient!)

 

now you need to need to instantiate a BTCard , BTCard represents the credit card data of customer and serve as input for tokenization.

 

let braintreeCard = BTCard(number: creditCardNumber, expirationMonth: expiryMonth, expirationYear: expiryYear, cvv: cvvNumber)

 

now tokenize the BTCard with  BTCARDClient as :

 

 cardClient.tokenizeCard(braintreeCard) { (tokenizedCard, error) in
       // tokenizedCard!.nonce genrated here can be send to your backend server and further processing take place at backend server.
     }

 

 send  resulting tokenizedCard!.nonce  to your backend server.

Thanks,

About Author

Author Image
Varun Wadhwa

Varun is a mobile developer with experience in various technologies like Titanium , Cordova and native iOS development.

Request for Proposal

Name is required

Comment is required

Sending message..