<?php

$path=dirname(__FILE__);

while(!file_exists($path.'/lib/onezphp.php')){

  $path=dirname($path);

  if($path=='.'){

    exit('onezphp not exists');

  }

}

include($path.'/lib/onezphp.php');





if(empty($GLOBALS['HTTP_RAW_POST_DATA'])) {  

  $xml = file_get_contents("php://input");

}else{

  $xml = $GLOBALS["HTTP_RAW_POST_DATA"];

}

!$xml && exit('error');



$postObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

$OpenId = (string)$postObj->OpenId;

$AppId = (string)$postObj->AppId;

$IsSubscribe = (string)$postObj->IsSubscribe;

$TimeStamp = (string)$postObj->TimeStamp;

$NonceStr = (string)$postObj->NonceStr;

$AppSignature = (string)$postObj->AppSignature;

$SignMethod = (string)$postObj->SignMethod;

$orderno = (string)$postObj->out_trade_no;

$total_fee = $postObj->total_fee/100;



$order=onez('order')->load($orderno);

if($order['sitetoken'] && onez()->exists($order['sitetoken'])){

  $G['this']=onez($order['sitetoken']);

}



include_once(dirname(__FILE__)."/WxPayPubHelper.php");

//使用通用通知接口

$notify = new Notify_pub();

$notify->saveData($xml);



//验证签名，并回应微信。

//对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，

//微信会通过一定的策略（如30分钟共8次）定期重新发起通知，

//尽可能提高通知的成功率，但微信不保证通知最终能成功。



if($notify->checkSign() == FALSE){

  $notify->setReturnParameter("return_code","FAIL");//返回状态码

  $notify->setReturnParameter("return_msg","签名失败");//返回信息

}else{

  $notify->setReturnParameter("return_code","SUCCESS");//设置返回码

  onez('wxpay')->payback($orderno,$total_fee,json_decode(json_encode($postObj),1));

}

$returnXml = $notify->returnXml();



echo $returnXml;