Search code examples
iosswiftpaypalbraintreebraintree-sandbox

Braintree Drop-In in iOS Swift: Where to get variable PaymentMethodNonce:


I have been following the instructions here https://developers.braintreepayments.com/start/hello-client/ios/v4 to integrate a drop-in payment form with braintree in iOS Swift. My code looks as follows, which, as far as I can tell, matches the documentation exactly.

func fetchClientToken() {
    // TODO: Switch this URL to your own authenticated API
    let clientTokenURL = NSURL(string: "https://braintree-sample-merchant.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)

        // As an example, you may wish to present Drop-in at this point.
        self.showDropIn(clientTokenOrTokenizationKey: clientToken!)

        // Continue to the next section to learn more...
        }.resume()
}

func postNonceToServer(paymentMethodNonce: String) {
    // Update URL with your server
    let paymentURL = URL(string: "https://your-server.example.com/payment-methods")!
    let request = NSMutableURLRequest(url: paymentURL)
    request.httpBody = "payment_method_nonce=\(paymentMethodNonce)".data(using: String.Encoding.utf8)
    request.httpMethod = "POST"
    URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
        // TODO: Handle success or failure
        }.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
            // result.paymentOptionType
            // result.paymentMethod
            // result.paymentIcon
            // result.paymentDescription
        }
        controller.dismiss(animated: true, completion: nil)
    }
    self.present(dropIn!, animated: true, completion: nil)
}

Now, I can call the function fetchClientToken which then in turn, as you can see, calls showDropIn, which works all fine.

However, to run postNonceToServer as the next function, I need a variable paymentMethodNonce and I have no idea where to get than from, I think this got missed in the documentation. So therefore I cannot continue after running showDropIn ...

I am quite confused, and I am sure there's an easy fix, but I cannot find any documentation on the point.


Solution

  • result.paymentMethod.nonce in function showDropIn contains the required information.

    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 {
    
                let out = result.paymentMethod!
                print(out.nonce)
                self.postNonceToServer(paymentMethodNonce: out.nonce)
    
                // Use the BTDropInResult properties to update your UI
                // result.paymentOptionType
                // result.paymentMethod
                // result.paymentIcon
                // result.paymentDescription
            }
            controller.dismiss(animated: true, completion: nil)
        }
        self.present(dropIn!, animated: true, completion: nil)
    }