OrderController.php
7.1 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
<?php
/**
* Created by PhpStorm.
* User: ruidiudiu
* Date: 2018/11/24
* Time: 8:54
*/
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use think\Db;
use wxapp\pay\WeixinPay;
/**
* @title 订单相关接口
* @description 订单相关接口
* @group 订单相关接口
*/
class OrderController extends HomeBaseController{
// /**
// * @title 生成订单
// * @description 生成订单,加入开始时间和用户id
// * @author 董瑞恩
// * @url /portal/order/createOrder
// * @method GET
// *
// * @param name:users_id type:String require:1 default:无 other: desc:用户id
// * @param name:name type:String require:1 default:无 other: desc:设备名称
// *
// */
public function createOrder($users_id,$name){
$order=[
'order_no' => cmf_get_order_sn(),
'eq_name' => $name,
'users_id' => $users_id,
'start_time' => time(),
'state' => 1
];
try{
Db::startTrans();
Db::name('users')->where('id',$users_id)->update(['is_use'=>1]);
Db::name('order')->insert($order);
}catch (\Exception $exception){
Db::rollback();
$data=[
'state'=>false,
'message'=>$exception->getMessage()
];
return $data;
}
Db::commit();
$data=[
'state'=>true,
'order_no' => $order['order_no']
];
return $data;
}
// /**
// * @title 完成订单
// * @description 订单完成,加入结束时间和结算费用,并调用微信统一下单
// * @author 董瑞恩
// * @url /portal/order/order
// * @method GET
// *
// * @param name:users_id type:String require:1 default:无 other: desc:用户id
// *
// * @return data:返回用于调用支付的参数
// */
public function order($users_id,$name){
$order= Db::name('order')->where(['users_id'=>$users_id,'state'=>1])->find();
$end_time=time();
$price=$this->getPrice($order['start_time'],$end_time);
$time=ceil(($end_time-$order['start_time'])/3600);
$data=[
'end_time'=>$end_time,
'time' => $time,
'price' => $price,
'state' => 2
];
try{
Db::startTrans();
Db::name('users')->where('id',$users_id)->update(['is_use'=>0]);
Db::name('equipment')->where('name',$name)->update(['use'=>0]);
Db::name('order')->where('order_no',$order['order_no'])->update($data);
}catch (\Exception $exception){
Db::rollback();
$data=[
'state'=>false,
'message'=>'数据库操作失败',
'error' =>$exception->getMessage()
];
$this->apiResponse(200,'success',$data);
}
Db::commit();
//调起支付
$this->pay($order['order_no']);
}
public function getPrice($start_time=1543298302,$end_time=1543303733){
$cost=Db::name('cost')->where('id',1)->find();
$interval = Db::name('interval')->where('id',1)->find();
//用了多少天
$date=floor(($end_time-$start_time)/86400);
//去余数
$yu=($end_time-$start_time)%86400;
$new_start_time=$end_time-$yu;//时间戳
$new_end_time= $end_time;//时间戳
dump(date('ymd his',$new_start_time));
$hours_start_time=date('H',$new_start_time);
$hours_end_time=date('H',$new_end_time);
$stateTime=strtotime($interval['start_time']);
$endTime=strtotime($interval['end_time']);
$price=1;
//判断是否跨天
if ($endTime < $stateTime){
if ($hours_end_time > $hours_start_time){
if ($new_start_time> strtotime('00:00:00') && $new_start_time < $endTime && $new_end_time > $endTime && $new_end_time < $stateTime){
$price=ceil(($endTime-$new_start_time)/3600) * $interval['price'] + ceil(($new_end_time-$endTime)/3600) * $cost['cost'];
}
if ($new_start_time> strtotime('00:00:00') && $new_start_time < $endTime && $new_end_time > $stateTime){
$price=ceil(($endTime-$new_start_time)/3600) * $interval['price'] + ceil(($new_end_time-$stateTime)/3600) * $interval['price'] + ceil(($stateTime-$endTime)/3600) * $cost['cost'];
}
if ($new_start_time > $endTime && $new_end_time < $stateTime){
$price=ceil(($new_end_time-$new_start_time)/3600) * $cost['cost'];
}
if ($new_start_time > $endTime && $new_end_time > $stateTime && $new_end_time < strtotime('24:00:00')){
$price= ceil(($stateTime-$new_start_time)/3600) * $cost['cost'] + ceil(($new_end_time-$stateTime)/3600) * $interval['price'];
}
}else{
}
}else{
//不跨天
}
return $price;
}
/**
* @title 统一下单
* @description 微信统一下单
* @author 董瑞恩
* @url /portal/order/pay
* @method GET
*
* @param name:order_no type:String require:1 default:无 other: desc:订单号
*
* @return data:返回用于调用支付的参数
*/
public function pay($order_no){
$order=Db::name('order')->where(['order_no'=>$order_no,'state'=>2])->find();
$openId=Db::name('users')->where('id',$order['users_id'])->find()['open_id'];
$body='支付';
$price=$order['price']*100;//订单价格
$notify_url=url('order/notify','','',true);//回调地址
$wxPay=new WeixinPay($openId,$order_no,$body,$price,$notify_url);
$pay=$wxPay->pay();
if (isset($pay['package'])){
$data=[
'state'=>1,
'pay'=>$pay
];
$this->apiResponse(200,'success',$data);
}else{
$data=[
'state'=>0,
'message'=>'统一下单失败',
'error' => $pay
];
$this->apiResponse(200,'success',$data);
}
}
//支付回调接口
public function notify(){
$param = $this->request->param();
if ($param == null) {
$param = file_get_contents("php://input");
if ($param == null) {
$param = $GLOBALS['HTTP_RAW_POST_DATA'];
}
}
$wxPay=new WeixinPay();
$data = $wxPay->xmlToArray($param);
$Sign = $data['sign'];
//支付成功回调后变更订单状态
$mySign = $wxPay->getSign($data);
$order_no = $data['out_trade_no'];
if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
try{
Db::name('order')->where(['order_no'=>$order_no])->update(['state'=>3]);
}catch (\Exception $exception){
$this->apiResponse(301,'error:'.$exception->getMessage());
}
return "<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>";
}
}
}