Pay.php
16.4 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace app\api\controller;
use app\common\controller\Api;
use EasyWeChat\Factory;
use function GuzzleHttp\Promise\inspect;
use think\Db;
use app\admin\model\User;
use think\Log;
use fast\Http;
use think\Validate;
use Exception;
use function EasyWeChat\Kernel\Support\generate_sign;
/**
* 支付接口
*/
class Pay extends Api
{
protected $noNeedRight = ['*'];
protected $uid = '';//token存贮uid
protected $order_status = [];//订单状态
public function _initialize()
{
parent::_initialize();
$this->uid = $this->auth->getUserId();
}
/**
* @ApiTitle (充值支付)
* @ApiSummary (充值支付)
* @ApiMethod (POST)
* @ApiRoute (/api/pay/pay)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="inter", required=true, description="订单id")
*
*/
public function pay(){
$rule = config('verify.path');
if($this->request->isPost()){
$order_id = $this->request->post('order_id');
$openid = $this->getopenid();
if(empty($openid)){
$this->error(['code'=>2,'msg'=>'openid不能为空']);
}
$data = Db::name('toporder')->where('id',$order_id)->find();
if($data){
if($data['status'] == 2){
$this->error('该订单已支付过了');
}else{
//创建支付对象
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => '山东航拍',
'out_trade_no' => $data['num'],//支付订单号
'total_fee' => $data['money']*100,//单位分
'notify_url' => $rule.'api/pay/payOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $openid,
]);
if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
$params = [
'appId' => $config['app_id'],
'timeStamp' => time(),
'nonceStr' => $result['nonce_str'], // 统一下单返回的随机字符串
'package' => 'prepay_id='.$result['prepay_id'], // 统一下单Id
'signType' => 'MD5', // 签名方法
];
// 注意这里用的是商户平台的Key进行二次签名
$params['paySign'] = generate_sign($params, $config['key']);
$this->success('成功',$params);
}
$this->error($result['err_code_des']);
}
}else{
$this->error('无效的订单');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (图片购买支付)
* @ApiSummary (图片购买支付)
* @ApiMethod (POST)
* @ApiRoute (/api/pay/picpay)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="inter", required=true, description="订单id")
* @ApiParams (name="type", type="inter", required=true, description="支付方式 1 为余额支付 2为微信支付")
*
*/
public function picpay(){
$rule = config('verify.path');
if($this->request->isPost()){
$order_id = $this->request->post('order_id');
$type = $this->request->post('type');
if(empty($type)){
$this->error(['code'=>2,'msg'=>'缺少必要参数']);
}
$openid = $this->getopenid();
if(empty($openid)){
$this->error(['code'=>2,'msg'=>'openid不能为空']);
}
$data = Db::name('order')->where('id',$order_id)->find();
if($type == 1){
if($data){
if($data['status'] == 2){
$this->error('该订单已支付过了');
}else{
$res = Db::name('user')
->where('id',$data['user_id'])
->find();
if($data['total'] > $res['money']){
$this->error(['code'=>2,'msg'=>'余额不足,请充值后重试']);
}
$money = $res['money'] - $data['total'];
// 启动事务
Db::startTrans();
try{
$arr = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$money]);
$res = Db::name('order')->where('id',$order_id)->update(['status'=>2,'paytime'=>time()]);
if($res && $arr){
true;
}else{
$this->error('购买失败');
}
// 提交事务
Db::commit();
} catch (\Exception $e) {
dump($e->getMessage());
// 回滚事务
Db::rollback();
}
}
}else{
$this->error('无效的订单');
}
}else{
if($data){
if($data['status'] == 2){
$this->error('该订单已支付过了');
}else{
//创建支付对象
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => '山东航拍',
'out_trade_no' => $data['num'],//支付订单号
'total_fee' => $data['total']*100,//单位分
'notify_url' => $rule.'api/pay/picOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $openid,
]);
if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
$params = [
'appId' => $config['app_id'],
'timeStamp' => time(),
'nonceStr' => $result['nonce_str'], // 统一下单返回的随机字符串
'package' => 'prepay_id='.$result['prepay_id'], // 统一下单Id
'signType' => 'MD5', // 签名方法
];
// 注意这里用的是商户平台的Key进行二次签名
$params['paySign'] = generate_sign($params, $config['key']);
$this->success('成功',$params);
}
$this->error($result['err_code_des']);
}
}else{
$this->error('无效的订单');
}
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (视频购买支付)
* @ApiSummary (视频购买支付)
* @ApiMethod (POST)
* @ApiRoute (/api/pay/videopay)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="inter", required=true, description="订单id")
* @ApiParams (name="type", type="inter", required=true, description="支付方式 1 为余额支付 2为微信支付")
*
*/
public function videopay(){
$rule = config('verify.path');
if($this->request->isPost()){
$order_id = $this->request->post('order_id');
$openid = $this->getopenid();
if(empty($openid)){
$this->error(['code'=>2,'msg'=>'openid不能为空']);
}
$type = $this->request->post('type');
if(empty($type)){
$this->error(['code'=>2,'msg'=>'缺少必要参数']);
}
$data = Db::name('order')->where('id',$order_id)->find();
if($type == 1){
if($data){
if($data['status'] == 2){
$this->error('该订单已支付过了');
}else{
$res = Db::name('user')
->where('id',$data['user_id'])
->find();
if($data['total'] > $res['money']){
$this->error(['code'=>2,'msg'=>'余额不足,请充值后重试']);
}
$money = $res['money'] - $data['total'];
// 启动事务
Db::startTrans();
try{
$arr = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$money]);
$res = Db::name('order')->where('id',$order_id)->update(['status'=>2,'paytime'=>time()]);
if($res && $arr){
true;
}else{
$this->error('购买失败');
}
// 提交事务
Db::commit();
} catch (\Exception $e) {
dump($e->getMessage());
// 回滚事务
Db::rollback();
}
}
}else{
$this->error('无效的订单');
}
}else{
if($data){
if($data['status'] == 2){
$this->error('该订单已支付过了');
}else{
//创建支付对象
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => '山东航拍',
'out_trade_no' => $data['num'],//支付订单号
'total_fee' => $data['total']*100,//单位分
'notify_url' => $rule.'api/pay/videoOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $openid,
]);
if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
$params = [
'appId' => $config['app_id'],
'timeStamp' => time(),
'nonceStr' => $result['nonce_str'], // 统一下单返回的随机字符串
'package' => 'prepay_id='.$result['prepay_id'], // 统一下单Id
'signType' => 'MD5', // 签名方法
];
// 注意这里用的是商户平台的Key进行二次签名
$params['paySign'] = generate_sign($params, $config['key']);
$this->success('成功',$params);
}
$this->error($result['err_code_des']);
}
}else{
$this->error('无效的订单');
}
}
}else{
$this->error('请求方式错误');
}
}
//支付订单结果通知(无需调用)
public function payOrderNotify(){
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$response = $app->handlePaidNotify(function($message, $fail){
$data = Db::name('toporder')
->where('num',$message['out_trade_no'])
->find();
//return_code 表示通信状态
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code'] === 'SUCCESS') {
//支付成功,更新订单状态
$toporder['status'] = 2;
$toporder['paytime'] = time();
$money = $data['money'];
// 启动事务
Db::startTrans();
try{
$res = Db::name('toporder')->where('num',$message['out_trade_no'])->update($toporder);
$arr = Db::name('user')->where('id',$data['user_id'])->find();
$list['money'] = $arr['money'] + $money;
$info = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$list['money']]);
if($res && $info){
true;
}else{
$this->error(['code'=>2,'msg'=>'充值失败']);
}
// 提交事务
Db::commit();
} catch (\Exception $e) {
dump($e->getMessage());
// 回滚事务
Db::rollback();
}
}else{
//支付失败
Db::name('toporder')->where('num',$message['out_trade_no'])->update(['status'=>1]);
}
}else{
return $fail('通信失败,请稍后再通知我');
}
return true; // 返回处理完成
});
$response->send();
}
//图片支付订单结果通知(无需调用)
public function picOrderNotify(){
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$response = $app->handlePaidNotify(function($message, $fail){
//return_code 表示通信状态
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code'] === 'SUCCESS') {
//支付成功,更新订单状态
$toporder['status'] = 2;
$toporder['paytime'] = time();
}else{
//支付失败
$toporder['status'] = 1;
}
}else{
return $fail('通信失败,请稍后再通知我');
}
Db::name('order')->where('num',$message['out_trade_no'])->update($toporder);
return true; // 返回处理完成
});
$response->send();
}
//视频支付订单结果通知(无需调用)
public function videoOrderNotify(){
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$response = $app->handlePaidNotify(function($message, $fail){
//return_code 表示通信状态
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code'] === 'SUCCESS') {
//支付成功,更新订单状态
$toporder['status'] = 2;
$toporder['paytime'] = time();
}else{
//支付失败
$toporder['status'] = 1;
}
}else{
return $fail('通信失败,请稍后再通知我');
}
Db::name('vorder')->where('num',$message['out_trade_no'])->update($toporder);
return true; // 返回处理完成
});
$response->send();
}
//获取用户openid
public function getopenid()
{
$user_id = $this->uid;
$openid = Db::name('user')
->where('id',$user_id)
->field('openid')
->find();
return $openid;
}
}