This repository was archived by the owner on Mar 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (71 loc) · 2.47 KB
/
index.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
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
const assert = require('power-assert');
const util = require('./util');
const Config = require('./config');
const { CODE, MSG } = require('./constant');
class WechatPay {
constructor(options = {}) {
assert(options.appid);
assert(options.mchId);
assert(options.secretKey);
this.appid = options.appid;
this.mch_id = options.mchId;
this.secretKey = options.secretKey;
}
createAppOrder(appParams) {
const basicParams = {
appid: this.appid,
mch_id: this.mch_id,
nonce_str: util.rand(),
trade_type: 'APP',
};
const mergeParams = Object.assign({}, basicParams, appParams);
const sign = util.makeSign(this.secretKey, mergeParams);
mergeParams.sign = sign;
return util.makeRequest(Config.UNIFIED_ORDER, mergeParams);
}
signPrePayOrder(prepayId, nonceStr, timestamp) {
const params = {
appid: this.appid,
partnerid: this.mch_id,
prepayid: prepayId,
package: 'Sign=WXPay',
noncestr: nonceStr,
timestamp,
}
const sign = util.makeSign(this.secretKey, params);
return { code: CODE.SUCCESS, msg: MSG.SUCCESS, data: { sign } }
}
verifyNotifyResponse(params) {
const { sign } = params;
delete params.sign;
const signResult = util.makeSign(this.secretKey, params);
if (signResult === sign) {
return { code: CODE.SUCCESS, msg: MSG.SUCCESS };
} else {
return { code: CODE.FAIL, msg: MSG.SIGN_ERROR };
}
}
queryorder(appParams) {
const basicParams = {
appid: this.appid,
mch_id: this.mch_id,
nonce_str: util.rand(),
}
const mergeParams = Object.assign({}, basicParams, appParams);
const sign = util.makeSign(this.secretKey, mergeParams);
mergeParams.sign = sign;
return util.makeRequest(Config.QUERY_ORDER, mergeParams);
}
// refund(appParams) {
// const basicParams = {
// appid: this.appid,
// mch_id: this.mch_id,
// nonce_str: util.rand(),
// }
// const mergeParams = Object.assign({}, basicParams, appParams);
// const sign = util.makeSign(this.secretKey, mergeParams);
// mergeParams.sign = sign;
// return util.makeRequest(Config.REFUND, mergeParams);
// }
}
module.exports = WechatPay;