Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix bug where amount for stripe is not correctly converted

Unknowndbcc7604b7ab
diff --git a/saleor/payment/gateways/stripe/utils.py b/saleor/payment/gateways/stripe/utils.py
index e88706ef7e..53915382dc 100644
--- a/saleor/payment/gateways/stripe/utils.py
+++ b/saleor/payment/gateways/stripe/utils.py
@@ -9,7 +9,9 @@ def get_amount_for_stripe(amount, currency):
Stripe is using currency's smallest unit such as cents for USD.
Stripe requires integer instead of decimal.
"""
- return int(amount * 100)
+ # Using int(Decimal) directly may yield wrong result
+ # such as int(Decimal(24.24)) will equal to 2423
+ return int((amount * 100).to_integral_value())
def get_amount_from_stripe(amount, currency):
@@ -17,7 +19,9 @@ def get_amount_from_stripe(amount, currency):
Stripe is using currency's smallest unit such as cents for USD.
Saleor requires decimal instead of float or integer.
"""
- return Decimal(amount / 100.0)
+ # Using Decimal(amount / 100.0) will convert to decimal from float
+ # which precision may be lost
+ return Decimal(amount) / Decimal(100)
def get_currency_for_stripe(currency):