<?php
 $DayMoneyValue=1000;
 $req = 'cmd=_notify-validate';
 foreach ($_POST as $key => $value)
 {
  $value = urlencode(stripslashes($value));
  $req .= "&$key=$value";
 }
 $host= "www.westpay.com.cn";
 $port= 80;
 $path="/pay/ISPN.asp";
 $url="http://".$host.$path;
 $referer="http://".$host;
 $len=strlen($req);
 $MerchantOrderNumber = $_POST['MerchantOrderNumber']; //和商户支付命令中的订单号相同
 $WestPayOrderNumber = $_POST['WestPayOrderNumber'];
 $PaidAmount = $_POST['PaidAmount']; //WestPay传回的实际支付金额。
 $MerchantID =$_POST['MerchantID'];

 $fp = fsockopen ($host, 80, $errno, $errstr, 30);
 if (!$fp)
 {
  echo "$errstr ($errno)<br>\n";
 }
 else
 {
$request="POST $url HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, *
Referer: $referer
Accept-Language: gb2312
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
Host: $host
Content-Length: $len
Proxy-Connection: Keep-Alive
Pragma: no-cache

$req";

  fputs ($fp, "$request");
  while (!feof($fp))
  {
   $res = fgets ($fp, 1024);
   if (strcmp ($res, "VERIFIED") == 0)
   {
	   if ($MerchantID = $MerchantID ) { //'判断此订单是不是该商户的订单。
		$msg = "支付通知验证成功";
		$state= 1;
	   } else {
		$state=2;
	   }
   }
   else if (strcmp ($res, "INVALID") == 0)
   {
    $msg = "支付通知验证失败";
    $state = 3;
   }
   else
   {
    $msg = "支付通知验证过程中出现错误";
    $state = 4;
   }
  }
  fclose ($fp);
 }

if ($state==1)
	//注：商户必须根据我们传回商户原始订单号找到原始订单，比较实付金额和原始订单金额，相同才是支付成功。
	if ($PaidAmount = $PaidAmount)
   		$PageMain=Insert_Back_value($MerchantOrderNumber,$PaidAmount);
		//1. 更改订单状态为已发货
		//2. 发卡
	else
		$PageMain=err("支付失败!","member.php","");
else
	$PageMain=err("签名认证错误!","member.php","");
?>