<?php
require '../conn/conn.php';
require '../conn/function.php';
require '../conn/sendmail.php';

$APPID = $C_wxpay_appid;
$MCHID = $C_wxpay_mchid;
$KEY = $C_wxpay_key;
$APPSECRET = $C_wxpay_appsecret;

if($MCHID=="" || $KEY=="") {
	die();
}

$postArr = file_get_contents("php://input");
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string( $postArr );

$appid=$postObj->appid;
$attach=$postObj->attach;
$bank_type=$postObj->bank_type;
$cash_fee=$postObj->cash_fee;
$device_info=$postObj->device_info;
$fee_type=$postObj->fee_type;
$is_subscribe=$postObj->is_subscribe;
$mch_id=$postObj->mch_id;
$nonce_str=$postObj->nonce_str;
$openid=$postObj->openid;
$out_trade_no=$postObj->out_trade_no;
$result_code=$postObj->result_code;
$return_code=$postObj->return_code;
$time_end=$postObj->time_end;
$total_fee=$postObj->total_fee;
$trade_type=$postObj->trade_type;
$transaction_id=$postObj->transaction_id;
$sign=$postObj->sign;

$O=getrs("select * from sl_orders where O_no='$out_trade_no'");

if($O["O_mid"]==0){
    $M_id=0;
}else{
	$M=getrs("select * from sl_member where M_id=".$O["O_mid"]);
	$M_id=$M["M_id"];
}

if (strtolower(MD5("appid=".$appid."&attach=".$attach."&bank_type=".$bank_type."&cash_fee=".$cash_fee."&fee_type=".$fee_type."&is_subscribe=".$is_subscribe."&mch_id=".$mch_id."&nonce_str=".$nonce_str."&openid=".$openid."&out_trade_no=".$out_trade_no."&result_code=".$result_code."&return_code=".$return_code."&time_end=".$time_end."&total_fee=".$total_fee."&trade_type=".$trade_type."&transaction_id=".$transaction_id."&key=".$KEY))==strtolower($sign)) {
	if($result_code=="SUCCESS") {
	    echo notify($transaction_id,$out_trade_no,$M_id);
	}
} else {
	echo 'fail';
}

?>