<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "paylib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";
require_once 'log.php';

header("Content-type: text/html; charset=utf-8"); 

//1.商户订单号
$strMchBillNo = $_REQUEST["mchid"];
$fltFreeTotal = $_REQUEST["freeT"];
$fltFreeRet = $_REQUEST["freeR"];
//$_REQUEST["out_trade_no"]= "122531270220150304194108";
///$_REQUEST["total_fee"]= "1";
//$_REQUEST["refund_fee"] = "1";

//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

function printf_info($data)
{
    foreach($data as $key=>$value){
        echo "<font color='#f00;'>$key</font> : $value <br/>";
    }
}

	//根据商户单号支付
	$out_trade_no = $strMchBillNo;
	$total_fee = $fltFreeTotal;
	$refund_fee = $fltFreeRet;
	$input = new WxPayRefund();
	$input->SetOut_trade_no($out_trade_no);
	$input->SetTotal_fee($total_fee);
	$input->SetRefund_fee($refund_fee);
    $input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
    $input->SetOp_user_id(WxPayConfig::MCHID);
	//输出处理结果
	$retbag = WxPayApi::refund($input);
	//printf_info(WxPayApi::refund($input));
	$strRetCode=strtoupper($retbag["return_code"]);
	$strResCode=strtoupper($retbag["result_code"]);
	if($strRetCode=='SUCCESS')
	{
		if($strResCode=='SUCCESS')
		{
			$strRefundNo=$retbag[out_refund_no];
			echo "1".$strRefundNo;
		}
		else
		{
			echo "2";
		}
	}
	else
	{
		echo "0";
	}
	//echo '1:'.$retbag["result_code"];
	//echo '2:'.$retbag["return_code"];
	//exit();
/*
if(isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != ""){
	$transaction_id = $_REQUEST["transaction_id"];
	$total_fee = $_REQUEST["total_fee"];
	$refund_fee = $_REQUEST["refund_fee"];
	$input = new WxPayRefund();
	$input->SetTransaction_id($transaction_id);
	$input->SetTotal_fee($total_fee);
	$input->SetRefund_fee($refund_fee);
    $input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
    $input->SetOp_user_id(WxPayConfig::MCHID);
	printf_info(WxPayApi::refund($input));
	exit();
}

if(isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != ""){
	$out_trade_no = $_REQUEST["out_trade_no"];
	$total_fee = $_REQUEST["total_fee"];
	$refund_fee = $_REQUEST["refund_fee"];
	$input = new WxPayRefund();
	$input->SetOut_trade_no($out_trade_no);
	$input->SetTotal_fee($total_fee);
	$input->SetRefund_fee($refund_fee);
    $input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
    $input->SetOp_user_id(WxPayConfig::MCHID);
	printf_info(WxPayApi::refund($input));
	exit();
}
*/
?>
