【发布时间】:2019-04-11 13:25:46
【问题描述】:
我收到 Stripe 的 502(错误网关)错误。付款成功地从卡中扣款并显示在 Stripe 仪表板中,但它没有在前端显示成功,而是出现 502 错误。
我是否想在下面添加一些内容以使前端显示付款成功?在此处使用文档: https://stripe.com/docs/stripe-js/elements/payment-request-button
// Send the token to your server to charge it!
fetch('/apple-pay', {
method: 'POST',
body: JSON.stringify({token: ev.token.id}),
headers: {'content-type': 'application/json'},
})
.then(function(response) {
console.log(response)
if (response.ok) {
// Report to the browser that the payment was successful, prompting
// it to close the browser payment interface.
ev.complete('success');
} else {
// Report to the browser that the payment failed, prompting it to
// re-show the payment interface, or show an error message and close
// the payment interface.
ev.complete('fail');
}
});
});
服务器端代码
app.post('/apple-pay', function(req, res, next) {
// Set your secret key: remember to change this to your live secret key in production
var stripe = require("stripe")("sk_test_xxx");
console.log('we got here....')
// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
const token = req.body.token;
console.log(req.body)
// Using Express
console.log('this is the Token...' + token)
const charge = stripe.charges.create({
amount: 499,
currency: 'usd',
description: 'Example charge',
source: token,
}, function(err, charge) {
// asynchronously called
console.log(err)
});
});
【问题讨论】:
-
问题可能来自代码服务器端,您还没有分享。您需要在代码中添加日志以准确跟踪您返回的值
-
@koopajah 添加
-
当然可以,但是您之前有另一个问题,您说收费不起作用。您是否将日志添加到您阅读并能够调试的代码中?例如,现在您的代码异步创建了一个费用,并且从不返回任何内容,没有响应,没有 200 等。您需要正确处理请求
-
@koopajah 是的!我设法解决了这个问题。现在付款成功完成。我可以在 Stripe 仪表板中看到它。但是在前端看起来它没有完成......
-
太棒了!我在上面解释了要寻找的内容。您需要确保您的代码等待费用创建完成,然后才返回一个值。您可能想了解 Node.js、异步流、如何处理服务器上的请求等。
标签: ios node.js express stripe-payments payment