Skip to content

Reject non-integer amounts instead of truncating them - #62

Open
loevgaard wants to merge 1 commit into
2.xfrom
fix/amounts-integer-only
Open

Reject non-integer amounts instead of truncating them#62
loevgaard wants to merge 1 commit into
2.xfrom
fix/amounts-integer-only

Conversation

@loevgaard

Copy link
Copy Markdown
Member

Fixes robustness finding R6 of #57.

Problem

Amounts::forOperation() accepted anything is_numeric() and cast it with (int). A fractional value was silently truncated: 249.99 became a capture/refund of 249. Since every amount in this gateway is minor units, a fractional value is always a caller bug — it can only mean kroner were passed where øre were expected — and truncation turns that bug into an undercharge instead of an error.

Fix

Accept integers and integer strings (the shape a serialization round trip in the consumer's storage may produce); everything else — floats, decimal strings, booleans — throws the same LogicException the other unusable shapes get, now with the offending value in the message. The positive-amount rule and the capture_amount/refund_amount override semantics are unchanged.

Tests

Data-provider cases for 249.99, 250.0, '249.99' and true; the existing integer-string acceptance is already pinned by shouldAcceptNumericStrings.

Amounts::forOperation() accepted anything is_numeric() and cast it with
(int), so a fractional value was silently truncated: 249.99 — which, in
minor units, can only mean someone passed kroner where øre were expected
— became a charge of 249 rather than an error.

Only integers and integer strings (the shape a serialization round trip
may produce) are accepted now; anything fractional throws the same
LogicException the other unusable shapes get, with the offending value
in the message.
@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.44%. Comparing base (6122f5f) to head (af2e79e).

Additional details and impacted files
@@            Coverage Diff            @@
##                2.x      #62   +/-   ##
=========================================
  Coverage     97.43%   97.44%           
- Complexity      126      129    +3     
=========================================
  Files            14       14           
  Lines           351      352    +1     
=========================================
+ Hits            342      343    +1     
  Misses            9        9           

☔ View full report in Codecov by Harness.
📢 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.

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.

1 participant