Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe Checkout Price error - Invalid Integer

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/

like image 747
torment32 Avatar asked Jan 19 '15 13:01

torment32


People also ask

What does invalid integer mean?

You are getting this error because you want to convert a string into integer. You can do this only if your string contains an integer value not the combination of character and integer value.

How do you handle stripe errors?

Errors and HTTP If an immediate problem prevents an API call from continuing, the Stripe Ruby library raises an exception. It's a best practice to catch and handle exceptions. To catch an exception, use Ruby's rescue keyword. Catch Stripe::StripeError or its subclassses to handle Stripe-specific exceptions only.

What is stripe code?

Received "Your Stripe verification code is" text message from Stripe. Payments. Checkout. You received this text message because you saved your phone number for future purchases using Link. Link lets you securely save and reuse your payment information for a faster checkout at tens of thousands of online businesses.


3 Answers

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

like image 88
koopajah Avatar answered Nov 04 '22 10:11

koopajah


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.

like image 38
mart Avatar answered Nov 04 '22 08:11

mart


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

like image 20
Alexander Mills Avatar answered Nov 04 '22 09:11

Alexander Mills