request-payment.js
1.24 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
const paymentUrl = require('../../../../config').paymentUrl
var app = getApp()
Page({
onLoad: function() {
},
requestPayment: function() {
var self = this
self.setData({
loading: true
})
// 此处需要先调用wx.login方法获取code,然后在服务端调用微信接口使用code换取下单用户的openId
// 具体文档参考https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html?t=20161230#wxloginobject
app.getUserOpenId(function(err, openid) {
if (!err) {
wx.request({
url: paymentUrl,
data: {
openid
},
method: 'POST',
success: function(res) {
console.log('unified order success, response is:', res)
var payargs = res.data.payargs
wx.requestPayment({
timeStamp: payargs.timeStamp,
nonceStr: payargs.nonceStr,
package: payargs.package,
signType: payargs.signType,
paySign: payargs.paySign
})
self.setData({
loading: false
})
}
})
} else {
console.log('err:', err)
self.setData({
loading: false
})
}
})
}
})