请在下面查看我的 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
这样就可以了。我没有意识到两个变量可以简单地通过“&”分隔传递。
关于ios - iOS Swift 中的 Braintree : how to pass different transaction amounts to ruby server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41335814/