Skip to content

Conversation

@zulquer
Copy link
Collaborator

@zulquer zulquer commented Dec 3, 2025

Description

  • Add validators to convert values to Decimal for precise calculations
  • Extract DecimalEncoder to separate util/json_encoder.py module
  • Ensure all monetary values use Decimal to avoid floating-point errors
  • Update order_manager to use DecimalEncoder for JSON serialization
  • Add CartItem.update_forward_refs() to resolve Weight forward reference
  • Fix checkout_data.py to multiply Decimal by int instead of float
  • Update all tests to assert Decimal values instead of float values

Copilot Description

This pull request introduces a comprehensive migration of monetary values in the codebase from floating-point types to the more precise Decimal type, improving accuracy in all financial calculations and API interactions. It also ensures correct JSON serialization of Decimal values by introducing a custom DecimalEncoder. 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_price in CartItem and amount in Costs from float to Decimal, and introduced Pydantic validators to ensure all assignments and inputs are converted to Decimal for 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 CartItem to use Decimal for 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:

  • Introduced a new DecimalEncoder class for JSON serialization, converting Decimal values to float when sending data to APIs. Updated all relevant API request methods in OrderManager to 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:

  • Added a recursive utility function to convert nested Decimal values to float for JSON serialization in utility modules. (src/multisafepay/util/total_amount.py)

Minor corrections and consistency:

  • Standardized the sign inversion in refund calculations for unit prices to use integer multiplication instead of float. (src/multisafepay/api/paths/orders/order_id/refund/request/components/checkout_data.py)
  • Updated typing hints and imports for better maintainability and type safety throughout the affected modules. [1] [2]

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

@codecov
Copy link

codecov bot commented Dec 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.48%. Comparing base (41f42f2) to head (cbb94e1).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #41      +/-   ##
==========================================
- Coverage   90.69%   90.48%   -0.21%     
==========================================
  Files         143      144       +1     
  Lines        2471     2534      +63     
==========================================
+ Hits         2241     2293      +52     
- Misses        230      241      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Replace float with Decimal in cart_item, costs, unit_price, total_amount
- Add validators to convert values to Decimal for precise calculations
- Extract DecimalEncoder to separate util/json_encoder.py module
- Ensure all monetary values use Decimal to avoid floating-point errors
- Update order_manager to use DecimalEncoder for JSON serialization
- Add CartItem.update_forward_refs() to resolve Weight forward reference
- Fix checkout_data.py to multiply Decimal by int instead of float
- Update all tests to assert Decimal values instead of float values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants