12

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: "payinguser@example.com"
    }, 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.

Community
  • 1
  • 1
Mars Robertson
  • 12,673
  • 11
  • 68
  • 89

2 Answers2

15

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.

Sunny
  • 5,825
  • 2
  • 31
  • 41
0

After npm install stripe do this

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;
        }
});
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • can this be written like this: var charge = await stripe.charges.create(chargeObject); ? (with a try catch to handle the error?) – MrLister Aug 25 '21 at 19:45