<?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;
		}
		
		ErrorLogger::doOutput("Compass...api_wx_pay_notify.php.....out_trade_no=" . $data["out_trade_no"], 0);
		
		$mchid_order_no = $data["transaction_id"];
		$type = substr($data["out_trade_no"],0,1);
		
		switch($type){
			// 购买视频支付后处理
			case 'C':
				$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];
				
				//更新支付状态
				$mediaDat = CourseMediaDat::getById($userMediaDat->media_id);
				$userMediaDat->money=$mediaDat->price;
				$userMediaDat->mchid_order_no=$mchid_order_no;
				$userMediaDat->status="SUCCESS";
				$userMediaDat->save();
				
				//添加用户支付记录汇总
				$userMst = UserMst::getById($userMediaDat->user_id);
				$userPayLog = new UserPayLog();
				$userPayLog->user_id = $userMst->id;
				$userPayLog->user_name = $userMst->name;
				$userPayLog->action_type = "MEDIA";
				$userPayLog->money = $mediaDat->price;
				$userPayLog->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->mchid_order_no=$mchid_order_no;
				$userDonation->status = "SUCCESS";
				$userDonation->save();
				
				//添加用户支付记录汇总
				$userMst = UserMst::getById($userDonation->user_id);
				$userPayLog = new UserPayLog();
				$userPayLog->user_id = $userMst->id;
				$userPayLog->user_name = $userMst->name;
				$userPayLog->action_type = "DONATION";
				$userPayLog->money = $userDonation->money;
				$userPayLog->save();

				break;
			// 会员购买支付后处理
			case 'M':
				$param = array();
				$param["delete_flg"] = false;
				$param["status"] = "NEW";
				$param["order_no"] = $data["out_trade_no"];
				$userBuyMemberList = UserBuyMemberDat::getList($param);
				// 找到订单
				if (count($userBuyMemberList) < 1) {
					ErrorLogger::doOutput("Compass...api_wx_pay_notify...userBuyMember is null.");
					return false;
				}
				$userBuyMemberDat = $userBuyMemberList[0];
				//更新支付状态
				$userBuyMemberDat->mchid_order_no=$mchid_order_no;
				$userBuyMemberDat->status = "SUCCESS";
				$userBuyMemberDat->save();
				
				$userMst = UserMst::getById($userBuyMemberDat->user_id);
				
				//添加用户支付记录汇总
				$userPayLog = new UserPayLog();
				$userPayLog->user_id = $userMst->id;
				$userPayLog->user_name = $userMst->name;
				$userPayLog->action_type = "MEMBER";
				$userPayLog->money = $userBuyMemberDat->money;
				$userPayLog->save();
				
				//用户状态更新
				$userMst->is_member = true;
				$userMst->save();

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

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