diff --git a/demo/index.js b/demo/index.js index 27da5eb..83d8e8c 100644 --- a/demo/index.js +++ b/demo/index.js @@ -258,9 +258,9 @@ app.get('/download-bill', function (req, res) { const query = req.query; wx.payment.downloadBill(query.billDate, Payment.DOWNLOAD_BILL_TYPE.SUCCESS) .then(result => { - console.log('digest: ', result.digest); - if(result.data) { - result.data.pipe(res); + if(result.body) { + res.type('zip'); + res.send(result.body); } }) .catch(err => { diff --git a/lib/Payment.js b/lib/Payment.js index d8af5c8..6acd78f 100644 --- a/lib/Payment.js +++ b/lib/Payment.js @@ -415,7 +415,8 @@ class Payment { ); return this.download(data, { gzip: !noGzip, - }); + encoding: null, // get zip file as buffer + }, this.paymentUrls.DOWNLOAD_BILL); } /** @@ -440,13 +441,14 @@ class Payment { return this.download(data, { agentOptions: this.getAgentOptions(), gzip: !noGzip, - }); + }, this.paymentUrls.DOWNLOAD_FUND_FLOW); } /** * Download functionality * @param {object} data request data * @param {object=} requestOptions options send to request + * @param url to wechat api endpoint * @return {Promise} resolve or reject with: * { * //an error object, rejected @@ -460,7 +462,7 @@ class Payment { * digest: 'SHA=ec45d7c24492dcd62d92472b0f2816c8d9a2d773', * } */ - download(data, requestOptions) { + download(data, requestOptions, url) { return utils.buildXML(data).then(xmlData => { const myOptions = Object.assign( { @@ -468,7 +470,7 @@ class Payment { json: false, gzip: true, method: 'POST', - url: this.paymentUrls.DOWNLOAD_FUND_FLOW, + url, body: xmlData, }, requestOptions @@ -502,6 +504,7 @@ class Payment { return resolve({ //return the request stream data: stream, + body, digest: response.headers['digest'], }); });