Stripe Checkout Price Error - Invalid Integer

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

I created a plan with the following information -

{ "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 am using checkout.js in my project. As soon as all the data is completed and we click on the payment of the above plan, the error "Invalid Integer 994.999999999" occurs.

This error does not occur for $ 9.94, $ 9.96, $ 29.95 and other values ​​that I used.

Is it a validation error or something to do with my settings?

Screenshot with error -

Strike error message

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

+9
source share
4 answers

The problem is a floating point error in Javascript. If you look at this updated version of jsfiddle , you will see what happens and how I fixed it. You need to round the result of your calculation to make sure that you get an integer:

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

To learn more about Javascript and floating point arithmetic, you should study the floating point guide.

+19
source

Before sending a variable to a strip, you must round to max. 2 decimal places. So it will work.

Why? Since Stripe multiplies your value by 100, and the result must be an integer - otherwise you will receive an error message.

+1
source

If the currency is the US dollar, the value is indicated in cents, not in dollars, so 2 means 2 cents, 50 - 50 cents. Apparently.

+1
source

This worked fine for me:

 Charge::create([ 'customer' => $stripe_customer->id, 'amount' => number_format($total_price), // number_format(10059.99Β’) = 10060Β’ /100 = 100.60 usd 'currency' => 'usd', ]); 
0
source

Source: https://habr.com/ru/post/981216/


All Articles