forked from thundernet8/AlipayOrdersSupervisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.js
49 lines (43 loc) · 1.78 KB
/
push.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var request = require('request');
var logger = require('./logger');
var crypto = require('crypto');
var Push = (function(){
var _push = function(apiUrl, appId, appKey, secret){
this.url = apiUrl + '?appId=' + appId + '&appKey=' + appKey + '&event=new_order';
this.secret = secret;
};
_push.prototype.pushState = function(orderData, callback){
// 签名
var md5 = crypto.createHash('md5');
var sig = [orderData.time.toString(), orderData.tradeNo.toString(), orderData.status.toString(), this.secret.toString()].join('|');
sig = md5.update(sig, 'utf8').digest('hex');
// Post body
orderData.sig = sig;
var form = orderData;
request.post(this.url, {timeout: 10000, rejectUnauthorized: false, form: form}, function(error, response, body){
if(error){
logger(error.code + ',' + error.message, 'pushError');
if(typeof(callback) == 'function'){
error.isError = true;
callback.call(this, error);
}
//return false;
}else if(!error && response.statusCode == 200){
logger('push order: ' + orderData.tradeNo + ' completed, Response: ' + body, 'push');
//console.log(body);
if(typeof(callback) == 'function'){
callback.call(this, body.toString());
}
// if(body == 'success'){
// return true;
// }else{
// return false;
// }
}else{
logger('push order: ' + orderData.tradeNo + ' completed, Response(Not 200 OK): ' + body, 'push');
}
});
};
return _push;
})();
module.exports = Push;