<%
''''''''''''''''''''''''''''''''''''''''''
' 该示范程序接收动感支付发来的支付成功通知，验证其真实性，并作相应的自动处理
''''''''''''''''''''''''''''''''''''''''''

option explicit

'接收来自动感的支付通知。该通知由类似如下的form发出
'<form name="myform" action="MerchantPostBackURL" method="post">
'	<input type="hidden" name="WestPayOrderNumber"  value="123456">
'	<input type="hidden" name="MerchantOrderNumber" value="20030101100001">
'	<input type="hidden" name="PaidAmount" value="0.01">
'	<input type="hidden" name="OrderDate" value="20030101">
' ...
' ...
'</form>

dim MerchantOrderNumber, WestPayOrderNumber, PaidAmount, MerchantID

MerchantOrderNumber=request("MerchantOrderNumber") '和商户支付命令中的订单号相同
WestPayOrderNumber=request("WestPayOrderNumber")
PaidAmount=ccur("0"&request("PaidAmount"))  '动感传回的实际支付金额。用CCUR转为数字型。
'注：商户必须根据我们传回商户原始订单号找到原始订单，比较实付金额和原始订单金额，相同才是支付成功。
MerchantID=request("MerchantID")
'注：商户必须判断此商户ID是不是您的商户ID

Dim objHttp, str

Dim PaymentCompleted
PaymentCompleted=false

' 准备回传支付通知表单
str = Request.Form & "&cmd=validate"

'set objHttp = Server.CreateObject("Msxml21.ServerXMLHTTP")
'set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.4.0")
'set objHttp = Server.CreateObject("Microsoft.XMLHTTP")
set objHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
 
'把动感传来的通知内容再传回到动感作验证以确保通知信息的真实性 
 
objHttp.open "POST", "http://www.WestPay.com.cn/pay/ISPN.asp", false
'ISPN: Instant Secure Payment Notification

objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHttp.Send str

if (objHttp.status <> 200 ) then
	    'HTTP 错误处理
		response.Write("Status="&objHttp.status)
elseif (objHttp.responseText = "VERIFIED") then
	
		'支付通知验证成功
		if MerchantID="9911.com.cn" then '判断此订单是不是该商户的订单。
			PaymentCompleted=true
		end if
	
elseif (objHttp.responseText = "INVALID") then
		'支付通知验证失败
		response.Write("Invalid")
else
		'支付通知验证过程中出现错误
		response.Write("Error")
end if
set objHttp = nothing
%>
<!--#include file="../conn/conn.asp"-->
<!--#include file="md5.asp"-->
<html>
<body>


<%
if PaymentCompleted then	'支付成功的处理...（以下程序仅供参考）
%>

	<%
	
	Dim rs, rss,userleixing,c_username,C_Merchant_url,urlback,urlback2,mydata,userid,rs1
	set rs=server.createobject("adodb.recordset")
		rs.open "select * from snsn_pay where C_Merchant_oid="&cstr(MerchantOrderNumber),conn,1,3
		if rs.recordcount=0 then 
response.write "<script LANGUAGE='javascript'>alert('对不起！错误！');history.go(-1);</script>"
else

	'注：商户必须根据我们传回商户原始订单号找到原始订单，比较实付金额和原始订单金额，相同才是支付成功。
	if rs("C_Merchant_amount")=PaidAmount then
	if rs("c_bankzt")=1 then
	response.write "<script language='javascript'>"
response.write "alert('对不起，你已经冲值过！');"
response.write"this.location.href='/index.asp';</SCRIPT>"
response.End()
	else
rs("c_bankzt")=1
end if
	else
rs("c_bankzt")=0
	end if
	c_username=rs("c_username")
	end if
	rs.update
rs.close
set rs=nothing
'成功
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from [snsn_user] where username='"&c_username&"'",conn,1,1 
if not(rs.bof and rs.eof) then
set rs1=server.CreateObject("adodb.recordset")
rs1.open "select * from [snsn_user] where username='"&c_username&"'",conn,1,3 
rs1("jine")=rs1("jine")+PaidAmount
rs1.update
session("jine")=rs1("jine")
response.write "<script language='javascript'>"
response.write "alert('恭喜您，冲值成功！');"
response.write"this.location.href='/index.asp';</SCRIPT>"
response.End()
rs1.close
else
response.write "<script language='javascript'>"
response.write "alert('对不起，用户出错！');"
response.write"this.location.href='/index.asp';</SCRIPT>"
response.End()
end if

else	'支付不成功的处理...
response.write "<script language='javascript'>"
response.write "alert('对不起，出错！');"
response.write"this.location.href='/index.asp';</SCRIPT>"
response.End()
''	%>
<%end if%>
</body>
</html>

