Enable Banking: support STET deferred-debit cards (OTHR purchases and negative ITAV balance)

DA
2d ago
+481-28

Fixes #6131

Summary

Support for French STET/BPCE deferred-debit credit cards (cartes à débit différé) via Enable Banking:

  1. Fetch OTHR rows on CreditCard accounts when pending sync is on (Importer#fetch_and_store_transactions). A 400/422 means "not supported": it is recorded as an info provider_sync_error debug log entry and leaves the pending set complete, since the PDNG rows were fetched. The rows go through the existing pending pipeline and settle through their entry_reference once booked.
  2. Cover a whole statement cycle. A purchase stays OTHR until the monthly statement closes (up to 31 days), so EnableBankingEntry::Processor::DEFERRED_DEBIT_CYCLE_DAYS (45) sets:
    • the OTHR request window, whatever the BOOK window: wider than a few-day incremental sync, and never as far back as an old sync start date, which could make the ASPSP reject the date_from without SCA (never before the user's sync start date either), with the post-fetch date filter applying the same bound to OTHR rows;
    • when such a purchase counts as stale: pending OTHR entries are marked extra.enable_banking.deferred_debit and EnableBankingAccount::Processor#clean_up_pending_transactions excludes them after 45 days instead of 8. They are also left out of the amount-only duplicate reconciliation: a cycle doesn't follow the calendar month, so a purchase made just before a statement close can share its amount with an older purchase that close books, and they settle by their own entry_reference anyway. Other pending entries keep both rules (Entry.auto_exclude_stale_pending and Entry.reconcile_pending_duplicates gain an optional entries: scope; default unchanged). For the same reason, Account::ProviderImportAdapter#find_pending_transaction doesn't let a new booking of the same amount claim them: such a claim would also make the purchase's own BOOK row, which carries the same id, be skipped as already claimed.
  3. Date unbooked OTHR rows by transaction_date instead of the cycle's shared future value_date, through one helper (EnableBankingEntry::Processor.raw_date_value) now used by transaction_date, compute_external_id, Importer#build_transaction_content_key, #newest_stored_transaction_date and #row_date. Without it, identical purchases made on different days of a cycle collapse in deduplicate_api_transactions.
  4. Keep identical same-day OTHR purchases apart: distinct_booked_reference? accepts OTHR rows, whose entry_reference is stable until booking.
  5. Let a negative CLAV/ITAV replace a stale PRCD/OPBD/CLBD on a credit card read as debt (Importer#fresher_balance). The sign comes from signed_balance_amount, so both "-120.50" and "120.50" + DBIT count. Non-negative ones stay excluded, including from the same-day tiebreak, keeping the #5886 / mBank PL guard.

Behaviour changes outside OTHR

  • transaction_date, newest_stored_transaction_date and the post-fetch date filter now skip an empty-string booking_date/value_date (.presence, as the dedup key and compute_external_id already did) instead of raising on Date.parse("") or leaving the row unfiltered.

Tests

  • importer_balance_test.rb: negative ITAV (signed amount and DBIT) replaces a stale PRCD; ITAV = 0.00 does not; a same-day positive ITAV stays out of the tiebreak. The existing "keeps a stale closing booked balance on a credit card over a newer available balance" test (positive ITAV) still covers the mBank guard.
  • importer_pdng_test.rb: OTHR rows stored as pending and not collapsed (same day and across the cycle); dropped once booked with the same entry_reference; 422 on OTHR tolerated without marking the pending set incomplete, with an info debug log entry; a whole cycle requested and kept on an incremental sync; only one cycle requested when a first fetch reaches further back; no OTHR request on a depository account; blank booking_date in the date filter.
  • enable_banking_entry/processor_test.rb: OTHR row dated by transaction_date; BOOK/PDNG unchanged; pending OTHR marked deferred_debit, PDNG not; a purchase settles in place when its statement books it under the same entry_reference; a booking of the same amount doesn't claim it.
  • enable_banking_account/processor_test.rb: a deferred-debit purchase stays pending within the cycle and is excluded after it; it is not reconciled against an earlier cycle's booking of the same amount, while an ordinary pending still is.
  • importer_pending_balance_test.rb: default empty OTHR page in the shared stub for the existing credit-card cases.

Validation

Run locally in the repo's devcontainer (Postgres 18, Redis):

  • bin/rails test test/models/enable_banking_item test/models/enable_banking_entry test/models/enable_banking_account test/models/enable_banking_account_test.rb test/models/entry_test.rb test/models/simplefin_item test/models/account/provider_import_adapter_test.rb: 784 runs, 0 failures, 0 errors
  • bin/rails test: 20314 runs, 0 failures, 0 errors, 9 skips
  • bin/rubocop on the changed files: no offenses
  • bin/brakeman --no-pager: no warnings

No ERB, JavaScript or UI changes, so ERB lint, Biome and system tests don't apply.

Open questions

See #6131: pending-inclusive ITAV on a liability, and the date jump at statement close.


Summary by cubic

Fixes #6131 by adding Enable Banking support for French STET/BPCE deferred-debit credit cards, making their purchases visible as pending entries and letting a negative available balance update the account.

  • Fetches OTHR transactions on credit card accounts during pending sync over a whole statement cycle (45 days), independent of the BOOK window and never before the user's sync start date, and tolerates 400/422 responses without marking the pending set incomplete.
  • Dates unbooked OTHR rows by transaction_date rather than the cycle's shared future value_date, and keeps identical same-day purchases distinct via entry_reference.
  • Marks pending OTHR entries as deferred_debit so they stay pending for a full cycle instead of 8 days; they are also left out of amount-only reconciliation and the pending claim heuristic, since a statement close can book an earlier cycle's purchase of the same amount and otherwise hide them.
  • Lets a negative CLAV/ITAV replace a stale PRCD/OPBD/CLBD balance on a credit card read as debt, from signed_balance_amount; non-negative available balances stay excluded to preserve the mBank PL guard.
  • transaction_date, newest_stored_transaction_date, and the post-fetch date filter now skip empty-string booking_date/value_date instead of raising on Date.parse("").

Summary by CodeRabbit

  • New Features
    • Credit-card imports now include eligible deferred-debit purchases as pending transactions, so card spending can appear before it is fully booked.
    • Deferred-debit purchases remain pending for up to 45 days.
  • Bug Fixes
    • Pending card transactions use a more appropriate date when booking details are unavailable.
    • Credit-card balances update more reliably: negative available balances can replace stale balances, while non-negative available credit does not incorrectly replace debt.
    • Matching booked transactions replace corresponding pending purchases to avoid duplicates.
    • Deferred-debit purchases are excluded from duplicate reconciliation with other pending transactions.