2016-12-06 112 views
2

我的问题是:我创建了一个本应接受信用卡/借记卡的应用程序。而且我在iOS中使用Braintree 4 SDK,使用可可豆荚进行swift。我可以很好地提出这个下降,但是它只是要求一个cc数然后消失,为什么?因为我在快速入门指南中使用了代码,所以应该这样做。但它没有说什么时候调用nonce函数或显示数量或任何东西!随着SDK 3是一个视图控制器,我可以把委托,几乎所有的东西除了记忆卡的作品。所以我的问题是我应该在哪里调用iOS中的nonce函数?总数应该显示在哪里?我如何向服务器BT付款?如何在iOS中使用Braintree实现支付应用程序

该网页真的缺乏一切的实际信息!帮帮我。

我的代码:

// Mark - Braintree methods 

    func showDropIn(clientTokenOrTokenizationKey: String) { 

      let request = BTDropInRequest() 
      request.amount = "\(total)" 
      request.currencyCode = "MXN" 
//   request. 
      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 
        print(result) 

//     controller. 
       } 
       controller.dismiss(animated: true, completion: nil) 
      } 
      self.present(dropIn!, animated: true, completion: nil) 

//  //create paymentrequest 
//  let 
//  paymentRequest: BTPaymentRequest = BTPaymentRequest() 
//  paymentRequest.summaryTitle = "Lavado Automozo" 
//  paymentRequest.summaryDescription = "$\(totalLabel.text!) precio total de los servicios solicitados." 
//  paymentRequest.displayAmount = "$\(total!).00 MXN" 
//  paymentRequest.currencyCode = "MXN" 
//  paymentRequest.callToActionText = "Aceptar compra." 
//  paymentRequest.shouldHideCallToAction = false 
//  //set delegate 
//  let dropInViewController = BTDropInViewController(apiClient: braintreeClient!) 
//  dropInViewController.delegate = self 
//  dropInViewController.paymentRequest = paymentRequest 
//  //add cancel button 
//  dropInViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel, target: self, action: #selector(ViewController.userDidCancelPayment)) 
//   
//  //present view 
//  let navigationController = UINavigationController(rootViewController: dropInViewController) 
//   
//  present(navigationController, animated: true, completion: nil) 
//   
     //  let request = BTDropInRequest() 
//  let dropIn = BTDropInController() 
//  { (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) 
    } 

而且我应该在哪里调用这个?:

func postNonceToServer(paymentMethodNonce: String) -> Bool { 

     PFCloud.callFunction(inBackground: "checkout", withParameters: ["payment_method_nonce" : paymentMethodNonce, "amount" : "\(total!).00"]) { 

      (response, error) -> Void in 

      //   let ratings = response as? Float 
      // ratings is 4.5 

      if error != nil { 


      } else { 


      } 

      print("the response \(response ?? "nil")") 
      print("The error: \(error?.localizedDescription)") 

      //self.clientToken = response as! String 

//   print(self.clientToken) 

     } 

     let dateFormatter = DateFormatter() 
     dateFormatter.dateFormat = "mmm. dd, YYYY HH:mm" 
     dateFormatter.timeZone = NSTimeZone.local 

     let fechaRegistro = dateFormatter.string(from: Date()) 

//  displayError("Exito", message: "Tu pago ha sido procesado, en unos momentos atenderemos tu orden. Total es de $\(totalLabel.text!) la fecha registrada \(fechaRegistro)") 

     let usuarioPagado: PFObject = PFObject(className: "Ordenes") 
     let location: PFGeoPoint = PFGeoPoint(latitude: ubicacion.latitude, longitude: ubicacion.longitude) 
     usuarioPagado["Ubicacion"] = location 
     usuarioPagado["NumeroExterior"] = numeroExteriorTextField.text! 
     usuarioPagado["NumeroDeTelefono"] = telefonoTextField.text! 
     usuarioPagado["LavadoCarro"] = numeroCarrosTextField.text! 
     usuarioPagado["LavadoMiniVan"] = numeroMinivanTextField.text! 
     usuarioPagado["LavadoPickUp"] = numeroPickUpsTextField.text! 
     usuarioPagado["LavadoDeVan"] = numeroVansTextField.text! 
     usuarioPagado["LavadoAspiradoCarro"] = numeroAspiradoCarrosTextField.text! 
     usuarioPagado["LavadoAspiradoMiniVan"] = numeroAspiradoMinivanTextField.text! 
     usuarioPagado["LavadoAspiradoPickUp"] = numeroPickUpsTextField.text! 
     usuarioPagado["LavadoAspiradoDeVan"] = numeroAspiradoVansTextField.text! 
     usuarioPagado["Monto"] = totalLabel.text! 
     usuarioPagado["NumeroDeTelefono"] = telefonoTextField.text! 
     usuarioPagado["TipoDeCelular"] = "iPhone" 
     usuarioPagado["FechaDeOrden"] = fechaRegistro 
     //usuarioPagado["TipoDeCelular"] 
     //usuarioPagado["PaymentConfirmation"] = completedPayment.confirmation.description 
     // 
     //  usuarioPagado.saveInBackground() { 
     //   (success: Bool, error: Error?) -> Void in 
     // 
     //   if error == nil { 
     // 
     //    //done 
     //    print("saved object") 
     // 
     //   } else { 
     // 
     //    //not done 
     //    print("not saved because \(error?.localizedDescription)") 
     // 
     //   } 
     //  } 

     do { 

      let result = try usuarioPagado.save() 

//   displayError("Exito", message: "Tu pago ha sido procesado, en unos momentos atenderemos tu orden. Total es de $\(totalLabel.text!) la fecha registrada \(fechaRegistro)") 
//    

     } catch let error { 

      print(error.localizedDescription) 
      self.displayError("Error", message: "Hubo un error al guardar tu informacion, ponte ne contacto con nosotros.") 
      return false 
     } 
     numeroCarrosTextField.text = "0" 
     numeroMinivanTextField.text = "0" 
     numeroPickUpsTextField.text = "0" 
     numeroVansTextField.text = "0" 

     numeroAspiradoCarrosTextField.text = "0" 
     numeroAspiradoMinivanTextField.text = "0" 
     numeroAspiradoPickUpsTextField.text = "0" 
     numeroAspiradoVansTextField.text = "0" 
     totalLabel.text = "00.00" 
     self.lavadoSwitch.isOn = false 
     self.lavadoYAspiradSwitch.isOn = false 

     self.numeroExteriorTextField.text = "" 
     self.telefonoTextField.text = "" 
//  displayError("Exito", message: "Tu pago ha sido procesado, en unos momentos atenderemos tu orden. Total es de $\(totalLabel.text!) la fecha registrada \(fechaRegistro)") 

     // 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() 
     return true 

    } 

编辑DropIn Payment method selected

Dummy card

假卡后它只是关闭。

+2

我在通过cocoapod安装Braintree时遇到此错误。 “无法找到BraintreeDropIn的规格” – VJVJ

+0

@ Pedro.Alonson您是否找到了如何向用户显示产品数量并进行购买的方法? – Ramis

+0

@VJVJ真的我不得不改变框架现在我正在使用Openpay –

回答

3

完全披露:我在布伦特里工作。如果您有任何其他问题,请随时联系support

您可以通过showDropIn()中的result对象访问付款方式随机数。这也是你可以拨打postNonceToServer()的地方。

func showDropIn(clientTokenOrTokenizationKey: String) { 
    let request = BTDropInRequest() 
    request.amount = "\(total)" 
    request.currencyCode = "MXN" 
    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 selectedPaymentMethod = result.paymentMethod! // retrieve the payment method. 
      self.postNonceToServer(paymentMethodNonce: selectedPaymentMethod.nonce) // call postNonceToServer() with the nonce from the selected payment method. 
     } 
     controller.dismiss(animated: true, completion: nil) 
    } 
    self.present(dropIn!, animated: true, completion: nil) 
} 

后,您成功地调用postNonceToServer()您可以在服务器上receive the payment method noncecreate a transaction

+0

并显示总数? –

+1

@Shea我在通过cocoapod安装Braintree时遇到此错误。 “无法找到BraintreeDropIn的规范 – VJVJ