<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"--><%
If Request("remark1")="" or RegExpTest("[^A-Za-z0-9]",Request("remark1")) Then
	ef=True
	ec="用户名没填或非法！"
End if

If not ef Then

NetPaypercent=Application("NetPaypercent")
'NetPaypercent=0

Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")

Set dsInfo = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_System_Info where id=0"
		dsInfo.Open Sql,conn,1,1


	sShopID		= dsInfo("NP_ChinaBank_ShopID")	' 这里替换为您的实际商户号
	sp_key	= dsInfo("NP_ChinaBank_Key")	' sp_key是32位商户密钥, 请替换为您的实际密钥
	'sShopID=sShopID

end If



'验证会员资格
If not ef Then
agentcannetpay=false
Set dsreg = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_AGENT where username='"&Request("remark1")&"' 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='"&Request("remark1")&"'"
		dsreg.Open Sql,conn,1,3
If dsreg.Eof Then
		ef=True
		ec="会员名不存在！"
Else

If dsreg("agent1")<>"n" then
  Set dsSend2Mailagn = Server.CreateObject("ADODB.Recordset")	
		Sql="Select NP_ali_ShopID,NP_ChinaBank_ShopID,NP_ChinaBank_Key from FreeHost.FreeHost_AGENT where username='"&dsreg("agent1")&"' and (NP_ChinaBank_ShopID is not null) and canusenetpay=1"
		dsSend2Mailagn.Open Sql,conn,1,1
If not dsSend2Mailagn.Eof Then
If Application("agentcannetpay")=True Then
agentcannetpay=True
sShopID=dsSend2Mailagn("NP_ChinaBank_ShopID")
sp_key	= dsSend2Mailagn("NP_ChinaBank_Key")
End If
End If
end If

end If
Else

If dsreg("agent1")<>"n" then
  Set dsSend2Mailagn = Server.CreateObject("ADODB.Recordset")	
		Sql="Select NP_ali_ShopID,NP_ChinaBank_ShopID,NP_ChinaBank_Key from FreeHost.FreeHost_AGENT where username='"&dsreg("agent1")&"' and (NP_ChinaBank_ShopID is not null) and canusenetpay=1"
		dsSend2Mailagn.Open Sql,conn,1,1
If not dsSend2Mailagn.Eof Then
If Application("agentcannetpay")=True Then
agentcannetpay=True
sShopID=dsSend2Mailagn("NP_ChinaBank_ShopID")
sp_key	= dsSend2Mailagn("NP_ChinaBank_Key")
End if
End If
end If

If dsreg("agentdegree")=0 Then
agentcannetpay=false
sShopID = dsInfo("NP_ChinaBank_ShopID")
sp_key	= dsInfo("NP_ChinaBank_Key")
End if

end if
end If

     ' 提取表单参数
	 ' 
	 ' v_oid
	 '      商户发送的v_oid定单编号     
	 ' v_pmode
	 '      支付方式（字符串）     
	 ' v_pstatus 
	 '      支付状态
     '      20（支付成功，对使用实时银行卡进行扣款的订单）
     '      30（支付失败，对使用实时银行卡进行扣款的订单）       
	 ' v_pstring
	 '      支付结果信息 
     '      支付完成（当v_pstatus=20时）；
     '      失败原因（当v_pstatus=30时,字符串）；
     ' v_md5str
     '      Md5校验串
	 ' v_amount
	 '      订单实际支付金额
	 ' v_moneytype 
	 '      订单实际支付币种
	 ' remark1 
	 '      备注字段1
	 ' remark2 
	 '      备注字段2
	 ' key
	 '      私钥值，商户可上chinabank后台自行设定
	 '
	 '/
v_oid=request("v_oid")
v_pmode=request("v_pmode")
v_pstatus=request("v_pstatus")
v_pstring=request("v_pstring")
v_amount=request("v_amount")
v_moneytype=request("v_moneytype")
remark1=request("remark1")
remark2=request("remark2")
v_md5str=request("v_md5str")

key=sp_key'Application("NP_ChinaBank_Key")

if request("v_md5str")="" then
	ef=True
	ec="加密参数为空！"    
end if



'md5校验

text = v_oid&v_pstatus&v_amount&v_moneytype&key
md5text = Ucase(trim(md5(text)))


if md5text<>v_md5str then
		ef=True
		ec="非法加密！"    
end if


if not ef then
SHOPID = sShopID'Application("NP_ChinaBank_ShopID")
ORDERID = v_oid
SucceedFlag = v_pstatus
ORDERMONEY = v_amount

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,1,3
if dsNP.eof then
		ef=True
		ec="不存在的订单！"
else
  if Clng(dsNP("SucceedFlag")) =20 then
		ef=True
		ec="状态已更新过了，不能重复操作！"
		response.write("ok")
		response.end
  end if
  if Clng(dsNP("SucceedFlag")) =30 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)<>20 then
dsNP("SucceedFlag")=SucceedFlag
dsNP.update
response.write("ok")
response.end
con="error"
con=v_pstring
		ef=True
		ec="支付不成功！返回状态是："&con
end if
end if

if not ef then
if v_amount<dsNP("OrderMoney") then
		ef=True
		ec="支付不成功！实际支付金额小于订单金额！"
end if
end if

if not ef then
agentcannetpay=dsNP("agentcannetpay")
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

If agentcannetpay=True Then
'使用代理自己的接口
agnusername=dsreg("agent1")
olduser_money=dsreg("waitmoney")
If NetPaypercent>0 Then OrderMoney=OrderMoney/(1+NetPaypercent)
moneych=OrderMoney
dsreg("waitmoney")=dsreg("waitmoney")+OrderMoney
newuser_money=dsreg("waitmoney")
dsreg.update

canautoenter=false
  Set dsSend2Mailagn = Server.CreateObject("ADODB.Recordset")	
		Sql="Select usermoney from FreeHost.FreeHost_AGENT where username='"&dsreg("agent1")&"'"
		dsSend2Mailagn.Open Sql,conn,3,2
If not dsSend2Mailagn.Eof Then
If dsSend2Mailagn("usermoney")>OrderMoney Or dsSend2Mailagn("usermoney")=OrderMoney Then
canautoenter=True
oldagn_money=dsSend2Mailagn("usermoney")
dsSend2Mailagn("usermoney")=dsSend2Mailagn("usermoney")-OrderMoney
dsSend2Mailagn.update
newagn_money=dsSend2Mailagn("usermoney")
dsreg("waitmoney")=dsreg("waitmoney")-OrderMoney
dsreg("usermoney")=dsreg("usermoney")+OrderMoney'给用户帐上加钱
dsreg.update
FreeHost_moneyNewlog conn,"y","n","n","n","y",isAgnHostSer,moneych,UserName,UserName,UserName,olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,"(独立)(自动入帐成功)网上支付网银接口订单"&ORDERID&",网站："&Request.ServerVariables("SERVER_NAME"),"(自动入帐成功)网上支付网银接口订单"&ORDERID&",(AG)网站："&Request.ServerVariables("SERVER_NAME"),whichProduct,pdid,pingzheng,type1,"独立支付",haveget,havecheck
FreeHost_moneyNewlog conn,"n","y","y","n","n",isAgnHostSer,moneych,dsreg("agent1"),dsreg("agent1"),dsreg("agent1"),norStmoney,norEndmoney,dsreg("agent1"),oldagn_money,newagn_money,supusername,"(独立支付接口)(自动入帐成功)(金额转移给名下用户"&UserName&")网上支付网银接口订单"&ORDERID&"网站："&Request.ServerVariables("SERVER_NAME"),"(独立支付接口)(自动入帐成功)(金额转移给名下用户"&UserName&")网上支付网银接口订单"&ORDERID&",(AG)网站："&Request.ServerVariables("SERVER_NAME"),whichProduct,pdid,pingzheng,type1,"独立支付",haveget,havecheck
End if
End If

If canautoenter=False Then FreeHost_moneyNewlog conn,"y","n","n","n","y",isAgnHostSer,moneych,UserName,UserName,UserName,olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,"(独立)(等待入帐)(用户"&UserName&")网上支付网银接口订单"&ORDERID&"网站："&Request.ServerVariables("SERVER_NAME"),"(独立支付接口)(等待入帐)(用户"&UserName&")网上支付网银接口订单"&ORDERID&",(AG)网站："&Request.ServerVariables("SERVER_NAME"),whichProduct,pdid,pingzheng,type1,"独立支付",haveget,havecheck


Else

olduser_money=dsreg("usermoney")
If Application("NetPaypercent")>0 Then OrderMoney=OrderMoney/(1+Application("NetPaypercent"))
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,"网上支付网银接口订单"&ORDERID&"网站："&Request.ServerVariables("SERVER_NAME"),"网上支付网银接口订单"&ORDERID&"网站："&Request.ServerVariables("SERVER_NAME"),whichProduct,pdid,pingzheng,type1,"在线支付",haveget,havecheck

End if

response.write("ok")
dsreg.close
set dsreg=nothing
conn.close
set conn=nothing


end if

if ef then
response.write("error")
end if%>