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

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

我一直在按照此处的说明进行操作 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,但我不知道从哪里获取,我认为文档中遗漏了这一点。因此,我无法在 运行ning showDropIn ...

之后继续

我很困惑,我相信有一个简单的解决方法,但我找不到关于这一点的任何文档。

result.paymentMethod.nonce 在函数 showDropIn 中包含所需的信息。

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)
}
- (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];

}