cbpr-validate 1.2.0__tar.gz → 1.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/.gitignore +2 -0
  2. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/ARCHITECTURE.md +4 -1
  3. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/BUILD_PLAN.md +8 -3
  4. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/CHANGELOG.md +54 -0
  5. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/CLAUDE.md +5 -0
  6. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/KNOWN_ISSUES.md +32 -0
  7. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/PKG-INFO +28 -3
  8. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/README.md +27 -2
  9. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/__init__.py +1 -1
  10. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/match/matcher.py +46 -28
  11. cbpr_validate-1.3.0/src/cbpr_validate/rules/_return_amounts.py +140 -0
  12. cbpr_validate-1.3.0/src/cbpr_validate/rules/returns.py +140 -0
  13. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_cli.py +4 -5
  14. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_interface_parity.py +61 -0
  15. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_match_correlation.py +15 -5
  16. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_report.py +7 -0
  17. cbpr_validate-1.3.0/tests/test_return_amounts.py +252 -0
  18. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_rules_returns.py +146 -111
  19. cbpr_validate-1.2.0/.claude/settings.local.json +0 -17
  20. cbpr_validate-1.2.0/src/cbpr_validate/rules/returns.py +0 -222
  21. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/.claude/settings.local.json.tmp.31316.7bec411697e7 +0 -0
  22. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/.github/workflows/ci.yml +0 -0
  23. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/.github/workflows/release.yml +0 -0
  24. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/add_currencies.py +0 -0
  25. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/iso4217_currencies.json +0 -0
  26. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/pyproject.toml +0 -0
  27. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/api/__init__.py +0 -0
  28. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/api/main.py +0 -0
  29. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/cli.py +0 -0
  30. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/codesets/__init__.py +0 -0
  31. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/codesets/data/iso20022_codesets.json +0 -0
  32. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/codesets/loader.py +0 -0
  33. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/config.py +0 -0
  34. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/core.py +0 -0
  35. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/match/__init__.py +0 -0
  36. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/match/result.py +0 -0
  37. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/model/__init__.py +0 -0
  38. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/model/finding.py +0 -0
  39. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/model/payment.py +0 -0
  40. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/model/validation_result.py +0 -0
  41. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/parsers/__init__.py +0 -0
  42. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/parsers/_common.py +0 -0
  43. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/parsers/detect.py +0 -0
  44. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/parsers/pacs002.py +0 -0
  45. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/parsers/pacs004.py +0 -0
  46. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/parsers/pacs008.py +0 -0
  47. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/parsers/pacs009.py +0 -0
  48. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/parsers/parse.py +0 -0
  49. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/profiles.py +0 -0
  50. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/report/__init__.py +0 -0
  51. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/report/json_report.py +0 -0
  52. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/report/junit_report.py +0 -0
  53. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/report/text_report.py +0 -0
  54. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/rules/__init__.py +0 -0
  55. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/rules/address.py +0 -0
  56. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/rules/agents.py +0 -0
  57. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/rules/amounts.py +0 -0
  58. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/rules/codes.py +0 -0
  59. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/rules/registry.py +0 -0
  60. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/rules/structural.py +0 -0
  61. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/schema/__init__.py +0 -0
  62. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/src/cbpr_validate/schema/xsd.py +0 -0
  63. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/__init__.py +0 -0
  64. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/conftest.py +0 -0
  65. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/fixtures/pacs008_valid_hybrid_ro.xml +0 -0
  66. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_addr004_country_codes.py +0 -0
  67. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_api.py +0 -0
  68. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_bicfi_agents.py +0 -0
  69. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_cod004_pacs002.py +0 -0
  70. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_core.py +0 -0
  71. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_golden_valid.py +0 -0
  72. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_misc_coverage.py +0 -0
  73. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_parsers.py +0 -0
  74. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_parsers_edgecases.py +0 -0
  75. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_parsers_phase4.py +0 -0
  76. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_phase3_rules.py +0 -0
  77. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_profiles.py +0 -0
  78. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_rules_address.py +0 -0
  79. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_rules_agents.py +0 -0
  80. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_schema_xsd.py +0 -0
  81. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_smoke.py +0 -0
  82. {cbpr_validate-1.2.0 → cbpr_validate-1.3.0}/tests/test_str003_mandatory_amount.py +0 -0
@@ -15,3 +15,5 @@ htmlcov/
15
15
 
16
16
  # Local learning notes
17
17
  learning_guide/
18
+
19
+ .claude/settings.local.json
@@ -110,7 +110,10 @@ disagree with another about what a profile means. Applying a profile is idempote
110
110
  - `correlate(a, b, direction=None)` infers the scenario from the message-type pair (either argument order):
111
111
  - `CBPR-COR-001` pacs.009 COV ↔ originating pacs.008
112
112
  - `CBPR-COR-002` pacs.002 ↔ pacs.008 (`direction` **required** — identity is never inferred from BICs)
113
- - `CBPR-COR-003` pacs.004 return ↔ original pacs.008
113
+ - `CBPR-COR-003` pacs.004 return ↔ original pacs.008. Amount checks carry their own sub-ids (v1.3.0):
114
+ the echo `OrgnlIntrBkSttlmAmt` must equal the original (`-ECHO-MISMATCH` ERROR, `-ECHO-ABSENT` INFO); the
115
+ returned amount is graded against the actual original by `rules/_return_amounts.py`, shared with
116
+ `CBPR-RTN-003` (`-RETURNED-LOWER` INFO, `-RETURNED-HIGHER` WARN, `-FX` INFO).
114
117
  - Primary match key is always **UETR**; `OrgnlTxId` / `OrgnlEndToEndId` are fallbacks only when a UETR is
115
118
  absent, and the result flags that fallback. Two UETRs that *disagree* are a real non-match, never a reason
116
119
  to try a weaker key.
@@ -176,8 +176,8 @@ Implement each as a registered rule with a stable ID. Group + severity below. **
176
176
  **Returns (`returns.py`)** — pacs.004 usage rules
177
177
  - `CBPR-RTN-001` Return reason code ∈ ExternalReturnReason1Code — ERROR
178
178
  - `CBPR-RTN-002` Mandatory original-reference fields present (OrgnlMsgId, OrgnlEndToEndId, OrgnlTxId, OrgnlUETR, OrgnlInterbankSettlementAmt, OrgnlInterbankSettlementDt) — ERROR
179
- - `CBPR-RTN-003` Returned interbank settlement amount ≤ original interbank settlement amount, net of disclosed charges *(verify exact CBPR+ tolerance/charge-deduction rule)* — ERROR/WARN
180
- - `CBPR-RTN-004` Charges-deducted breakdown (`ChrgsInf`), if present, reconciles with original vs. returned amount — WARN
179
+ - `CBPR-RTN-003-*` Returned interbank settlement amount vs the original amount the return quotes *(SME rules, v1.3.0)*: `-RETURNED-LOWER` charges deducted, states whether `ChrgsInf` reconciles — INFO; `-RETURNED-HIGHER` same-currency over-return — WARN; `-FX` different currencies — INFO
180
+ - ~~`CBPR-RTN-004`~~ Retired in v1.3.0 (reconciliation is stated inside `CBPR-RTN-003-RETURNED-LOWER`); id reserved
181
181
  - `CBPR-RTN-005` Compensation/interest fields, if present, well-formed — INFO
182
182
 
183
183
  ---
@@ -190,7 +190,7 @@ Primary match key is always **UETR** (CBPR+ mandates it persist end-to-end acros
190
190
 
191
191
  - `CBPR-COR-001` **pacs.009 COV ↔ originating pacs.008** — match `UndrlygCstmrCdtTrf.PmtId.UETR` (in the COV's embedded underlying transaction) against the originating `pacs.008`'s `PmtId.UETR`. On match, cross-check settlement amount/currency and Dbtr/Cdtr consistency between the two.
192
192
  - `CBPR-COR-002` **pacs.002 ↔ pacs.008 (bidirectional)** — match `TxInfAndSts.OrgnlUETR` (falling back to `OrgnlTxId`/`OrgnlEndToEndId`) against the `pacs.008`'s `PmtId`. Direction (was the `pacs.008` sent by you or received by you) is passed explicitly via a `direction: outbound|inbound` parameter — the tool does not infer identity from BICs; see CLI below.
193
- - `CBPR-COR-003` **pacs.004 return ↔ original pacs.008** — match `OrgnlGrpInf`/`TxInf.OrgnlUETR` against the original `pacs.008`'s `PmtId.UETR`. On match, cross-check returned amount against original (feeds `CBPR-RTN-003`).
193
+ - `CBPR-COR-003` **pacs.004 return ↔ original pacs.008** — match `OrgnlGrpInf`/`TxInf.OrgnlUETR` against the original `pacs.008`'s `PmtId.UETR`. On match, check the echo `OrgnlIntrBkSttlmAmt` equals the original exactly (`-ECHO-MISMATCH` ERROR, `-ECHO-ABSENT` INFO) and grade the returned amount against the actual original (`-RETURNED-LOWER` INFO, `-RETURNED-HIGHER` WARN, `-FX` INFO) - see v1.3.0 below.
194
194
 
195
195
  **Interface shape (Phase 5):**
196
196
  ```
@@ -464,6 +464,11 @@ Dockerfile, mkdocs site, README polish, badges, benchmark, TestPyPI → PyPI, ta
464
464
  `profile=` to the library, a `profile` field to `POST /validate`, `Finding.base_severity`, and the profile in
465
465
  all three reporters. `--fail-on` now accepts `warn` and `warning` on both commands.
466
466
  Deferred work is logged in `KNOWN_ISSUES.md`.
467
+ - **v1.3.0** — **Return amount rules** (SME-specified). A lower returned amount is the normal
468
+ charges-deducted return, not a mismatch; the original-amount echo is now checked. `CBPR-COR-003` amount checks
469
+ split into `-ECHO-MISMATCH` (ERROR), `-ECHO-ABSENT`, `-RETURNED-LOWER`, `-FX` (INFO) and `-RETURNED-HIGHER`
470
+ (WARN); `CBPR-RTN-003` aligned to the same grading via the shared `rules/_return_amounts.py`; `CBPR-RTN-004`
471
+ retired. Behaviour change - see `CHANGELOG.md`.
467
472
 
468
473
  ---
469
474
 
@@ -3,6 +3,60 @@
3
3
  All notable changes to this project are documented here. This project adheres
4
4
  to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5
5
 
6
+ ## [1.3.0] - Unreleased
7
+
8
+ ### Changed - BEHAVIOUR CHANGE, please read
9
+
10
+ **A lower returned amount is no longer a mismatch; the original-amount echo is
11
+ now checked.** Banks normally deduct their charges from a returned payment, so
12
+ a pacs.004 that returns less than the original is the normal case, not drift.
13
+ What must match exactly is the return's echo of the original
14
+ (`OrgnlIntrBkSttlmAmt`).
15
+
16
+ Correlation (`pacs.004 <-> pacs.008`, `match` / `/correlate` / `correlate()`):
17
+ the amount checks are split out of `CBPR-COR-003` into stable sub-ids.
18
+
19
+ | Case | Rule id | Before 1.3.0 | From 1.3.0 |
20
+ | --- | --- | --- | --- |
21
+ | Echo differs from the original amount or currency | `CBPR-COR-003-ECHO-MISMATCH` | ERROR (as `CBPR-COR-003`) | ERROR |
22
+ | Echo absent | `CBPR-COR-003-ECHO-ABSENT` | nothing | INFO |
23
+ | Same currency, returned lower | `CBPR-COR-003-RETURNED-LOWER` | nothing | INFO, says whether ChrgsInf reconciles |
24
+ | Same currency, returned higher | `CBPR-COR-003-RETURNED-HIGHER` | ERROR (as `CBPR-COR-003`) | **WARN** |
25
+ | Returned currency differs (FX return) | `CBPR-COR-003-FX` | nothing | INFO |
26
+
27
+ `is_consistent` is still false only when there is an ERROR. **A same-currency
28
+ over-return no longer makes a pair inconsistent, and `cbpr-validate match` exits
29
+ `0` for it instead of `1`.** A broken link and mis-echoed references are still
30
+ reported as `CBPR-COR-003`.
31
+
32
+ Single-message validation of a pacs.004 (`validate` / `check` / `/validate`):
33
+ `CBPR-RTN-003` now grades the returned amount the same way, against the
34
+ original amount the return quotes.
35
+
36
+ | Case | Before 1.3.0 | From 1.3.0 |
37
+ | --- | --- | --- |
38
+ | Returned currency differs from the quoted original | `CBPR-RTN-003` ERROR | `CBPR-RTN-003-FX` INFO |
39
+ | Same currency, returned higher | `CBPR-RTN-003` ERROR | `CBPR-RTN-003-RETURNED-HIGHER` WARN |
40
+ | Same currency, returned lower, not explained by ChrgsInf | `CBPR-RTN-003` WARN | `CBPR-RTN-003-RETURNED-LOWER` INFO |
41
+ | Same currency, returned lower, explained by ChrgsInf | nothing | `CBPR-RTN-003-RETURNED-LOWER` INFO |
42
+
43
+ **An FX or over-returned pacs.004 that was `NOT COMPLIANT` in 1.2.0 may now be
44
+ `COMPLIANT`, and a CLI call that exited `1` may now exit `0`.** A run with
45
+ `--fail-on warning` still fails on an over-return.
46
+
47
+ ### Removed
48
+
49
+ - `CBPR-RTN-004` (ChrgsInf does not reconcile, WARN) is retired and its id is
50
+ reserved. Whether returned + charges reconciles is now stated inside the
51
+ `RETURNED-LOWER` INFO finding, with the difference when it does not.
52
+
53
+ ### Notes
54
+
55
+ - Only charges in the returned currency are added up when reconciling;
56
+ charges in another currency are counted and mentioned, not converted.
57
+ - All amount arithmetic is `Decimal`.
58
+ - COV (`CBPR-COR-001`) and pacs.002 (`CBPR-COR-002`) correlation are unchanged.
59
+
6
60
  ## [1.2.0] - 2026-10-12
7
61
 
8
62
  ### Changed — BEHAVIOUR CHANGE, please read
@@ -66,6 +66,11 @@ TestPyPI -> PyPI, tag v1.0.0).
66
66
  - The three interfaces share `parsers/parse.py`, `rules/registry.run_all` and
67
67
  `report/json_report`. Never reimplement parsing or formatting inside `cli.py`
68
68
  or `api/main.py` — `tests/test_interface_parity.py` will fail if you do.
69
+ - **Return amount rules** (v1.3.0) are SME decisions: a lower returned amount is the
70
+ normal charges-deducted return (INFO, never a mismatch); only the echo
71
+ `OrgnlIntrBkSttlmAmt` must match the original exactly (ERROR). Over-return in
72
+ the same currency is WARN, a currency difference is an FX return (INFO).
73
+ `CBPR-RTN-004` is retired, its id reserved. Do not re-tighten these.
69
74
  - JUnit maps **only** ERROR to `<failure>`. WARN/INFO are passing cases, so a
70
75
  build fails exactly when the message is non-compliant.
71
76
 
@@ -92,3 +92,35 @@ they come from the ISO 20022 schema itself (`Max70Text`, `maxOccurs=7`).
92
92
 
93
93
  **What it would take.** Confirmation of the cap from the usage guidelines, then
94
94
  a registered rule plus a passing and an adversarial fixture.
95
+
96
+ ---
97
+
98
+ ## 5. Return charges in another currency are not converted
99
+
100
+ **What.** When `CBPR-COR-003-RETURNED-LOWER` / `CBPR-RTN-003-RETURNED-LOWER`
101
+ reconcile returned + ChrgsInf against the original, only charges in the
102
+ returned currency are added up. A charge in another currency is counted and
103
+ mentioned in the message ("1 charge(s) in other currencies not included"), not
104
+ converted.
105
+
106
+ **Why deferred.** Converting would need an FX rate the message does not carry,
107
+ and the reconciliation is INFO either way (v1.3.0 return amount rules), so no
108
+ verdict depends on it.
109
+
110
+ **What it would take.** A rate source (or the rate from the message's own
111
+ `XchgRate`, where present) and a decision on rounding tolerance - which falls
112
+ under CLAUDE.md operating rule 3 and needs SME confirmation first.
113
+
114
+ ---
115
+
116
+ ## 6. `ExternalReturnReason1Code` snapshot is incomplete
117
+
118
+ **What.** `codesets/data/iso20022_codesets.json` holds only `CURR`, `DNCL` and
119
+ `CANC` for `ExternalReturnReason1Code`. Common real codes such as `AC01`,
120
+ `AC04`, `AM05` and `MD06` therefore fail `CBPR-RTN-001` (ERROR).
121
+
122
+ **Why deferred.** Found while building synthetic returns for the v1.3.0 amount
123
+ rules; refreshing a code-set snapshot is a separate, data-only change.
124
+
125
+ **What it would take.** Regenerate the snapshot from the published ISO 20022
126
+ External Code Sets and add passing fixtures for a few common return codes.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: cbpr-validate
3
- Version: 1.2.0
3
+ Version: 1.3.0
4
4
  Summary: Validate ISO 20022 CBPR+ payment messages against the usage guidelines, not just the XSD.
5
5
  Author: Tarun Pandya
6
6
  Requires-Python: >=3.11
@@ -174,6 +174,31 @@ Matching is always keyed on **UETR** first (CBPR+ mandates it persist end-to-end
174
174
  that is flagged. For `pacs.002 ↔ pacs.008` the caller must pass
175
175
  `direction="outbound" | "inbound"` — the tool never infers identity from BICs.
176
176
 
177
+ ### Return amounts (pacs.004 ↔ pacs.008), since 1.3.0
178
+
179
+ Banks normally deduct their charges and return the rest, so a **lower returned
180
+ amount is normal, not a mismatch**. What must match exactly is the return's
181
+ *echo* of the original amount. Each case has its own stable rule id:
182
+
183
+ | Case | Rule id | Severity |
184
+ | --- | --- | --- |
185
+ | `OrgnlIntrBkSttlmAmt` present and differs from the original `IntrBkSttlmAmt` (amount or currency) | `CBPR-COR-003-ECHO-MISMATCH` | ERROR |
186
+ | `OrgnlIntrBkSttlmAmt` absent - only the returned amount can be compared | `CBPR-COR-003-ECHO-ABSENT` | INFO |
187
+ | Same currency, returned equal to original | - | none |
188
+ | Same currency, returned lower - charges deducted. If `ChrgsInf` is present, the message says whether returned + charges (same currency) reconciles to the original, and shows the difference if not | `CBPR-COR-003-RETURNED-LOWER` | INFO |
189
+ | Same currency, returned higher - unusual, review | `CBPR-COR-003-RETURNED-HIGHER` | WARN |
190
+ | Returned currency differs from original - FX return, not directly comparable | `CBPR-COR-003-FX` | INFO |
191
+
192
+ The returned amount is always compared with the **actual** original amount,
193
+ never with the echo. `is_consistent` is false only when there is an ERROR, so a
194
+ normal charges-deducted return is consistent. `CBPR-COR-003` itself still
195
+ reports a broken link and mis-echoed references (OrgnlMsgId, OrgnlTxId,
196
+ OrgnlEndToEndId).
197
+
198
+ Validating a single pacs.004 applies the same grading to the returned amount
199
+ against the original amount the return quotes: `CBPR-RTN-003-RETURNED-LOWER`
200
+ (INFO), `CBPR-RTN-003-RETURNED-HIGHER` (WARN), `CBPR-RTN-003-FX` (INFO).
201
+
177
202
  v1 is deliberately **pairwise and stateless**: you supply the two messages to
178
203
  compare. A stateful message store is v2 (see BUILD_PLAN §12).
179
204
 
@@ -262,7 +287,7 @@ guidelines are the bar.
262
287
  | Codes | `CBPR-COD-001..004` |
263
288
  | Amounts | `CBPR-AMT-001..003` |
264
289
  | Agents / COV | `CBPR-AGT-001` (WARN), `CBPR-AGT-002` (ERROR), `CBPR-AGT-003` (INFO) |
265
- | Returns (pacs.004) | `CBPR-RTN-001/002/003` (ERROR/WARN), `CBPR-RTN-004` (WARN), `CBPR-RTN-005` (INFO) |
290
+ | Returns (pacs.004) | `CBPR-RTN-001/002` (ERROR), `CBPR-RTN-003-RETURNED-LOWER` (INFO), `CBPR-RTN-003-RETURNED-HIGHER` (WARN), `CBPR-RTN-003-FX` (INFO), `CBPR-RTN-005` (INFO). `RTN-004` retired in 1.3.0 - see [Return amounts](#return-amounts-pacs004--pacs008-since-130) |
266
291
 
267
292
  ## Correlation scenarios
268
293
 
@@ -270,7 +295,7 @@ guidelines are the bar.
270
295
  | --- | --- | --- | --- |
271
296
  | COV | `CBPR-COR-001` | pacs.009 COV ↔ originating pacs.008 | `UndrlygCstmrCdtTrf` UETR |
272
297
  | Status | `CBPR-COR-002` | pacs.002 ↔ pacs.008 (bidirectional, `direction` required) | `OrgnlUETR` |
273
- | Return | `CBPR-COR-003` | pacs.004 ↔ original pacs.008 | `OrgnlUETR` |
298
+ | Return | `CBPR-COR-003`, amounts `CBPR-COR-003-*` | pacs.004 ↔ original pacs.008 | `OrgnlUETR` |
274
299
 
275
300
  ## Verification
276
301
 
@@ -155,6 +155,31 @@ Matching is always keyed on **UETR** first (CBPR+ mandates it persist end-to-end
155
155
  that is flagged. For `pacs.002 ↔ pacs.008` the caller must pass
156
156
  `direction="outbound" | "inbound"` — the tool never infers identity from BICs.
157
157
 
158
+ ### Return amounts (pacs.004 ↔ pacs.008), since 1.3.0
159
+
160
+ Banks normally deduct their charges and return the rest, so a **lower returned
161
+ amount is normal, not a mismatch**. What must match exactly is the return's
162
+ *echo* of the original amount. Each case has its own stable rule id:
163
+
164
+ | Case | Rule id | Severity |
165
+ | --- | --- | --- |
166
+ | `OrgnlIntrBkSttlmAmt` present and differs from the original `IntrBkSttlmAmt` (amount or currency) | `CBPR-COR-003-ECHO-MISMATCH` | ERROR |
167
+ | `OrgnlIntrBkSttlmAmt` absent - only the returned amount can be compared | `CBPR-COR-003-ECHO-ABSENT` | INFO |
168
+ | Same currency, returned equal to original | - | none |
169
+ | Same currency, returned lower - charges deducted. If `ChrgsInf` is present, the message says whether returned + charges (same currency) reconciles to the original, and shows the difference if not | `CBPR-COR-003-RETURNED-LOWER` | INFO |
170
+ | Same currency, returned higher - unusual, review | `CBPR-COR-003-RETURNED-HIGHER` | WARN |
171
+ | Returned currency differs from original - FX return, not directly comparable | `CBPR-COR-003-FX` | INFO |
172
+
173
+ The returned amount is always compared with the **actual** original amount,
174
+ never with the echo. `is_consistent` is false only when there is an ERROR, so a
175
+ normal charges-deducted return is consistent. `CBPR-COR-003` itself still
176
+ reports a broken link and mis-echoed references (OrgnlMsgId, OrgnlTxId,
177
+ OrgnlEndToEndId).
178
+
179
+ Validating a single pacs.004 applies the same grading to the returned amount
180
+ against the original amount the return quotes: `CBPR-RTN-003-RETURNED-LOWER`
181
+ (INFO), `CBPR-RTN-003-RETURNED-HIGHER` (WARN), `CBPR-RTN-003-FX` (INFO).
182
+
158
183
  v1 is deliberately **pairwise and stateless**: you supply the two messages to
159
184
  compare. A stateful message store is v2 (see BUILD_PLAN §12).
160
185
 
@@ -243,7 +268,7 @@ guidelines are the bar.
243
268
  | Codes | `CBPR-COD-001..004` |
244
269
  | Amounts | `CBPR-AMT-001..003` |
245
270
  | Agents / COV | `CBPR-AGT-001` (WARN), `CBPR-AGT-002` (ERROR), `CBPR-AGT-003` (INFO) |
246
- | Returns (pacs.004) | `CBPR-RTN-001/002/003` (ERROR/WARN), `CBPR-RTN-004` (WARN), `CBPR-RTN-005` (INFO) |
271
+ | Returns (pacs.004) | `CBPR-RTN-001/002` (ERROR), `CBPR-RTN-003-RETURNED-LOWER` (INFO), `CBPR-RTN-003-RETURNED-HIGHER` (WARN), `CBPR-RTN-003-FX` (INFO), `CBPR-RTN-005` (INFO). `RTN-004` retired in 1.3.0 - see [Return amounts](#return-amounts-pacs004--pacs008-since-130) |
247
272
 
248
273
  ## Correlation scenarios
249
274
 
@@ -251,7 +276,7 @@ guidelines are the bar.
251
276
  | --- | --- | --- | --- |
252
277
  | COV | `CBPR-COR-001` | pacs.009 COV ↔ originating pacs.008 | `UndrlygCstmrCdtTrf` UETR |
253
278
  | Status | `CBPR-COR-002` | pacs.002 ↔ pacs.008 (bidirectional, `direction` required) | `OrgnlUETR` |
254
- | Return | `CBPR-COR-003` | pacs.004 ↔ original pacs.008 | `OrgnlUETR` |
279
+ | Return | `CBPR-COR-003`, amounts `CBPR-COR-003-*` | pacs.004 ↔ original pacs.008 | `OrgnlUETR` |
255
280
 
256
281
  ## Verification
257
282
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  from cbpr_validate.core import validate_bytes, validate_file, validate_string
10
10
 
11
- __version__ = "1.2.0"
11
+ __version__ = "1.3.0"
12
12
 
13
13
  __all__ = [
14
14
  "__version__",
@@ -27,6 +27,7 @@ from cbpr_validate.match.result import (
27
27
  )
28
28
  from cbpr_validate.model.finding import Finding, Severity
29
29
  from cbpr_validate.model.payment import Amount, Payment
30
+ from cbpr_validate.rules._return_amounts import grade_returned_amount
30
31
 
31
32
  _SPEC_COV = (
32
33
  "CBPR+ Usage Guidelines - pacs.009 COV cover of an underlying "
@@ -35,6 +36,7 @@ _SPEC_COV = (
35
36
  _SPEC_STATUS = "CBPR+ Usage Guidelines - pacs.002 original references (OrgnlUETR/OrgnlTxId)"
36
37
  _SPEC_RETURN = "CBPR+ Usage Guidelines - pacs.004 original references and returned amount"
37
38
  _SPEC_UETR = "CBPR+ Usage Guidelines - UETR persists end-to-end across the payment lifecycle"
39
+ _ECHO_LOCATION = "TxInf/OrgnlTxRef/OrgnlIntrBkSttlmAmt"
38
40
 
39
41
 
40
42
  class UnsupportedPairError(ValueError):
@@ -323,14 +325,23 @@ def _correlate_status(
323
325
  def _check_returned_amount(rtn: Payment, original: Payment) -> list[Finding]:
324
326
  """Cross-check the pacs.004's amounts against the *actual* original.
325
327
 
326
- ``CBPR-RTN-003`` already checks returned <= original using the amounts the
327
- return itself declares. The cross-message value-add here is checking those
328
- declared amounts against the pacs.008 they claim to return - a return can be
329
- internally consistent and still misstate the original.
328
+ Two separate questions, each with its own rule id:
329
+
330
+ 1. The echo. ``OrgnlIntrBkSttlmAmt`` on the return must equal the original
331
+ pacs.008's ``IntrBkSttlmAmt`` exactly (amount and currency). A wrong echo
332
+ means the return references the wrong payment or was built incorrectly:
333
+ ``CBPR-COR-003-ECHO-MISMATCH`` (ERROR). No echo at all is
334
+ ``CBPR-COR-003-ECHO-ABSENT`` (INFO) - only the returned amount can then
335
+ be compared.
336
+ 2. The returned amount, always against the *actual* original amount (never
337
+ against the echo, which may itself be wrong). Graded by
338
+ :func:`grade_returned_amount`: lower is normal (charges deducted, INFO),
339
+ higher in the same currency is unusual (WARN), a different currency is
340
+ an FX return (INFO).
330
341
 
331
342
  Only compares against the pacs.008's interbank settlement amount; if the
332
- original does not carry one there is nothing like-for-like to compare an
333
- ``OrgnlIntrBkSttlmAmt`` with, and we emit nothing rather than guess.
343
+ original does not carry one there is nothing like-for-like to compare with,
344
+ and we emit nothing rather than guess.
334
345
  """
335
346
  actual = original.interbank_settlement_amount
336
347
  if actual is None:
@@ -338,43 +349,50 @@ def _check_returned_amount(rtn: Payment, original: Payment) -> list[Finding]:
338
349
 
339
350
  findings: list[Finding] = []
340
351
  declared = rtn.orgnl_interbank_settlement_amount
341
- if declared is not None and (
342
- declared.currency != actual.currency or declared.value != actual.value
343
- ):
352
+ if declared is None:
344
353
  findings.append(
345
354
  Finding(
346
- rule_id="CBPR-COR-003",
347
- severity=Severity.ERROR,
355
+ rule_id="CBPR-COR-003-ECHO-ABSENT",
356
+ severity=Severity.INFO,
348
357
  message=(
349
- f"OrgnlIntrBkSttlmAmt on the return ({declared.value} "
350
- f"{declared.currency}) does not match the original pacs.008 "
351
- f"({actual.value} {actual.currency})"
358
+ "Original amount echo not provided (no OrgnlIntrBkSttlmAmt); only the "
359
+ "returned amount could be compared with the original payment"
360
+ ),
361
+ location=_ECHO_LOCATION,
362
+ remediation=(
363
+ "Populate OrgnlIntrBkSttlmAmt from the original pacs.008 so the "
364
+ "return can be fully reconciled"
352
365
  ),
353
- location="TxInf/OrgnlTxRef/OrgnlIntrBkSttlmAmt",
354
- remediation="Quote the original settlement amount exactly as it was sent",
355
366
  spec_reference=_SPEC_RETURN,
356
367
  )
357
368
  )
358
-
359
- returned = rtn.returned_interbank_settlement_amount
360
- if (
361
- returned is not None
362
- and returned.currency == actual.currency
363
- and returned.value > actual.value
364
- ):
369
+ elif declared.currency != actual.currency or declared.value != actual.value:
365
370
  findings.append(
366
371
  Finding(
367
- rule_id="CBPR-COR-003",
372
+ rule_id="CBPR-COR-003-ECHO-MISMATCH",
368
373
  severity=Severity.ERROR,
369
374
  message=(
370
- f"Returned amount {returned.value} {returned.currency} exceeds the "
371
- f"original pacs.008 settlement amount {actual.value} {actual.currency}"
375
+ f"Original amount echo does not match the original payment: "
376
+ f"OrgnlIntrBkSttlmAmt on the return is {declared.value} "
377
+ f"{declared.currency}, the original pacs.008 settled {actual.value} "
378
+ f"{actual.currency}"
372
379
  ),
373
- location="TxInf/RtrdIntrBkSttlmAmt",
374
- remediation="A return may never settle for more than the original payment",
380
+ location=_ECHO_LOCATION,
381
+ remediation="Quote the original settlement amount exactly as it was sent",
375
382
  spec_reference=_SPEC_RETURN,
376
383
  )
377
384
  )
385
+
386
+ returned = rtn.returned_interbank_settlement_amount
387
+ if returned is not None:
388
+ findings += grade_returned_amount(
389
+ returned,
390
+ actual,
391
+ rtn.charges,
392
+ rule_prefix="CBPR-COR-003",
393
+ label="the original",
394
+ spec_reference=_SPEC_RETURN,
395
+ )
378
396
  return findings
379
397
 
380
398
 
@@ -0,0 +1,140 @@
1
+ """Grade a pacs.004 returned amount against the amount it returns.
2
+
3
+ Shared by two callers that ask the same payments question from different
4
+ vantage points:
5
+
6
+ * ``rules/returns.py`` (``CBPR-RTN-003-*``) - one message on its own: the
7
+ returned amount against the original amount the return *quotes*
8
+ (``OrgnlIntrBkSttlmAmt``).
9
+ * ``match/matcher.py`` (``CBPR-COR-003-*``) - correlation: the returned amount
10
+ against the *actual* settlement amount of the original pacs.008.
11
+
12
+ The grading (SME-specified, v1.3.0):
13
+
14
+ * same currency, equal -> no finding
15
+ * same currency, returned lower -> INFO. Normal: the returning banks deduct
16
+ their charges. If ChrgsInf is present we say whether returned + charges (same
17
+ currency only) reconciles to the reference, and show the difference if not.
18
+ It stays INFO either way - an unreconciled shortfall is worth seeing, not a
19
+ reason to call the return non-compliant.
20
+ * same currency, returned higher -> WARN. Unusual; review.
21
+ * different currencies -> INFO. An FX return (recalculated at the
22
+ current rate) - the two amounts are not directly comparable.
23
+
24
+ All arithmetic is ``Decimal``; a float never touches an amount.
25
+ """
26
+
27
+ from __future__ import annotations
28
+
29
+ from decimal import Decimal
30
+
31
+ from cbpr_validate.model.finding import Finding, Severity
32
+ from cbpr_validate.model.payment import Amount, ChargeInfo
33
+
34
+ RETURNED_LOCATION = "TxInf/RtrdIntrBkSttlmAmt"
35
+
36
+
37
+ def _charges_note(
38
+ returned: Amount, reference: Amount, charges: list[ChargeInfo] | None, label: str
39
+ ) -> tuple[str, bool]:
40
+ """The reconciliation sentence for a lower return, and whether it reconciles."""
41
+ amounts = [c.amount for c in charges or [] if c.amount is not None]
42
+ same = [a.value for a in amounts if a.currency == returned.currency]
43
+ other = len(amounts) - len(same)
44
+ ccy = returned.currency
45
+
46
+ if not amounts:
47
+ note, reconciles = " No ChrgsInf provided to reconcile the difference.", False
48
+ elif not same:
49
+ note, reconciles = f" No ChrgsInf in {ccy} to reconcile the difference.", False
50
+ else:
51
+ total_charges = sum(same, Decimal("0"))
52
+ total = returned.value + total_charges
53
+ reconciles = total == reference.value
54
+ prefix = f" Returned {returned.value} + charges {total_charges} = {total} {ccy}"
55
+ if reconciles:
56
+ note = f"{prefix} reconciles to {label}."
57
+ else:
58
+ gap = abs(reference.value - total)
59
+ note = f"{prefix} does not reconcile to {label} (difference {gap} {ccy})."
60
+
61
+ if other:
62
+ note += f" {other} charge(s) in other currencies not included."
63
+ return note, reconciles
64
+
65
+
66
+ def grade_returned_amount(
67
+ returned: Amount,
68
+ reference: Amount,
69
+ charges: list[ChargeInfo] | None,
70
+ *,
71
+ rule_prefix: str,
72
+ label: str,
73
+ spec_reference: str,
74
+ ) -> list[Finding]:
75
+ """Findings for one returned amount against its reference amount.
76
+
77
+ ``rule_prefix`` is ``CBPR-COR-003`` or ``CBPR-RTN-003``; the case suffix
78
+ (``-RETURNED-LOWER`` / ``-RETURNED-HIGHER`` / ``-FX``) is appended so every
79
+ case has its own stable rule id. ``label`` names the reference amount in
80
+ the message ("the original", or "the original amount quoted on this
81
+ return").
82
+ """
83
+ if returned.currency != reference.currency:
84
+ return [
85
+ Finding(
86
+ rule_id=f"{rule_prefix}-FX",
87
+ severity=Severity.INFO,
88
+ message=(
89
+ f"FX return - returned amount {returned.value} {returned.currency} and "
90
+ f"{label} {reference.value} {reference.currency} are in different "
91
+ f"currencies and are not directly comparable"
92
+ ),
93
+ location=RETURNED_LOCATION,
94
+ remediation=None,
95
+ spec_reference=spec_reference,
96
+ )
97
+ ]
98
+
99
+ ccy = returned.currency
100
+ if returned.value > reference.value:
101
+ return [
102
+ Finding(
103
+ rule_id=f"{rule_prefix}-RETURNED-HIGHER",
104
+ severity=Severity.WARN,
105
+ message=(
106
+ f"Returned amount {returned.value} {ccy} exceeds {label} "
107
+ f"{reference.value} {ccy} in the same currency - unusual, review"
108
+ ),
109
+ location=RETURNED_LOCATION,
110
+ remediation=(
111
+ "Check why the return settles for more than the original; a "
112
+ "same-currency over-return is unusual"
113
+ ),
114
+ spec_reference=spec_reference,
115
+ )
116
+ ]
117
+
118
+ if returned.value < reference.value:
119
+ difference = reference.value - returned.value
120
+ note, reconciles = _charges_note(returned, reference, charges, label)
121
+ return [
122
+ Finding(
123
+ rule_id=f"{rule_prefix}-RETURNED-LOWER",
124
+ severity=Severity.INFO,
125
+ message=(
126
+ f"Returned amount {returned.value} {ccy} is lower than {label} "
127
+ f"{reference.value} {ccy} - charges deducted (difference "
128
+ f"{difference} {ccy}).{note}"
129
+ ),
130
+ location=RETURNED_LOCATION,
131
+ remediation=(
132
+ None
133
+ if reconciles
134
+ else "Disclose every deducted charge in ChrgsInf so the amounts reconcile"
135
+ ),
136
+ spec_reference=spec_reference,
137
+ )
138
+ ]
139
+
140
+ return [] # same currency, equal: nothing to say