user462794 user462794 - 2 months ago 13
Node.js Question

Stripe dividing by 100 in nodejs

So here is my code :

PaymentDaemon.authorize = function(req, callback) {
console.log("on est dans authorize");
console.log(req.amount);
stripe.charges.create({
amount: 3,//req.amount,
currency: req.currency,
source: req.card,
description: req.details,
capture: false
}, function(err, charge) {
if (err != null)
console.error("ERROR in PaymentDaemon.authorize: " + err);
else {
req.paidDate = null;
req.way = cfg.payment.way.in;
req.stripe = charge.id;
var search = {};
if (req.payment != null)
search._id = req.payment;
Payment.update(search, req, { upsert: true }, function(error) {
if (error != null) {
console.error("ERROR in PaymentDaemon.authorize: " + err);
console.error("ERROR Data not inserted in DB: " + JSON.stringify(req));
}
});
}
callback(err, charge);
});
}


I ve put 3 because this function is called elsewhere and i am trying to find where the error come from.

here is the callling code :

var req = {
amount: amount,
currency: currency,
card: idcard,
details: null, // fixme
};
console.log("on est dans charge");
PaymentDaemon.authorize(req, function(error, charge) {
if (error != null)
{
console.error('ERROR PaymentDaemon.createPayment() (4): ' + error);
}
else
{
console.log(charge);
res.status(200).send(charge);
}
});


and when i call it, i have this error :

ERROR in PaymentDaemon.authorize: Error: Amount must convert to at least 50 cents. €0.03 converts to approximately $0.03.
ERROR PaymentDaemon.createPayment() (4): Error: Amount must convert to at least 50 cents. €0.03 converts to approximately $0.03.


So stripe seems to divide my amount by 100, any idea why ?

Thanks and regards.

Answer

Stripe expects amount to be set in smallest possible units. By providing 3 as value and USD as currency, you are basically charging $0.03.

Per their API reference:

amount (positive integer or zero)

A positive integer in the smallest currency unit (e.g., 100 cents to charge $1.00 or 100 to charge ¥100, a 0-decimal currency) representing how much to charge. The minimum amount is $0.50 US or equivalent in charge currency.

Comments