Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add fractional amount to the Money type (#18007)
Patryk Zawadzki697cbdc85b4a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3a0f947f3d..976a424ce5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -91,6 +91,7 @@ Like `reference`, the `single-reference` type can target entities defined in the- Extended support for filtering `products` by associated attributes- Attribute slug is now optional when filtering by attribute values- Added support for filtering by associated reference objects (e.g., `products`, `pages`, `variants`)+- Added `fractionalAmount` and `fractionDigits` fields to the `Money` type. These fields allow monetary values to be represented as a pair of integers, which is often required when integrating with payment service providers.### Webhooks- Transaction webhooks responsible for processing payments can now return payment method details`, which will be associated with the corresponding transaction. See [docs](https://docs.saleor.io/developer/extending/webhooks/synchronous-events/transaction#response-4) to learn more.diff --git a/saleor/graphql/core/types/money.py b/saleor/graphql/core/types/money.pyindex bb4e6a4536..0e8c0e8f78 100644--- a/saleor/graphql/core/types/money.py+++ b/saleor/graphql/core/types/money.py@@ -1,4 +1,5 @@import graphene+from babel.numbers import get_currency_precisionfrom ....core.prices import quantize_pricefrom ...core.doc_category import DOC_CATEGORY_TAXES@@ -8,6 +9,14 @@ from ...core.types import BaseObjectTypeclass Money(graphene.ObjectType):currency = graphene.String(description="Currency code.", required=True)amount = graphene.Float(description="Amount of money.", required=True)+ fractional_amount = graphene.Int(+ description="Amount of money represented as an integer in the smallest currency unit.",+ required=True,+ )+ fraction_digits = graphene.Int(+ description="Number of digits after the decimal point in the currency.",+ required=True,+ )class Meta:description = "Represents amount of money in specific currency."@@ -16,6 +25,15 @@ class Money(graphene.ObjectType):def resolve_amount(root, _info):return quantize_price(root.amount, root.currency)+ @staticmethod+ def resolve_fractional_amount(root, _info):+ precision = get_currency_precision(root.currency)+ return int(quantize_price(root.amount, root.currency) * (10**precision))++ @staticmethod+ def resolve_fraction_digits(root, _info):+ return get_currency_precision(root.currency)+class MoneyRange(graphene.ObjectType):start = graphene.Field(Money, description="Lower bound of a price range.")diff --git a/saleor/graphql/core/types/tests/__init__.py b/saleor/graphql/core/types/tests/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/graphql/core/types/tests/test_money.py b/saleor/graphql/core/types/tests/test_money.pynew file mode 100644index 0000000000..e212da2d67--- /dev/null+++ b/saleor/graphql/core/types/tests/test_money.py@@ -0,0 +1,23 @@+from decimal import Decimal++from prices import Money++from ..money import Money as MoneyObject+++def test_money_object_usd():+ money = Money(Decimal("12.950000"), "USD")+ resolve_info = None++ assert MoneyObject.resolve_amount(money, resolve_info) == Decimal("12.95")+ assert MoneyObject.resolve_fractional_amount(money, resolve_info) == 1295+ assert MoneyObject.resolve_fraction_digits(money, resolve_info) == 2+++def test_money_object_jpy():+ money = Money(Decimal(1234), "JPY")+ resolve_info = None++ assert MoneyObject.resolve_amount(money, resolve_info) == Decimal(1234)+ assert MoneyObject.resolve_fractional_amount(money, resolve_info) == 1234+ assert MoneyObject.resolve_fraction_digits(money, resolve_info) == 0diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 456ca5969b..a0ffc26c90 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3781,6 +3781,14 @@ type Money {"""Amount of money."""amount: Float!++ """+ Amount of money represented as an integer in the smallest currency unit.+ """+ fractionalAmount: Int!++ """Number of digits after the decimal point in the currency."""+ fractionDigits: Int!}"""