首页 文章

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

提问于
浏览
11

客户

我正在使用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);
        })
    }
  })

使用 customsimple 相反 - How can I modify Stripe Checkout to instead send an AJAX request? - 因为 simple 不允许我进行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!")
    });
});

这是错误:

enter image description here

在我看来,我有 last4exp_monthexp_year 但由于某种原因,我没有 number . 有什么建议/提示/想法吗?

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

2 回答

  • 14

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

    你需要改变:

    var stripeToken = req.body.token;
    

    至:

    var stripeToken = req.body.token.id;
    

    关于这个 card 选项的文档不是很清楚,但Stripe API Reference有一个例子 .

  • 0

    npm install stripe 之后这样做

    var stripe = require("stripe")("sk_yourstripeserversecretkey");
    var chargeObject = {};
    chargeObject.amount = grandTotal * 100;
    chargeObject.currency = "usd";
    chargeObject.source = token-from-client;
    chargeObject.description = "Charge for joe@blow.com";
    
    stripe.charges.create(chargeObject)
    .then((charge) => {
        // New charge created. record charge object
    }).catch((err) => {
        // charge failed. Alert user that charge failed somehow
    
            switch (err.type) {
              case 'StripeCardError':
                // A declined card error
                err.message; // => e.g. "Your card's expiration year is invalid."
                break;
              case 'StripeInvalidRequestError':
                // Invalid parameters were supplied to Stripe's API
                break;
              case 'StripeAPIError':
                // An error occurred internally with Stripe's API
                break;
              case 'StripeConnectionError':
                // Some kind of error occurred during the HTTPS communication
                break;
              case 'StripeAuthenticationError':
                // You probably used an incorrect API key
                break;
              case 'StripeRateLimitError':
                // Too many requests hit the API too quickly
                break;
            }
    });
    

相关问题