~wechat.js
2.9 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/7/22
*/
'use strict';
const moment = require('moment');
const rq = require('request-promise');
const common = require('./common');
const sign = require('./sign');
const md5 = require('md5');
const _ = require('lodash');
const logger = global.yoho.logger;
const config = global.yoho.config;
const wechatUnifiedOrderUrl = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
const Wechat = {
pay(user, order, info) {
let expire = common.getPayExpireCouple(order.payExpire);
info = JSON.parse(info.payParams || '{}');
let unifiedOrder = {
body: 'BLK订单号:' + order.orderCode,
out_trade_no: order.orderCode,
total_fee: parseInt(order.paymentAmount * 100, 10),
time_start: moment(expire.start).format('YYYYMMDDHHmmss'),
time_expire: moment(expire.end).format('YYYYMMDDHHmmss'),
trade_type: 'NATIVE',
product_id: order.orderCode,
notify_url: config.pay.serviceNotify + 'payment/weixin_notify',
appid: info.app_id,
mch_id: info.partner_id,
nonce_str: common.nonceStr()
};
let signStr = md5(sign.raw(unifiedOrder) + '&key=' + info.partner_key);
unifiedOrder.sign = _.toUpper(signStr);
return this.unifiedOrder(unifiedOrder).then(result => {
if (result && result.xml) {
let data = result.xml || {};
if (data.return_code === 'SUCCESS' && Wechat.checkSign(data, info)) {
return {
code: 200,
data: {
href: data.code_url
}
};
}
}
return {
code: 400,
message: '请稍后重试'
};
});
},
unifiedOrder(unifiedOrder) {
let xml = common.toXml(unifiedOrder);
return rq({
method: 'POST',
uri: wechatUnifiedOrderUrl,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(xml)
},
body: xml,
timeout: 1000
}).then(result => {
if (result) {
return common.xml2Obj(result);
} else {
return {};
}
}).catch(err => {
logger.error(`call wechat pay unifiedOrder fail. order=${JSON.stringify(unifiedOrder)}`, err);
return {};
});
},
notify(data, info) { // eslint-disable-line
},
checkSign(data, info) {
if (data && data.sign) {
let signStr = data.sign || '';
delete data.sign;
return signStr === md5(sign.raw(data) + info.partner_key);
}
return false;
}
};
module.exports = Wechat;