NotifyController.php 1.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/6/13
 * Time: 17:53
 */

namespace api\portal\controller;

use api\portal\controller\NotifyCommonController;

class NotifyController extends NotifyCommonController
{

    // 微信支付回调
    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") {
                    $where['order_sn'] = $data['attach'];
                    if (!$where['order_sn']) {
                        $where['order_sn'] = $data['out_trade_no'];
                    }
//                    $where['money'] = $data['total_fee']/100;
                    $result = $this->changeOrderStatus($where,$data['transaction_id'],1,'pc微信扫码支付');
                    if($result) {
                        $this->return_success();
                    }
                }
            }
        }
    }

    /*
     * 给微信发送确认订单金额和签名正确,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;
    }
}