如何在Stripe nodejs库中正确创建'charge'?

Mar*_*son 11 node.js stripe-payments

客户

我正在使用Stripe Checkout自定义集成 - https://stripe.com/docs/checkout#integration-custom-以下列方式:

  var handler = StripeCheckout.configure({
    key: 'YOUR_KEY_HERE',
    image: 'images/logo-48px.png',
    token: function(token, args) {
        $.post("http://localhost:3000/charge", {token: token}, function(res) {
            console.log("response from charge: " + res);
        })
    }
  })
Run Code Online (Sandbox Code Playgroud)

使用自定义简单相反- 如何修改Stripe Checkout而不是发送AJAX请求?- 因为简单不允许我进行AJAX调用.

服务器

https://stripe.com/docs/tutorials/charges

您已获得用户信用卡详细信息的令牌,现在是什么?现在你向他们收钱.

app.post('/charge', function(req, res) {
    console.log(JSON.stringify(req.body, null, 2));
    var stripeToken = req.body.token;

    var charge = stripe.charges.create({
        amount: 0005, // amount in cents, again
        currency: "usd",
        card: stripeToken,
        description: "payinguser@example.com"
    }, function(err, charge) {
        if (err && err.type === 'StripeCardError') {
            console.log(JSON.stringify(err, null, 2));
        }
        res.send("completed payment!")
    });
});
Run Code Online (Sandbox Code Playgroud)

这是错误:

在此输入图像描述

在我看来,我有last4,exp_month,exp_year,但由于某种原因,我没有数字.有什么建议/提示/想法吗?

谷歌搜索"The card object must have a value for 'number'"- 12个结果,没有多大帮助.

Sun*_*nny 14

您必须提供的"令牌" card实际上应该只是令牌ID(例如:"tok_425dVa2eZvKYlo2CLCK8DNwq"),而不是完整对象.使用Checkout,您的应用永远不会看到卡号.

你需要改变:

var stripeToken = req.body.token;
Run Code Online (Sandbox Code Playgroud)

至:

var stripeToken = req.body.token.id;
Run Code Online (Sandbox Code Playgroud)

文档对此card选项不是很清楚,但Stripe API Reference有一个例子.

  • 还应该注意到```最低金额是£0.50```(我收费太少) (3认同)