2016-12-09 5 views
3

こちらの手順に従っていますhttps://developers.braintreepayments.com/start/hello-client/ios/v4 iOS Swiftのbraintreeとドロップインペイメントフォームを統合するには、私のコードは次のように見えますが、私が知る限り、正確に文書にマッチします。Braintree Drop-In in 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) 
} 

今、私はその後、順番に、あなたが見ることができるように、すべて正常に動作され、showDropInを呼び出す関数fetchClientTokenを呼び出すことができます。

しかし、postNonceToServerを次の関数として実行するには、変数paymentMethodNonceが必要です。どこから取得するのかわからないため、ドキュメントに記載されていないと思います。したがって、私はshowDropInを実行した後に続けることができません...

私はかなり混乱していますが、簡単な修正があると確信していますが、その点に関するドキュメントは見つかりません。

+0

cocoapodを通じてブレインをインストールしている間、私はこのエラーを取得しています。 「BraintreeDropInの仕様を見つけることができません」 – VJVJ

答えて

1

result.paymentMethod.nonce in function 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

トークンキーの下のサンドボックスコントロールパネルから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]; 

}

関連する問題