2883: Fix unidentified card holds

Merged
OT
2mo ago
+1188-52

Fixes #2883

Problem

Certain European ASPSPs under Open Banking / PSD2 return unfinalized card authorization holds with:

  • Missing transaction_id, entry_reference, and counterparty information.
  • Inverted direction / sign: card purchase holds are reported as credits (CRDT indicator or positive amounts) instead of debits.
  • Remittance containing only a masked card PAN (e.g. 474836xxxxxx6879 003015315475).
    In Sure, this caused holds to import as settled income (+€14.97). When the settled purchase arrived (-€14.97), pending-to-posted reconciliation failed due to the mismatched sign, stranding duplicate phantom income in user ledgers and creating garbage ProviderMerchant records.

Resulting Behavior

  1. Masked PAN Pattern: Added MASKED_CARD_DESCRIPTOR = /\A\d{4,6}[xX*]{4,8}\d{4}(?:\s+\d+)?\z/ to detect raw card authorization holds without matching legitimate merchant names.
  2. Sign Normalization: Inverted credit holds are normalized to positive outflows (expenses in Sure's sign convention) across both Enable Banking and Lunchflow. Legitimate credits without card descriptors remain inflows.
  3. Clean Naming & Merchant Suppression: Holds are named "Card Authorization Hold", merchant creation is suppressed (merchant: nil), and raw PAN strings are preserved in notes.
  4. Pending Lifecycle & Reconciliation:
    • Both providers flag holds as pending (extra[provider]["pending"] = true) and respect their respective include_pending settings.
    • Matching amounts allow Account::ProviderImportAdapter to claim the pending hold upon settlement. Lunchflow's find_existing_posted_version bypasses strict name matching for holds so out-of-order settlement reconciles cleanly.

Validation

  • test/models/enable_banking_entry/processor_test.rb: verified masked descriptor detection, amount normalization, non-inversion of legitimate credits, and end-to-end settlement claiming.
  • test/models/lunchflow_entry/processor_test.rb: verified hold detection, amount normalization, and posted reconciliation without name constraint.

Summary by CodeRabbit

  • New Features
    • Masked card authorization holds are recognized as pending transactions, even when a provider omits pending status or a transaction identifier.
    • Holds appear as “Card Authorization Hold,” with positive amounts and no assigned merchant.
    • Pending transaction settings apply to these holds across supported bank connections, including removing stored holds when pending transactions are excluded.
  • Bug Fixes
    • When a hold later appears as a posted transaction, it can be matched to the existing entry instead of duplicated.
    • Empty transaction responses now correctly update stored transactions when pending transactions are excluded.