【问题标题】:How to correctly create 'charge' in Stripe nodejs library?如何在 Stripe nodejs 库中正确创建“charge”?
【发布时间】:2024-01-12 17:24:01
【问题描述】:

客户

我正在通过以下方式使用 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);
        })
    }
  })

使用 custom 而不是 simple - 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!")
    });
});

这是错误:

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

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

【问题讨论】:

    标签: node.js stripe-payments


    【解决方案1】:

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

    因此你需要改变:

    var stripeToken = req.body.token;
    

    到:

    var stripeToken = req.body.token.id;
    

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

    【讨论】:

    • 还要注意The minimum amount is £0.50(我充的太少了)
    • 我不敢相信这在文档中是多么的不清楚。只花了我大约一个小时!
    【解决方案2】:

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

    【讨论】:

    • 可以这样写:var charge = await stripe.charges.create(chargeObject); ? (用 try catch 来处理错误?)