Skip to content

Commit

Permalink
Fix #287 (#288)
Browse files Browse the repository at this point in the history
* Fix #287

* Update deps

* Publish beta version 3.0.7-beta.1

* Make bic optional

* Publish beta version 3.0.7-beta.2

* Publish beta version 3.0.8-beta.1

* Add missing ?

Co-authored-by: Daniel Reinón García <[email protected]>

* Change pain type

* Revert "Change pain type"

This reverts commit a22e39d.

* Reapply "Change pain type"

This reverts commit 7207fb4.

* Fix tests

* Fix comment

---------

Co-authored-by: Daniel Reinón García <[email protected]>
  • Loading branch information
jerebtw and dreinon authored Oct 10, 2024
1 parent 3b9b0a1 commit 6251f34
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 80 deletions.
Binary file modified bun.lockb
Binary file not shown.
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"sepa",
"xml"
],
"version": "3.0.7",
"version": "3.0.8-beta.1",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"type": "module",
"repository": "https://github.com/jerebtw/Sepa-JS-XML.git",
"repository": {
"url": "git+https://github.com/jerebtw/Sepa-JS-XML.git"
},
"author": "Jerebtw <[email protected]>",
"license": "MIT",
"files": [
Expand All @@ -32,14 +34,14 @@
},
"devDependencies": {
"@swc/cli": "^0.4.0",
"@swc/core": "^1.7.0",
"@types/bun": "^1.1.6",
"dayjs": "^1.11.12",
"@swc/core": "^1.7.28",
"@types/bun": "^1.1.10",
"dayjs": "^1.11.13",
"gh-pages": "^6.1.1",
"gzip-size-cli": "^5.1.0",
"prettier": "^3.3.3",
"typedoc": "^0.26.5",
"typescript": "^5.5.3"
"typedoc": "^0.26.7",
"typescript": "^5.6.2"
},
"dependencies": {
"ibantools": "^4.5.1",
Expand Down
267 changes: 197 additions & 70 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ test("check currency", () => {
});

test("check id error", () => {
try {
expect(() =>
createSepaXML(
{
id: "123456789123456789123456789123456789",
Expand All @@ -116,21 +116,14 @@ test("check id error", () => {
checkIBAN: true,
checkBIC: true,
},
);
} catch (e) {
if (e instanceof Error) {
expect(e.message).toEqual(
"Max length for sepaData.id is 35 (123456789123456789123456789123456789)",
);
return;
}
}

throw new Error("Error not thrown");
),
).toThrow(
"Max length for sepaData.id is 35 (123456789123456789123456789123456789)",
);
});

test("check iban validation", () => {
try {
expect(() =>
createSepaXML({
id: "Test",
creationDate: new Date(),
Expand All @@ -145,21 +138,12 @@ test("check iban validation", () => {
requestedExecutionDate: new Date(),
},
],
});
} catch (e) {
if (e instanceof Error) {
expect(e.message).toEqual(
"sepaData.positions[0].iban is not valid (Test)",
);
return;
}
}

throw new Error("Error not thrown");
}),
).toThrow("sepaData.positions[0].iban is not valid (Test)");
});

test("check bic validation", () => {
try {
expect(() =>
createSepaXML({
id: "Test",
creationDate: new Date(),
Expand All @@ -174,55 +158,198 @@ test("check bic validation", () => {
requestedExecutionDate: new Date(),
},
],
});
} catch (e) {
if (e instanceof Error) {
expect(e.message).toEqual(
"sepaData.positions[0].bic is not valid (Test)",
);
return;
}
}

throw new Error("Error not thrown");
}),
).toThrow("sepaData.positions[0].bic is not valid (Test)");
});

test("issue #172", () => {
const result = createSepaXML(
{
painVersion: "pain.001.001.03",
id: "Test1345",
creationDate: dayjs.utc("2022-06-16").toDate(),
initiatorName: "Test Company",
positions: [
{
id: "Test123",
batchBooking: false,
iban: "DE02701500000000594937",
bic: "Test",
requestedExecutionDate: dayjs.utc("2022-06-16").toDate(),
name: "Pos 1",
payments: [
{
id: "123",
amount: 230,
currency: "EUR",
bic: "Test",
name: "Money Company",
iban: "DE02701500000000594937",
remittanceInformation: "Money please",
end2endReference: "lol",
},
],
},
],
},
{
checkIBAN: false,
checkBIC: false,
},
expect(
createSepaXML(
{
painVersion: "pain.001.001.03",
id: "Test1345",
creationDate: dayjs.utc("2022-06-16").toDate(),
initiatorName: "Test Company",
positions: [
{
id: "Test123",
batchBooking: false,
iban: "DE02701500000000594937",
bic: "Test",
requestedExecutionDate: dayjs.utc("2022-06-16").toDate(),
name: "Pos 1",
payments: [
{
id: "123",
amount: 230,
currency: "EUR",
bic: "Test",
name: "Money Company",
iban: "DE02701500000000594937",
remittanceInformation: "Money please",
end2endReference: "lol",
},
],
},
],
},
{
checkIBAN: false,
checkBIC: false,
},
),
).toBe(
`<?xml version="1.0" encoding="UTF-8"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd"><CstmrCdtTrfInitn><GrpHdr><MsgId>Test1345</MsgId><CreDtTm>2022-06-16T00:00:00</CreDtTm><NbOfTxs>1</NbOfTxs><CtrlSum>230.00</CtrlSum><InitgPty><Nm>Test Company</Nm></InitgPty></GrpHdr><PmtInf><PmtInfId>Test123</PmtInfId><PmtMtd>TRF</PmtMtd><BtchBookg>false</BtchBookg><NbOfTxs>1</NbOfTxs><CtrlSum>230.00</CtrlSum><PmtTpInf><SvcLvl><Cd>SEPA</Cd></SvcLvl></PmtTpInf><ReqdExctnDt>2022-06-16</ReqdExctnDt><Dbtr><Nm>Pos 1</Nm></Dbtr><DbtrAcct><Id><IBAN>DE02701500000000594937</IBAN></Id></DbtrAcct><DbtrAgt><FinInstnId><BIC>Test</BIC></FinInstnId></DbtrAgt><ChrgBr>SLEV</ChrgBr><CdtTrfTxInf><PmtId><InstrId>123</InstrId><EndToEndId>lol</EndToEndId></PmtId><Amt><InstdAmt Ccy="EUR">230.00</InstdAmt></Amt><CdtrAgt><FinInstnId><BIC>Test</BIC></FinInstnId></CdtrAgt><Cdtr><Nm>Money Company</Nm></Cdtr><CdtrAcct><Id><IBAN>DE02701500000000594937</IBAN></Id></CdtrAcct><RmtInf><Ustrd>Money please</Ustrd></RmtInf></CdtTrfTxInf></PmtInf></CstmrCdtTrfInitn></Document>`,
);
expect(result).toBe(
});

test("issue #287", () => {
expect(
createSepaXML(
{
painVersion: "pain.001.001.03",
id: "Test1345",
creationDate: dayjs.utc("2022-06-16").toDate(),
initiatorName: "Test Company",
positions: [
{
id: "Test123",
batchBooking: false,
iban: "DE02701500000000594937",
requestedExecutionDate: dayjs.utc("2022-06-16").toDate(),
name: "Pos 1",
payments: [
{
id: "123",
amount: 230,
currency: "EUR",
name: "Money Company",
iban: "DE02701500000000594937",
bic: "Test",
remittanceInformation: "Money please",
end2endReference: "lol",
},
],
},
],
},
{
checkIBAN: true,
checkBIC: true,
},
),
).toBe(
`<?xml version="1.0" encoding="UTF-8"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd"><CstmrCdtTrfInitn><GrpHdr><MsgId>Test1345</MsgId><CreDtTm>2022-06-16T00:00:00</CreDtTm><NbOfTxs>1</NbOfTxs><CtrlSum>230.00</CtrlSum><InitgPty><Nm>Test Company</Nm></InitgPty></GrpHdr><PmtInf><PmtInfId>Test123</PmtInfId><PmtMtd>TRF</PmtMtd><BtchBookg>false</BtchBookg><NbOfTxs>1</NbOfTxs><CtrlSum>230.00</CtrlSum><PmtTpInf><SvcLvl><Cd>SEPA</Cd></SvcLvl></PmtTpInf><ReqdExctnDt>2022-06-16</ReqdExctnDt><Dbtr><Nm>Pos 1</Nm></Dbtr><DbtrAcct><Id><IBAN>DE02701500000000594937</IBAN></Id></DbtrAcct><DbtrAgt><FinInstnId><BIC/></FinInstnId></DbtrAgt><ChrgBr>SLEV</ChrgBr><CdtTrfTxInf><PmtId><InstrId>123</InstrId><EndToEndId>lol</EndToEndId></PmtId><Amt><InstdAmt Ccy="EUR">230.00</InstdAmt></Amt><CdtrAgt><FinInstnId><BIC>Test</BIC></FinInstnId></CdtrAgt><Cdtr><Nm>Money Company</Nm></Cdtr><CdtrAcct><Id><IBAN>DE02701500000000594937</IBAN></Id></CdtrAcct><RmtInf><Ustrd>Money please</Ustrd></RmtInf></CdtTrfTxInf></PmtInf></CstmrCdtTrfInitn></Document>`,
);
expect(
createSepaXML(
{
painVersion: "pain.001.001.03",
id: "Test1345",
creationDate: dayjs.utc("2022-06-16").toDate(),
initiatorName: "Test Company",
positions: [
{
id: "Test123",
batchBooking: false,
iban: "DE02701500000000594937",
bic: "Test",
requestedExecutionDate: dayjs.utc("2022-06-16").toDate(),
name: "Pos 1",
payments: [
{
id: "123",
amount: 230,
currency: "EUR",
name: "Money Company",
iban: "DE02701500000000594937",
bic: "Test",
remittanceInformation: "Money please",
end2endReference: "lol",
},
],
},
],
},
{
checkIBAN: true,
checkBIC: false,
},
),
).toBe(
`<?xml version="1.0" encoding="UTF-8"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd"><CstmrCdtTrfInitn><GrpHdr><MsgId>Test1345</MsgId><CreDtTm>2022-06-16T00:00:00</CreDtTm><NbOfTxs>1</NbOfTxs><CtrlSum>230.00</CtrlSum><InitgPty><Nm>Test Company</Nm></InitgPty></GrpHdr><PmtInf><PmtInfId>Test123</PmtInfId><PmtMtd>TRF</PmtMtd><BtchBookg>false</BtchBookg><NbOfTxs>1</NbOfTxs><CtrlSum>230.00</CtrlSum><PmtTpInf><SvcLvl><Cd>SEPA</Cd></SvcLvl></PmtTpInf><ReqdExctnDt>2022-06-16</ReqdExctnDt><Dbtr><Nm>Pos 1</Nm></Dbtr><DbtrAcct><Id><IBAN>DE02701500000000594937</IBAN></Id></DbtrAcct><DbtrAgt><FinInstnId><BIC>Test</BIC></FinInstnId></DbtrAgt><ChrgBr>SLEV</ChrgBr><CdtTrfTxInf><PmtId><InstrId>123</InstrId><EndToEndId>lol</EndToEndId></PmtId><Amt><InstdAmt Ccy="EUR">230.00</InstdAmt></Amt><CdtrAgt><FinInstnId><BIC>Test</BIC></FinInstnId></CdtrAgt><Cdtr><Nm>Money Company</Nm></Cdtr><CdtrAcct><Id><IBAN>DE02701500000000594937</IBAN></Id></CdtrAcct><RmtInf><Ustrd>Money please</Ustrd></RmtInf></CdtTrfTxInf></PmtInf></CstmrCdtTrfInitn></Document>`,
);
expect(
createSepaXML(
{
painVersion: "pain.001.001.03",
id: "Test1345",
creationDate: dayjs.utc("2022-06-16").toDate(),
initiatorName: "Test Company",
positions: [
{
id: "Test123",
batchBooking: false,
iban: "DE02701500000000594937",
requestedExecutionDate: dayjs.utc("2022-06-16").toDate(),
name: "Pos 1",
payments: [
{
id: "123",
amount: 230,
currency: "EUR",
name: "Money Company",
iban: "DE02701500000000594937",
bic: "Test",
remittanceInformation: "Money please",
end2endReference: "lol",
},
],
},
],
},
{
checkIBAN: true,
checkBIC: false,
},
),
).toBe(
`<?xml version="1.0" encoding="UTF-8"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd"><CstmrCdtTrfInitn><GrpHdr><MsgId>Test1345</MsgId><CreDtTm>2022-06-16T00:00:00</CreDtTm><NbOfTxs>1</NbOfTxs><CtrlSum>230.00</CtrlSum><InitgPty><Nm>Test Company</Nm></InitgPty></GrpHdr><PmtInf><PmtInfId>Test123</PmtInfId><PmtMtd>TRF</PmtMtd><BtchBookg>false</BtchBookg><NbOfTxs>1</NbOfTxs><CtrlSum>230.00</CtrlSum><PmtTpInf><SvcLvl><Cd>SEPA</Cd></SvcLvl></PmtTpInf><ReqdExctnDt>2022-06-16</ReqdExctnDt><Dbtr><Nm>Pos 1</Nm></Dbtr><DbtrAcct><Id><IBAN>DE02701500000000594937</IBAN></Id></DbtrAcct><DbtrAgt><FinInstnId><BIC/></FinInstnId></DbtrAgt><ChrgBr>SLEV</ChrgBr><CdtTrfTxInf><PmtId><InstrId>123</InstrId><EndToEndId>lol</EndToEndId></PmtId><Amt><InstdAmt Ccy="EUR">230.00</InstdAmt></Amt><CdtrAgt><FinInstnId><BIC>Test</BIC></FinInstnId></CdtrAgt><Cdtr><Nm>Money Company</Nm></Cdtr><CdtrAcct><Id><IBAN>DE02701500000000594937</IBAN></Id></CdtrAcct><RmtInf><Ustrd>Money please</Ustrd></RmtInf></CdtTrfTxInf></PmtInf></CstmrCdtTrfInitn></Document>`,
);
expect(() =>
createSepaXML(
{
painVersion: "pain.001.001.03",
id: "Test1345",
creationDate: dayjs.utc("2022-06-16").toDate(),
initiatorName: "Test Company",
positions: [
{
id: "Test123",
batchBooking: false,
iban: "DE02701500000000594937",
bic: "Test",
requestedExecutionDate: dayjs.utc("2022-06-16").toDate(),
name: "Pos 1",
payments: [
{
id: "123",
amount: 230,
currency: "EUR",
name: "Money Company",
iban: "DE02701500000000594937",
bic: "Test",
remittanceInformation: "Money please",
end2endReference: "lol",
},
],
},
],
},
{
checkIBAN: true,
checkBIC: true,
},
),
).toThrow("sepaData.positions[0].bic is not valid (Test)");
});
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export interface Payment {
id: string;
name: string;
iban: string;
bic: string;
/** Optional sometimes see https://github.com/jerebtw/Sepa-JS-XML/issues/287 */
bic?: string;
mandateId?: string;
mandateSignatureDate?: Date;

Expand Down Expand Up @@ -66,7 +67,8 @@ export interface CreditorPayments {

name: string;
iban: string;
bic: string;
/** Optional sometimes see https://github.com/jerebtw/Sepa-JS-XML/issues/287 */
bic?: string;

payments: Payment[];
}
Expand Down Expand Up @@ -220,7 +222,8 @@ function getPmtInf(
);
}

if (options?.checkBIC && !isValidBIC(item.bic)) {
const bicOptional = PAIN_TYPES[painFormat] === "CstmrDrctDbtInitn";
if (options?.checkBIC && (item.bic ? !isValidBIC(item.bic) : bicOptional)) {
throw new Error(
`sepaData.positions[${index}].bic is not valid (${item.bic})`,
);
Expand Down Expand Up @@ -285,7 +288,11 @@ function getPmtInf(
IBAN: item.iban,
},
};

pmtInfData.DbtrAgt = { FinInstnId: { BIC: item.bic } };
if (bicOptional && !item.bic) {
pmtInfData.DbtrAgt = { FinInstnId: null };
}
}

pmtInfData.ChrgBr = "SLEV";
Expand Down Expand Up @@ -364,9 +371,15 @@ function getPayments(
},
};

const bicOptional = PAIN_TYPES[painFormat] === "CstmrDrctDbtInitn";
paymentData.CdtrAgt = {
FinInstnId: { BIC: payment.bic },
};

if (!payment.bic && bicOptional) {
paymentData.CdtrAgt = { FinInstnId: null };
}

paymentData.Cdtr = { Nm: payment.name };
paymentData.CdtrAcct = {
Id: {
Expand Down

0 comments on commit 6251f34

Please sign in to comment.