24

I have a stripe account and am using the test API keys.

I have created a Plan with following info -

{
  "amount": 995, 
  "created": 1418800758, 
  "currency": "usd", 
  "id": "c06e1791-1c6a-45fe-9c26-8f0c07dda967", 
  "interval": "month", 
  "interval_count": 1, 
  "livemode": false, 
  "metadata": {}, 
  "name": "Pro2", 
  "object": "plan", 
  "statement_description": null, 
  "statement_descriptor": null, 
  "trial_period_days": null
}

I'm using checkout.js in my project. Once all the data is filled and we click on pay for the above plan, it raises an error 'Invalid Integer 994.999999999'.

This error is not raised for the $9.94, $9.96, $29.95 and other values tried by me.

Is this a checkout bug or something to do with my settings ??

Screenshot of the error -

Stripe error message

jsfiddle reproducing the error - http://jsfiddle.net/f30z9uc6/2/

torment32
  • 243
  • 1
  • 2
  • 6
  • Where is the error happening exactly? On Stripe Checkout when creating the token or on the server when Creating a charge or a subscription? Could you share your code ? Are you trying to calculate the amount and then maybe a rounding error issue? – koopajah Jan 19 '15 at 23:23
  • The error occurs on checkout before creating the token. And the price is static.Fetched from the stripe plan. – torment32 Jan 20 '15 at 06:37
  • 1
    the amount `995` works as shown here: http://jsfiddle.net/4ykh2dh7/ there must be something else going on. Can you show a URL where this happens? You must have something parsing the price and setting `data-amount` to the wrong value – koopajah Jan 20 '15 at 12:36
  • @koopajah I was able to reproduce the error in jsfiddle - http://jsfiddle.net/f30z9uc6/2/ – torment32 Jan 21 '15 at 06:30

4 Answers4

42

The problem here is a floating point error in Javascript. If you look at this updated version of your jsfiddle you'll see what's happening and how I fixed it. You need to round the result of your calculation to ensure you end up with an integer:

var amount = Math.round(9.95*100); // gives 995

To read more about Javascript and floating point arithmetic you should look into The Floating-Point Guide

koopajah
  • 23,792
  • 9
  • 78
  • 104
  • 7
    This was just useful for me with Stripe amounts. Works like this if anybody needs it: `Math.round(TotalPrice.toFixed(2)*100)` – Matthew Bennett Jan 24 '18 at 18:32
  • 3
    Does rounding make +/- in price ammount @MatthewBennett ? – codemilan Mar 30 '18 at 10:56
  • Yes and no. I mean you might lost a tenth of a cent but that's not something that seems important. You can always round up if it matters. – koopajah Mar 30 '18 at 14:15
  • Seems to be working so far, @codemilan, at least with all of the prices I've checked it with. Stripe has recently raised a couple of decimal errors, but I think that's related to something else, not this code. – Matthew Bennett Apr 16 '18 at 16:04
  • @MatthewBennett this is a Perfect solution thank you – Mars2024 Feb 14 '22 at 21:57
4

Before you send the variable to strip you have to round to max. 2 decimals. So it will work.

Why? Because Stripe multiplies your value with 100 and the result has to be an integer - otherwise you get the error message.

mart
  • 354
  • 3
  • 14
3

If currency is USD, the value is in cents not dollars, so 2, is 2 cents, 50, is 50 cents. Apparently.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
0

i was getting the same issue in stripe account before passing the value to the backend just convert your value from object to the simple integer like this

enter code here

 const TotalItems = data
 .map(function(item) {
  return item.total;
})
.reduce(function(curval, newval) {
  return curval + newval;
});