Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly create 'charge' in Stripe nodejs library?

Client

I'm using Stripe Checkout custom integration - https://stripe.com/docs/checkout#integration-custom - in a following way:

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

Using custom contrary to simple - How can I modify Stripe Checkout to instead send an AJAX request? - because simple does not allow me to make an AJAX call.

Server

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

You've got the token for your user's credit card details, now what? Now you charge them money.

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: "[email protected]"
    }, function(err, charge) {
        if (err && err.type === 'StripeCardError') {
            console.log(JSON.stringify(err, null, 2));
        }
        res.send("completed payment!")
    });
});

Here is the error:

enter image description here

Is seems to me like I have last4, exp_month, exp_year but for some reason I don't have number. Any suggestions / hints / ideas?

Googling for "The card object must have a value for 'number'" - 12 results, not much help.

like image 834
Mars Robertson Avatar asked Mar 10 '14 00:03

Mars Robertson


1 Answers

The "token" you have to give as the card argument should actually just be the token id (e.g.: "tok_425dVa2eZvKYlo2CLCK8DNwq"), not the full object. Using Checkout your app never sees the card number.

You therefeore need to change:

var stripeToken = req.body.token;

to:

var stripeToken = req.body.token.id;

The documentation isn't very clear about this card option, but the Stripe API Reference has an example.

like image 172
Sunny Avatar answered Nov 05 '22 11:11

Sunny