Order.php
8.8 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
<?php
namespace addons\litestore\controller\api;
use app\common\controller\Api;
use addons\litestore\model\Litestoreorder;
use addons\third\model\Third;
use EasyWeChat\Factory as WXPAY_APP;
class Order extends Api
{
protected $noNeedRight = ['*'];
protected $noNeedLogin = ['callback_for_wxapp','callback_for_wxgzh'];
public function _initialize()
{
parent::_initialize();
$this->user_id = $this->auth->id;
$this->model = new Litestoreorder;
}
public function buyNow()
{
$goods_id = $this->request->request('goods_id');
$goods_num = $this->request->request('goods_num');
$goods_sku_id = $this->request->request('goods_sku_id');
$order = $this->model->getBuyNow($this->user_id, $goods_id, $goods_num, $goods_sku_id);
return $this->success('获取成功',$order);
}
public function buyNow_pay(){
$goods_id = $this->request->request('goods_id');
$goods_num = $this->request->request('goods_num');
$goods_sku_id = $this->request->request('goods_sku_id');
$order = $this->model->getBuyNow($this->user_id, $goods_id, $goods_num, $goods_sku_id);
if ($this->model->hasError()) {
return $this->error($this->model->getError());
}
// 创建订单
if ($this->model->order_add($this->user_id, $order)) {
// 发起微信支付
if($this->request->request('type')&&$this->request->request('type')=='gzh'){
$this->init_wx_pay_for_gzh(true);
$this->make_wx_pay('wechat');
}else{
$this->init_wx_pay_for_wxapp();
$this->make_wx_pay('wxapp');
}
}
$error = $this->model->getError() ?: '订单创建失败';
return $this->error($error);
}
public function Get_order_num(){
$NoPayNum = $this->model->where(['user_id' => $this->user_id, 'pay_status' => '10', 'freight_status' => '10', 'order_status' => '10', 'receipt_status' => '10'])->count();
$NoFreightNum = $this->model->where(['user_id' => $this->user_id, 'pay_status' => '20', 'freight_status' => '10', 'order_status' => '10', 'receipt_status' => '10'])->count();
$NoReceiptNum = $this->model->where(['user_id' => $this->user_id, 'pay_status' => '20', 'freight_status' => '20', 'order_status' => '10', 'receipt_status' => '10'])->count();
return $this->success('',['NoPayNum'=>$NoPayNum,'NoFreightNum'=>$NoFreightNum,'NoReceiptNum'=>$NoReceiptNum]);
}
public function my(){
$list = $this->model->getList($this->user_id);
return $this->success('',$list);
}
public function detail(){
$id = $this->request->request('id');
$order = $this->model->getOrderDetail($id, $this->user_id);
return $this->success('',['order' => $order]);
}
public function cart_pay()
{
$order = $this->model->getCart($this->user_id);
// 创建订单
if ($this->model->order_add($this->user_id, $order)) {
// 清空购物车
$this->model->CarclearAll($this->user_id);
// 发起微信支付
if($this->request->request('type')&&$this->request->request('type')=='gzh'){
$this->init_wx_pay_for_gzh(true);
$this->make_wx_pay('wechat');
}else{
$this->init_wx_pay_for_wxapp();
$this->make_wx_pay('wxapp');
}
}
$this->error($this->model->getError() ?: '订单创建失败');
}
public function finish(){
$id = $this->request->post("id");
$order = $this->model;
if ($order->finish($this->user_id,$id)) {
return $this->success('');
}
return $this->error($order->getError());
}
public function cancel(){
$id = $this->request->post("id");
$order = $this->model->getOrderDetail($id, $this->user_id);
if ($order->cancel($this->user_id,$id)) {
return $this->success('');
}
return $this->error($order->getError());
}
public function order_pay(){
$id = $this->request->post("id");
$order = $this->model->getOrderDetail($id, $this->user_id);
if (!$order->checkGoodsStatusFromOrder($order['goods'])) {
return $this->error($order->getError());
}
$this->model = $order;
// 发起微信支付
if($this->request->post('type')&&$this->request->post('type')=='gzh'){
$this->init_wx_pay_for_gzh(true);
$this->make_wx_pay('wechat');
}else{
$this->init_wx_pay_for_wxapp();
$this->make_wx_pay('wxapp');
}
}
private function init_wx_pay_for_gzh($Ischeck=false){
//这里首先判断 此用户是否绑定了微信公众号
if($Ischeck){
$third = Third::where(['user_id' => $this->user_id, 'platform' => 'wechat'])->find();
if(!$third){
//从这里自动绑定微信公众号的账户
$this->error('您未绑定微信号',null,1008);
}
}
$config = get_addon_config('litestore');
$third_config = get_addon_config('third');
$third_options = array_intersect_key($third_config, array_flip(['wechat']));
$third_options = $third_options['wechat'];
$options = [
'debug' => true,
'log' => [
'level' => 'debug',
'file' => '/tmp/easywechat.log',
],
'app_id' => $third_options['app_id'],
'mch_id' => $config['MCHIDGZH'],
'key' => $config['APIKEYGZH'],
'notify_url' => \think\Request::instance()->domain().addon_url('litestore/api.order/callback_for_wxgzh'),
];
$this->wxapp = WXPAY_APP::payment($options);
}
private function init_wx_pay_for_wxapp(){
$config = get_addon_config('litestore');
$options = [
'debug' => true,
'log' => [
'level' => 'debug',
'file' => '/tmp/easywechat.log',
],
'app_id' => $config['AppID'],
'mch_id' => $config['MCHID'],
'key' => $config['APIKEY'],
'notify_url' => \think\Request::instance()->domain().addon_url('litestore/api.order/callback_for_wxapp'),
];
$this->wxapp = WXPAY_APP::payment($options);
}
private function make_wx_pay($platform){
$third = Third::where(['user_id' => $this->user_id, 'platform' => $platform])->find();
$payment = $this->wxapp;
$attributes = [
'trade_type' => 'JSAPI',
'body' => $this->model['order_no'],
'detail' => 'OrderID:'.$this->model['id'],
'out_trade_no' => $this->model['order_no'],
//'total_fee' => $this->model['pay_price'] * 100, // 单位:分
'total_fee' => 1, // 单位:分
'openid' => $third['openid'],
];
$result = $payment->order->unify($attributes);
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
$prepayId = $result['prepay_id'];
$config = $payment->jssdk->sdkConfig($prepayId);
return $this->success('预支付成功',$config);
}
return $this->error('微信支付调用失败',$result);
}
public function callback_for_wxgzh(){
$this->init_wx_pay_for_gzh(false);
$response = $this->wxapp->handlePaidNotify(function ($message, $fail) {
$order = $this->model->payDetail($message['out_trade_no']);
if (empty($order)) { // 如果订单不存在
return true;
//return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code']=== 'SUCCESS') {
$order->updatePayStatus($message['transaction_id']);
}
}
return true; // 返回处理完成
});
$response->send();
}
public function callback_for_wxapp(){
$this->init_wx_pay_for_wxapp();
$response = $this->wxapp->handlePaidNotify(function ($message, $fail) {
$order = $this->model->payDetail($message['out_trade_no']);
if (empty($order)) { // 如果订单不存在
return true;
//return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code']=== 'SUCCESS') {
$order->updatePayStatus($message['transaction_id']);
}
}
return true; // 返回处理完成
});
$response->send();
}
}