javascript - Stripe 充电未通过

标签 javascript node.js stripe-payments

我正在使用 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:对于任何寻找答案的人,我找到了答案并将其发布在下面。

最佳答案

一些事情:

  1. 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/

    相关文章:

    java - 如何在网络收获中从字符串中减去子字符串

    javascript - Node 模块的可能结构

    node.js - 如何在 Node.js 中指定不同的配置文件?

    ruby-on-rails - 无法向 Stripe Webhook 返回 200

    android - Google Pay API AutoResolveHelper生命周期

    javascript - 从 map 对象中设置和删除时确保原始对象的不变性

    javascript - 显示同位素图像所需的建议加载更多功能

    node.js - "This site can’ t be reached"using Nginx as reverse proxy for Express Nodejs

    php - 如何添加银行账户并将其附加到 Stripe 上的 "connected account"?

    javascript - 如何在 Safari 调试器中调试延迟加载的 javascript