forked from gooking/uni-app-mall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpay.js
More file actions
149 lines (147 loc) · 3.25 KB
/
Copy pathpay.js
File metadata and controls
149 lines (147 loc) · 3.25 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
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
const WXAPI = require('apifm-uniapp')
import store from '@/store'
/**
* provider 支付通道 alipay wxpay baidu appleiap
* postDataExt 需要额外提交的参数放这里,如果没有额外提交参数,填 {}
* money: 发起支付的金额
* payName: 弹起用户观看的付款信息
* remark 本次支付的备注信息,记录在后台支付记录的备注字段
* nextAction 下一步需要做的工作,充值请填 null
* successFuc 支付成功的回调函数
* failFuc 支付失败的回调函数
*/
function pay(provider, postDataExt, money, payName, remark, nextAction, successFuc, failFuc) {
const postData = {
token: store.state.token,
money,
payName,
remark,
...postDataExt
}
if (nextAction) {
postData.nextAction = JSON.stringify(nextAction)
}
// #ifdef H5
const ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
// 微信浏览器
WXAPI.wxpayJsapi(postData).then(res => {
if (res.code != 0) {
uni.showToast({
title: res.msg,
icon: 'none'
})
return
}
window.WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
'appId': res.data.appid,
'timeStamp': res.data.timeStamp,
'nonceStr': res.data.nonceStr,
'package': 'prepay_id=' + res.data.prepayId,
'signType': 'MD5',
'paySign': res.data.sign
},
function(res) {
if (res.err_msg === 'get_brand_wcpay_request:ok') {
successFuc()
} else {
failFuc()
}
})
})
return
} else {
// 普通浏览器
WXAPI.wxpayH5(postData).then(res => {
if (res.code != 0) {
uni.showToast({
title: res.msg,
icon: 'none'
})
return
}
window.location.href = res.data.mweb_url
})
return
}
// #endif
// #ifdef MP-WEIXIN
if (provider == 'wxpay') {
WXAPI.wxpay(postData).then(res => {
if (res.code != 0) {
uni.showToast({
title: res.msg,
icon: 'none'
})
return
}
uni.requestPayment({
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonceStr,
package: res.data.package,
signType: res.data.signType,
paySign: res.data.paySign,
fail: (err) => {
console.error(err);
failFuc(err)
},
success: () => {
successFuc()
}
})
})
}
// #endif
// #ifdef APP-PLUS
if (provider == 'wxpay') {
const res = WXAPI.wxpayApp(postData).then(res => {
if (res.code != 0) {
uni.showToast({
title: res.msg,
icon: 'none'
})
return
}
uni.requestPayment({
provider: 'wxpay', // alipay wxpay baidu appleiap
orderInfo: res.data, // https://uniapp.dcloud.io/api/plugins/payment?id=orderinfo
success: () => {
successFuc()
},
fail: (err) => {
console.error(err);
failFuc(err)
}
})
})
return
}
if (provider == 'alipay') {
const res = WXAPI.alipayAPP(postData).then(res => {
if (res.code != 0) {
uni.showToast({
title: res.msg,
icon: 'none'
})
return
}
uni.requestPayment({
provider: 'alipay', // alipay wxpay baidu appleiap
orderInfo: res.data.url, // https://uniapp.dcloud.io/api/plugins/payment?id=orderinfo
success: () => {
successFuc()
},
fail: (err) => {
console.error(err);
failFuc(err)
}
})
})
return
}
// #endif
}
module.exports = {
pay: pay
}