<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%If Request.Form ("username")="" or RegExpTest("[^A-Za-z0-9]",Request.Form ("username")) Then
	ef=True
	ec="用户名没填或非法！"
ElseIf Not IsNumeric(request.form("ORDERMONEY")) Then
	ef=True
	ec="金额必须是数字！"
ElseIf Clng(request.form("ORDERMONEY"))<Application("netpaylessmoney") Then
	ef=True
	ec="金额不能小于"&Application("netpaylessmoney")&"元"
End if

'验证会员资格
If not ef Then
NetPaypercent=Application("NetPaypercent")
'NetPaypercent=0
end if



if not ef then
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位商户密钥, 请替换为您的实际密钥

end if

'验证会员资格
If not ef Then
agentcannetpay=false
Set dsreg = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_AGENT where username='"&Request.Form ("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='"&Request.Form ("username")&"'"
		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

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


if not ef then
sUserName = Trim(LCase(request.form("USERNAME")))
sOrderMoney=request.form("ORDERMONEY")

If NetPaypercent>0 Then sOrderMoney=sOrderMoney*(1+NetPaypercent)
sOrderMoney=FormatNumber(sOrderMoney,2,-1,-1,0)

SERVER_NAME=Request.ServerVariables("SERVER_NAME")
If Request.ServerVariables("HTTP_FreeHostAPISITE")<>"" Then SERVER_NAME=Request.ServerVariables("HTTP_FreeHostAPISITE")

	' 下面是请求参数
	cmdno		= "1"				' 财付通支付为"1" (当前只支持 cmdno=1)	
	bill_date	= CFTGetServerDate	' 交易日期 (yyyymmdd)	
	bank_type	= "0"				' 银行类型:	0		财付通
									'			1001	招商银行   
									'			1002	中国工商银行  
									'			1003	中国建设银行  
									'			1004	上海浦东发展银行   
									'			1005	中国农业银行  
									'			1006	中国民生银行  
									'			1008	深圳发展银行   
									'			1009	兴业银行   

	desc		= sUserName&"网上充值:"&SERVER_NAME		' 商品名称
	purchaser_id = ""				' 用户财付通帐号，如果没有可以置空
	bargainor_id = sShopID				' 商户号


	'根据系统时间产生订单，格式：YYYYMMDD HHMMSS
'	yy=Right(year(date),2)
'	mm=right("00"&month(date),2)
'	dd=right("00"&day(date),2)
	'生成订单号所有所需元素,格式为：小时，分钟，秒
	xiaoshi=right("00"&hour(time),2)
	fenzhong=right("00"&minute(time),2)
	miao=right("00"&second(time),2)
	BillNo=xiaoshi&fenzhong&miao

    sp_billno=BillNo
'	sp_billno	 = "1000002006"		' 商户生成的订单号(最多32位)	

	' 重要:
	' 交易单号(28位): 商户号(10位) + 日期(8位) + 流水号(10位), 必须按此格式生成, 且不能重复
	' 如果sp_billno超过10位, 则截取其中的流水号部分加到transaction_id后部(不足10位左补0)
	' 如果sp_billno不足10位, 则左补0, 加到transaction_id后部
	transaction_id = sShopID & bill_date & sp_billno 
	If Len(transaction_id)=27 Then transaction_id=transaction_id&"0"
	If Len(transaction_id)=26 Then transaction_id=transaction_id&"00"
	If Len(transaction_id)=25 Then transaction_id=transaction_id&"000"
	If Len(transaction_id)=24 Then transaction_id=transaction_id&"0000"
	If Len(transaction_id)=23 Then transaction_id=transaction_id&"00000"
	If Len(transaction_id)=22 Then transaction_id=transaction_id&"000000"
	If Len(transaction_id)=21 Then transaction_id=transaction_id&"0000000"
	If Len(transaction_id)=20 Then transaction_id=transaction_id&"00000000"
	If Len(transaction_id)=19 Then transaction_id=transaction_id&"000000000"


	total_fee	 = sOrderMoney*100				' 总金额, 分为单位
	fee_type	 = "1"				' 货币类型: 1 – RMB(人民币) 2 - USD(美元) 3 - HKD(港币)
'	return_url	 = "http://www.yoursitename.com/notify_handler.asp" ' 财付通回调页面地址, (最长255个字符)
	return_url="http://"&SERVER_NAME&"/netpay/tenpay/notify_handler.asp"


	attach		 = sUserName	' 商户私有数据, 请求回调页面时原样返回

	' 生成MD5签名    

spbill_create_ip=Request.ServerVariables("REMOTE_ADDR")
If Request.ServerVariables("HTTP_FreeHostAPIIP")<>"" Then spbill_create_ip=Request.ServerVariables("HTTP_FreeHostAPIIP")

'	sign_text = "agentid=1201983901&attach="&attach&"&bank_type=0&bargainor_id=" & bargainor_id &"&cmdno=" & cmdno & "&date=" & bill_date &"&desc="&desc&"&fee_type=" & fee_type & "&key_index=1&return_url=" & return_url &"&sp_billno=" & sp_billno&"&total_fee=" & total_fee &"&transaction_id=" & transaction_id&"&ver=3&verify_relation_flag=1&key=" & sp_key

	sign_text = "agentid=1201983901&attach="&attach&"&bank_type=0&bargainor_id=" &_
				bargainor_id &"&cmdno=" & cmdno & "&date=" & bill_date &"&desc="&_
				desc&"&fee_type=" & fee_type & "&key_index=1&return_url=" & return_url &_
				"&sp_billno=" & sp_billno&"&spbill_create_ip="&spbill_create_ip&"&total_fee=" &_
				total_fee &"&transaction_id=" & transaction_id&"&ver=3&verify_relation_flag=1&key=" & sp_key



'response.write 	sign_text

	md5_sign = UCase(md5(sign_text))        ' 转换为大写  





'sOrderID = sp_billno
sOrderID = transaction_id

Set dsNP = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Netpay where ORDERID='"&sOrderID&"'"
		dsNP.Open Sql,conn,1,3
if not dsNP.eof then
		ef=True
		ec="重复订单！"
end if
end if

if not ef then

dsNP.addnew
dsNP("username")=sUserName
dsNP("SHOPID")=sShopID
dsNP("ORDERID")=sOrderID
dsNP("ORDERMONEY")=sOrderMoney
dsNP("SUCCEEDFLAG")=-1
dsNP("paytime")=now
dsNP("ip")=Request.ServerVariables("REMOTE_ADDR")
dsNP("agentcannetpay")=agentcannetpay
		dsNP.update
		dsNP.close
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>　网上支付
<%=htmlwrite("html3")%>
<br>
用户名:<%=sUserName%> 请确认用户名是否正确 支付金额:<%=sOrderMoney%> 元<%If NetPaypercent>0 Then %>(增加<%=NetPaypercent*100%>%手续费)<%End if%>
<br>
<strong><font color="#FF0000"></font></strong><br>


<form action="https://www.tenpay.com/cgi-bin/v1.0/pay_gate.cgi">


<input type=hidden name="agentid"				value="1201983901">
<input type=hidden name="key_index"				value="1">
<input type=hidden name="verify_relation_flag"				value="1">
<input type=hidden name="ver"				value="3">

<input type=hidden name="cmdno"				value="<%=cmdno%>">
<input type=hidden name="date"			    value="<%=bill_date%>">
<input type=hidden name="bank_type"			value="<%=bank_type%>">
<input type=hidden name="desc"				value="<%=desc%>">
<input type=hidden name="purchaser_id"		value="<%=purchaser_id%>">
<input type=hidden name="bargainor_id"		value="<%=bargainor_id%>">
<input type=hidden name="transaction_id"	value="<%=transaction_id%>">
<input type=hidden name="sp_billno"			value="<%=sp_billno%>">
<input type=hidden name="spbill_create_ip"	value="<%=spbill_create_ip%>">
<input type=hidden name="total_fee"			value="<%=total_fee%>">
<input type=hidden name="fee_type"			value="<%=fee_type%>">
<input type=hidden name="return_url"		value="<%=return_url%>">
<input type=hidden name="attach"			value="<%=attach%>">

<input type=hidden name="sign"				value="<%=md5_sign%>">
<input type=submit name=submit value=财付通支付>


</form>
<%=htmlwrite("html4")%>
<%
end if

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

	' 获取服务器日期，格式YYYYMMDD
	Function CFTGetServerDate 
		Dim strTmp, iYear,iMonth,iDate 
		iYear = Year(Date) 
		iMonth = Month(Date) 
		iDate = Day(Date) 

		strTmp = CStr(iYear)
		If iMonth < 10 Then 
			strTmp = strTmp & "0" & Cstr(iMonth)
		Else 
			strTmp = strTmp & Cstr(iMonth)
		End If 
		If iDate < 10 Then 
			strTmp = strTmp & "0" & Cstr(iDate) 
		Else 
			strTmp = strTmp & Cstr(iDate) 
		End If 
		CFTGetServerDate = strTmp 
	End Function

%>
