Created
February 13, 2017 04:37
-
-
Save anonymous/e0825fd399a57bbc518f5eb9d7c949f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// PaypalDemo | |
// | |
// Created by Hongli Yu on 09/02/2017. | |
// Copyright © 2017 Hongli Yu. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
var braintreeClient: BTAPIClient! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// self.setupPayPalUI() | |
self.fetchClientToken() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func fetchClientToken() { | |
// TODO: Switch this URL to your own authenticated API | |
let clientTokenURL = NSURL(string: "https://cryptic-citadel-99786.herokuapp.com/client_token")! | |
let clientTokenRequest = NSMutableURLRequest(url: clientTokenURL as URL) | |
clientTokenRequest.setValue("text/plain", forHTTPHeaderField: "Accept") | |
URLSession.shared.dataTask(with: clientTokenRequest as URLRequest) { (data, response, error) -> Void in | |
// TODO: Handle errors | |
let clientToken = String(data: data!, encoding: String.Encoding.utf8) | |
print(clientToken ?? "no token") | |
self.showDropIn(clientTokenOrTokenizationKey: clientToken!) | |
// As an example, you may wish to present Drop-in at this point. | |
// Continue to the next section to learn more... | |
}.resume() | |
} | |
func showDropIn(clientTokenOrTokenizationKey: String) { | |
let request = BTDropInRequest() | |
let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request) | |
{ (controller, result, error) in | |
if (error != nil) { | |
print("ERROR") | |
} else if (result?.isCancelled == true) { | |
print("CANCELLED") | |
} else if let result = result { | |
// Use the BTDropInResult properties to update your UI | |
print("\(result.paymentOptionType) + \(result.paymentMethod) + \(result.paymentIcon) + \(result.paymentDescription)") | |
self.postNonceToServer(paymentMethodNonce: "fake-valid-nonce") // result.paymentMethod | |
} | |
controller.dismiss(animated: true, completion: nil) | |
} | |
self.present(dropIn!, animated: true, completion: nil) | |
} | |
func postNonceToServer(paymentMethodNonce: String) { | |
// Update URL with your server | |
let paymentURL = URL(string: "https://cryptic-citadel-99786.herokuapp.com/checkout")! | |
// let paymentURL = URL(string: "https://cryptic-citadel-99786.herokuapp.com/paypal_checkout")! | |
var request = URLRequest(url: paymentURL) | |
request.httpBody = "payment_method_nonce=\(paymentMethodNonce)".data(using: String.Encoding.utf8) | |
request.httpMethod = "POST" | |
URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in | |
if data != nil { | |
let tempstr = String(data: data!, encoding: String.Encoding.utf8) | |
print(tempstr ?? "nothing returned") | |
} | |
if let response = response { | |
print(response) | |
} | |
if let error = error { | |
print(error) | |
} | |
}.resume() | |
} | |
} | |
extension ViewController: BTViewControllerPresentingDelegate, BTAppSwitchDelegate { | |
func setupPayPalUI() { | |
self.braintreeClient = BTAPIClient(authorization: "sandbox_pqtk9jxg_2h94hr4c75p4r2cc") | |
let customPayPalButton = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 120)) | |
customPayPalButton.addTarget(self, action: #selector(customPayPalButtonTapped(button:)), for: UIControlEvents.touchUpInside) | |
customPayPalButton.backgroundColor = UIColor.green | |
self.view.addSubview(customPayPalButton) | |
} | |
func customPayPalButtonTapped(button: UIButton) { | |
let payPalDriver = BTPayPalDriver(apiClient: self.braintreeClient) | |
payPalDriver.viewControllerPresentingDelegate = self | |
payPalDriver.appSwitchDelegate = self | |
// Start the Vault flow, or... | |
payPalDriver.authorizeAccount() { (tokenizedPayPalAccount, error) -> Void in | |
print(tokenizedPayPalAccount?.nonce ?? "nothing") | |
// | |
} | |
} | |
// MARK: - BTViewControllerPresentingDelegate | |
func paymentDriver(_ driver: Any, requestsPresentationOf viewController: UIViewController) { | |
present(viewController, animated: true, completion: nil) | |
} | |
func paymentDriver(_ driver: Any, requestsDismissalOf viewController: UIViewController) { | |
viewController.dismiss(animated: true, completion: nil) | |
} | |
// MARK: - BTAppSwitchDelegate | |
// Optional - display and hide loading indicator UI | |
func appSwitcherWillPerformAppSwitch(_ appSwitcher: Any) { | |
showLoadingUI() | |
NotificationCenter.default.addObserver(self, selector: #selector(hideLoadingUI), | |
name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil) | |
} | |
func appSwitcherWillProcessPaymentInfo(_ appSwitcher: Any) { | |
hideLoadingUI() | |
} | |
func appSwitcher(_ appSwitcher: Any, didPerformSwitchTo target: BTAppSwitchTarget) { | |
} | |
// MARK: - Private methods | |
func showLoadingUI() { | |
// ... | |
} | |
func hideLoadingUI() { | |
NotificationCenter | |
.default | |
.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil) | |
// ... | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment