The personal finance app for everyone (by everyone)
Ruby
Skip the account holder's name when matching card payment merchants (#4840)
Krish Wanchoo426fefe9dc55
diff --git a/app/models/enable_banking_entry/processor.rb b/app/models/enable_banking_entry/processor.rbindex fa8c4bf3c6..1ebd31b4ed 100644--- a/app/models/enable_banking_entry/processor.rb+++ b/app/models/enable_banking_entry/processor.rb@@ -450,7 +450,12 @@ class EnableBankingEntry::Processordescriptive = lines.find { |line| !technical_remittance_line?(line) } || lines.firstreturn descriptive if descriptive.blank?- matched_known_merchant_name(descriptive) || strip_payment_processor_prefix(descriptive)+ cleaned = strip_payment_processor_prefix(descriptive)+ # Keep a holder-only remittance whole, so a known merchant matching part+ # of the name ("Jane" in "Jane Doe") is not picked out of it.+ return cleaned if account_holder_name?(cleaned)++ matched_known_merchant_name(descriptive) || cleanedend# The remittance lines describing who was paid, one per element, with any@@ -486,7 +491,9 @@ class EnableBankingEntry::Processor# so e.g. "BILLA DANKT 0007114" resolves to "Billa", not "BILLA". Longest match# wins when multiple known names match (prefer the more specific one).def matched_known_merchant_name(line)- candidates = known_merchant_names.select { |name| name.length >= MIN_KNOWN_MERCHANT_MATCH_LENGTH }+ candidates = known_merchant_names.select do |name|+ name.length >= MIN_KNOWN_MERCHANT_MATCH_LENGTH && !account_holder_name?(name)+ end# Lookaround instead of \b at both ends: \b only fires on a word/non-word# transition, so it silently fails to match right after a name that itself# ends in punctuation (e.g. "A+B (Café)" ends in ")" -- a non-word char next@@ -503,6 +510,28 @@ class EnableBankingEntry::Processor@known_merchant_names ||= account&.family&.known_merchant_names || []end+ # Some banks end a card payment's remittance with the cardholder's name, and+ # a transfer between the holder's own accounts can make that name a known+ # merchant. The holder is never the payee, so their name is not matched.+ def account_holder_name?(name)+ account_holder_names.any? { |holder| holder.casecmp?(name) }+ end++ # The account holder as Enable Banking names the account, plus this+ # transaction's own side: the debtor of a payment, the creditor of a credit.+ def account_holder_names+ @account_holder_names ||= begin+ payload = enable_banking_account.raw_payload+ own_side = credit_debit_indicator == "CRDT" ? :creditor : :debtor++ [+ payload.is_a?(Hash) ? (payload["name"] || payload[:name]) : nil,+ self.class.party_name(data, own_side),+ data[:"#{own_side}_name"]+ ].filter_map { |value| value.to_s.strip.presence }+ end+ end+def identifierless_external_ids@identifierless_external_ids ||= self.class.identifierless_external_ids_for(account)end@@ -604,6 +633,8 @@ class EnableBankingEntry::Processorremittance = primary_remittance_informationreturn nil if remittance.blank?+ # The remittance may be nothing but the holder's name, which never names the payee.+ return nil if account_holder_name?(remittance)# Trust remittance-derived text as a merchant candidate when either the# counterparty was a technical CARD-* placeholder (existing Wise case), ordiff --git a/test/models/enable_banking_entry/processor_test.rb b/test/models/enable_banking_entry/processor_test.rbindex 0a0073a3e2..0f42c40973 100644--- a/test/models/enable_banking_entry/processor_test.rb+++ b/test/models/enable_banking_entry/processor_test.rb@@ -257,6 +257,101 @@ class EnableBankingEntry::ProcessorTest < ActiveSupport::TestCaseassert_equal "Billa", entry.transaction.merchant&.nameend+ test "does not take the account holder's name at the end of a card payment as the merchant" do+ @family.merchants.create!(name: "Jane Doe")+ @enable_banking_account.update!(raw_payload: { "uid" => "eb_uid_1", "name" => "JANE DOE" })++ tx = {+ entry_reference: "ref_cardholder_name",+ transaction_id: nil,+ booking_date: Date.current.to_s,+ transaction_amount: { amount: "12.50", currency: "EUR" },+ bank_transaction_code: { code: "PMNT" },+ credit_debit_indicator: "DBIT",+ remittance_information: [ "EXAMPLE SHOP 1000 CITY Card payment 05-08-2026 13.07 1234 56XX XXXX 7890 JANE DOE" ],+ status: "BOOK"+ }++ EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process+ entry = @account.entries.find_by!(external_id: "enable_banking_ref_cardholder_name")++ assert_nil entry.transaction.merchant+ assert_not_equal "Jane Doe", entry.name+ end++ test "does not match the paying side of a transaction as a known merchant" do+ # The holder's name is the longer match, so only the holder check keeps it out.+ @family.merchants.create!(name: "Jane Elizabeth Doe")+ @family.merchants.create!(name: "Example Shop")++ name = build_name_with_family(+ credit_debit_indicator: "DBIT",+ debtor: { name: "JANE ELIZABETH DOE" },+ creditor: { name: "" },+ bank_transaction_code: nil,+ remittance_information: [ "EXAMPLE SHOP 1000 CITY Card payment JANE ELIZABETH DOE" ]+ )++ assert_equal "Example Shop", name+ end++ test "does not assign the holder as merchant when the remittance is only the holder's name" do+ @family.merchants.create!(name: "Jane Doe")+ @enable_banking_account.update!(raw_payload: { "uid" => "eb_uid_1", "name" => "JANE DOE" })++ tx = {+ entry_reference: "ref_holder_only_remittance",+ transaction_id: nil,+ booking_date: Date.current.to_s,+ transaction_amount: { amount: "8.00", currency: "EUR" },+ creditor: { name: "" },+ bank_transaction_code: nil,+ credit_debit_indicator: "DBIT",+ remittance_information: [ "Jane Doe" ],+ status: "BOOK"+ }++ EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process++ assert_nil @account.entries.find_by!(external_id: "enable_banking_ref_holder_only_remittance").transaction.merchant+ end++ test "does not pick a known merchant out of a remittance that is only the holder's name" do+ @family.merchants.create!(name: "Jane")+ @enable_banking_account.update!(raw_payload: { "uid" => "eb_uid_1", "name" => "JANE DOE" })++ tx = {+ entry_reference: "ref_holder_name_partial_match",+ transaction_id: nil,+ booking_date: Date.current.to_s,+ transaction_amount: { amount: "8.00", currency: "EUR" },+ creditor: { name: "" },+ bank_transaction_code: nil,+ credit_debit_indicator: "DBIT",+ remittance_information: [ "Jane Doe" ],+ status: "BOOK"+ }++ EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process++ assert_nil @account.entries.find_by!(external_id: "enable_banking_ref_holder_name_partial_match").transaction.merchant+ end++ test "reads the holder from a debtor given as a plain string" do+ @family.merchants.create!(name: "Jane Elizabeth Doe")+ @family.merchants.create!(name: "Example Shop")++ name = build_name_with_family(+ credit_debit_indicator: "DBIT",+ debtor: "JANE ELIZABETH DOE",+ creditor: { name: "" },+ bank_transaction_code: nil,+ remittance_information: [ "EXAMPLE SHOP 1000 CITY Card payment JANE ELIZABETH DOE" ]+ )++ assert_equal "Example Shop", name+ end+test "matches known merchants regardless of retailer chain" do%w[Bipa Spar Hofer Lidl Penny].each do |chain_name|@family.merchants.create!(name: chain_name)