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,
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Varun Wadhwa
Varun is a mobile developer with experience in various technologies like Titanium , Cordova and native iOS development.