iOS Swift中的Braintree Drop-In:在哪里获得变量PaymentMethodNonce:

时间:2016-12-09 00:28:41

标签: ios swift paypal braintree braintree-sandbox

我一直按照此处的说明https://developers.braintreepayments.com/start/hello-client/ios/v4 在iOS Swift中将支持付款表单与braintree集成。我的代码如下所示,据我所知,它与文档完全匹配。

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)
}

现在,我可以调用函数fetchClientToken,然后你可以看到调用showDropIn,它可以正常工作。

但是,要将postNonceToServer作为下一个函数运行,我需要一个变量paymentMethodNonce,我不知道从哪里得到,我认为这在文档中被遗漏了。因此,在运行showDropIn后,我无法继续......

我很困惑,我确信这很容易解决,但我找不到任何关于这一点的文档。

2 个答案:

答案 0 :(得分:1)

函数showDropIn中的

result.paymentMethod.nonce包含所需的信息。

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)
}

答案 1 :(得分: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];

}