PayController.php 3.1 KB
<?php
namespace app\pay\controller;
use cmf\controller\HomeBaseController;
use think\Db;

class PayController extends HomeBaseController
{

    function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        if (empty(session('user.id'))) {
            $this->error('登录失败');
        }
    }

    //提交订单
    public function done()
    {
        $request = request();
        if ($request->isAjax()) {
            $consignee = Db::name('zj_user_place')->field('name,province,city,county,mobile,place')
                ->where(['id' => $request->param('address_id'), 'uid' => session('user.id')])
                ->find();
            $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();
            $whole = 0;
            $orderGoods = [];
            foreach ($data as $item) {
                $whole += $item['num'] * $item['price'];
            }

            $order = [
                'order_num' => date('YmdHis') . rand(100000, 999999),
                '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' => 0,
                'pay_type' => $request->param('pay_type'),
                'step' => 1
            ];
            Db::startTrans();
            if (Db::name('zj_order')->insert($order)) {
                $oid = Db::name('zj_order')->getLastInsID();
                foreach ($data as $k => $v) {
                    $orderGoods[$k] = [
                        'oid' => $oid,
                        'gid' => $v['id'],
                        'num' => $v['num']
                    ];
                }
                if (Db::name('zj_order_goods')->insertAll($orderGoods)) {
                    Db::commit();
                    if ($order['pay_type'] == 1) {
                        $info = [
                            'attach' => $oid,
                            'openid' => session('openid'),
                            'body' => '微信支付-' . $order['name'],
                            'total_fee' => $order['whole']
                        ];
                        $this->wxPay($info);
                    }
                }
            }
        }
    }

    //微信支付
    public function wxPay($info)
    {
        require_once EXTEND_PATH . '/Payment.php';
        $pay = new \Payment($info['attach'], $info['openid'], $info['body'], $info['total_fee']);
        $this->success('微信支付', '', $pay->pay());
    }

    public function notify() {
        require_once EXTEND_PATH . '/Payment.php';
        $pay = new \Payment();
        $pay->handleNotify();
    }

}