微信支付(下)--回調

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

裏面有個支付回調的設置,當用戶支付成功或者失敗的時候,微信會回調到這個(抓包是抓不到的,因爲沒有包),並且微信會給一些參數用於判斷是否支付成功的。這裏需要用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);

來返回給微信。

改了這個文件中



這個本來是說在基類重寫的,但我沒有繼承,所以在這修改


新增






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