Skip to content

Commit

Permalink
对公有方法使用Promise包装以支持async/await #50
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayi.xu committed Dec 2, 2017
1 parent 8bb6151 commit a3d79a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,9 @@ var order = {
payment.getBrandWCPayRequestParams(order, function(err, payargs){
res.json(payargs);
});

// 也可以使用`async/await`形式
// let payargs = await payment.getBrandWCPayRequestParams(order)
```

注:
Expand Down
47 changes: 40 additions & 7 deletions lib/payment.js
Expand Up @@ -251,11 +251,11 @@ Payment.prototype._signedQuery = function(url, params, options, callback) {
}

var request = (options.https ? this._httpsRequest : this._httpRequest).bind(this);
request(url, this.buildXml(params), function(err, body) {
request(url, this._buildXml(params), function(err, body) {
if (err) {
return callback(err);
}
self.validate(body, callback);
self._validate(body, callback);
});

};
Expand Down Expand Up @@ -303,7 +303,7 @@ Payment.prototype.downloadBill = function(params, callback) {
}, function(err, rawData) {
if (err) {
if (err.name == 'XMLParseError') {
callback(null, self.parseCsv(rawData));
callback(null, self._parseCsv(rawData));
} else {
callback(err);
}
Expand All @@ -323,7 +323,7 @@ Payment.prototype.closeOrder = function(params, callback) {
}, callback);
};

Payment.prototype.parseCsv = function(text) {
Payment.prototype._parseCsv = function(text) {
var rows = text.trim().split(/\r?\n/);

function toArr(rows) {
Expand All @@ -347,7 +347,7 @@ Payment.prototype.parseCsv = function(text) {
};
};

Payment.prototype.buildXml = function(obj) {
Payment.prototype._buildXml = function(obj) {
var builder = new xml2js.Builder({
allowSurrogateChars: true
});
Expand All @@ -357,7 +357,7 @@ Payment.prototype.buildXml = function(obj) {
return xml;
};

Payment.prototype.validate = function(xml, callback) {
Payment.prototype._validate = function(xml, callback) {
var self = this;
xml2js.parseString(xml, {
trim: true,
Expand Down Expand Up @@ -463,4 +463,37 @@ Payment.prototype._generateNonceStr = function(length) {
return noceStr;
};

exports.Payment = Payment;
/**
* Promisify for public functions
*/
if (global.Promise) {
for (let key in Payment.prototype) {
let func = Payment.prototype[key]
if (typeof func == 'function' && key.indexOf('_') !== 0) {
Payment.prototype[key] = function () {
let args = Array.prototype.slice.call(arguments)
let originCallback = args[args.length - 1]
return new Promise((resolve, reject) => {
let handleResult = function (err, result) {
if (err) {
reject(err)
} else {
resolve(result)
}
}
if (typeof originCallback !== 'function') {
args.push(handleResult)
} else {
args[args.length - 1] = function (err, result) {
handleResult(err, result)
originCallback(err, result)
}
}
func.apply(this, args)
})
}
}
}
}

exports.Payment = Payment;
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "wechat-pay",
"version": "0.2.6",
"version": "0.3.0",
"description": "wechat payment api for document v3.3.5",
"main": "index.js",
"scripts":
Expand Down

0 comments on commit a3d79a0

Please sign in to comment.