Skip to content

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

Add utils to reformat currency for/from stripe

Unknown9702c3535be1
diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.py
index 85045d92ef..ba3ab482b5 100644
--- a/saleor/payment/gateways/stripe/__init__.py
+++ b/saleor/payment/gateways/stripe/__init__.py
@@ -5,7 +5,9 @@ from ... import TransactionKind
from ...utils import create_transaction
from . import errors
from .forms import StripePaymentModalForm
-from .utils import get_amount_for_stripe, get_amount_from_stripe
+from .utils import (
+ get_amount_for_stripe, get_amount_from_stripe,
+ get_currency_for_stripe, get_currency_from_stripe)
def get_client_token(**_):
@@ -171,7 +173,7 @@ def _get_client(**connection_params):
def _create_stripe_charge(client, payment, amount, payment_token, capture):
"""Create a charge with specific amount, ignoring payment's total."""
# Get currency
- currency = str(payment.currency).upper()
+ currency = get_currency_for_stripe(payment.currency)
# Get appropriate amount for stripe
stripe_amount = get_amount_for_stripe(amount, currency)
@@ -205,7 +207,8 @@ def _create_stripe_charge(client, payment, amount, payment_token, capture):
def _create_transaction(payment, amount, kind, response):
# Get currency from response or payment
- currency = response.get('currency', payment.currency)
+ currency = get_currency_from_stripe(
+ response.get('currency', payment.currency))
# Get amount from response or payment
if 'amount' in response:
diff --git a/saleor/payment/gateways/stripe/utils.py b/saleor/payment/gateways/stripe/utils.py
index ba32ad5d9e..e88706ef7e 100644
--- a/saleor/payment/gateways/stripe/utils.py
+++ b/saleor/payment/gateways/stripe/utils.py
@@ -18,3 +18,17 @@ def get_amount_from_stripe(amount, currency):
Saleor requires decimal instead of float or integer.
"""
return Decimal(amount / 100.0)
+
+
+def get_currency_for_stripe(currency):
+ """Convert Saleor's currency format to Stripe's currency format.
+ Stripe's currency is using lowercase while Saleor is using uppercase.
+ """
+ return currency.lower()
+
+
+def get_currency_from_stripe(currency):
+ """Convert Stripe's currency format to Saleor's currency format.
+ Stripe's currency is using lowercase while Saleor is using uppercase.
+ """
+ return currency.upper()