4

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.

Krish Munot
  • 1,093
  • 2
  • 18
  • 29
AJGronevelt
  • 551
  • 1
  • 6
  • 22
  • I am getting this error while installing Braintree through cocoapod. "Unable to find a specification for BraintreeDropIn" – VJVJ Dec 20 '16 at 07:13

2 Answers2

2

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)
}
AJGronevelt
  • 551
  • 1
  • 6
  • 22
  • i got TokenizationKey from sandbox control panel under Tokenization Keys. Still its not going inside the function and not showing any error. – VJVJ Dec 20 '16 at 10:41
  • Hi Did you get any response in the return? – Rajesh Maurya Mar 09 '18 at 14:37
  • Hi Rajesh not sure I understand the question. This last bit of code which I posted in response to my own question did the job for me. – AJGronevelt Mar 11 '18 at 17:24
1
- (void)showDropIn:(NSString *)clientTokenOrTokenizationKey {
BTDropInRequest *request = [[BTDropInRequest alloc] init];
request.amount = @"10";
BTDropInController *dropIn = [[BTDropInController alloc] initWithAuthorization:clientTokenOrTokenizationKey request:request handler:^(BTDropInController * _Nonnull controller, BTDropInResult * _Nullable result, NSError * _Nullable error) {

    if (error != nil) {
        NSLog(@"ERROR");
    } else if (result.cancelled) {
        NSLog(@"CANCELLED");
    } else {

        self.selectedNonce = result.paymentMethod;
       [self postNonceToServer:self.selectedNonce.nonce];

    }
}];
[self presentViewController:dropIn animated:YES completion:nil];

}

Jason
  • 151
  • 1
  • 13