Voyz's Studio.

详解小程序如何接入微信支付(云函数)

字数统计: 847阅读时长: 4 min
2019/01/02 Share

小程序接入微信支付.jpg

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
183
184
185
186
187
188
npm install request
npm install wx-server-sdk


pages/order/order.js

toPay() {
var that = this
if (that.data.hasAddress) {
// 地址信息
console.log(that.data.address)
// 总价信息
console.log(that.data)
// 支付后订单信息
that.getListAfterPay(that)

// 获取prepay_id,所需的签名字符串
var p = new Promise((resolve,reject)=>{
// 生成订单号
var out_trade_no = (new Date().getTime() + app.RndNum(6)).toString()

// 生成字符串
var stringA =
"appid="+app.globalData.appid
+ "&attach=test"
+ "&body=JSAPItest"
+ "&device_info=WEB"
+ "&mch_id="+app.globalData.mch_id
+ "&nonce_str="+that.data.nonce_str
+ "&notify_url=http://www.weixin.qq.com/wxpay/pay.php"
+ "&openid="+that.data.openid
+ "&out_trade_no="+out_trade_no
+ "&spbill_create_ip="+that.data.spbill_create_ip
+ "&time_expire="+app.beforeNowtimeByMin(-15)
+ "&time_start="+app.CurrentTime()
+ "&total_fee="+parseInt(that.data.total*100)
+ "&trade_type=JSAPI";

var stringSignTemp = stringA +"&key="+app.globalData.apikey
// 签名MD5加密
var sign = md5.md5(stringSignTemp).toUpperCase()
// console.log("签名:" + stringSignTemp)

// openid
var openid = that.data.openid

resolve([sign,openid,out_trade_no])
})
p.then(e => {
// 生成获取prepay_id请求的xml参数
var xmlData = '<xml>'+
'<appid>'+app.globalData.appid+'</appid>'+
'<attach>test</attach>'+
'<body>JSAPItest</body>'+
'<device_info>WEB</device_info>'+
'<mch_id>'+app.globalData.mch_id+'</mch_id>' +
'<nonce_str>'+that.data.nonce_str+'</nonce_str>' +
'<notify_url>http://www.weixin.qq.com/wxpay/pay.php</notify_url>' +
'<openid>'+that.data.openid+'</openid>'+
'<out_trade_no>'+e[2]+'</out_trade_no>'+
'<spbill_create_ip>'+that.data.spbill_create_ip+'</spbill_create_ip>'+
'<time_expire>'+app.beforeNowtimeByMin(-15)+'</time_expire>'+
'<time_start>'+app.CurrentTime()+'</time_start>'+
'<total_fee>'+parseInt(that.data.total * 100)+'</total_fee>'+
'<trade_type>JSAPI</trade_type>'+
'<sign>'+e[0]+'</sign>'+
'</xml>'

// console.log(xmlData)

// 获取prepay_id,发送支付请求
wx.cloud.callFunction({
name:'pay',
data:{
xmlData:xmlData
}
})
.then(res=>{
if(res){
var prepay_id = res.result.body.split("<prepay_id><![CDATA[")[1].split("]]></prepay_id>")[0];
var timeStamp = Math.round((Date.now() / 1000)).toString()
var nonceStr = app.RndNum()
var stringB =
"appId=" + app.globalData.appid
+ "&nonceStr=" + nonceStr
+ "&package=" + 'prepay_id=' + prepay_id
+ "&signType=MD5"
+ "&timeStamp=" + timeStamp
var paySignTemp = stringB + "&key=" + app.globalData.apikey
// 签名MD5加密
var paySign = md5.md5(paySignTemp).toUpperCase()
// 调起请求
wx.requestPayment({
appId: app.globalData.appid,
timeStamp: timeStamp,
nonceStr: nonceStr,
package: 'prepay_id=' + prepay_id,
signType: 'MD5',
paySign: paySign,
success: function (e) {
console.log(e)
}
})
}
})
.catch(err=>{
console.log(err)
})

// end获取openid
})

// end if 地址
}

else{
wx.showModal({
title: 'Oh No',
content: '请填写收货地址~',
})
}
}

cloudfunctions/pay/index.js

// ------获取prepay_id-------
// 云函数入口文件
const cloud = require('wx-server-sdk')
const request = require('request')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
let result = await getPrepayIdPromise(event);
return result
}

const getPrepayIdPromise = function (event) {
return new Promise((resolve, reject) => {
let options = {
"url": 'https://api.mch.weixin.qq.com/pay/unifiedorder',
"method": "POST",
"headers": {
"Content-Type": "text/xml",
"charset": "utf-8"
},
"form": event.xmlData
};
request.post(options, (err, result, body) => {
resolve({
err: err,
body: body
})
})
})
}


cloudfunctions/getIP/index.js


//-------获取IP地址------
// 云函数入口文件
const cloud = require('wx-server-sdk')
const request = require('request')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
let result = await getIPAddressPromise();
return result
}

const getIPAddressPromise = function () {
return new Promise((resolve, reject) => {
let options = {
"url": 'http://ip-api.com/json'
};
request.post(options, (err, result, body) => {
resolve({
err: err,
body: body
})
})
})
}
CATALOG