forked from stavyan/TinyShop-UniApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment.js
More file actions
executable file
·270 lines (266 loc) · 6.05 KB
/
payment.js
File metadata and controls
executable file
·270 lines (266 loc) · 6.05 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
/* eslint-disable */
// #ifdef H5
import { wechatConfig } from '@/api/basic';
import $mConfig from '@/config/index.config';
import jweixin from '@/common/jweixin';
// #endif
import { isBindingCheck } from '@/api/login';
import { payCreate } from '@/api/basic';
import { http } from '@/utils/request';
import mHelper from '@/utils/helper';
import mRouter from '@/utils/router';
import { orderPay } from '@/api/product';
export default {
// 判断是否公众号(微信H5)
isWechat() {
// #ifdef H5
const ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/micromessenger/i) == 'micromessenger') {
return true;
} else {
return false;
}
// #endif
},
// wxjssdk
async wxConfigH5(url) {
if (this.isWechat()) {
const jsApiList = JSON.stringify([
'chooseWXPay',
'scanQRCode',
'updateAppMessageShareData',
'updateTimelineShareData'
]);
await http
.post(`${wechatConfig}`, {
url: url,
jsApiList, // 需要调用微信的原生方法
debug: false // 是否打开调试
})
.then(r => {
jweixin.config({
...r.data
});
});
}
},
/*
*@des 微信支付
*
*@param order_group 订单:order;充值:recharge;
*@param data 订单 {“order_id”:199} 充值 {“money”:100};
*/
async weixinPay(order_group, data, route = '/pages/user/money/success', code) {
// #ifdef H5
if (!this.isWechat()) {
mHelper.toast('仅支持微信H5、微信小程序、APP支付');
return;
}
// #endif
// #ifdef MP-QQ
mHelper.toast('QQ小程序暂不支持充值');
return;
// #endif
// #ifdef APP-PLUS
await http
.post(`${payCreate}`, {
order_group,
pay_type: 1,
trade_type: 'app',
data
})
.then(r => {
uni.requestPayment({
provider: 'wxpay', // 微信支付
orderInfo: JSON.stringify(r.data.config), //微信订单数据 r.data.config
success: function() {
mHelper.toast('支付成功');
mRouter.redirectTo({ route });
},
fail: function(err) {
console.log('err', err);
mHelper.toast('支付已取消');
}
});
});
// #endif
// #ifndef APP-PLUS
await http
.post(`${isBindingCheck}`, {
// #ifdef H5
oauth_client: 'wechat',
// #endif
// #ifdef MP-WEIXIN
oauth_client: 'wechatMp'
// #endif
})
.then(async res => {
if (res.data.openid) {
await http
.post(`${payCreate}`, {
order_group,
pay_type: 1,
// #ifdef H5
trade_type: 'js',
// #endif
// #ifdef MP-WEIXIN
trade_type: 'mini_program',
// #endif
data,
openid: res.data.openid
})
.then(r => {
// #ifdef H5
jweixin.ready(() => {
jweixin.chooseWXPay({
...r.data.config,
success() {
mHelper.toast('支付成功');
mRouter.redirectTo({ route });
},
fail(res) {
// 支付成功后的回调函数
mHelper.toast('支付已取消');
}
});
});
// #endif
// #ifdef MP-WEIXIN
uni.requestPayment({
...r.data.config,
timeStamp: r.data.config.timestamp,
success: () => {
mHelper.toast('支付成功');
mRouter.redirectTo({ route });
},
fail: res => {
mHelper.toast('支付已取消');
},
complete: () => {}
});
// #endif
});
} else {
// #ifdef MP-WEIXIN
uni.login({
provider: 'weixin',
success: function(loginRes) {
http
.post(`${payCreate}?code=${loginRes.code}`, {
order_group,
pay_type: 1,
trade_type: 'mini_program',
data
})
.then(r => {
uni.requestPayment({
...r.data.config,
timeStamp: r.data.config.timestamp,
success: () => {
mHelper.toast('支付成功');
mRouter.redirectTo({ route });
},
fail: res => {
mHelper.toast('支付已取消');
},
complete: () => {}
});
});
},
fail: function(err) {
mHelper.log(err);
}
});
// #endif
// #ifdef H5
await http
.post(`${payCreate}?code=${code}`, {
order_group,
pay_type: 1,
trade_type: 'js',
data
})
.then(r => {
jweixin.ready(() => {
jweixin.chooseWXPay({
...r.data.config,
success() {
mHelper.toast('支付成功');
mRouter.redirectTo({ route });
},
fail(res) {
// 支付成功后的回调函数
mHelper.toast('支付已取消');
}
});
});
});
// #endif
}
});
// #endif
},
/*
*@des 支付宝支付
*
*@param order_group 订单:order;充值:recharge;
*@param data 订单 {“order_id”:199} 充值 {“money”:100};
*/
async aliPay(order_group, data, route = '/pages/user/money/success') {
// #ifdef MP-QQ
mHelper.toast('QQ小程序不支持支付宝充值');
return;
// #endif
// #ifdef MP-WEIXIN
mHelper.toast('微信小程序不支持支付宝充值');
return;
// #endif
await http
.post(`${payCreate}`, {
order_group,
pay_type: 2,
// #ifdef H5
trade_type: 'wap',
// #endif
// #ifdef APP-PLUS
trade_type: 'app',
// #endif
data
})
.then(r => {
// #ifdef H5
window.location.href = r.data.config.config;
// #endif
// #ifdef APP-PLUS
uni.requestPayment({
provider: 'alipay',
orderInfo: r.data.config.config, //微信、支付宝订单数据
success: function() {
mHelper.toast('支付成功');
mRouter.redirectTo({ route });
},
fail: function(err) {
mHelper.toast('支付已取消');
}
});
// #endif
});
},
/*
*@des 余额支付
*
*@param data 支付参数
*/
async balancePay(data, route = '/pages/user/money/success') {
await http
.post(`${orderPay}`, {
order_group: 'order',
trade_type: 'js',
pay_type: 5,
data
})
.then(() => {
mRouter.redirectTo({ route });
});
}
};