2018-05-17 15:03:59 +00:00
'use strict' ;
2018-06-01 13:16:43 +00:00
const $request = require ( 'request' ) ;
2018-05-17 15:03:59 +00:00
2018-06-15 06:33:41 +00:00
const signer = require ( './middleware/signer' ) ;
const serializer = require ( './middleware/serializer' ) ;
const response = require ( './middleware/response' ) ;
2018-06-11 08:38:32 +00:00
2018-05-17 15:03:59 +00:00
module . exports = class Client {
2018-06-15 06:33:41 +00:00
constructor ( url ) {
2018-06-11 08:38:32 +00:00
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
async initialization ( order ) {
2018-06-11 12:25:07 +00:00
const res = await this . ebicsRequest ( order ) ;
2018-06-11 08:38:32 +00:00
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 12:25:07 +00:00
const res = await this . ebicsRequest ( 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 ( ) )
2018-06-11 12:25:07 +00:00
await this . ebicsRequest ( 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 12:25:07 +00:00
let res = await this . ebicsRequest ( order ) ;
2018-06-11 08:38:32 +00:00
const transactionId = res . transactionId ( ) ;
const orderId = res . orderId ( ) ;
order . transactionId = transactionId ;
2018-05-31 09:08:07 +00:00
2018-06-11 12:25:07 +00:00
res = await this . ebicsRequest ( 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-11 12:25:07 +00:00
ebicsRequest ( order ) {
2018-05-17 15:03:59 +00:00
return new Promise ( ( resolve , reject ) => {
2018-06-15 06:33:41 +00:00
const { version , keys } = order ;
// const s = signer.version(version).use(serializer.use(order).toXML(), keys).digest().sign().toXML(); // new (signer.version(version))(serializer.use(order).toXML(), keys).digest().sign().toXML();
2018-05-17 15:03:59 +00:00
$request . post ( {
2018-06-01 13:16:43 +00:00
url : this . url ,
2018-06-15 06:33:41 +00:00
body : signer . version ( version ) . sign ( serializer . use ( order ) . toXML ( ) , keys ) , // s, // new (signer.version(version))(serializer.use(order).toXML(), keys).digest().sign().toXML(),
2018-06-01 13:16:43 +00:00
headers : { 'content-type' : 'text/xml;charset=UTF-8' } ,
2018-06-15 06:33:41 +00:00
} , ( err , res , data ) => ( err ? reject ( err ) : resolve ( response . version ( version ) ( data , keys ) ) ) ) ;
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-15 06:33:41 +00:00
ini ( order ) {
return this . initialization ( order ) ;
}
payment ( order ) {
return this . upload ( order ) ;
}
statement ( order ) {
return this . download ( order ) ;
}
2018-05-17 15:03:59 +00:00
2018-06-15 06:33:41 +00:00
status ( order ) {
return this . download ( order ) ;
2018-05-17 15:03:59 +00:00
}
} ;