Skip to content

Commit

Permalink
Fixed pain version 003 handling and carried missing arguments through…
Browse files Browse the repository at this point in the history
… to the constructors where necessary
  • Loading branch information
JamesLefrere committed Aug 28, 2016
1 parent 1c8428f commit 30882f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
39 changes: 24 additions & 15 deletions lib/sepa.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@
return parseInt(painFormat.substr(-2), 10) + inc;
}

function SepaDocument(painFormat) {
this._painFormat = painFormat || 'pain.008.001.02';
this._type = SEPATypes[painFormat];
function SepaDocument(options) {
options.painFormat = options.painFormat || 'pain.008.001.02';
this._painFormat = options.painFormat;
this._type = SEPATypes[options.painFormat];
this._paymentInfo = [];
this.grpHdr = new SepaGroupHeader(this._painFormat);
this.grpHdr = new SepaGroupHeader(options);
}

SepaDocument.Types = SEPATypes;
Expand Down Expand Up @@ -144,8 +145,11 @@
/**
* Wrapper class for the SEPA <GrpHdr> element.
*/
function SepaGroupHeader(painFormat) {
this._painFormat = painFormat;
function SepaGroupHeader(options) {
this._painFormat = options.painFormat;
this.id = options.id;
this.created = options.created;
this.initiatorName = options.initiatorName;
}

SepaGroupHeader.prototype = {
Expand Down Expand Up @@ -310,11 +314,7 @@
throw new Error('Given Transaction is not member of the SepaTransaction class');
}

if (pmt.id) {
pmt.id = this.id + ID_SEPARATOR + pmt.id;
} else {
pmt.id = this.id + ID_SEPARATOR + this._payments.length;
}
pmt.id = (pmt.id || this.id + ID_SEPARATOR + this._payments.length).slice(0, 35);
this._payments.push(pmt);
},

Expand All @@ -336,7 +336,9 @@
r(pmtInf, 'PmtInfId', this.id);
r(pmtInf, 'PmtMtd', this.method);
// XML v3 formats, add grouping + batch booking nodes
if (getPainXMLVersion(this._painFormat) === 3) {

var painVersion = getPainXMLVersion(this._painFormat);
if (painVersion === 3) {
r(pmtInf, 'BtchBookg', this.batchBooking.toString());
r(pmtInf, 'NbOfTxs', this.transactionCount);
r(pmtInf, 'CtrlSum', this.controlSum.toFixed(2));
Expand Down Expand Up @@ -368,11 +370,13 @@
r(pstl, 'AdrLine', this[pullFrom + 'City']);
}

var agentName = painVersion === 3 ? 'Agt' : 'Agnt';

r(pmtInf, emitterNodeName + 'Acct', 'Id', 'IBAN', this[pullFrom + 'IBAN']);
if (this[pullFrom + 'BIC']) {
r(pmtInf, emitterNodeName + 'Agt', 'FinInstnId', 'BIC', this[pullFrom + 'BIC']);
r(pmtInf, emitterNodeName + agentName, 'FinInstnId', 'BIC', this[pullFrom + 'BIC']);
} else {
r(pmtInf, emitterNodeName + 'Agnt', 'FinInstnId', 'Othr', 'Id', 'NOTPROVIDED');
r(pmtInf, emitterNodeName + agentName, 'FinInstnId', 'Othr', 'Id', 'NOTPROVIDED');
}

r(pmtInf, 'ChrgBr', 'SLEV');
Expand Down Expand Up @@ -463,6 +467,8 @@
var pullFrom = this._type === TransactionTypes.Transfer ? 'creditor' : 'debtor';
var receiverNodeName = this._type === TransactionTypes.Transfer ? 'Cdtr' : 'Dbtr';

var painVersion = getPainXMLVersion(this._painFormat);

var n = createXMLHelper(doc, true, false);
var o = createXMLHelper(doc, false, true);
var r = createXMLHelper(doc, true, true);
Expand Down Expand Up @@ -510,7 +516,10 @@
r(txInf, receiverNodeName + 'Acct', 'Id', 'IBAN', this[pullFrom + 'IBAN']);

r(txInf, 'RmtInf', 'Ustrd', this.remittanceInfo);
o(txInf, 'Purp', 'Cd', this.purposeCode);

if (painVersion !== 3) {
o(txInf, 'Purp', 'Cd', this.purposeCode);
}

return txInf;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Create SEPA XML for business transactions",
"license": "MPL-2.0",
"main":"lib/sepa.js",
"version": "1.0.3",
"version": "1.0.4",
"repository": {
"url": "https://github.com/CandisIO/sepa.js"
},
Expand Down

0 comments on commit 30882f0

Please sign in to comment.