Notify.php
5.6 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
<?php
namespace app\api\controller;
use addons\epay\library\Service;
use app\common\controller\Api;
use Exception;
use GuzzleHttp\Client;
use think\Db;
/**
* @ApiInternal
*/
class Notify extends Api
{
protected $noNeedRight = ['*'];
protected $noNeedLogin = ['*'];
/**
* 订单支付回调
*/
public function orderNotify()
{
$paytype = $this->request->param('type');
$pay = Service::checkNotify($paytype);
if (!$pay) {
return '签名错误';
}
$data = $pay->verify();
if (!is_array($data)) $data = json_decode($data,true);
$model = new \app\api\model\Order();
$goodsmodel = new \app\api\model\OrderGoods();
$skumodel = new \app\api\model\GoodsSpec();
$goods_model = new \app\api\model\Goods();
$buymodel = new \app\api\model\UserBuylist();
try {
$out_trade_no = $data['out_trade_no'];
$order = $model
->where('order_no',$out_trade_no)
->where('pay_status','10')
->where('order_status','10')
->find();
if (!$order) $this->error('订单不存在');
$order->pay_status = '20';
$order->transaction_id = $data['transaction_id'];
$order->pay_time = time();
$order->isUpdate()->save();
// 减少库存
$sales_actual = 0;
$goods_ids = [];
$list = $goodsmodel->where('order_id',$order['id'])->select();
foreach ($list as $key => $value){
$goods_ids[] = $value['goods_id'];
$sales_actual+=$value['total_num'];
if ($value['deduct_stock_type'] == 20){
$skumodel->where('goods_spec_id',$value['goods_spec_id'])->setDec('stock_num',$value['total_num']);
}
}
// 增加销量
$goods_model->whereIn('goods_id',$goods_ids)->setInc('sales_actual',$sales_actual);
// 判断是否邀请成功
$user = \app\api\model\User::get($order['user_id']);
if ($user['invite_user_id'] > 0 && $user['invite_status'] != 1) {
$user->invite_status = '1';
$user->isUpdate()->save();
$coupon = Db::name('coupon')->where('id',1)->find();
if ($coupon['endtime'] > time()){
$data = [];
$time = time();
for ($i=1;$i<=$coupon['send_number'];$i++){
$data[] = [
'user_id' => $user->invite_user_id,
'coupon_id' => $coupon['id'],
'name' => $coupon['name'],
'price' => $coupon['price'],
'full_price' => $coupon['full_price'],
'endtime' => $time+$coupon['days']*86400,
'createtime' => $time,
];
}
Db::name('user_coupon')->insertAll($data);
}
}
// 加入我常买
$data = [];
foreach ($list as $key => $value){
$data[] = [
'user_id' => $order['user_id'],
'goods_id' => $value['goods_id'],
'sku_id' => $value['goods_spec_id'],
];
}
$buymodel->isUpdate(false)->saveAll($data);
// 给后台发送新订单提醒
$client = new Client();
$domain = config('socketio.domain');
$http_port = config('socketio.http_port');
$client->request('POST', $domain.":{$http_port}/", [
'form_params' => [
'type' => 'publish',
'content' => '您有新的订单,请注意查收',
'to' => '',
]
]);
} catch (Exception $e) {
}
return $pay->success()->send();
}
/**
* 订单退款回调
*/
public function refund()
{
$paytype = $this->request->param('type');
$pay = Service::checkNotify($paytype,'refund');
if (!$pay) {
echo '签名错误';
return;
}
$data = $pay->verify();
$data = json_decode($data,true);
$model = new \app\api\model\Order();
$goodsmodel = new \app\api\model\OrderGoods();
$skumodel = new \app\api\model\GoodsSpec();
$goods_model = new \app\api\model\Goods();
try {
$out_trade_no = $data['out_refund_no'];
$order = $model
->where('refund_no',$out_trade_no)
->where('pay_status','20')
->where('order_status','10')
->find();
if (!$order) $this->error('订单不存在');
$order->refund_id = $data['refund_id'];
$order->refund_time = time();
$order->order_status = '20';
$order->isUpdate()->save();
// 增加库存
$sales_actual = 0;
$list = $goodsmodel->where('order_id',$order['id'])->select();
foreach ($list as $key => $value){
$sales_actual+=$value['total_num'];
if ($value['deduct_stock_type'] == 20){
$skumodel->where('goods_spec_id',$value['goods_spec_id'])->setInc('stock_num',$value['total_num']);
}
}
// 减少销量
$goods_model->setDec('sales_actual',$sales_actual);
//你可以在此编写订单逻辑
} catch (Exception $e) {
}
echo $pay->success()->send();
}
}