Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Combined (working) Braintree Swift example for iOS 9
import UIKit
import Braintree
let testToken = "eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiJkODIwNjRmN2ZhZjExNWJiYTI5NjdmNGEwNmM2OWJhZjQxODVjYjJiZjg2NWNkYzRjNzRiZmIzMzdjNmU5MDMwfGNyZWF0ZWRfYXQ9MjAxNi0wNC0xMlQxNjowNzoyOS44MTc4NjgzNjYrMDAwMFx1MDAyNm1lcmNoYW50X2lkPTM0OHBrOWNnZjNiZ3l3MmJcdTAwMjZwdWJsaWNfa2V5PTJuMjQ3ZHY4OWJxOXZtcHIiLCJjb25maWdVcmwiOiJodHRwczovL2FwaS5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tOjQ0My9tZXJjaGFudHMvMzQ4cGs5Y2dmM2JneXcyYi9jbGllbnRfYXBpL3YxL2NvbmZpZ3VyYXRpb24iLCJjaGFsbGVuZ2VzIjpbXSwiZW52aXJvbm1lbnQiOiJzYW5kYm94IiwiY2xpZW50QXBpVXJsIjoiaHR0cHM6Ly9hcGkuc2FuZGJveC5icmFpbnRyZWVnYXRld2F5LmNvbTo0NDMvbWVyY2hhbnRzLzM0OHBrOWNnZjNiZ3l3MmIvY2xpZW50X2FwaSIsImFzc2V0c1VybCI6Imh0dHBzOi8vYXNzZXRzLmJyYWludHJlZWdhdGV3YXkuY29tIiwiYXV0aFVybCI6Imh0dHBzOi8vYXV0aC52ZW5tby5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tIiwiYW5hbHl0aWNzIjp7InVybCI6Imh0dHBzOi8vY2xpZW50LWFuYWx5dGljcy5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tLzM0OHBrOWNnZjNiZ3l3MmIifSwidGhyZWVEU2VjdXJlRW5hYmxlZCI6dHJ1ZSwicGF5cGFsRW5hYmxlZCI6dHJ1ZSwicGF5cGFsIjp7ImRpc3BsYXlOYW1lIjoiQWNtZSBXaWRnZXRzLCBMdGQuIChTYW5kYm94KSIsImNsaWVudElkIjpudWxsLCJwcml2YWN5VXJsIjoiaHR0cDovL2V4YW1wbGUuY29tL3BwIiwidXNlckFncmVlbWVudFVybCI6Imh0dHA6Ly9leGFtcGxlLmNvbS90b3MiLCJiYXNlVXJsIjoiaHR0cHM6Ly9hc3NldHMuYnJhaW50cmVlZ2F0ZXdheS5jb20iLCJhc3NldHNVcmwiOiJodHRwczovL2NoZWNrb3V0LnBheXBhbC5jb20iLCJkaXJlY3RCYXNlVXJsIjpudWxsLCJhbGxvd0h0dHAiOnRydWUsImVudmlyb25tZW50Tm9OZXR3b3JrIjp0cnVlLCJlbnZpcm9ubWVudCI6Im9mZmxpbmUiLCJ1bnZldHRlZE1lcmNoYW50IjpmYWxzZSwiYnJhaW50cmVlQ2xpZW50SWQiOiJtYXN0ZXJjbGllbnQzIiwiYmlsbGluZ0FncmVlbWVudHNFbmFibGVkIjp0cnVlLCJtZXJjaGFudEFjY291bnRJZCI6ImFjbWV3aWRnZXRzbHRkc2FuZGJveCIsImN1cnJlbmN5SXNvQ29kZSI6IlVTRCJ9LCJjb2luYmFzZUVuYWJsZWQiOmZhbHNlLCJtZXJjaGFudElkIjoiMzQ4cGs5Y2dmM2JneXcyYiIsInZlbm1vIjoib2ZmIn0="
class BraintreeViewController: UIViewController, BTDropInViewControllerDelegate {
var braintreeClient: BTAPIClient?
var btnc: UINavigationController?
override func viewDidLoad() {
super.viewDidLoad()
// todo: request from strobe server
let clientTokenURL = NSURL(string: "https://braintree-sample-merchant.herokuapp.com/client_token")!
let clientTokenRequest = NSMutableURLRequest(URL: clientTokenURL)
clientTokenRequest.setValue("text/plain", forHTTPHeaderField: "Accept")
NSURLSession.sharedSession().dataTaskWithRequest(clientTokenRequest) {
(data, response, error) -> Void in
// TODO: Handle errors
let clientToken = String(data: data!, encoding: NSUTF8StringEncoding)
self.braintreeClient = BTAPIClient(authorization: clientToken!)
// As an example, you may wish to present our Drop-in UI at this point.
print("Got token: \(clientToken)")
}.resume()
}
func dropInViewController(viewController: BTDropInViewController,
didSucceedWithTokenization paymentMethodNonce: BTPaymentMethodNonce) {
// Send payment method nonce to your server for processing
print("didSucceedWithTokenization \(paymentMethodNonce)")
postNonceToServer(paymentMethodNonce.nonce)
dismissViewControllerAnimated(true, completion: nil)
}
func dropInViewControllerDidCancel(viewController: BTDropInViewController) {
dismissViewControllerAnimated(true, completion: nil)
}
func userDidCancelPayment() {
print("userDidCancelPayment")
btnc?.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func tappedPay(sender: UIButton) {
let dropInViewController = BTDropInViewController(APIClient: braintreeClient!)
dropInViewController.delegate = self
dropInViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: UIBarButtonSystemItem.Cancel,
target: self, action: #selector(userDidCancelPayment))
btnc = UINavigationController(rootViewController: dropInViewController)
presentViewController(btnc!, animated: true, completion: nil)
}
func postNonceToServer(paymentMethodNonce: String) {
let paymentURL = NSURL(string: "https://your-server.example.com/payment-methods")!
let request = NSMutableURLRequest(URL: paymentURL)
request.HTTPBody = "payment_method_nonce=\(paymentMethodNonce)"
.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = "POST"
NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data, response, error) -> Void in
// TODO: Handle success or failure
}.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.