如何在iOS中使用Braintree实施付款应用 [英] How to implement a payment app with Braintree in iOS

查看:92
本文介绍了如何在iOS中使用Braintree实施付款应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:我创建了一个应该接受信用卡/借记卡的应用程序.我在iOS中使用Braintree 4 SDK在可可豆荚中快速使用.我可以很好地呈现该下降,但是这样做只是问一个抄送号,然后消失了,为什么?因为我使用了快速入门指南中的代码,所以应该这样做.但这并没有说明何时调用nonce函数或显示金额或任何内容!使用SDK 3是一个视图控制器,我可以在其中放置委托,除了记住卡片,几乎所有东西都可以使用.所以我的问题是我应该在iOS的哪里调用nonce函数?总数应显示在哪里?以及如何向服务器BT付款?

My issue is this: I have created an app that is supposed to accept Credit/Debit cards. And Im using Braintree 4 SDK in iOS with swift using a cocoa pod. I can present the drop in just fine but what it does is that just ask for a cc number and then disappears, why? Because I use the code in the quick-start guide and it's supposed to do that. But it doesn't say anything about when to call the nonce function or show the amount or anything! With SDK 3 is a view controller where I can put the delegate and almost everything works except remembering cards. So my question are where am I supposed to call the nonce function in iOS? Where does the total should be shown? And how do I make a payment to the server BT?.

该页面实际上缺少所有内容的实际信息!帮助.

The page is really lacking actual information for everything! Help.

我的代码:

// 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)
    }

我应该在哪里称呼它?:

And Where am I supposed to call this?:

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

    }

编辑

虚拟卡关闭后,它会自动关闭.

After the dummy card it just closes.

推荐答案

完整披露:我在Braintree工作.如有其他疑问,请随时与支持.

您可以通过showDropIn()中的result对象访问付款方式随机数.您也可以在这里调用 postNonceToServer() .

You can access the payment method nonce via the result object within showDropIn(). This is also where you may call 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()后,您可以接收付款方式随机数

After you successfully call postNonceToServer() you can receive the payment method nonce and create a transaction on your server.

这篇关于如何在iOS中使用Braintree实施付款应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆