PayController.php
23.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use think\Db;
use app\portal\controller\BirdController;
/**
* 微信支付,退款,提现DEMO
* Class PayController
* @package app\portal\controller
*/
class PayController extends HomeBaseController
{
protected $options;
function _initialize()
{
parent::_initialize();
$this->options = [
'app_id' => config('wechat_config.app_id'),
'secret' => config('wechat_config.secret'),
'payment' => config('wechat_config.payment'),
];
}
public function text(){
dump(cache('aa'));
dump(cache('bb'));
dump(cache('cc'));
dump(cache('dd'));
}
/**
* 微信支付
*/
public function index(){
$this->error('系统订单功能暂时关闭,请等待恢复','','','');
$id = $this->request->param('id',0,'intval');
if(empty($id)){
$this->error('缺少必要参数','','','');
}
$data = Db::name('indent')->where('id',$id)->find();
if(empty($data)){
$this->error('查询为空','','','');
}
if($data['state'] != 4){
$this->error('订单不是待支付状态','','','');
}
$openid = Db::name('third_party_user')->where('user_id',$data['uid'])->value('openid');
$attributes = [
'trade_type' => 'JSAPI',
'body' => '百荣科技',
'detail' => '以客户为中心 以奋斗者文本',
'out_trade_no' => $data['order_number'],
'total_fee' => $data['money']*100, // 单位:分
'notify_url' => url('portal/pay/notify','','',true), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'openid' => $openid, // trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识,
];
$order = new Order($attributes);
$app = new Application($this->options);
$payment = $app->payment;
$result = $payment->prepare($order);
cache('result',$result);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
$prepayId = $result->prepay_id;
$jsApiParameters=$payment->configForJSSDKPayment($prepayId);
$this->assign('name','北京煊坤博文图书有限公司');
$this->assign('price',$data['money']);
$this->assign('order_number',$data['order_number']);
$this->assign('indent_id',$data['id']);
$this->assign('jsApiParameters',json_encode($jsApiParameters));
return $this->fetch();
}else{
dump($result);
exit();
$this->error('支付参数错误','',$result);
}
}
/**
* 支付回调
* @throws \EasyWeChat\Core\Exceptions\FaultException
*/
public function notify(){
cache('nnn',111);
$app = new Application($this->options);
$response = $app->payment->handleNotify(function($notify, $successful){
$out_trade_no=$notify->out_trade_no;
//查询订单信息
$order = Db::name('indent')->where('order_number',$out_trade_no)->find();
if (!$order) { // 如果订单不存在
return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
if(!empty($order['pay_time'])){
return true;
}
//查询订单下的所有商品 增加销量
$order_indent_goods_data = Db::name('indent_goods') -> where('indent_id',$order['id']) -> select();
foreach ($order_indent_goods_data as $key => $val){
Db::name('goods') -> where('id',$val['goods_id']) -> setInc('sales',$val['number']);
//删除购物车中的商品
Db::name('shopping_cart') -> where("uid=".$order['uid']." and goods_id=".$val['goods_id']) -> delete();
}
if($successful){
Db::startTrans();
try {
$order = Db::name('indent')->where('order_number',$out_trade_no)->lock(true)->find();
$update['state'] = 2;
$update['pay_time'] = time();
//更新订单状态为待发货
Db::name('indent')->where('order_number',$out_trade_no)->update($update);
$uid = $order['uid'];
//查询当前用户身份
$data_user = Db::name('my_user')->where('uid',$uid)->find();
if($order['indent_type'] == 1){
//如果是平台商品 学生购买
if($data_user['status'] == 4){
//查询平台商品老师和业务员所占的佣金比例
$money_ratio = Db::name('money_ratio') -> where('id',1) -> find();
//查询这条订单下的所有商品
$data_indent_goods = Db::name('indent_goods') -> where('indent_id',$order['id']) -> select();
$money_salesman = 0;
//查询老师的uid
$teacher = Db::name('my_user') -> where('id',$data_user['pid']) -> find();
//查询业务员的uid
$salesman = Db::name('my_user') -> where('id',$teacher['pid']) -> find();
foreach ($data_indent_goods as $key => $val){
$money_salesman += $val['price']*$val['number']*$money_ratio['salesman_ratio'];
$money_salesman_in = $val['price']*$val['number']*$money_ratio['salesman_ratio'];
$data_money_income_salesman['create_time'] = time();
$data_money_income_salesman['money'] = $money_salesman_in;
$data_money_income_salesman['type'] = 1;
$data_money_income_salesman['book_name'] = $val['book_name'];
$data_money_income_salesman['book_thumbnail'] = $val['thumbnail'];
$data_money_income_salesman['book_num'] = $val['number'];
$data_money_income_salesman['uid'] = $salesman['uid'];
$data_money_income_salesman['indent_id'] = $order['id'];
Db::name('money_income') -> insert($data_money_income_salesman);
}
Db::name('my_user') -> where('uid',$salesman['uid']) -> setInc('balance',$money_salesman);
$money_teacher = 0;
foreach ($data_indent_goods as $key => $val){
$money_teacher += $val['price']*$val['number']*$money_ratio['teacher_ratio'];
$money_teacher_in = $val['price']*$val['number']*$money_ratio['teacher_ratio'];
$data_money_income_teacher['create_time'] = time();
$data_money_income_teacher['money'] = $money_teacher_in;
$data_money_income_teacher['type'] = 1;
$data_money_income_teacher['book_name'] = $val['book_name'];
$data_money_income_teacher['book_thumbnail'] = $val['thumbnail'];
$data_money_income_teacher['book_num'] = $val['number'];
$data_money_income_teacher['uid'] = $teacher['uid'];
$data_money_income_teacher['indent_id'] = $order['id'];
Db::name('money_income') -> insert($data_money_income_teacher);
}
Db::name('my_user') -> where('uid',$teacher['uid']) -> setInc('balance',$money_teacher);
}
//如果是平台商品 老师购买
if($data_user['status'] == 3){
//查询平台商品老师和业务员所占的佣金比例
$money_ratio = Db::name('money_ratio') -> where('id',1) -> find();
//查询这条订单下的所有商品
$data_indent_goods = Db::name('indent_goods') -> where('indent_id',$order['id']) -> select();
$money_salesman = 0;
//查询老师的uid
/* $teacher = Db::name('my_user') -> where('id',$data_user['pid']) -> find();*/
//查询业务员的uid
$salesman = Db::name('my_user') -> where('id',$data_user['pid']) -> find();
foreach ($data_indent_goods as $key => $val){
$money_salesman += $val['price']*$val['number']*$money_ratio['salesman_ratio'];
$money_salesman_in = $val['price']*$val['number']*$money_ratio['salesman_ratio'];
$data_money_income_salesman['create_time'] = time();
$data_money_income_salesman['money'] = $money_salesman_in;
$data_money_income_salesman['type'] = 1;
$data_money_income_salesman['book_name'] = $val['book_name'];
$data_money_income_salesman['book_thumbnail'] = $val['thumbnail'];
$data_money_income_salesman['book_num'] = $val['number'];
$data_money_income_salesman['uid'] = $salesman['uid'];
$data_money_income_salesman['indent_id'] = $order['id'];
Db::name('money_income') -> insert($data_money_income_salesman);
}
Db::name('my_user') -> where('uid',$salesman['uid']) -> setInc('balance',$money_salesman);
$money_teacher = 0;
foreach ($data_indent_goods as $key => $val){
$money_teacher += $val['price']*$val['number']*$money_ratio['teacher_ratio'];
$money_teacher_in = $val['price']*$val['number']*$money_ratio['teacher_ratio'];
$data_money_income_teacher['create_time'] = time();
$data_money_income_teacher['money'] = $money_teacher_in;
$data_money_income_teacher['type'] = 1;
$data_money_income_teacher['book_name'] = $val['book_name'];
$data_money_income_teacher['book_thumbnail'] = $val['thumbnail'];
$data_money_income_teacher['book_num'] = $val['number'];
$data_money_income_teacher['uid'] = $data_user['uid'];
$data_money_income_teacher['indent_id'] = $order['id'];
Db::name('money_income') -> insert($data_money_income_teacher);
}
Db::name('my_user') -> where('uid',$data_user['uid']) -> setInc('balance',$money_teacher);
}
cache('a1',"789");
// $bird = new BirdController;
// $createOrder = $bird -> createOrder($order['id']);
// cache('yuyue1',$createOrder);
// $takeOrder = $bird -> takeOrder($order['id']);
// cache('dingyue1',$takeOrder);
}else if($order['indent_type'] == 2){
//如果是业务员订单
//查询平台商品老师和业务员所占的佣金比例
$money_ratio = Db::name('money_ratio') -> where('id',1) -> find();
//判断这条订单是否为快递配送
if($order['is_courier'] == 2){
$order['money'] = $order['money']-$money_ratio['courier'];
}
//查询这条订单下的所有商品
$data_indent_goods = Db::name('indent_goods') -> where('indent_id',$order['id']) -> select();
//业务员商品学生购买
if($data_user['status'] == 4){
$teacher = Db::name('my_user') -> where('id',$data_user['pid']) -> find();
$money = 0;
foreach ($data_indent_goods as $key => $val){
$money += $val['commission']*$val['number'];
$money_in = $val['commission']*$val['number'];
$money_income_teacher['create_time'] = time();
$money_income_teacher['create_time_detail'] = msectime();
$money_income_teacher['indent_goods_id'] = $val['id'];
$money_income_teacher['money'] = $money_in;
$money_income_teacher['type'] = 2;
$money_income_teacher['book_name'] = $val['book_name'];
$money_income_teacher['book_thumbnail'] = $val['thumbnail'];
$money_income_teacher['book_num'] = $val['number'];
$money_income_teacher['uid'] = $teacher['uid'];
$money_income_teacher['indent_id'] = $order['id'];
$money_income_teacher['indent_json'] = json_encode($data_indent_goods,JSON_UNESCAPED_UNICODE);
Db::name('money_income') -> insert($money_income_teacher);
}
//增加老师余额
Db::name('my_user') -> where('uid',$teacher['uid']) -> setInc('balance',$money);
//平台抽成收益加入数据库
$as_income_money['money'] = $order['money']*$money_ratio['platform_ratio'];
$as_income_money['time'] = time();
Db::name('income_money') -> insert($as_income_money);
//计算业务员这条订单的收入
if($order['is_courier'] == 2){
//如果是快递订单给业务员添加快递费
$balance_salesman = $order['money'] - $order['money']*$money_ratio['platform_ratio'] - $money + $money_ratio['courier'];
}else{
//如果不是业务员订单则不用添加快递费
$balance_salesman = $order['money'] - $order['money']*$money_ratio['platform_ratio'] - $money;
}
//增加业务员的余额
Db::name('my_user') -> where('uid',$order['salesman_uid']) -> setInc('balance',$balance_salesman);
//添加业务员的收入记录
$money_income_salesman_indent['create_time'] = time();
$money_income_salesman_indent['create_time_detail'] = msectime();
$money_income_salesman_indent['uid'] = $order['salesman_uid'];
$money_income_salesman_indent['money'] = $balance_salesman;
$money_income_salesman_indent['type'] = 2;
$money_income_salesman_indent['indent_id'] = $order['id'];
$money_income_teacher['indent_json'] = json_encode($data_indent_goods,JSON_UNESCAPED_UNICODE);
Db::name('money_income') -> insert($money_income_salesman_indent);
}
//业务员商品老师购买
elseif ($data_user['status'] == 3){
$teacher = $data_user['uid'];
$money = 0;
foreach ($data_indent_goods as $key => $val){
$money += $val['commission']*$val['number'];
$money_in = $val['commission']*$val['number'];
$money_income_teacher['create_time'] = time();
$money_income_teacher['money'] = $money_in;
$money_income_teacher['type'] = 2;
$money_income_teacher['book_name'] = $val['book_name'];
$money_income_teacher['book_thumbnail'] = $val['thumbnail'];
$money_income_teacher['book_num'] = $val['number'];
$money_income_teacher['uid'] = $teacher;
$money_income_teacher['indent_id'] = $order['id'];
Db::name('money_income') -> insert($money_income_teacher);
}
//增加老师余额
Db::name('my_user') -> where('uid',$teacher) -> setInc('balance',$money);
//平台抽成收益加入数据库
$ass_income_money['money'] = $order['money']*$money_ratio['platform_ratio'];
$ass_income_money['time'] = time();
Db::name('income_money') -> insert($ass_income_money);
//判断是不是快递配送
if($order['is_courier'] == 2){
//计算业务员这条订单的收入
$balance_salesman = $order['money'] - $order['money']*$money_ratio['platform_ratio'] - $money + $money_ratio['courier'];
}else{
//计算业务员这条订单的收入
$balance_salesman = $order['money'] - $order['money']*$money_ratio['platform_ratio'] - $money;
}
//增加业务员的余额
Db::name('my_user') -> where('uid',$order['salesman_uid']) -> setInc('balance',$balance_salesman);
cache('aa',$balance_salesman);
cache('bb',$order['money']);
cache('cc',$money_ratio['platform_ratio']);
cache('dd',$money);
//添加业务员的收入记录
$money_income_salesman_indent['create_time'] = time();
$money_income_salesman_indent['uid'] = $order['salesman_uid'];
$money_income_salesman_indent['money'] = $balance_salesman;
$money_income_salesman_indent['type'] = 2;
$money_income_salesman_indent['indent_id'] = $order['id'];
Db::name('money_income') -> insert($money_income_salesman_indent);
}
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
}
}
return true; // 返回处理完成
});
$response->send();
}
/**
* 查询订单
*/
public function checkOrder(){
$app = new Application($this->options);
$payment = $app->payment;
$orderNo = "2018080397545210";//商户系统内部的订单号(out_trade_no)
$result=$payment->query($orderNo);
var_dump($result);
}
/**
* 退款
*/
public function refund(){
//todo 退款逻辑应该加入百荣签名验证规则,避免出现被盗用
/*$param=$this->request->param();
$signature = $param['s'];
$arithmetic['timeStamp']= $param['t'];
$arithmetic['randomStr']= $param['r'];
$arithmetic['orderSn']= $param['o'];
$str = arithmetic($arithmetic);
if($str != $signature){
$this->error('签名验证失败');
}*/
$app = new Application($this->options);
$payment = $app->payment;
//使用商户订单号退款 PS.其他形式参考文档
$orderNo = "2018080397100101";//商户系统内部的订单号(out_trade_no)
$refundNo = cmf_get_order_sn();//退款单号
$result = $payment->refund($orderNo, $refundNo, 1, 1); // 总金额 100, 退款 80,refundFee可选(为空时全额退款)
var_dump($result);
}
/**
* 查询退款
*/
public function checkRefund(){
$app = new Application($this->options);
$payment = $app->payment;
$outTradeNo="2018080210097519";//商户系统内部的订单号(out_trade_no)
$result = $payment->queryRefund($outTradeNo);
var_dump($result);
}
/**
* 退款结果回调
*/
public function refundNotify() {
cache('test',123123);
$app = new Application($this->options);
$response = $app->payment->handleRefundNotify(function ($message, $reqInfo) {
cache('message',$message);
cache('reqInfo',$reqInfo);
// 其中 $message['req_info'] 获取到的是加密信息
// $reqInfo 为 message['req_info'] 解密后的信息
// 你的业务逻辑...
return true; // 返回 true 告诉微信“我已处理完成”
// 或返回错误原因 $fail('参数格式校验错误');
});
$response->send();
}
/**
* 红包
*/
public function luckyMoney(){
//todo 退款逻辑应该加入百荣签名验证规则,避免出现被盗用
/* $param=$this->request->param();
$signature = $param['s'];
$arithmetic['timeStamp']= $param['t'];
$arithmetic['randomStr']= $param['r'];
$arithmetic['orderSn']= $param['o'];
$str = arithmetic($arithmetic);
if($str != $signature){
$this->error('签名验证失败');
}*/
$app = new Application($this->options);
$luckyMoney = $app->lucky_money;
$luckyMoneyData = [
'mch_billno' => 'xy123456',
'send_name' => '测试红包',
're_openid' => 'oxTWIuGaIt6gTKsQRLau2M0yL16E',
'total_num' => 1, //普通红包固定为1,裂变红包不小于3
'total_amount' => 100, //单位为分,普通红包不小于100,裂变红包不小于300
'wishing' => '祝福语',
'client_ip' => '192.168.0.1', //可不传,不传则由 SDK 取当前客户端 IP
'act_name' => '测试活动',
'remark' => '测试备注',
// ...
];
//普通红包
$result = $luckyMoney->send($luckyMoneyData, \EasyWeChat\Payment\LuckyMoney\API::TYPE_NORMAL);
var_dump($result);
}
/**
* 查询红包
*/
public function checkLuckyMoney(){
$mchBillNo = "商户系统内部的订单号(mch_billno)";
$app = new Application($this->options);
$luckyMoney = $app->lucky_money;
$luckyMoney->query($mchBillNo);
}
/**
* 生成签名DEMO
* @return string
*/
public function getSignatureDemo(){
$timeStamp = time();
$randomStr = cmf_random_string(8);
$arithmetic['timeStamp']= $timeStamp;
$arithmetic['randomStr']= $randomStr;
$signature = arithmetic($arithmetic);
$notifyParam=array('t'=>$timeStamp,'r'=>$randomStr,'s'=>$signature);
$url=url('your url 表达式',$notifyParam,true,true);
return $url;
}
}