条纹充电未通过

时间:2019-04-17 03:42:22

标签: 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:对于想找答案的任何人,我都想出了答案并将其发布在下面。

4 个答案:

答案 0 :(得分:1)

create方法异步运行。如果要使用刚创建的令牌,则需要在回调中使用。为了更好地理解这一点,请检查以下代码:

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;
});

console.log(pay); // This will not be what you expected.

pay不会是token的值,因为请求尚未完成。

相反,您需要在令牌的回调内部创建费用。 Web令牌创建请求完成后,将执行此回调函数。

var stripe = require("stripe")(<key>);

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)
  stripe.charges.create({
    amount: 500,
    currency: "usd",
    source: token, // note I changed this to token 
    description: "Test charge"
  }, function(err, charge) {
    console.log(err); // hopefully null now 
    console.log(charge); // hopefully has charge info now
    // add code to display a confirmation message or whatever you want to do with the successful charge here
  });
});

免责声明:我对Stripe代码不熟悉。我只是了解异步JS的工作原理。每次创建令牌可能不是最佳实践。

答案 1 :(得分:1)

几件事:

  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);
});
  1. 您不应该在后端生成令牌,这会使您受到PCI合规性https://stripe.com/docs/security#validating-pci-compliance的最大影响。而是在前端使用Stripe Elements或旧版Checkout来对卡进行令牌化。
  2. JavaScript是异步的而不是同步的,如果需要同步操作,请考虑使用async/await

答案 2 :(得分:1)

您可以想到,仅使用用户创建的条带客户ID中的默认来源即可。

return stripe.customers.retrieve(stripeCustomerId)
                .then((customer) => customer.default_source)

您将获得客户的默认来源(如果有),然后使用该来源创建费用。

const strpChrg = {
            customer: stripeCustomerId,
            amount: _.round(amount * 100), // convert to cents
            currency: _.toLower(currency), // usd
            source: sourceId
 };
 // create stripe charge using customer token
 return stripe.charges.create(strpChrg).then((charge) => {
            // add charge to data
            return charge;
 });

您不应在后端创建令牌,而应使用客户添加的条带默认源。

答案 3 :(得分:0)

对于任何想要以这种方式创建收费的人,这就是我使用Node.js弄清楚的方式(对于像我这样来自python的人,这将非常有帮助):

stripe.tokens.create({
card: {
    number: '<card number>',
    exp_month: <exp>,
    exp_year: <year>,
    cvc: '<cvc>'
}
}, function(err, token) {
   console.log(token.id); // you can log the entire token, or you can just check for the piece you need
   stripe.charges.create({
      amount: 800,
      currency: "usd",
      source: token.id,  // instead of in python where you can use a variable, in node you have to use specifically the id
      description: "Test charge"
  }, function(err, charge) {
      console.log(err);  // if it does not work, make sure to print the error
      console.log(charge) // print the charge to make sure it went through
   });
});

希望这会对沿途的人有所帮助!

注意:正如其他人提到的那样,对于生产而言,这可能不是您想要的,该解决方案将更多地是为了了解Stripe API或测试快速收费。