shopro-pay.js
7.2 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
/**
* ShoproPay v1.0.0
* @Class ShoproPay
* @description shopro-pay 1.0.0 统一支付
* @Author llidongtony
* @Date 2020-04-09
* @Email lidongtony@qq.com
*/
import api from '@/common/request/index'
import store from '@/common/store'
import tools from '@/common/utils/tools'
// #ifdef H5
import wxsdk from '@/common/wechat/sdk'
import Wechat from '@/common/wechat/wechat'
// #endif
import Router from '@/common/router';
export default class ShoproPay {
// wxOfficialAccount wxMiniProgram App H5
// wechat 公众号JSSDK支付 小程序支付 微信开放平台支付 H5网页支付
// alipay 复制网址 复制网址 支付宝开放平台支付 直接跳转链接
// wallet v v v v
constructor(payment, order) {
this.payment = payment;
this.order = order;
this.platform = uni.getStorageSync('platform');
let payMehod = this.getPayMethod();
payMehod();
}
getPayMethod() {
var payMethod = {
'wxOfficialAccount': {
'wechat': () => {
this.wxOfficialAccountPay()
},
'alipay': () => {
this.copyPayLink()
},
'wallet': () => {
this.walletPay()
}
},
'wxMiniProgram': {
'wechat': () => {
this.wxMiniProgramPay()
},
'alipay': () => {
this.copyPayLink()
},
'wallet': () => {
this.walletPay()
}
},
'App': {
'wechat': () => {
this.wechatPay()
},
'alipay': () => {
this.aliPay()
},
'wallet': () => {
this.walletPay()
},
},
'H5': {
'wechat': () => {
this.wechatWapPay()
},
'alipay': () => {
this.goToPayLink()
},
'wallet': () => {
this.walletPay()
},
},
}
return payMethod[this.platform][this.payment];
}
prepay() {
let that = this;
uni.showLoading({
title: '支付中',
mask: true
});
return new Promise((resolve, reject) => {
let that = this;
let params = {
order_sn: that.order.order_sn,
payment: that.payment
}
if (uni.getStorageSync('openid')) {
params.openid = uni.getStorageSync('openid');
}
api('pay.prepay', params).then(res => {
if (res.code === 1) {
if (res.data === 'no_openid') {
uni.showModal({
title: '微信支付',
content: '点击确定后请再次使用微信支付',
success: function(res) {
if (res.confirm) {
//静默获取openid
let wechat = new Wechat();
wechat.wxOfficialAccountBaseLogin();
} else if (res.cancel) {
console.log('用户点击取消');
}
},
fail: (err) => {
uni.hideLoading();
}
});
} else {
uni.hideLoading();
resolve(res);
}
} else {
uni.hideLoading();
uni.showToast({
title: res.msg,
icon: 'none'
})
}
})
});
}
async wxOfficialAccountPay() {
let that = this;
let result = await this.prepay();
wxsdk.wxpay(result.data.pay_data, (res) => {
if (res.errMsg == "chooseWXPay:ok") {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 1
}
});
} else if (res.errMsg === 'chooseWXPay:cancel') {
//取消支付
} else {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 0
}
});
}
});
}
//
async wechatWapPay() {
let that = this;
let result = await this.prepay();
if (result.code === 1) {
var url = result.data.pay_data.match(/url\=\'(\S*)\'/);
let reg = new RegExp('&', 'g') //g代表全部
let newUrl = url[1].replace(reg, '&');
let domain = uni.getStorageSync('sysInfo').domain; //域名需要https
let params = encodeURIComponent(
`${domain}/pages/order/payment/result?orderSn=${that.order.order_sn}&type=${that.payment}&pay=1`)
window.location.href = newUrl + '&redirect_url=' + params;
}
}
async wxMiniProgramPay() {
let that = this;
let result = await this.prepay();
let payData = result.data.pay_data;
uni.requestPayment({
provider: 'wxpay',
timeStamp: payData.timeStamp,
nonceStr: payData.nonceStr,
package: payData.package,
signType: payData.signType,
paySign: payData.paySign,
success: function(res) {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 1
}
});
},
fail: function(err) {
if (err.errMsg !== "requestPayment:fail cancel") {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 0
}
});
}
}
});
}
async walletPay() {
let that = this;
let result = await this.prepay();
if (result.code === 1) {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 1
}
});
}
}
async copyPayLink() {
let that = this;
let result = await this.prepay();
if (result.code === 1) {
//引入showModal 点击确认 复制链接;
uni.showModal({
title: '支付宝支付',
content: '复制链接到外部浏览器',
confirmText: '复制链接',
success: (res) => {
if (res.confirm) {
uni.setClipboardData({
data: result.data.pay_data,
success: function(data) {
//#ifdef H5
tools.toast('已复制到剪切板');
//#endif
},
fail: function(err) {},
complete: function(res) {}
});
} else if (res.cancel) {
}
}
})
}
}
async goToPayLink() {
let that = this;
let result = await this.prepay();
if (result.code === 1) {
window.location = result.data.pay_data;
}
}
async aliPay() {
let that = this;
let result = await this.prepay();
if (result.code === 1) {
uni.requestPayment({
provider: 'alipay',
orderInfo: result.data.pay_data, //支付宝订单数据
success: function(res) {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 1
}
});
},
fail: function(err) {
if (err.errMsg !== "requestPayment:fail cancel") {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 0
}
});
}
}
});
}
}
async wechatPay() {
let that = this;
let result = await this.prepay();
if (result.code === 1) {
uni.requestPayment({
provider: 'wxpay',
orderInfo: JSON.parse(result.data.pay_data), //微信订单数据(官方说是string。实测为object)
success: function(res) {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 1
}
});
},
fail: function(err) {
if (err.errMsg !== "requestPayment:fail cancel") {
Router.replace({
path: '/pages/order/payment/result',
query: {
orderSn: that.order.order_sn,
type: that.payment,
pay: 0
}
});
}
}
});
}
}
}