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

时间:2014-03-10 00:15:16

标签: 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);
        })
    }
  })

使用自定义简单 - How can I modify Stripe Checkout to instead send an AJAX request?相反 - 因为简单不允许我进行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

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

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

2 个答案:

答案 0 :(得分:14)

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

你需要改变:

var stripeToken = req.body.token;

为:

var stripeToken = req.body.token.id;

关于此card选项的文档不是很清楚,但Stripe API Reference有一个示例。

答案 1 :(得分: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;
        }
});