-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat_pay.js
182 lines (159 loc) · 4.72 KB
/
wechat_pay.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// https://github.com/node-weixin/node-weixin-pay
import wechatAuth from './wechat_auth.js'
import utils from './wechat_util.js'
import wechatRequest from './wechat_request.js'
var xml = require('xml')
const config = Meteor.settings.private.wechat
if (!config) {
console.log('error', 'Please Add wechat_open setting.')
}
WechatPay = {}
const crypto = require('crypto')
const baseUrl = 'https://api.weixin.qq.com/cgi-bin/ticket/'
const TICKET_EXP = 7200 * 1000
var userAgent = 'Meteor'
if (Meteor.release)
userAgent += '/' + Meteor.release
WechatPay.init = function (pfx) {
config.certificate = {
pfx: pfx,
pfxKey: config.merchant.id
}
}
WechatPay.unified = function (data, callback) {
data.device_info = 'WEB'
data.trade_type = 'JSAPI'
data.appid = config.app.id
/* eslint camelcase: [2, {properties: "never"}] */
data.mch_id = config.merchant.id
pay.request('https://api.mch.weixin.qq.com/pay/unifiedorder', data, (error, result) => {
if (error) {
if (callback)
callback(error, result)
return
}
var ret = pay.prepay(config.app, config.merchant, result.prepay_id);
if (callback)
callback(null, ret)
})
}
WechatPay.transfer = function (data, callback) {
//企业付款
data.mch_appid = config.app.id
data.mchid = config.merchant.id
pay.request('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', data, (error, result) => {
if (error) {
if (callback)
callback(error, result)
return
}
console.log(error);
console.log(result);
if (callback)
callback(null, result)
})
}
WechatPay.sendredpack = function (data, callback) {
//发送普通红包
data.wxappid = config.app.id
data.mch_id = config.merchant.id
pay.request('https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack', data, (error, result) => {
if (error) {
if (callback)
callback(error, result)
return
}
console.log(error);
console.log(result);
if (callback)
callback(null, result)
})
}
WechatPay.query = function (data, cb) {
var url = 'https://api.mch.weixin.qq.com/pay/orderquery'
pay.request(url, data, cb)
}
WechatPay.close = function (data, cb) {
var url = 'https://api.mch.weixin.qq.com/pay/closeorder'
pay.request(url, data, cb)
}
WechatPay.createRefund = function (data, cb) {
var url = 'https://api.mch.weixin.qq.com/secapi/pay/refund'
pay.request(url, data, cb)
}
WechatPay.queryRefund = function (data, cb) {
var url = 'https://api.mch.weixin.qq.com/pay/refundquery'
pay.request(url, data, cb)
}
WechatPay.statements = function (data, cb) {
var url = 'https://api.mch.weixin.qq.com/pay/downloadbill'
pay.request(url, data, cb)
}
WechatPay.report = function (data, cb) {
var url = 'https://api.mch.weixin.qq.com/payitil/report'
pay.request(url, data, cb)
}
var pay = {
/**
* Basic http request wrapper for pay apis, which need to be encrypted and verified for their data format
*
* @param url Requesting url
* @param data Data to be sent
* @param sendConfig Sending data validation configuration
* @param receiveConfig Receiving data validation configuration
* @param certificate Certificate from Tencent Pay
* @param cb Callback Function
*/
request: function (url, data, cb) {
var error = {}
var params = _.clone(data)
params.nonce_str = utils.getNonce()
var sign = pay.sign(config.merchant, params)
params.sign = sign
console.log(params);
var xml = utils.toXml(params)
wechatRequest.xmlssl(url, xml, config.certificate, cb)
},
/**
* Sign all data with merchant key
*
* @param merchant
* @param params
* @returns {string}
*/
sign: function (merchant, params) {
var temp = utils.marshall(params)
temp += '&key=' + String(merchant.key)
temp = new Buffer(temp)
temp = temp.toString('binary')
var crypt = crypto.createHash('MD5')
crypt.update(temp)
return crypt.digest('hex').toUpperCase()
},
/**
* Make prepay data for jssdk
*
* @param app
* @param merchant
* @param prepayId
* @returns {{appId: *, timeStamp: string, nonceStr, packagee: string, signType: string}}
*/
prepay: function (app, merchant, prepayId) {
var crypto = require('crypto')
var md5 = crypto.createHash('md5')
var timeStamp = String(new Date().getTime())
md5.update(timeStamp)
timeStamp = Math.floor(timeStamp / 1000)
var nonceStr = md5.digest('hex')
var data = {
appId: app.id,
timeStamp: String(timeStamp),
nonceStr: nonceStr,
package: 'prepay_id=' + prepayId,
signType: 'MD5'
}
data.paySign = this.sign(merchant, data)
return data
}
}
export default WechatPay