<?php
require_once('global.php');
require_once(R_P.'require/posthost.php');

InitGP(array('action','out_trade_no','trade_status','buyer_email','notify_id'));

$url = '';
!empty($_POST) && $_GET = $_POST;
foreach ($_GET as $key => $value) {
	if ($key <> 'action' && $value) {
		$url .= "$key=".urlencode($value)."&";
	}
}

$veryfy_result2 = PostHost("http://notify.alipay.com/trade/notify_query.do","notify_id=$notify_id&partner=2088001505801569",'POST');
$veryfy_result1 = PostHost("http://pay.phpwind.net/pay/alipay_notify.php",$url,'POST');

if (!eregi("true$",$veryfy_result1) || !eregi("true$",$veryfy_result2)) {
	paymsg('userpay.php','alipay_failure','fail');
}

if (empty($action)) {

	include_once(D_P.'data/bbscache/ol_config.php');
	if (!$ol_onlinepay) {
		Showmsg($ol_whycolse);
	}
	if (!$ol_payto) {
		Showmsg('olpay_seterror');
	}
	$rt = $db->get_one('SELECT c.*,m.username FROM pw_clientorder c LEFT JOIN pw_members m USING(uid) WHERE order_no='.pwEscape($out_trade_no));
	if (!$rt) {
		paymsg('userpay.php','alipay_ordersfailure');
	}
	if (intval($rt['number']) != intval($_GET['total_fee']) || $_GET['seller_email'] != $ol_payto) {
		paymsg('userpay.php','alipay_failure');
	}
	if ($trade_status == 'TRADE_FINISHED') {
		if ($rt['state'] == 2) {
			paymsg('userpay.php','alipay_orderssuccess');
		}
		$rmbrate = $db_creditpay[$rt['paycredit']]['rmbrate'];
		!$rmbrate && $rmbrate = 10;
		$currency = $rt['number'] * $rmbrate;

		require_once(R_P.'require/credit.php');
		$credit->addLog('main_olpay',array($rt['paycredit'] => $currency),array(
			'uid'		=> $rt['uid'],
			'username'	=> $rt['username'],
			'ip'		=> $onlineip,
			'number'	=> $rt['number']
		));
		$credit->set($rt['uid'],$rt['paycredit'],$currency);

		$descrip = getLangInfo('other','alipay_orders');
		$db->update("UPDATE pw_clientorder SET payemail=".pwEscape($buyer_email).",state=2,descrip='$descrip' WHERE order_no=".pwEscape($out_trade_no));

		require_once(R_P.'require/msg.php');
		$message = array(
			'toUser'	=> $rt['username'],
			'subject'	=> 'olpay_title',
			'content'	=> 'olpay_content_2',
			'other'		=> array(
				'currency'	=> $currency,
				'cname'		=> $credit->cType[$rt['paycredit']],
				'number'	=> $rt['number']
			)
		);
		pwSendMsg($message);
		paymsg('userpay.php','alipay_orderssuccess');
	} else {
		paymsg('userpay.php','alipay_topayfailure');
	}
} elseif ($action == 'trade') {

	$rt = $db->get_one("SELECT tid,ifpay FROM pw_tradeorder WHERE order_no='$out_trade_no'");

	if (empty($rt) || $rt['ifpay'] == '3') {
		paymsg('index.php','alipay_ordersfailure');
	}
	switch ($trade_status) {
		case 'WAIT_SELLER_SEND_GOODS':
			$db->update("UPDATE pw_tradeorder SET ifpay='1',tradedate='$timestamp',payment='2' WHERE order_no='$out_trade_no'");
			break;
		case 'WAIT_BUYER_CONFIRM_GOODS':
			$db->update("UPDATE pw_tradeorder SET ifpay='2',tradedate='$timestamp' WHERE order_no='$out_trade_no'");
			break;
		case 'TRADE_FINISHED':
			$db->update("UPDATE pw_tradeorder SET ifpay='3',tradedate='$timestamp' WHERE order_no='$out_trade_no'");
			$db->update("UPDATE pw_trade SET salenum=salenum+1 WHERE tid=".pwEscape($rt['tid']));
			break;
		default:
			$db->update("UPDATE pw_tradeorder SET ifpay='4',tradedate='$timestamp' WHERE order_no='$out_trade_no'");
	}
	paymsg('index.php','operate_success');
}

function paymsg($url,$msg,$notify = 'success') {
	if (empty($_POST)) {
		refreshto($url,$msg);
	}
	exit($notify);
}
?>