payUtil.js
10.9 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @FileDescription:
* @Author: kahu
* @Date: 2022/11/2
* @LastEditors: kahu
* @LastEditTime: 2022/11/2
*/
const NET = require('./request')
const API = require('../config/api')
// #ifdef H5
const jweixin = require('jweixin-module')
/**
* 普通H5处理
* @param payInfo 结算返回的支付信息
*/
async function payH5InEquipment(payInfo) {
try {
console.log('11111111111111111111111111111')
// uni.navigateTo({
// url: '/pages_category_page1/orderModule/index?type=2'
// })
// NET.request(API.gotoH5Pay, payInfo, 'POST').then(res1=>{
console.log('支付参数',payInfo)
NET.request(API.Wenlv, payInfo, 'POST').then(res1=>{
NET.request(API.Gethu,payInfo, 'POST').then(res=>{
// location.replace(res1.data.mwebUrl) 老
location.replace(res1.data.codeUrl)
}).catch(err=>{
console.log(err)
})
})
} catch (e) {
this.submitActive = true
uni.showToast({
title: '支付失败',
icon: 'none'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
} finally {
console.log('33333')
uni.hideLoading()
}
}
/**
* 微信内H5处理
* @param payInfo 结算返回的支付信息
* @param orderId 订单ID
*/
async function payH5InWechat(payInfo) {
payInfo.paymentMode = 1
const res = await NET.request(API.gotoPay, payInfo, 'POST')
jweixin.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: res.data.appId, // 必填,公众号的唯一标识
timestamp: res.data.timeStamp, // 必填,生成签名的时间戳
nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
signature: res.data.paySign, // 必填,签名,见附录1
jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
jweixin.ready(function () {
jweixin.checkJsApi({
jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function (res) {
},
fail: function (res) {
}
});
jweixin.chooseWXPay({
timestamp: res.data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
package: res.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
signType: res.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: res.data.paySign, // 支付签名
totalFee: res.data.money,
success: function (res) {
// 支付成功后的回调函数
uni.showToast({
icon: 'none',
title: '支付成功'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
})
},
cancel: function (r) {
uni.showToast({
icon: 'none',
title: '用户取消支付'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
},
fail: function (res) {
uni.showToast({
icon: 'none',
title: '微信内支付错误'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
}
});
});
jweixin.error(function (res) {
uni.showToast({
icon: 'none',
title: '微信内支付加载失败',
duration: 3000
});
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
});
}
/**
* H5拉起支付
* @param payInfo 结算返回的支付信息
*/
async function h5Pay(payInfo) {
let ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
console.log('1111111111111111111111')
await payH5InWechat(payInfo)
} else {
console.log('22222222222222')
await payH5InEquipment(payInfo)
}
}
// #endif
// #ifdef MP-ALIPAY
/**
* 支付宝小程序拉起支付
* @param payInfo 结算返回的支付信息
* @return {Promise<void>}
*/
async function aliPay(payInfo) {
try {
const res = await NET.request(API.gotoPay, payInfo, 'POST')
uni.requestPayment({
provider: 'alipay',
orderInfo: res.data.tradeNo,
success: function (payRes) {
if (payRes.resultCode == '6001') {
uni.showToast({
icon: 'none',
title: '取消支付'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
}
if (payRes.resultCode == '9000') {
uni.showToast({
icon: 'none',
title: '支付成功'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + orderId
})
}
},
fail: function (err) {
uni.showToast({
icon: 'none',
title: '支付取消'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
}
});
} catch (e) {
uni.showToast({
title: '支付宝支付异常',
icon: 'none'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
} finally {
uni.hideLoading()
}
}
// #endif
// #ifdef MP-WEIXIN
/**
* 微信小程序拉起支付
* @param payInfo
* @return {Promise<void>}
*/
async function wechatPay(payInfo) {
console.log('进入微信支付')
try {
const res = await NET.request(API.gotoPay, payInfo, 'POST')
uni.requestPayment({
provider: 'wxpay',
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonceStr,
package: res.data.package,
signType: res.data.signType,
paySign: res.data.paySign,
success: async (payRes) => {
// 拼团微信支付成功回调
if (payInfo.collageId) {
await NET.request(API.paySuccess, {
orderId: payInfo.orderId,
collageId: payInfo.collageId
}, 'POST')
}
uni.showToast({
icon: 'none',
title: '支付成功'
})
//console.log(submitResult.orderId, 'order Id')
uni.navigateTo({
url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
})
},
fail: function (err) {
uni.showToast({
icon: 'none',
title: '用户取消支付'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
}
})
} catch (e) {
uni.showToast({
title: '微信支付拉起失败',
icon: 'none'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
}
}
// #endif
// #ifdef APP-PLUS
/**
* App拉起微信支付
* @param payInfo
* @return {Promise<void>}
*/
async function appWechatPay(payInfo) {
try {
const res = await NET.request(API.gotoAppPay, payInfo, 'POST')
const obj = {
appid: res.data.appId,
noncestr: res.data.nonceStr,
package: 'Sign=WXPay',
prepayid: res.data.prepayId,
timestamp: res.data.timeStamp,
sign: res.data.paySign,
partnerid: res.data.partnerId
}
uni.requestPayment({
provider: 'wxpay',
orderInfo: obj,
success: function (payRes) {
uni.showToast({
icon: 'none',
title: '支付成功'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
})
},
fail: function (err) {
uni.showToast({
icon: 'none',
title: '用户取消支付'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
}
})
} catch (e) {
console.log(e)
uni.showToast({
title: 'APP拉起微信支付失败',
icon: 'none'
})
uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=1'
})
} finally {
uni.hideLoading()
}
}
// #endif
/**
* 处理支付
* @param submitResult 结算结果
*/
export async function handleDoPay(submitResult) {
uni.showLoading({
mask: true,
title: '支付中...',
})
console.log('进入支付')
const {paymentMode} = submitResult
if (paymentMode === 1) {
// 微信支付
// #ifdef APP-PLUS
await appWechatPay(submitResult)
// #endif
// #ifdef MP-WEIXIN
console.log('进入微信支付')
await wechatPay(submitResult)
// #endif
// #ifdef H5
await h5Pay(submitResult)
// #endif
} else if ([2, 3].includes(paymentMode)) {
// 支付宝
// #ifdef MP-ALIPAY
await aliPay(submitResult)
// #endif
// #ifndef MP-ALIPAY
// await appWechatPay(submitResult,this.orderId)
console.error("支付宝相关支付暂时只支持支付宝小程序")
throw new Error('支付宝相关支付暂时只支持支付宝小程序')
// #endif
}
setTimeout(()=>{
// uni.navigateTo({
// url: '/pages_category_page1/orderModule/index?type=2'
// })
// 修改过
uni.switchTab({
url: '/pages/tabbar/user/index'
})
},1000)
/* uni.navigateTo({
url: '/pages_category_page1/orderModule/index?type=2'
})*/
// uni.hideLoading()
}