NotifyController.class.php 2.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/5/12
 * Time: 15:09
 */

namespace Portal\Controller;

use Common\Controller\HomebaseController;

class NotifyController extends HomebaseController {

    function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub
        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';
    }

    // pc端微信支付回调
    public function notify_web() {
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];  //微信的回调数据
        $base = new \WxPayResults();            //实例化数据对象结果类
        $data = $base->FromXml($xml);
        if($base->CheckSign() == true){
            if ($data["return_code"] == "SUCCESS") {
                $where['order_sn'] = $data['attach'];
                if(!$where['order_sn']) {
                    $where['order_sn'] = $data['out_trade_no'];
                }
                $where['price_count'] = $data['total_fee'];
                $info['pay_sort'] = 1;
                $info['ptime'] = time();
                $info['status'] = 2;
                $info['transaction_id'] = $data['transaction_id'];
                $order_model = D('Common/Order');
                $result = $order_model->where($where)->save($info);
                if($result) {
                    $orderInfo = $order_model->where($where)->find();
                    if($orderInfo['sheet']&&$orderInfo['sheet_id']) {
                        $sheet_model = D('Common/'.$orderInfo['sheet'].'Apply');
                        $word = substr(lcfirst($orderInfo['sheet']),0,1);
                        $sheet_model->where(array($word.'id'=>$orderInfo['sheet_id']))->save(array('is_pay'=>1));
                    }
//                    $pay_log_model = D('Common/PayLog');
                }
            }
        }
    }

    // 查询订单是否支付完成
    public function searchStatus() {
        if(IS_AJAX) {
            $order_sn = I('post.order_sn');
            $order_model = D('Common/Order');
            $status = $order_model->where(array('order_sn'=>$order_sn))->getField('status');
            if($status >= 2 && $status <6) {
                $this->ajaxReturn(array('status'=>true,'msg'=>'支付成功'));
            } else {
                $this->ajaxReturn(array('status'=>false,'msg'=>'未支付完成'));
            }
        }
    }
}