审查视图

app/portal/controller/SomepayController.php 5.8 KB
郭鑫 authored
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
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use think\Db;



class SomepayController extends HomeBaseController
{
    public function _initialize()
    {
        parent::_initialize();
//        微信
        require_once EXTEND_PATH . "WxpayAPI/lib/WxPay.Api.php";
        require_once EXTEND_PATH . "WxpayAPI/example/WxPay.JsApiPay.php";
        require_once EXTEND_PATH . "WxpayAPI/lib/WxPay.Notify.php";
        require_once EXTEND_PATH . 'WxpayAPI/example/log.php';
    }

//    微信支付
    public function pay($openId , $price , $order_sn , $url,$content)
    {
        $price = intval($price * 100);
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($content);
        $input->SetAttach($content);
        $input->SetOut_trade_no($order_sn);
        $input->SetTotal_fee($price);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag($content);
        $input->SetNotify_url($url);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new \WxPayConfig();
        $order = \WxPayApi::unifiedOrder($config, $input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return json_decode($jsApiParameters);
    }


    //微信回调
    public function pay_notify()
    {
        $config = new \WxPayConfig();
        $notify = new \WxPayNotify();
        $notify->Handle($config, false);
        $xml = file_get_contents("php://input");
        $base = new \WxPayResults();
        $data = $base->FromXml($xml);
        //验签
        if ($base->CheckSign($config)) {
            if ($data["return_code"] == "SUCCESS") {
//                支付成功
郭鑫 authored
64
//                Db::startTrans();
郭鑫 authored
65
////                更新主表状态
郭鑫 authored
66 67 68 69 70 71
                $where_orderN['order_sn'] = $data['out_trade_no'];
                $where_orderN['status'] = 3;
                $order = Db::name('Order')->where($where_orderN)->find();
                if($order){
                    $up_order['status'] = 10;
                    $up_order['update_time'] = time();
郭鑫 authored
72
                    $up_order['pay_time'] = time();
郭鑫 authored
73 74
                    $up_order['trans_id'] = $data['transaction_id'];
                    $res_order = Db::name('Order')->where($where_orderN)->update($up_order);
郭鑫 authored
75
郭鑫 authored
76
                    if($res_order){
郭鑫 authored
77 78 79 80 81
//                        更新雇人表
                        $where_hire['id'] = $order['hire_id'];
                        $where_hire['status'] = 3;
                        $up_hire['status'] = 10;
                        $up_hire['update_time'] = time();
郭鑫 authored
82
                        $res_hire = Db::name('Hire')->where($where_hire)->update($up_hire);
郭鑫 authored
83
                        if($res_hire){
郭鑫 authored
84
                            Db::commit();
郭鑫 authored
85 86
                            return true;
                        }else{
郭鑫 authored
87
                            Db::rollback();
郭鑫 authored
88 89
                            return false;
                        }
郭鑫 authored
90
                    }else{
郭鑫 authored
91
                        Db::rollback();
郭鑫 authored
92 93 94
                        return false;
                    }
                }
郭鑫 authored
95 96 97 98 99 100

            }
        }
    }

郭鑫 authored
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
//    微信提现
    public function getWxpay($openid,$order_sn,$money){
        require_once EXTEND_PATH . 'WxpayAPI/WxPay.Merch.php';
        $desc = '青云职上提现';
        $MerchPay = new \MerchPay();
//        $money = 1;
        $result = $MerchPay->pay($openid, $order_sn, $money, $desc);
        if($result['result_code'] == "SUCCESS"){
            Db::startTrans();
            $where_order['order_sn'] = $result['partner_trade_no'];
            $where_order['status'] = 11;
            $order = Db::name('Order')->where($where_order)->find();
            if($order){
                $up_order['status'] = 12;
                $up_order['update_time'] = time();
                $res_order = Db::name('Order')->where($where_order)->update($up_order);
                if($res_order){
//                    减少店铺金额
                    $where_store['id'] = $order['store_id'];
                    $sall = Db::name('Store')->where($where_store)->setDec('money',$order['order_price']);
                    if(!$sall){
                        Db::rollback();
                        $this->apiResponse('0','失败3');
                    }
//                    加入提现记录
                    $add_log['store_id'] = $order['store_id'];
                    $add_log['order_id'] = $order['id'];
                    $add_log['user_id'] = $order['user_id'];
                    $add_log['money'] = $order['order_price'];
                    $add_log['status'] = 3;
                    $add_log['create_time'] = time();
                    $add_log['update_time'] = time();
                    $res_log = Db::name('GetMoney')->insertGetId($add_log);
                    if($res_log){
                        Db::commit();
                        $this->apiResponse('1','提现成功');
                    }else{
                        Db::rollback();
                        $this->apiResponse('0','提现失败');
                    }
                }else{
                    Db::rollback();
                    $this->apiResponse('0','获取订单失败2');
                }
            }else{
                Db::rollback();
                $this->apiResponse('0','获取订单失败');
            }
        }
郭鑫 authored
150
郭鑫 authored
151
    }
郭鑫 authored
152 153

}