<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<!--#include file="md5.inc"-->
<%
response.end
     ' 提取表单参数
	 ' 
	 ' 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=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 = Application("NP_BJ_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
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"))
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"),whichProduct,pdid,pingzheng,type1,"在线支付",haveget,havecheck

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


end if

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