<?php
// 微信支付结果通知
require_once ("../user_include.inc");

ErrorLogger::doOutput($_REQUEST);

class PayNotifyCallBack extends WxPayNotify
{
	//查询订单
	public function Queryorder($transaction_id)
	{
		$input = new WxPayOrderQuery();
		$input->SetTransaction_id($transaction_id);
		$result = WxPayApi::orderQuery($input);
		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 NotifyProcess($data, &$msg)
	{
		ErrorLogger::doOutput("Compass...api_wx_pay_notify...call back:" . json_encode($data));
		
		if(!array_key_exists("transaction_id", $data)){
			$msg = "输入参数不正确";
			return false;
		}
		//查询订单，判断订单真实性
		if(!$this->Queryorder($data["transaction_id"])){
			$msg = "订单查询失败";
			return false;
		}
		$type = substr($data["out_trade_no"],0,1);
		
		switch($type){
			// 购买视频支付后处理
			case 'M':
				$param = array();
				$param["delete_flg"] = false;
				$param["status"] = "NEW";
				$param["order_no"] = $data["out_trade_no"];
				$userMediaDatList = UserMediaDat::getList($param);
				// 找到订单
				if (count($userMediaDatList) < 1) {
					ErrorLogger::doOutput("Compass...api_wx_pay_notify...UserMediaDat is null.");
					return false;
				}
				$userMediaDat = $userMediaDatList[0];
				//找到对应的用户
				$user_mst = UserMst::getById($donation_dat->user_id);
				if ($user_mst == null) {
					ErrorLogger::doOutput("Compass...api_wx_pay_notify...user is null.");
					return false;
				}
				
				//更新支付状态
				$userMediaDat->status="SUCCESS";
				$userMediaDat->save();

				break;
				
			// 募捐支付后处理
			case 'D':
				$param = array();
				$param["delete_flg"] = false;
				$param["status"] = "NEW";
				$param["order_no"] = $data["out_trade_no"];
				$userDonationList = UserDonationDat::getList($param);
				// 找到订单
				if (count($userDonationList) < 1) {
					ErrorLogger::doOutput("Compass...api_wx_pay_notify...userDonation is null.");
					return false;
				}
				$userDonation = $userDonationList[0];
				//更新支付状态
				
				//更新用户账户
				$userDonation->status = "SUCCESS";
				$userDonation->save();

				break;
			default:
				break;
		}
		return true;
	}
}

$notify = new PayNotifyCallBack();
$notify->Handle(false);
