Skip to content

Commit

Permalink
fix: Wrong payeeName used for CBC_CREGBEBB (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
MMichotte authored Sep 17, 2024
1 parent f78383b commit 7b314e3
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/app-gocardless/bank-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BankinterBkbkesmm from './banks/bankinter-bkbkesmm.js';
import Belfius from './banks/belfius_gkccbebb.js';
import Berliner_Sparkasse_beladebexxx from './banks/berliner_sparkasse_beladebexxx.js';
import BnpBeGebabebb from './banks/bnp-be-gebabebb.js';
import CBCcregbebb from './banks/cbc_cregbebb.js';
import DanskeBankDabNO22 from './banks/danskebank-dabno22.js';
import EasybankBawaatww from './banks/easybank-bawaatww.js';
import Fortuneo from './banks/FORTUNEO_FTNOFRP1XXX.js';
Expand Down Expand Up @@ -33,6 +34,7 @@ export const banks = [
Belfius,
Berliner_Sparkasse_beladebexxx,
BnpBeGebabebb,
CBCcregbebb,
DanskeBankDabNO22,
EasybankBawaatww,
Fortuneo,
Expand Down
36 changes: 36 additions & 0 deletions src/app-gocardless/banks/cbc_cregbebb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { extractPayeeNameFromRemittanceInfo } from './util/extract-payeeName-from-remittanceInfo.js';
import Fallback from './integration-bank.js';

/** @type {import('./bank.interface.js').IBank} */
export default {
...Fallback,

institutionIds: ['CBC_CREGBEBB'],

/**
* For negative amounts, the only payee information we have is returned in
* remittanceInformationUnstructured.
*/
normalizeTransaction(transaction, _booked) {
if (Number(transaction.transactionAmount.amount) > 0) {
return {
...transaction,
payeeName:
transaction.debtorName ||
transaction.remittanceInformationUnstructured,
date: transaction.bookingDate || transaction.valueDate,
};
}

return {
...transaction,
payeeName:
transaction.creditorName ||
extractPayeeNameFromRemittanceInfo(
transaction.remittanceInformationUnstructured,
['Paiement', 'Domiciliation', 'Transfert', 'Ordre permanent'],
),
date: transaction.bookingDate || transaction.valueDate,
};
},
};
25 changes: 5 additions & 20 deletions src/app-gocardless/banks/kbc_kredbebb.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
import { extractPayeeNameFromRemittanceInfo } from './util/extract-payeeName-from-remittanceInfo.js';
import Fallback from './integration-bank.js';

/**
* The remittance information contains creditorName, payments method, dates, etc.
* This function makes sure to only extract the creditorName based on the different indicators like "Betaling met".
* f.e. Proxy Poel BE Gent Betaling met Apple Pay via Maestro 23-08-2024 om 14.03 uur XXXX XXXX XXXX XXXX -> Proxy Poel BE Gent
*/
function extractPayeeName(remittanceInformationUnstructured) {
const indices = [
remittanceInformationUnstructured.lastIndexOf(' Betaling met'),
remittanceInformationUnstructured.lastIndexOf(' Domiciliëring'),
remittanceInformationUnstructured.lastIndexOf(' Overschrijving'),
];

const indexForRemoval = Math.max(...indices);

return indexForRemoval > -1
? remittanceInformationUnstructured.substring(0, indexForRemoval)
: remittanceInformationUnstructured;
}

/** @type {import('./bank.interface.js').IBank} */
export default {
...Fallback,
Expand All @@ -44,7 +26,10 @@ export default {
...transaction,
payeeName:
transaction.creditorName ||
extractPayeeName(transaction.remittanceInformationUnstructured),
extractPayeeNameFromRemittanceInfo(
transaction.remittanceInformationUnstructured,
['Betaling met', 'Domiciliëring', 'Overschrijving'],
),
date: transaction.bookingDate || transaction.valueDate,
};
},
Expand Down
32 changes: 32 additions & 0 deletions src/app-gocardless/banks/tests/cbc_cregbebb.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import CBCcregbebb from '../cbc_cregbebb.js';

describe('cbc_cregbebb', () => {
describe('#normalizeTransaction', () => {
it('returns the remittanceInformationUnstructured as payeeName when the amount is negative', () => {
const transaction = {
remittanceInformationUnstructured:
'ONKART FR Viry Paiement Maestro par Carte de débit CBC 05-09-2024 à 15.43 heures 6703 19XX XXXX X201 5 JOHN DOE',
transactionAmount: { amount: '-45.00', currency: 'EUR' },
};
const normalizedTransaction = CBCcregbebb.normalizeTransaction(
transaction,
true,
);
expect(normalizedTransaction.payeeName).toEqual('ONKART FR Viry');
});

it('returns the debtorName as payeeName when the amount is positive', () => {
const transaction = {
debtorName: 'ONKART FR Viry',
remittanceInformationUnstructured:
'ONKART FR Viry Paiement Maestro par Carte de débit CBC 05-09-2024 à 15.43 heures 6703 19XX XXXX X201 5 JOHN DOE',
transactionAmount: { amount: '10.99', currency: 'EUR' },
};
const normalizedTransaction = CBCcregbebb.normalizeTransaction(
transaction,
true,
);
expect(normalizedTransaction.payeeName).toEqual('ONKART FR Viry');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
/**
* Extracts the payee name from the unstructured remittance information string based on pattern detection.
*
* This function scans the `remittanceInformationUnstructured` string for the presence of
* any of the specified patterns and removes the substring from the position of the last
* occurrence of the most relevant pattern. If no patterns are found, it returns the original string.
*
* @param {string} [remittanceInformationUnstructured=''] - The unstructured remittance information from which to extract the payee name.
* @param {string[]} [patterns=[]] - An array of patterns to look for within the remittance information.
* These patterns are used to identify and remove unwanted parts of the remittance information.
* @returns {string} - The extracted payee name, cleaned of any matched patterns, or the original
* remittance information if no patterns are found.
*
* @example
* const remittanceInfo = 'John Doe Paiement Maestro par Carte de débit CBC 05-09-2024 à 15.43 heures 6703 19XX XXXX X...';
* const patterns = ['Paiement', 'Domiciliation', 'Transfert', 'Ordre permanent'];
* const payeeName = extractPayeeNameFromRemittanceInfo(remittanceInfo, patterns); // --> 'John Doe'
*/
export function extractPayeeNameFromRemittanceInfo(
remittanceInformationUnstructured,
patterns,
) {
if (!remittanceInformationUnstructured || !patterns.length) {
return remittanceInformationUnstructured;
}

const indexForRemoval = patterns.reduce((maxIndex, pattern) => {
const index = remittanceInformationUnstructured.lastIndexOf(pattern);
return index > maxIndex ? index : maxIndex;
}, -1);

return indexForRemoval > -1
? remittanceInformationUnstructured.substring(0, indexForRemoval).trim()
: remittanceInformationUnstructured;
}
6 changes: 6 additions & 0 deletions upcoming-release-notes/451.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [MMichotte]
---

Fix wrong payeeName used for CBC_CREGBEBB

0 comments on commit 7b314e3

Please sign in to comment.