我目前正在使用沙箱测试 Braintree API。当我将随机数发布到服务器时,我从 Braintree API 收到消息:需要金额。无法确定付款方式。我发现我可以对随机数使用硬编码值: paymentMethodNonce: "fake-valid-nonce" 在这种情况下,我可以在沙箱中看到交易。但我想查看我在嵌入式用户界面中输入的信用卡。出现“无法确定付款方式”消息的原因可能是什么?我的服务器端node.js代码如下:
var amount = req.body.amount;
// Use the payment method nonce here
var nonceFromTheClient = req.body.paymentMethodNonce;
var newTransaction = gateway.transaction.sale({
amount: amount,
//paymentMethodNonce: "fake-valid-nonce",
paymentMethodNonce: nonceFromTheClient,
options: {
submitForSettlement: true }
}, function(error, result) {
if (result) {
res.send(result);
} else {
res.status(500).send(error);
}
});
Run Code Online (Sandbox Code Playgroud)
我的 Swift 客户端代码:
func sendRequestPaymentToServer(nonce: String, amount: String) {
let paymentURL = URL(string: "http://localhost:5000/checkout")!
var request = URLRequest(url: paymentURL)
request.httpBody = "paymentMethodNonce=\(nonce)&amount=\(amount)".data(using: String.Encoding.utf8)
request.httpMethod = "POST"
URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
guard let data = data else {
print(error!.localizedDescription)
return
}
if let result = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
if result?["success"] as? Bool == true {
print("Successfully charged. Thanks So Much :)")
}
else if let message = result?["message"] {
print(message)
}
//dump(result)
} else {
print("No json result.")
}
}.resume()
}
Run Code Online (Sandbox Code Playgroud)
问题出在我的正文解析器中 - 它配置不正确。通过以下代码行解决了该问题:
.use(bodyParser.urlencoded({extended: true}))
Run Code Online (Sandbox Code Playgroud)
在用户 @CJoseph 确认该代码在她这边有效之后。
| 归档时间: |
|
| 查看次数: |
4354 次 |
| 最近记录: |