<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<% Response.Buffer = true %>
<meta name="TENCENT_ONELINE_PAYMENT" content="China TENCENT">
<html>
<%

SERVER_NAME=Request.ServerVariables("SERVER_NAME")
If Request.ServerVariables("HTTP_FreeHostAPISITE")<>"" Then SERVER_NAME=Request.ServerVariables("HTTP_FreeHostAPISITE")
'		response.write 		origText
'		response.end
If Request("attach")="" or RegExpTest("[^A-Za-z0-9]",Request("attach")) 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("tenpayid")	' 这里替换为您的实际商户号
	sp_key	= dsInfo("tenpaykey")	' 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("attach")&"' 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("attach")&"'"
		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,tenpayid,tenpaykey from FreeHost.FreeHost_AGENT where username='"&dsreg("agent1")&"' and (tenpayid 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("tenpayid")
sp_key	= dsSend2Mailagn("tenpaykey")
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,tenpayid,tenpaykey from FreeHost.FreeHost_AGENT where username='"&dsreg("agent1")&"' and (tenpayid 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("tenpayid")
sp_key	= dsSend2Mailagn("tenpaykey")
End if
End If
end If

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

end if
end If



'取返回参数
cmdno			= Request("cmdno")
pay_result	= Request("pay_result")
pay_info		= Request("pay_info")
bill_date		= Request("date")
bargainor_id	= Request("bargainor_id")
transaction_id	= Request("transaction_id")
sp_billno		= Request("sp_billno")
total_fee		= Request("total_fee")
fee_type		= Request("fee_type")
attach			= Request("attach")
md5_sign		= Request("sign")

Dim origText2,localSignText

'本地参数
'sShopID	= "1000000301"	' 这里替换为您的实际商户号
'sp_key = "01234567890123456789012345678901"		' sp_key是32位商户密钥, 请替换为您的实际密钥

'返回值定义
Private Const retOK = 0					' 成功					
Private Const invalidsShopID = 1			' 商户号错误
Private Const invalidSign = 2			' 签名错误
Private Const tenpayErr	= 3				' 财付通返回支付失败

'验签函数
Function verifyMd5Sign

	origText = "cmdno=" & cmdno & "&pay_result=" & pay_result &_ 
		       "&date=" & bill_date & "&transaction_id=" & transaction_id &_
			   "&sp_billno=" & sp_billno & "&total_fee=" & total_fee &_
			   "&fee_type=" & fee_type & "&attach=" & attach &_
			   "&key=" & sp_key
	
		origText = "agentid=1201983901&attach="&attach&"&bargainor_id=" & bargainor_id &"&cmdno=" & cmdno & "&date=" & bill_date &"&fee_type=" & fee_type & "&key_index=1&pay_info=" & pay_info & "&pay_result=" & pay_result & "&sp_billno=" & sp_billno&"&total_fee=" & total_fee &"&transaction_id=" & transaction_id&"&ver=3&key=" & sp_key

				origText2 = "agentid=1201983901&attach="&attach&"&bargainor_id=" & bargainor_id &"&cmdno=" & cmdno & "&date=" & bill_date &"&fee_type=" & fee_type & "&key_index=1&pay_info=" & pay_info & "&pay_result=" & pay_result & "&sp_billno=" & sp_billno&"&total_fee=" & total_fee &"&transaction_id=" & transaction_id&"&ver=3&key="

'agentid=1201983901&attach=test&bargainor_id=1900000109&cmdno=1&date=20090611&fee_type=1&key_index=1&pay_info=OK&pay_result=0&pay_time=1244731239&sign=F1F53B17566D3C842DB860542DB3C366&sp_billno=223955&total_fee=1&transaction_id=1900000109200906112239550000&ver=3&bankname=cmb
'url ie8


'agentid=1201983901&attach=test&bargainor_id=1900000109&cmdno=1&date=20090611&fee_type=1&key_index=1&pay_info=OK&pay_result=0&sp_billno=223955&total_fee=1&transaction_id=1900000109200906112239550000&ver=3&verify_relation_flag=1&key=8934e7d15453e97507ef794cf7b0519d
'agentid=2000000000&attach=my_magic_string&bargainor_id=2000000501&cmdno=1&date=20080520&fee_type=1&key_index=2&pay_info=OK&pay_result=0&sp_billno=1211251265&total_fee=1&transaction_id=2000000501200805201211251265&ver=3&verify_relation_flag=1&key=abc12345

'验证MD5签名失败!原始加密字符:agentid=1201983901&attach=test&bargainor_id=1900000109&cmdno=1&date=20090611&fee_type=1&key_index=1&pay_info=OK&pay_result=0&sp_billno=223955&total_fee=1&transaction_id=1900000109200906112239550000&ver=3&verify_relation_flag=1&key=8934e7d15453e97507ef794cf7b0519d当前md5:FA11CC455F4AF8B125E5A05869FFE328接受md5:F1F53B17566D3C842DB860542DB3C366 

	localSignText = UCase(md5(origText)) ' 转换为大写
	verifyMd5Sign = (localSignText = md5_sign)
	
End Function

'返回值
Dim retValue
retValue = retOK

'判断商户号
If bargainor_id <> sShopID Then
	retValue = invalidsShopID
Else 
' 验签
	If verifyMd5Sign <> True Then
		retValue = invalidSign
	Else
' 检查财付通返回值
		If pay_result <> 0 Then
			retValue = tenpayErr
		End If
	End If
End If


If retValue = retOK Then
If pay_result="0" then
'在这里处理业务逻辑 

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



if not ef then
'ORDERID = sp_billno
ORDERID = transaction_id
SucceedFlag = pay_result
ORDERMONEY = total_fee/100
Set dsNP = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Netpay where ORDERID='"&ORDERID&"' and SHOPID='"&sShopID&"' "

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

		dsNP.Open Sql,conn,1,3
if dsNP.eof then
		ef=True
		ec="不存在的订单:"&Server.HTMLEncode(ORDERID)&"-"&sShopID
Else
  if Clng(dsNP("SucceedFlag")) =20 then
		ef=True
		ec="状态已更新过了，不能重复操作！"
  end if
  if dsNP("SucceedFlag") ="30" then
		ef=True
		ec="财付通支付失败!succeedflag=30"
  end if
end if
end if

if not ef then
if Clng(SucceedFlag)<>0 then
dsNP("SucceedFlag")=30
dsNP.update
		ef=True
		ec="财付通支付失败!succeedflag,pay_result="&pay_result
end If
end if

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

if not ef then
dsNP("SucceedFlag")=20
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&",网站："&SERVER_NAME,"(自动入帐成功)网上支付财付通接口订单"&ORDERID&",(AG)网站："&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&",(AG)网站："&SERVER_NAME,"(独立支付接口)(自动入帐成功)(金额转移给名下用户"&UserName&")网上支付财付通接口订单"&ORDERID&",(AG)网站："&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&")网站："&SERVER_NAME,"(独立支付接口)(等待入帐)(用户"&UserName&")网上支付财付通接口订单"&ORDERID&",(AG)网站："&SERVER_NAME,whichProduct,pdid,pingzheng,type1,"独立支付",haveget,havecheck


Else
'使用主站接口
olduser_money=dsreg("usermoney")
If NetPaypercent>0 Then OrderMoney=OrderMoney/(1+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&",网站："&SERVER_NAME,"网上支付财付通接口订单"&ORDERID&",网站："&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

















Else
pay_msg = "财付通支付失败!pay_result="&pay_result
End if
End if

'输出信息
Dim pay_msg
Select Case retValue
	Case retOK			pay_msg = "支付成功!"
	Case invalidsShopID	pay_msg = "错误的商户号!"	
	Case invalidSign	pay_msg = "验证MD5签名失败!原始加密字符:"&origText2&"当前md5:"&localSignText&"接受md5:"&md5_sign
	Case Else			pay_msg = "支付失败!"
End Select

If ef Then pay_msg=ec

'Response.write pay_msg
'Response.End 

return_url="http://"&SERVER_NAME&"/netpay/tenpay/showResult.asp"

Response.write "<script language=javascript>"
Response.write "window.location.href='"&return_url&"?msg=" & server.URLEncode(pay_msg) & "';"  '替换成商户网站相应页面地址
Response.write "</script>"
%>
</html>
