2018-05-17 15:03:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
// const fs = require('fs');
|
|
|
|
// const path = require('path');
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const $request = require('request');
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const XMLSign = require('./middleware/XMLSign');
|
2018-05-17 15:03:59 +00:00
|
|
|
const ParseResponse = require('./middleware/ParseResponse');
|
2018-06-11 08:38:32 +00:00
|
|
|
// const BankLetter = require('./BankLetter');
|
|
|
|
|
|
|
|
const OrderSerializer = require('./orders/H004/OrderSerializer');
|
|
|
|
|
|
|
|
|
|
|
|
/* const defaultIniTemplatePath = path.join(__dirname, '../templates/ini.hbs');
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
const utils = {
|
2018-06-11 08:38:32 +00:00
|
|
|
mapObject: (o = {}, predicate = v => v) => Object.entries(o).reduce((r, [key, value]) => { r[key] = value; return r; }, o),
|
2018-05-17 15:03:59 +00:00
|
|
|
exponent: {
|
|
|
|
// str = 65537 => AQAB
|
|
|
|
toBase64(str) {
|
|
|
|
return new BN(str).toBuffer().toString('base64');
|
|
|
|
},
|
|
|
|
// str = AQAB => 65537
|
|
|
|
fromBase64(str) {
|
|
|
|
return new BN(Buffer.from(str, 'base64'), 2).toNumber();
|
2018-06-01 13:16:43 +00:00
|
|
|
},
|
|
|
|
},
|
2018-06-11 08:38:32 +00:00
|
|
|
}; */
|
|
|
|
|
2018-05-17 15:03:59 +00:00
|
|
|
module.exports = class Client {
|
2018-06-11 08:38:32 +00:00
|
|
|
/* constructor({
|
|
|
|
url,
|
|
|
|
hostId,
|
|
|
|
userId,
|
|
|
|
partnerId,
|
|
|
|
keyManager = new FsKeyManager({ path: './keys.ebics', passphrase: 'node-ebics' }),
|
|
|
|
}) {
|
2018-06-01 13:16:43 +00:00
|
|
|
this.url = url;
|
|
|
|
this.hostId = hostId;
|
|
|
|
this.userId = userId;
|
|
|
|
this.partnerId = partnerId;
|
2018-06-11 08:38:32 +00:00
|
|
|
this.keyManager = keyManager;
|
|
|
|
} */
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
constructor({ url }) {
|
|
|
|
this.url = url;
|
2018-06-01 13:16:43 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
/*
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
saveIniLetter(bankName, target, template) {
|
|
|
|
const letter = new BankLetter({
|
|
|
|
client: this,
|
|
|
|
bankName,
|
|
|
|
template: template || fs.readFileSync(defaultIniTemplatePath, { encoding: 'utf8' }),
|
|
|
|
});
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
try {
|
2018-06-01 13:16:43 +00:00
|
|
|
fs.writeFileSync(target, letter.generate());
|
2018-05-17 15:03:59 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
2018-06-01 13:16:43 +00:00
|
|
|
}
|
2018-06-11 08:38:32 +00:00
|
|
|
*/
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
async initialization(order) {
|
|
|
|
const res = await this.ebicsRequest(OrderSerializer.serialize(order));
|
|
|
|
const xml = res.orderData();
|
2018-06-01 13:16:43 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
return {
|
|
|
|
orderData: xml,
|
|
|
|
orderId: res.orderId(),
|
|
|
|
returnCode: res.returnCode(),
|
|
|
|
reportText: res.reportText(),
|
|
|
|
bankKeys: res.bankKeys(),
|
|
|
|
};
|
2018-05-17 15:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async download(order) {
|
2018-06-11 08:38:32 +00:00
|
|
|
const res = await this.ebicsRequest(OrderSerializer.serialize(order));
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
order.transactionId = res.transactionId();
|
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
if (res.isSegmented() && res.isLastSegment())
|
|
|
|
await this.ebicsRequest(OrderSerializer.serialize(order));
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
// return res.orderData();
|
|
|
|
return {
|
|
|
|
orderData: res.orderData(),
|
|
|
|
orderId: res.orderId(),
|
|
|
|
returnCode: res.returnCode(),
|
|
|
|
reportText: res.reportText(),
|
|
|
|
};
|
2018-06-01 13:16:43 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-05-31 09:08:07 +00:00
|
|
|
async upload(order) {
|
2018-06-11 08:38:32 +00:00
|
|
|
let res = await this.ebicsRequest(OrderSerializer.serialize(order));
|
|
|
|
const transactionId = res.transactionId();
|
|
|
|
const orderId = res.orderId();
|
|
|
|
|
|
|
|
order.transactionId = transactionId;
|
2018-05-31 09:08:07 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
res = await this.ebicsRequest(OrderSerializer.serialize(order));
|
2018-05-31 09:08:07 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
return [transactionId, orderId];
|
2018-05-31 09:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
async downloadAndUnzip(order) { // eslint-disable-line
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
ebicsRequest(serializedOrder) {
|
|
|
|
const { keys } = serializedOrder;
|
|
|
|
|
2018-05-17 15:03:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-06-11 08:38:32 +00:00
|
|
|
const s = XMLSign.go(keys, serializedOrder.toXML());
|
2018-05-17 15:03:59 +00:00
|
|
|
$request.post({
|
2018-06-01 13:16:43 +00:00
|
|
|
url: this.url,
|
2018-06-11 08:38:32 +00:00
|
|
|
body: s, // XMLSign.go(this, serializedOrder),
|
2018-06-01 13:16:43 +00:00
|
|
|
headers: { 'content-type': 'text/xml;charset=UTF-8' },
|
2018-06-11 08:38:32 +00:00
|
|
|
}, (err, res, data) => (err ? reject(err) : resolve(ParseResponse.go(keys, data))));
|
2018-05-17 15:03:59 +00:00
|
|
|
});
|
2018-06-01 13:16:43 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
request(order) {
|
|
|
|
if (order.type.toLowerCase() === 'ini') return this.initialization(order);
|
|
|
|
if (order.type.toLowerCase() === 'payment') return this.upload(order);
|
|
|
|
if (order.type.toLowerCase() === 'status') return this.download(order);
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
throw Error('Invalid order type');
|
2018-05-17 15:03:59 +00:00
|
|
|
}
|
|
|
|
};
|