Jon
Jon

Reputation: 141

Django Python: Convert price to a Stripe amount

I am using Stripe and they request the value in a cent/pence unit rather than rounded to two decimal places. I am using the below, which works great so far, I was just worried about the cast to str() and whether this will cause any unexpected problems?

It passes to stripe ok, and saves in my DecimalField ok also.

stripeAmount = str(int(round(plan.price, 2) * 100))

Upvotes: 0

Views: 919

Answers (1)

v3nkman
v3nkman

Reputation: 1179

Since creating a PaymentIntent for example [1] receives an Integer there is no need to cast it to a String. I would encourage you to store your prices as is done in Stripe, i.e. as whole unit integers representing the smallest currency unit. For example 200 usd is represented as 20000 cents.

Storing things as float and decimals and then rounding them will often times lead to rounding errors.

[1] https://stripe.com/docs/api/payment_intents/create?lang=python

Upvotes: 3

Related Questions