<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%

If not ef Then
If Application("NP_NPS_ShopID")="123" Or Application("NP_NPS_ShopID")="" Then
		ef=True
		ec="商号尚未设置！"
End if
end If

	OrderInfo 	= Request.Form("OrderMessage")
	signMsg 	= Request.Form("digest")

	' 创建NPS安全组件
	Set NPSMAC = Server.CreateObject("NPSDigest.NPSSecurity")
	' 设置密钥
	NPSMAC.NPSKey = Application("NP_NPS_Key")
	NPSMAC.OrderInfo = OrderInfo
	digest = NPSMAC.GetDigest
	
if (signMsg <> digest) then
		ef=True
		ec="非法加密！"
end if

if (Application("NP_NPS_IP") <> Request.ServerVariables("REMOTE_HOST")) then
'		ef=True
'		ec="非法IP！"
end if


if not ef then

		decrypt = NPSMAC.GetDecrypt									'解密

	'	response.write "返回支付信息  =" & decrypt & "<br>" & "<br>"


		V= Split(decrypt,"|")

		For i = 0 To UBound(V)
		m_id		=	V(0)
		m_orderid	= 	V(1)
		m_oamount	= 	V(2)
		m_ocurrency = 	V(3)
		m_url		= 	V(4)
		m_language	= 	V(5)
		s_name		= 	V(6)
		s_addr		= 	V(7)
		s_postcode	= 	V(8)
		s_tel		= 	V(9)
		s_eml		= 	V(10)
		r_name		= 	V(11)
		r_addr		= 	V(12)
		r_postcode	= 	V(13)
		r_tel		= 	V(14)
		r_eml		= 	V(15)
		m_ocomment	= 	V(16)	
		modate		=	V(17)
		m_status	=	V(18)
		Next	

	'返回的认证-----------
'	FD_BankId	=	request.form("FD_BankId")				'返回所支付的银行
'	FD_TranDate	=	request.form("FD_TranDate")				'返回所支付的时间
'	FD_NPSId	=	request.form("FD_NPSId")				'返回所支付的NPS订单号
	FD_OrderId	=	m_orderid				'返回商家的订单号
	FD_Status	=	m_status			'返回成功的状态******(作为认证)******
	FD_TxAmt	=	m_oamount			'返回所支付的金额


SHOPID = Application("NP_NPS_ShopID")
Application("NPS_IP")=Request.ServerVariables("REMOTE_ADDR")
ORDERID = FD_OrderId
SucceedFlag = FD_Status

Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
Set dsNP = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Netpay where ORDERID='"&ORDERID&"' and SHOPID='"&SHOPID&"' "

if RegExpTest("[^A-Za-z0-9]",ORDERID) Or RegExpTest("[^A-Za-z0-9]",SHOPID) then
	ef=True
	ec="ORDERID或SHOPID不正确！用户试图SQL注入被拦截,内容:"&ORDERID&SHOPID
FreeHost_oplog "error","信息","实时返回支付出错"&ec
response.write "fail"&ec
response.end
end If

'	Application("NPS_test")=sql&"   "&FD_NPSId
'	response.write sql
'	response.end
		dsNP.Open Sql,conn,3,2
if dsNP.eof then
		ef=True
		ec="不存在的订单！"
else
  if dsNP("SucceedFlag") <>0 then
		ef=True
		ec="状态已更新过了，不能重复操作！"
  end if
'  if dsNP("ORDERMONEY") <>ORDERMONEY then
'		ef=True
'		ec="金额不正确！"
'  end if
end if
end if



if not ef then
if Clng(SucceedFlag)<>2 then
dsNP("SucceedFlag")=SucceedFlag
dsNP.update
con="error"
if SUCCEEDFLAG=-1  then con="校验失败"
if SUCCEEDFLAG=-2  then con="没有找到相应店铺"
if SUCCEEDFLAG=-3  then con="缺少必需参数"
if SUCCEEDFLAG=-4  then con="支付方式初始化故障"
if SUCCEEDFLAG=-5	then con="银行返回失败"
		ef=True
		ec="支付不成功！返回状态是："&con
end if
end if

if not ef then

UserName=dsNP("UserName")
OrderMoney=dsNP("OrderMoney")
Set dsreg = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_AGENT where username='"&UserName&"' and agenttype<>'n'"
		dsreg.Open Sql,conn,1,3
If dsreg.Eof Then
Set dsreg = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_USER where username='"&UserName&"'"
		dsreg.Open Sql,conn,1,3
If dsreg.Eof Then
		ef=True
		ec="会员名不存在！"
end if
end if
end if


if not ef then
dsNP("SucceedFlag")=SucceedFlag
dsNP.update
olduser_money=dsreg("usermoney")
If Application("NetPaypercent")>0 Then OrderMoney=OrderMoney/(1+Application("NetPaypercent"))
OrderMoney=Round(OrderMoney,2)
moneych=OrderMoney
dsreg("usermoney")=dsreg("usermoney")+OrderMoney
newuser_money=dsreg("usermoney")
dsreg.update

if not isnull(dsreg("kefunow")) then
Set dskefu = Server.CreateObject("ADODB.Recordset")	
	Sql="Select finishmoney from FreeHost.FreeHost_KeFu where kefumd5='"&md5(trim(dsreg("kefunow")))&"'"
	dskefu.Open Sql,conn,1,3
if not dskefu.Eof then
dskefu("finishmoney")=dskefu("finishmoney")+OrderMoney
dim kefu
kefu=dsreg("kefunow")
dskefu.update
end if
dskefu.close
end if

FreeHost_moneyNewlog conn,"y","n","n","n","y",isAgnHostSer,moneych,UserName,UserName,UserName,olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,"网上支付Nps4接口订单"&ORDERID&",网站："&Request.ServerVariables("SERVER_NAME"),"网上支付Nps4接口订单"&ORDERID&",网站："&Request.ServerVariables("SERVER_NAME"),whichProduct,pdid,pingzheng,type1,"在线支付",haveget,havecheck
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>　返回结果
<%=htmlwrite("html3")%>
支付完成。
用户的原金额：<%=olduser_money%>元<br>
用户的金额变化：<font color=red><%=moneych%>元</font><br>
用户的新金额：<%=newuser_money%>元<br>
<%dsreg.close
set dsreg=nothing
conn.close
set conn=nothing
%>
<%=htmlwrite("html4")%>
<%
end if

if ef then
Response.Redirect "e.asp?e="&ec
end if%>
