<?php
header('Content-Type:text/html;charset=utf-8');
/**
 *	
 * 功能：接收并处理扫码支付状态
 * 改者：【梦行Monxin】积木式建站系统 www.monxin.com
 * 日期：2015-07-14
 *	
 */
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);

require_once "./lib/WxPay.Api.php";
require_once './lib/WxPay.Notify.php';

/*
require_once 'log.php';
//初始化日志
$logHandler= new CLogFileHandler("./logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
*/
class NativeNotifyCallBack extends WxPayNotify
{
	public function unifiedorder($openId, $product_id)
	{
		//统一下单
		$input = new WxPayUnifiedOrder();
		$input->SetBody("test");
		$input->SetAttach("test");
		$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
		$input->SetTotal_fee("1");
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag("test");
		$input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
		$input->SetTrade_type("NATIVE");
		$input->SetOpenid($openId);
		$input->SetProduct_id($product_id);
		$result = WxPayApi::unifiedOrder($input);
		//Log::DEBUG("unifiedorder:" . json_encode($result));
		return $result;
	}
	
	public function NotifyProcess($data, &$msg)
	{
		
		//file_put_contents('./r.txt',$data);
		
		//echo "处理回调";
		//Log::DEBUG("call back:" . json_encode($data));
		
		//if(!array_key_exists("openid", $data) || !array_key_exists("product_id", $data))
		if(!array_key_exists("openid", $data))
		{
			$msg = "回调数据异常";
			return false;
		}
		 
		$openid = $data["openid"];
		$product_id = $data["product_id"];
		
		//统一下单
		$result = $this->unifiedorder($openid, $product_id);
		if(!array_key_exists("appid", $result) ||
			 !array_key_exists("mch_id", $result) ||
			 !array_key_exists("prepay_id", $result))
		{
		 	$msg = "统一下单失败";
		 	return false;
		 }
		
		$this->SetData("appid", $result["appid"]);
		$this->SetData("mch_id", $result["mch_id"]);
		$this->SetData("nonce_str", WxPayApi::getNonceStr());
		$this->SetData("prepay_id", $result["prepay_id"]);
		$this->SetData("result_code", "SUCCESS");
		$this->SetData("err_code_des", "OK");
		
		
		//===============================================================================================================================更新充值状态 start	
		$config=require("./config.php");
		if($config['state']!='opening'){exit('closed');}
		$config=require('../../config.php');
		$timeoffset=($config['other']['timeoffset']>0)? "-".$config['other']['timeoffset']:str_replace("-","+",$config['other']['timeoffset']);
		date_default_timezone_set("Etc/GMT$timeoffset");
		
		$language=require('../../language/'.$config['web']['language'].'.php');
		require_once '../../config/functions.php';
		require_once '../../lib/ConnectPDO.class.php';
		$pdo=new  ConnectPDO();
		$id=$data["out_trade_no"];
		//file_put_contents('./rr.txt',$id);
		update_recharge($pdo,$config,$language,$id,$data['total_fee']/100);
		//===============================================================================================================================更新充值状态 end	
		return true;
	}
}

//Log::DEBUG("begin notify!");
$notify = new NativeNotifyCallBack();
$notify->Handle(true);
