2016-12-09 23 views
3

我一直在按照這裏的說明https://developers.braintreepayments.com/start/hello-client/ios/v4 在iOS Swift中集成一個支付形式與braintree。我的代碼如下所示,據我所知,它完全匹配文檔。現在iOS中的布蘭特裏嵌入式Swift:從哪裏獲得可變PaymentMethodNonce:

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後...

我很困惑,我相信有一個簡單的修復,但我找不到任何文件的重點。

+0

,同時通過cocoapod安裝布倫特裏我得到這個錯誤。 「無法找到BraintreeDropIn的規範」 – VJVJ

回答

1

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

我從Tokenization Keys下的沙箱控制面板獲得TokenizationKey。它仍然沒有進入功能,並沒有顯示任何錯誤。 – VJVJ

+0

嗨你有沒有在回報中得到任何迴應? – Rajesh

0
- (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]; 

}