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

Posted

技术标签:

【中文标题】iOS Swift 中的 Braintree Drop-In:在哪里获取变量 PaymentMethodNonce:【英文标题】:Braintree Drop-In in iOS Swift: Where to get variable PaymentMethodNonce: 【发布时间】:2017-04-24 09:20:20 【问题描述】:

我一直按照这里的说明进行操作https://developers.braintreepayments.com/start/hello-client/ios/v4 在 iOS Swift 中集成一个 drop-in 支付表单和 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 后无法继续...

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

【问题讨论】:

我在通过 cocoapod 安装 Braintree 时遇到此错误。 “无法找到 BraintreeDropIn 的规范” 【参考方案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)

【讨论】:

我从沙盒控制面板的 Tokenization Keys 下获得了 TokenizationKey。它仍然没有进入函数内部并且没有显示任何错误。 嗨,您在退货中收到任何回复了吗? 嗨,Rajesh 不确定我是否理解这个问题。我为回答我自己的问题而发布的最后一段代码为我完成了这项工作。【参考方案2】:
- (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];

【讨论】:

以上是关于iOS Swift 中的 Braintree Drop-In:在哪里获取变量 PaymentMethodNonce:的主要内容,如果未能解决你的问题,请参考以下文章

PayPal Checkout 直接使用 Braintree SDK - Swift

如何在 iOS 中使用 Braintree 实现支付应用

Braintree iOS v4 SDK

Braintree - 插入式 UI - Apple Pay - Swift 3

通过 Braintree PayPal 付款总是失败 iOS

Braintree 点对点 iOS