审查视图

app/portal/controller/SomepayController.php 3.5 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
<?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");
郭鑫 authored
61 62
        $a['a'] ='333';
        Db::name('Test')->insertGetId($a);
郭鑫 authored
63
        $base = new \WxPayResults();
郭鑫 authored
64 65
        $a['a'] ='888';
        Db::name('Test')->insertGetId($a);
郭鑫 authored
66
        $data = $base->FromXml($xml);
郭鑫 authored
67
        $a['a'] ='234';
郭鑫 authored
68
        $a['f'] = json_encode($data);
郭鑫 authored
69
        Db::name('Test')->insertGetId($a);
郭鑫 authored
70 71 72
        //验签
        if ($base->CheckSign($config)) {
            if ($data["return_code"] == "SUCCESS") {
郭鑫 authored
73
                $a['a'] ='99';
郭鑫 authored
74 75 76
                $a['c'] = json_decode($data);
                Db::name('Test')->insertGetId($a);
//                支付成功
郭鑫 authored
77
                Db::startTrans();
郭鑫 authored
78
////                更新主表状态
郭鑫 authored
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
                $where_orderN['order_sn'] = $data['out_trade_no'];
                $where_orderN['status'] = 3;
                $where_orderN['pay_time'] = time();
                $order = Db::name('Order')->where($where_orderN)->find();
                if($order){
                    $up_order['status'] = 10;
                    $up_order['update_time'] = time();
                    $up_order['trans_id'] = $data['transaction_id'];
                    $res_order = Db::name('Order')->where($where_orderN)->update($up_order);
                    if($res_order){
                        Db::commit();
                        return true;
                    }else{
                        Db::rollback();
                        return false;
                    }
                }
郭鑫 authored
96 97 98 99 100 101 102 103 104

            }
        }
    }




}