iOS Swift 中的 Braintree:如何将不同的交易金额传递给 ruby 服务器?

Braintree in iOS Swift: how to pass different transaction amounts to ruby server?

请在下面查看我的 Braintree 插件 Swift 代码和我的 ruby 服务器代码,以使用 Braintree 插件和创建交易。这很好地创建了一个 1 美元的交易,并且一切都在 Braintree 中正确预订。

现在我的问题是如何更改金额?我不知道如何将变量(包含任何所需数量)从我的 swift 代码传递到我的 ruby 服务器,而且我想知道这样做是否安全或者数量是否应该通过时加密?

顺便说一句,我在我看到的 Swift 代码中遇到了涉及 'request.amount = "23.00"' 的语句(这可能是将金额从 Swift 传递到 ruby),但是,同样,我不知道如何正确使用它,而且在我使用的 Braintree 网站上也没有解释:https://developers.braintreepayments.com/start/hello-server/python#create-a-transaction

SWIFT(应用内):

func postNonceToServer(paymentMethodNonce: String) {
    let paymentURL = URL(string: "https://myexample-31423.herokuapp.com/checkout")!
    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 {       
            let selectedPaymentMethod = result.paymentMethod!
            self.postNonceToServer(paymentMethodNonce: selectedPaymentMethod.nonce)
        }
        controller.dismiss(animated: true, completion: nil)
    }
    self.present(dropIn!, animated: true, completion: nil)
}

RUBY(在 Heroku 上):

post "/checkout" do
    nonce_from_the_client  = params[:payment_method_nonce]
    result = Braintree::Transaction.sale(
                       :amount => "1.00",
                       :payment_method_nonce => nonce_from_the_client,
                       :options => {
                         :submit_for_settlement => true
                       }
    )
end

好的,我自己解决了。在 Swift 中,例如使用此行:

let amount       = "50" as String
request.httpBody = "payment_method_nonce=\(paymentMethodNonce)&amount=\(amount)".data(using: String.Encoding.utf8)

然后在ruby中,使用以下内容。

post "/checkout" do
    nonce_from_the_client  = params[:payment_method_nonce]
    amount                 = params[:amount]
    result = Braintree::Transaction.sale(
                                     :amount => amount,
                                     :payment_method_nonce => nonce_from_the_client,
                                     :options => {
                                     :submit_for_settlement => true
                                     }
                                     )
end

就可以了。我没有意识到两个变量可以简单地通过'&'分隔传递。