我正在使用 Stripe API 来测试收费,但收费未通过。我对信用卡进行标记,然后使用该标记作为“源”参数。我在 Python 中做了完全相同的事情,并且运行良好,但是当我尝试在 Node.js 中使用它时,它不起作用。
这是我的代码:
var stripe = require("stripe")(<key>);
var pay = stripe.tokens.create({
card: {
number: '<number>', // note, for now I'm just using the stripe test credentials
exp_month: <exp>,
exp_year: <year>,
cvc: '<cvc>'
}
}, function(err, token) {
console.log(err); // null
console.log(token); // token information (works fine)
return token;
});
stripe.charges.create({
amount: 500,
currency: "usd",
source: pay,
description: "Test charge"
}, function(err, charge) {
console.log(err); // error message 400, missing parameter
console.log(charge); // still null
});
我做错了什么?正如我所说,当我使用 Python 执行此操作时,效果很好。
更新:我已尝试了下面发布的所有解决方案,但它们仍然返回错误 400 代码。如果有人有其他解决方案,我们将不胜感激。
_____________________________________________________________________________ 更新 2:对于任何寻找答案的人,我找到了答案并将其发布在下面。
最佳答案
一些事情:
stripe.tokens.create
返回一个 Promise,而不是一个值。您必须将token
分配给正确范围内的某些内容才能在其他地方使用它,或者只是将其传递给使用它的函数:
stripe.tokens.create({
card: {
number: '<number>', // note, for now I'm just using the stripe test credentials
exp_month: <exp>,
exp_year: <year>,
cvc: '<cvc>'
}
}, function(err, token) {
console.log(err); // null
console.log(token); // token information (works fine)
// call a function which in turn calls stripe.charges.create
makeCharge(token);
});
- 您不应在后端生成 token ,这将使您遵守最广泛的 PCI 合规性 https://stripe.com/docs/security#validating-pci-compliance 。相反,请在前端使用 Stripe Elements 或旧版 Checkout 来标记卡片。
- JavaScript 是异步的而不是同步的,请考虑使用 async/await如果您想要同步操作。
关于javascript - Stripe 充电未通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55719539/