PayController.php
12.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
<?php
namespace app\pay\controller;
use cmf\controller\PayBaseController;
use app\integral\model\IntegralModel;
use think\Db;
class PayController extends PayBaseController
{
//提交订单
public function done()
{
$request = request();
$data = [];
if ($request->isAjax()) {
if (empty($request->param('address_id'))) {
$this->success('请填写配送地址', '', false);
}
if (empty($request->param('pay_type'))) {
$this->success('请选择支付方式', '', false);
}
$consignee = Db::name('zj_user_place')->field('name,province,city,county,mobile,place')
->where(['id' => $request->param('address_id'), 'uid' => session('user.id')])
->find();
if (session('cart.id') != null && session('goods.id') == null) {
//通过购物车
$data = Db::name('zj_cart')->alias('c')
->join('zj_goods g', 'c.gid=g.id')
->field('c.num,g.price,g.id')
->where(['c.uid' => session('user.id'), 'c.id' => ['in', session('cart.id')]])
->select();
} elseif (session('cart.id') == null && session('goods.id') != null) {
//直接购买
$data = Db::name('zj_goods')
->field('price,id')
->where(['id' => session('goods.id')])
->select();
} else {
$this->success('您已下过该订单', '', false);
}
$whole = 0;
$orderGoods = [];
foreach ($data as $item) {
if (session('cart.id') == null && session('goods.id') != null) {
$item['num'] = 1;
}
$whole += $item['num'] * $item['price'];
}
if ($request->param('pay_type') == 2) {
$whole_num = $request->param('pay_num');
} elseif ($request->param('pay_type') == 1) {
$whole_num = 0;
} else {
$whole_num = $whole;
}
$sn = date('YmdHis') . rand(100000, 999999);
$order = [
'order_num' => $sn,
'step' => 1,
'uid' => session('user.id'),
'site' => $consignee['province'] . $consignee['city'] . $consignee['county'] . $consignee['place'],
'name' => $consignee['name'],
'mobile' => $consignee['mobile'],
'remark' => $request->param('remark'),
'create_time' => time(),
'whole' => $whole,
'whole_num' => $whole_num,
'pay_type' => $request->param('pay_type'),
'step' => 1,
//购买同时加入一级分类,用于判断是否赠送积分用
'cid' => $request->param('cid'),
//二级分类字符串-1-,-2-,-3-
'cids' => $this->getSonCategoryStr($data),
'province' => $consignee['province'],
'city' => $consignee['city'],
'county' => $consignee['county'],
'place' => $consignee['place']
];
Db::startTrans();
if (Db::name('zj_order')->insert($order)) {
$oid = Db::name('zj_order')->getLastInsID();
foreach ($data as $k => $v) {
if (session('cart.id') == null && session('goods.id') != null) {
$v['num'] = 1;
}
$orderGoods[$k] = [
'oid' => $oid,
'gid' => $v['id'],
'num' => $v['num'],
'price' => Db::name('zj_goods')->where(['id' => $v['id']])->value('price')
];
}
if (Db::name('zj_order_goods')->insertAll($orderGoods)) {
//微信支付
if ($order['pay_type'] == 1) {
Db::commit();
session('goods.id', null);
Db::name('zj_cart')->where(['id' => ['in', session('cart.id')]])->delete();
session('cart.id', null);
$info = [
'attach' => $oid,
'openid' => session('user.openid'),
'body' => '微信支付-天生红商城',
'total_fee' => $order['whole']
];
$this->wxPay($info);
} //积分支付
elseif ($order['pay_type'] == 3) {
//先判断剩余积分是否足够支付
$balance = Db::name('user')->where(['id' => session('user.id')])->value('balance');
if ($balance < $whole) {
Db::rollback();
$this->success('您的积分不足', '', false);
} else {
//先减积分
$balance -= $whole;
if (Db::name('user')->update(['id' => session('user.id'), 'balance' => $balance])) {
//记录积分日志
$model = new IntegralModel;
$model->insertIntegralLog('', session('user.id'), time(), $whole, 2, '', $sn);
//支付后改变订单状态
$update = [
'id' => $oid,
'step' => 2,
'pay_time' => time()
];
if (Db::name('zj_order')->update($update)) {
Db::commit();
session('goods.id', null);
Db::name('zj_cart')->where(['id' => ['in', session('cart.id')]])->delete();
session('cart.id', null);
$this->success('积分支付成功', url('user/Center/orderList'), true);
} else {
Db::rollback();
}
} else {
Db::rollback();
}
}
} //组合支付
elseif ($order['pay_type'] == 2) {
$pay_cash = $request->param('pay_cash'); //支付的金额
$pay_num = $request->param('pay_num'); //支付的积分
$balance = Db::name('user')->where(['id' => session('user.id')])->value('balance');
if ($pay_cash > $balance) {
$this->success('您的积分不足', '', false);
} else {
//先减余额
$balance -= $pay_num;
Db::name('user')->update(['id' => session('user.id'), 'balance' => (int)$balance]);
//记录积分日志
$model = new IntegralModel;
$model->insertIntegralLog('', session('user.id'), time(), $pay_num, 2, '', $sn);
Db::commit();
session('goods.id', null);
Db::name('zj_cart')->where(['id' => ['in', session('cart.id')]])->delete();
session('cart.id', null);
$info = [
'attach' => $oid,
'openid' => session('user.openid'),
'body' => '微信支付-天生红商城',
'total_fee' => $pay_cash
];
$this->wxPay($info);
}
} else {
$this->success('请选择支付方式', '', false);
}
} else {
Db::rollback();
$this->success('未知错误', '', false);
}
} else {
Db::rollback();
$this->success('未知错误', '', false);
}
}
}
//个人中心微信支付
public function payFromCenter()
{
$request = request();
if ($request->isAjax()) {
$oid = $request->param('oid');
$total_fee = Db::name('zj_order')->where(['id' => $oid])->value('whole');
if (Db::name('zj_order')->where(['id' => $oid])->value('pay_type') == 2) {
$total_fee -= Db::name('zj_order')->where(['id' => $oid])->value('whole_num');
}
$info = [
'attach' => $oid,
'openid' => session('user.openid'),
'body' => '微信支付-天生红商城',
'total_fee' => $total_fee
];
$this->wxPay($info);
}
}
private function wxPay($info)
{
//只有未支付的订单才能唤起微信支付
if (Db::name('zj_order')->where(['id' => $info['attach']])->value('step') == 1) {
require_once EXTEND_PATH . '/Payment.php';
$pay = new \Payment($info['attach'], $info['openid'], $info['body'], $info['total_fee'] * 100);
$this->success('ok', url('user/center/orderList'), $pay->pay());
} else {
$this->success('该订单禁止支付', '', false);
}
}
public function notify()
{
require_once EXTEND_PATH . '/Payment.php';
$pay = new \Payment();
$notify = $pay->handleNotify();
if (!empty($notify)) {
$oid = $notify['attach'];
if (Db::name('zj_order')->where(['id' => $oid])->value('step') == 2) {
echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
exit();
} else {
//改变订单状态
$update = [
'id' => $oid,
'step' => 2,
'out_trade_no' => $notify['out_trade_no'],
'pay_time' => time()
];
if (Db::name('zj_order')->update($update)) {
//赠送积分逻辑
$this->integral($oid);
echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
exit();
}
}
}
}
//赠送积分模块
private function integral($oid)
{
//判断该订单是否属于赠送积分订单
$info = Db::name('zj_order')->field('uid,step,cid,pay_type')->where(['id' => $oid])->find();
if ($info['cid'] == 2 && $info['step'] == 2) {
if ($info['pay_type'] == 1 || $info['pay_type'] == 2) {
//计算该笔订单应给积分总额
$data = Db::name('zj_order_goods')->alias('o')
->field('g.price,o.num')
->join('zj_goods g', 'g.id=o.gid')
->where(['oid' => $oid])
->select();
$integral = 0;
foreach ($data as $item) {
$integral += $item['price'] * $item['num'] * 1.5;
}
$ratio = Db::name('zj_system')->where(['id' => 1])->value('integral');
$model = new IntegralModel;
//插入配送计划表,先将此积分的一半立刻给用户,剩余一半每日按固定百分比给用户,直到发完为止
$integral_half = $integral / 2;
Db::startTrans();
$integral_give_id = $model->insertIntegralGive($info['uid'], $ratio, $integral_half, $integral_half, time());
if ($integral_give_id) {
if ($model->insertIntegralLog($integral_give_id, $info['uid'], time(), $integral_half, 1)) {
if (Db::name('user')->where(['id' => $info['uid']])->setInc('balance', $integral_half)) {
Db::commit();
} else {
Db::rollback();
}
} else {
Db::rollback();
}
} else {
Db::rollback();
}
}
}
}
//获取购买商品的二级分类字符串
private function getSonCategoryStr($data)
{
$str = '';
foreach ($data as $item) {
$str .= '-' . Db::name('zj_goods')->where(['id' => $item['id']])->value('cid') . '-,';
}
return substr($str, 0, strlen($str) - 1);
}
}