Skip to content

Commit

Permalink
Fix broken downloadBill (#23)
Browse files Browse the repository at this point in the history
* use download bill api end point, and include buffer in return

* update demo accordingly

* typo

* remove default parameter as it's broke in node 4
  • Loading branch information
lk9100 authored and JasonBoy committed Aug 8, 2018
1 parent 26d1c06 commit 5a21158
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
11 changes: 7 additions & 4 deletions lib/Payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ class Payment {
);
return this.download(data, {
gzip: !noGzip,
});
encoding: null, // get zip file as buffer
}, this.paymentUrls.DOWNLOAD_BILL);
}

/**
Expand All @@ -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
Expand All @@ -460,15 +462,15 @@ class Payment {
* digest: 'SHA=ec45d7c24492dcd62d92472b0f2816c8d9a2d773',
* }
*/
download(data, requestOptions) {
download(data, requestOptions, url) {
return utils.buildXML(data).then(xmlData => {
const myOptions = Object.assign(
{
strictSSL: false,
json: false,
gzip: true,
method: 'POST',
url: this.paymentUrls.DOWNLOAD_FUND_FLOW,
url,
body: xmlData,
},
requestOptions
Expand Down Expand Up @@ -502,6 +504,7 @@ class Payment {
return resolve({
//return the request stream
data: stream,
body,
digest: response.headers['digest'],
});
});
Expand Down

0 comments on commit 5a21158

Please sign in to comment.