审查视图

app/portal/controller/NotifyController.php 3.0 KB
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/6/13
 * Time: 17:53
 */

namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use think\Db;
郭鑫 authored
13
use api\portal\model\OrderModel;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

class NotifyController extends HomeBaseController
{

    // 微信支付回调
    public function notify() {
        require_once VENDOR_PATH."WxpayAPI/lib/WxPay.Api.php";
        require_once VENDOR_PATH."WxpayAPI/lib/WxPay.Notify.php";
        require_once VENDOR_PATH.'WxpayAPI/example/log.php';

        $post = $this->request->param();
        if($post==null){
            $post = file_get_contents("php://input");
            if($post == null){
                $post = $GLOBALS['HTTP_RAW_POST_DATA'];
            }
        }
        if(!empty($post)) {
            $xml = $post;  //微信的回调数据
            $base = new \WxPayResults();            //实例化数据对象结果类
            $data = $base->FromXml($xml);
            if($base->CheckSign() == true){
                if ($data["return_code"] == "SUCCESS") {
郭鑫 authored
37
                    $order_sn = $where['order_sn'] = $data['attach'];
38 39 40
                    if (!$where['order_sn']) {
                        $where['order_sn'] = $data['out_trade_no'];
                    }
郭鑫 authored
41 42
//                    $where['money'] = $data['total_fee']/100;
                    $where['money'] = 1;
郭鑫 authored
43
                    $order_model = new OrderModel();
郭鑫 authored
44 45 46
                    $orderInfo = $order_model->where(['order_sn'=>$order_sn])->find();
                    if($orderInfo['status'] == 1) {
                        $info = [
郭鑫 authored
47
                            'pay_type'=>1,
郭鑫 authored
48 49 50 51 52 53 54 55
                            'pay_time'=>time(),
                            'status'=>2,
                            'transaction_id'=>$data['transaction_id']
                        ];
                        $results = $order_model->where($where)->update($info);
                        if($results) {
                            $this->return_success();
                        }
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
                    }
                }
            }
        }
    }

    /*
     * 给微信发送确认订单金额和签名正确,SUCCESS信息
     */
    private function return_success(){
        $return['return_code'] = 'SUCCESS';
        $return['return_msg'] = 'OK';
        $xml_post = '<xml>
                    <return_code>'.$return['return_code'].'</return_code>
                    <return_msg>'.$return['return_msg'].'</return_msg>
                    </xml>';
        echo $xml_post;exit;
    }

    // 查询订单是否支付完成
    public function searchStatus() {
        if($this->request->isAjax()) {
            $data = $this->request->param();
            $order_sn = $data['order_sn'];
            $user_licence_model = Db::name('UserLicence');
            $status = $user_licence_model->where(['order_sn'=>$order_sn])->value('status');
            if($status == 1) {
                return ['code'=>1,'msg'=>'支付成功'];
            } else {
                return ['code'=>0,'msg'=>'未支付完成'];
            }
        }
    }
}