审查视图

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

class PayController extends HomeBaseController
{

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

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

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

                //插入配送计划表,先将此积分的一半立刻给用户,剩余一半每日按固定百分比给用户,直到发完为止
                $integral_half = $integral / 2;
                Db::startTrans();
lihan authored
264
                $integral_give_id = $model->insertIntegralGive(session('user.id'), $ratio, $integral_half, $integral, time());
lihan authored
265
                if ($integral_give_id) {
lihan authored
266
                    if ($model->insertIntegralLog($integral_give_id, session('user.id'), time(), $integral_half, 1)) {
lihan authored
267 268 269 270 271
                        if(Db::name('user')->where(['id'=>session('user.id')])->setInc('balance', $integral_half)) {
                            Db::commit();
                        }else {
                            Db::rollback();
                        }
lihan authored
272 273 274 275 276 277
                    } else {
                        Db::rollback();
                    }
                } else {
                    Db::rollback();
                }
lihan authored
278 279 280 281
            }
        }
    }
282
}