审查视图

app/pay/controller/PayController.php 12.9 KB
1 2
<?php
namespace app\pay\controller;
lihan authored
3
use cmf\controller\PayBaseController;
lihan authored
4
use app\integral\model\IntegralModel;
lihan authored
5
use think\Db;
6
lihan authored
7
class PayController extends PayBaseController
8 9 10 11 12 13
{

    //提交订单
    public function done()
    {
        $request = request();
lihan authored
14
        $data = [];
lihan authored
15
        if ($request->isAjax()) {
lihan authored
16
            if (empty($request->param('address_id'))) {
lihan authored
17 18
                $this->success('请填写配送地址', '', false);
            }
lihan authored
19 20 21
            if (empty($request->param('pay_type'))) {
                $this->success('请选择支付方式', '', false);
            }
lihan authored
22
            $consignee = Db::name('zj_user_place')->field('name,province,city,county,mobile,place')
lihan authored
23 24
                ->where(['id' => $request->param('address_id'), 'uid' => session('user.id')])
                ->find();
lihan authored
25
            if (session('cart.id') != null && session('goods.id') == null) {
lihan authored
26
                //通过购物车
lihan authored
27 28 29 30 31 32
                $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) {
lihan authored
33
                //直接购买
lihan authored
34
                $data = Db::name('zj_goods')
lihan authored
35
                    ->field('price,id')
lihan authored
36
                    ->where(['id' => session('goods.id')])
lihan authored
37 38
                    ->select();
            } else {
lihan authored
39
                $this->success('您已下过该订单', '', false);
lihan authored
40
            }
lihan authored
41 42 43
            $whole = 0;
            $orderGoods = [];
            foreach ($data as $item) {
lihan authored
44 45 46
                if (session('cart.id') == null && session('goods.id') != null) {
                    $item['num'] = 1;
                }
lihan authored
47 48
                $whole += $item['num'] * $item['price'];
            }
lihan authored
49 50 51 52 53 54 55
            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;
            }
lihan authored
56
            $sn = date('YmdHis') . rand(100000, 999999);
57
            $order = [
lihan authored
58
                'order_num' => $sn,
59 60
                'step' => 1,
                'uid' => session('user.id'),
lihan authored
61 62 63
                'site' => $consignee['province'] . $consignee['city'] . $consignee['county'] . $consignee['place'],
                'name' => $consignee['name'],
                'mobile' => $consignee['mobile'],
64 65
                'remark' => $request->param('remark'),
                'create_time' => time(),
lihan authored
66
                'whole' => $whole,
lihan authored
67
                'whole_num' => $whole_num,
lihan authored
68
                'pay_type' => $request->param('pay_type'),
lihan authored
69 70
                'step' => 1,
                //购买同时加入一级分类,用于判断是否赠送积分用
lihan authored
71 72 73
                'cid' => $request->param('cid'),
                //二级分类字符串-1-,-2-,-3-
                'cids' => $this->getSonCategoryStr($data),
lihan authored
74 75 76
                'province' => $consignee['province'],
                'city' => $consignee['city'],
                'county' => $consignee['county'],
lihan authored
77
                'place' => $consignee['place']
78
            ];
lihan authored
79 80 81 82
            Db::startTrans();
            if (Db::name('zj_order')->insert($order)) {
                $oid = Db::name('zj_order')->getLastInsID();
                foreach ($data as $k => $v) {
lihan authored
83 84 85
                    if (session('cart.id') == null && session('goods.id') != null) {
                        $v['num'] = 1;
                    }
lihan authored
86 87 88
                    $orderGoods[$k] = [
                        'oid' => $oid,
                        'gid' => $v['id'],
lihan authored
89
                        'num' => $v['num'],
lihan authored
90
                        'price' => Db::name('zj_goods')->where(['id' => $v['id']])->value('price')
lihan authored
91 92 93
                    ];
                }
                if (Db::name('zj_order_goods')->insertAll($orderGoods)) {
lihan authored
94
                    //微信支付
lihan authored
95
                    if ($order['pay_type'] == 1) {
lihan authored
96 97 98 99
                        Db::commit();
                        session('goods.id', null);
                        Db::name('zj_cart')->where(['id' => ['in', session('cart.id')]])->delete();
                        session('cart.id', null);
lihan authored
100 101
                        $info = [
                            'attach' => $oid,
lihan authored
102
                            'openid' => session('user.openid'),
lihan authored
103
                            'body' => '微信支付-天生红商城',
lihan authored
104 105 106
                            'total_fee' => $order['whole']
                        ];
                        $this->wxPay($info);
lihan authored
107
                    } //积分支付
lihan authored
108 109
                    elseif ($order['pay_type'] == 3) {
                        //先判断剩余积分是否足够支付
lihan authored
110 111
                        $balance = Db::name('user')->where(['id' => session('user.id')])->value('balance');
                        if ($balance < $whole) {
lihan authored
112
                            Db::rollback();
lihan authored
113
                            $this->success('您的积分不足', '', false);
lihan authored
114
                        } else {
lihan authored
115
                            //先减积分
lihan authored
116 117
                            $balance -= $whole;
                            if (Db::name('user')->update(['id' => session('user.id'), 'balance' => $balance])) {
lihan authored
118 119
                                //记录积分日志
                                $model = new IntegralModel;
lihan authored
120
                                $model->insertIntegralLog('', session('user.id'), time(), $whole, 2, '', $sn);
lihan authored
121 122 123 124
                                //支付后改变订单状态
                                $update = [
                                    'id' => $oid,
                                    'step' => 2,
lihan authored
125
                                    'pay_time' => time()
lihan authored
126
                                ];
lihan authored
127
                                if (Db::name('zj_order')->update($update)) {
lihan authored
128
                                    Db::commit();
lihan authored
129 130 131
                                    session('goods.id', null);
                                    Db::name('zj_cart')->where(['id' => ['in', session('cart.id')]])->delete();
                                    session('cart.id', null);
lihan authored
132
                                    $this->success('积分支付成功', url('user/Center/orderList'), true);
lihan authored
133
                                } else {
lihan authored
134 135
                                    Db::rollback();
                                }
lihan authored
136
                            } else {
lihan authored
137 138 139
                                Db::rollback();
                            }
                        }
lihan authored
140 141
                    } //组合支付
                    elseif ($order['pay_type'] == 2) {
lihan authored
142 143 144
                        $pay_cash = $request->param('pay_cash');    //支付的金额
                        $pay_num = $request->param('pay_num');      //支付的积分
                        $balance = Db::name('user')->where(['id' => session('user.id')])->value('balance');
lihan authored
145
                        if ($pay_cash > $balance) {
lihan authored
146 147
                            $this->success('您的积分不足', '', false);
                        } else {
lihan authored
148 149 150 151 152
                            //先减余额
                            $balance -= $pay_num;
                            Db::name('user')->update(['id' => session('user.id'), 'balance' => (int)$balance]);
                            //记录积分日志
                            $model = new IntegralModel;
lihan authored
153
                            $model->insertIntegralLog('', session('user.id'), time(), $pay_num, 2, '', $sn);
lihan authored
154 155 156 157 158 159 160 161 162 163 164
                            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);
lihan authored
165
                        }
lihan authored
166 167
                    } else {
                        $this->success('请选择支付方式', '', false);
lihan authored
168
                    }
lihan authored
169 170
                } else {
                    Db::rollback();
lihan authored
171
                    $this->success('未知错误', '', false);
lihan authored
172
                }
lihan authored
173 174
            } else {
                Db::rollback();
lihan authored
175
                $this->success('未知错误', '', false);
lihan authored
176
            }
177 178 179
        }
    }
lihan authored
180
181
    //个人中心微信支付
lihan authored
182 183
    public function payFromCenter()
    {
184
        $request = request();
lihan authored
185
        if ($request->isAjax()) {
186
            $oid = $request->param('oid');
lihan authored
187 188 189 190
            $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');
            }
191 192 193 194
            $info = [
                'attach' => $oid,
                'openid' => session('user.openid'),
                'body' => '微信支付-天生红商城',
lihan authored
195
                'total_fee' => $total_fee
196 197 198 199 200 201
            ];
            $this->wxPay($info);
        }
    }

    private function wxPay($info)
202
    {
lihan authored
203
        //只有未支付的订单才能唤起微信支付
lihan authored
204
        if (Db::name('zj_order')->where(['id' => $info['attach']])->value('step') == 1) {
lihan authored
205
            require_once EXTEND_PATH . '/Payment.php';
lihan authored
206
            $pay = new \Payment($info['attach'], $info['openid'], $info['body'], $info['total_fee'] * 100);
lihan authored
207
            $this->success('ok', url('user/center/orderList'), $pay->pay());
lihan authored
208
        } else {
lihan authored
209 210
            $this->success('该订单禁止支付', '', false);
        }
lihan authored
211
    }
212
lihan authored
213 214
    public function notify()
    {
lihan authored
215
        require_once EXTEND_PATH . '/Payment.php';
lihan authored
216
        $pay = new \Payment();
lihan authored
217
        $notify = $pay->handleNotify();
lihan authored
218
        if (!empty($notify)) {
lihan authored
219
            $oid = $notify['attach'];
lihan authored
220
            if (Db::name('zj_order')->where(['id' => $oid])->value('step') == 2) {
lihan authored
221 222
                echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                exit();
lihan authored
223
            } else {
lihan authored
224 225 226
                //改变订单状态
                $update = [
                    'id' => $oid,
227
                    'step' => 2,
lihan authored
228 229
                    'out_trade_no' => $notify['out_trade_no'],
                    'pay_time' => time()
lihan authored
230
                ];
lihan authored
231
                if (Db::name('zj_order')->update($update)) {
lihan authored
232
                    //赠送积分逻辑
lihan authored
233
                    $this->integral($oid);
lihan authored
234 235 236 237 238
                    echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                    exit();
                }
            }
        }
239 240
    }
lihan authored
241
    //赠送积分模块
lihan authored
242
    private function integral($oid)
lihan authored
243
    {
lihan authored
244
        //判断该订单是否属于赠送积分订单
lihan authored
245
        $info = Db::name('zj_order')->field('uid,step,cid,pay_type')->where(['id' => $oid])->find();
lihan authored
246 247
        if ($info['cid'] == 2 && $info['step'] == 2) {
            if ($info['pay_type'] == 1 || $info['pay_type'] == 2) {
lihan authored
248 249
                //计算该笔订单应给积分总额
                $data = Db::name('zj_order_goods')->alias('o')
lihan authored
250
                    ->field('g.price,o.num')
lihan authored
251
                    ->join('zj_goods g', 'g.id=o.gid')
lihan authored
252
                    ->where(['oid' => $oid])
lihan authored
253 254 255
                    ->select();
                $integral = 0;
                foreach ($data as $item) {
256
                    $integral += $item['price'] * $item['num'] * 2;
lihan authored
257
                }
lihan authored
258 259 260 261 262 263 264

                $ratio = Db::name('zj_system')->where(['id' => 1])->value('integral');
                $model = new IntegralModel;

                //插入配送计划表,先将此积分的一半立刻给用户,剩余一半每日按固定百分比给用户,直到发完为止
                $integral_half = $integral / 2;
                Db::startTrans();
265
                $integral_give_id = $model->insertIntegralGive($info['uid'], $ratio, $integral_half, $integral_half, time());
lihan authored
266
                if ($integral_give_id) {
lihan authored
267
                    if ($model->insertIntegralLog($integral_give_id, $info['uid'], time(), $integral_half, 1)) {
lihan authored
268
                        if (Db::name('user')->where(['id' => $info['uid']])->setInc('balance', $integral_half)) {
lihan authored
269
                            Db::commit();
lihan authored
270
                        } else {
lihan authored
271 272
                            Db::rollback();
                        }
lihan authored
273 274 275 276 277 278
                    } else {
                        Db::rollback();
                    }
                } else {
                    Db::rollback();
                }
lihan authored
279 280 281 282
            }
        }
    }
lihan authored
283 284 285 286 287 288 289 290 291 292
    //获取购买商品的二级分类字符串
    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);
    }
293
}