微信支付(下)--回调

不懂支付的请看我的上一篇文章

里面有个支付回调的设置,当用户支付成功或者失败的时候,微信会回调到这个(抓包是抓不到的,因为没有包),并且微信会给一些参数用于判断是否支付成功的。这里需要用input的来接收。微信给的参数是xml格式的参数。重要,会滴函数里结束后我们也要返回参数给微信,而且也是要xml格式的,不然微信会每隔几十秒通知我们支付是否成功。所以一定得返回,而且返回格式要正确,这是一个坑。上代码

1、新建一个控制器用于做回调的,代码如下

require_once INIT_PATH . "vendor/wxpay/lib/WxPay.Api.php";
require_once INIT_PATH . "vendor/wxpay/lib/WxPay.Notify.php";
require_once INIT_PATH . "vendor/wxpay/log.php";

InitPHP::import('web/components/baseController.php');
class notifyController extends baseController
{

    public $initphp_list = [
        'run',
    ];

    public function run()
    {
        $notify = new WxPayNotify();
        $notify->Handle();
        $data = $notify->returnValue();
        writeLog(json_encode($data),'wx_callback');
        $msg = $data["transaction_id"];
        if(!array_key_exists("transaction_id", $data)){
            $msg .= "输入参数不正确";
            writeLog($msg,'wx_callback');
            $notify->SetReturn_code("FAIL");
            $notify->SetReturn_msg($msg);
            $notify->ReplyNotify(false);
            exit;
        }
        if (empty($data['out_trade_no'])) {
            $msg .= '查无订单';
            writeLog($msg,'wx_callback');
            $notify->SetReturn_code("FAIL");
            $notify->SetReturn_msg($msg);
            $notify->ReplyNotify(false);
            exit;
        }
        $info = $this->getMemberCardOrderDao()->getOneByField(['order_id' => $data['out_trade_no']]);

        if (!isset($info['id'])) {
            $msg .= '无订单信息';
            writeLog($msg,'wx_callback');
            $notify->SetReturn_code("FAIL");
            $notify->SetReturn_msg($msg);
            $notify->ReplyNotify(false);
            exit;
        }
        if ($info['status'] == 1) {
            $msg .= '订单已存在';
            writeLog($msg,'wx_callback');
            $notify->SetReturn_code("FAIL");
            $notify->SetReturn_msg($msg);
            $notify->ReplyNotify(false);
            exit;
        }
        $updateData = [
            'status' => 1,
            'pay_time' => time(),
        ];

        $userInfo = $this->getMemberDao()->getOne($info['uid']);
        if (!isset($userInfo['id'])) {
            $msg .= '无该会员信息,user_info='.json_encode($info);
            writeLog($msg,'wx_callback');
            $notify->SetReturn_code("FAIL");
            $notify->SetReturn_msg($msg);
            $notify->ReplyNotify(false);
            exit;
        }

        $cardTime = (int)$this->getMemberCardOrderDao()->getCardType($info['card_type']);
        $this->getMemberCardOrderDao()->update($info['id'],$updateData);

        if ($userInfo['card_expire_at'] == '-1') {
            $card_expire_at = $cardTime + time();
        } else {
            $card_expire_at = intval($userInfo['card_expire_at']);
            $card_expire_at = $card_expire_at + $cardTime;
        }
        $this->getMemberDao()->update($userInfo['id'],array('card_expire_at' => $card_expire_at));
        $notify->ReplyNotify(false);
        exit;
    }

    /**
     *
     * @return memberCardOrderDao
     * */
    private function getMemberCardOrderDao()
    {
        return InitPHP::getDao('memberCardOrder');
    }
}

注意:这里最好的是继承自己的基类,不继承基类,到时插入数据库又不好操作,因为这个是我们不能才微信回调这里跳转,所以这里我改了一下sdk的代码

<?php
/**
 * 
 * 回调基础类
 * @author widyhu
 *
 */
class WxPayNotify extends WxPayNotifyReply
{
	private $returnData = [];
    /**
	 * 
	 * 回调入口
	 */
	final public function Handle()
	{
		//当返回false的时候,表示notify中调用NotifyCallBack回调失败获取签名校验失败,此时直接回复失败
		$result = WxpayApi::notify(array($this, 'NotifyCallBack'), $msg);
		if($result == false){
			$this->SetReturn_code("FAIL");
			$this->SetReturn_msg($msg);
			$this->ReplyNotify(false);
			return;
		} else {
			//该分支在成功回调到NotifyCallBack方法,处理完成之后流程
			$this->SetReturn_code("SUCCESS");
			$this->SetReturn_msg("OK");
		}
		//$this->ReplyNotify($needSign);
	}
	
	/**
	 * 
	 * 回调方法入口,子类可重写该方法
	 * 注意:
	 * 1、微信回调超时时间为2s,建议用户使用异步处理流程,确认成功之后立刻回复微信服务器
	 * 2、微信服务器在调用失败或者接到回包为非确认包的时候,会发起重试,需确保你的回调是可以重入
	 * @param array $data 回调解释出的参数
	 * @param string $msg 如果回调处理失败,可以将错误信息输出到该方法
	 * @return true回调出来完成不需要继续回调,false回调处理未完成需要继续回调
	 */
	public function NotifyProcess($data, &$msg)
	{

        //Log::DEBUG("call back:" . json_encode($data));
        $notfiyOutput = array();

        if(!array_key_exists("transaction_id", $data)){
            $msg = "输入参数不正确";
            return false;
        }
        //查询订单,判断订单真实性
        if(!$this->Queryorder($data["transaction_id"])){
            $msg = "订单查询失败";
            return false;
        }
		return true;
	}
    //查询订单
    public function Queryorder($transaction_id)
    {
        $input = new WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = WxPayApi::orderQuery($input);
        //Log::DEBUG("query:" . json_encode($result));
        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS")
        {
            return true;
        }
        return false;
    }
    public function returnValue()
    {
        return $this->returnData;
    }
	
	/**
	 * 
	 * notify回调方法,该方法中需要赋值需要输出的参数,不可重写
	 * @param array $data
	 * @return true回调出来完成不需要继续回调,false回调处理未完成需要继续回调
	 */
	final public function NotifyCallBack($data)
	{
		$msg = "OK";
		$result = $this->NotifyProcess($data, $msg);
		
		if($result == true){
		    $this->returnData = $data;
			$this->SetReturn_code("SUCCESS");
			$this->SetReturn_msg("OK");
		} else {
			$this->SetReturn_code("FAIL");
			$this->SetReturn_msg($msg);
		}
		return $result;
	}
	
	/**
	 * 
	 * 回复通知
	 * @param bool $needSign 是否需要签名输出
	 */
	final public function ReplyNotify($needSign = true)
	{
		//如果需要签名
		if($needSign == true &&
			$this->GetReturn_code() == "SUCCESS")
		{
			$this->SetSign();
		}
		WxpayApi::replyNotify($this->ToXml());
	}
}

注意控制器要调用 

$notify->ReplyNotify(false);

来返回给微信。

改了这个文件中



这个本来是说在基类重写的,但我没有继承,所以在这修改


新增






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章