PTHMINT-91: Implement Decimal precision for monetary calculations #41
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Copilot Description
This pull request introduces a comprehensive migration of monetary values in the codebase from floating-point types to the more precise
Decimaltype, improving accuracy in all financial calculations and API interactions. It also ensures correct JSON serialization ofDecimalvalues by introducing a customDecimalEncoder. The changes affect cart items, costs, order management, and utility functions, providing stricter type handling and validation for monetary fields.Precision improvements for monetary values:
Changed
unit_priceinCartItemandamountinCostsfromfloattoDecimal, and introduced Pydantic validators to ensure all assignments and inputs are converted toDecimalfor accuracy. Methods that add or manipulate these values have been updated to accept multiple types and convert them as needed. (src/multisafepay/api/shared/cart/cart_item.py,src/multisafepay/api/shared/costs.py) [1] [2] [3] [4] [5] [6]Updated tax rate and percentage handling in
CartItemto useDecimalfor all arithmetic, with improved validation and conversion logic. (src/multisafepay/api/shared/cart/cart_item.py) [1] [2] [3] [4] [5]JSON serialization and API compatibility:
DecimalEncoderclass for JSON serialization, convertingDecimalvalues to float when sending data to APIs. Updated all relevant API request methods inOrderManagerto use this encoder. (src/multisafepay/util/json_encoder.py,src/multisafepay/api/paths/orders/order_manager.py,src/multisafepay/util/__init__.py) [1] [2] [3] [4] [5] [6] [7]Utility and support improvements:
Decimalvalues to float for JSON serialization in utility modules. (src/multisafepay/util/total_amount.py)Minor corrections and consistency:
src/multisafepay/api/paths/orders/order_id/refund/request/components/checkout_data.py)These changes ensure financial calculations throughout the SDK are reliable and consistent, eliminating floating-point errors and improving compatibility with APIs expecting float values in JSON.- Replace float with Decimal in cart_item, costs, unit_price, total_amount