<%@ 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
If Application("NP_ChinaBank_Key")="123" Or Application("NP_ChinaBank_Key")="" Then
		ef=True
		ec="商号尚未设置！"
End if
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("NP_ChinaBank_ShopID")	' 这里替换为您的实际商户号
	sp_key	= dsInfo("NP_ChinaBank_Key")	' 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_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_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

if not ef then
sUserName = request.form("USERNAME")
'sShopID = Application("NP_ChinaBank_ShopID")
sOrderMoney=request.form("ORDERMONEY")
If Application("NetPaypercent")>0 Then sOrderMoney=sOrderMoney*(1+Application("NetPaypercent"))
sOrderMoney=FormatNumber(sOrderMoney,2,-1,-1,0)

	v_rcvname=sUserName'收货人姓名
	v_rcvaddr=""'收货人地址
	v_rcvtel=""'收货人电话
	v_rcvpost=""'收货人邮编
	v_ordername=sUserName'订货人姓名
	v_amount=sOrderMoney
	style="0"
	remark1=""
	remark2=""

	'根据系统时间产生订单，格式：YYYYMMDD HHMMSS
	yy=year(date)
	mm=right("00"&month(date),2)
	dd=right("00"&day(date),2)
	v_ymd=yy&mm&dd

	'生成订单号所有所需元素,格式为：小时，分钟，秒
	xiaoshi=right("00"&hour(time),2)
	fenzhong=right("00"&minute(time),2)
	miao=right("00"&second(time),2)

	BillNo=xiaoshi&fenzhong&miao

	v_mid = sShopID
	key = sp_key'Application("NP_ChinaBank_Key")
	v_oid=v_ymd&"-"&v_mid&"-"&BillNo
	v_moneytype = "0" 
	v_orderstatus = "0" 
	'v_url="http://××××××/receive.asp" '譬如http://www.xxx.com/xxx.asp
	sReturnURL="http://"&Request.ServerVariables("SERVER_NAME")&":"&Request.ServerVariables("server_port")&"/netpay/chinabank/receive.asp"
	v_url=sReturnURL
	


dim iii,ooo
iii=split(v_amount,".")
if ubound(iii)=0 then
v_amount=cstr(v_amount) & ".00"
else
if len(iii(1))=1 then v_amount=cstr(iii(0)) & "." &cstr(iii(1)) & "0"
if len(iii(1))=2 then v_amount=cstr(v_amount)
if len(iii(1))>2 then v_amount=cstr(iii(0)) & "." & left(iii(1),2)
end if

yy=year(date)
mm=month(now)
if len(mm)=1 then mm="0"&mm
dd=day(Date)
if len(dd)=1 then dd="0"&dd
v_ymd=yy&mm&dd 

'拼凑串里勿使用中文字符，目前MD5在ASP、asp.net、java等环境下对中文处理有偏差现象
key_key = v_amount&v_moneytype&v_oid&v_mid&v_url&key

'md5.asp文件中，LCase是将字符转换为小写; Ucase是将字符转换为大写
'网银支付平台对MD5值只认大写字符串，所以小写的MD5值得转换为大写
v_md5info=Ucase(trim(md5(key_key)))

sOrderID = v_oid

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

if not ef then

v_mid   = sShopID     '商户编号，改成IPAY负责分配给商户的编号
v_oid   =sOrderID    '定单号，由商户负责产生的6位数字编号，一天内不能重复
'v_amount=sOrderMoney     '定单金额，单位元，小数点后保留两位，如10或12.34，如果为整数，则不需要小数点
sOrderMoney=v_amount
v_key     =sp_key'Application("NP_Ipay_Key")     '默认的私钥值，更改私钥后要修改这里

dsNP.addnew
dsNP("username")=sUserName
dsNP("SHOPID")=sShopID
dsNP("ORDERID")=sOrderID
dsNP("ORDERMONEY")=sOrderMoney
dsNP("SUCCEEDFLAG")=0
dsNP("paytime")=now
dsNP("ip")=Request.ServerVariables("REMOTE_ADDR")
dsNP("agentcannetpay")=agentcannetpay
		dsNP.update
		dsNP.close

remark1=v_rcvname'新加2008-10-21
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>　网上支付
<%=htmlwrite("html3")%>
<br>
用户名:<%=sUserName%> 请确认用户名是否正确 支付金额:<%=sOrderMoney%> 元<%If Application("NetPaypercent")>0 Then %>(增加<%=Application("NetPaypercent")*100%>%手续费)<%End if%>
<br>
<strong><font color="#FF0000">请您支付后点击到商城取货，才能自动入款。</font></strong><br>
<form method="post" action="https://pay3.chinabank.com.cn/select_bank" name="E_FORM">
	<input type="hidden" name="v_md5info" size="100"  value="<%=v_md5info%>">
	<input type="hidden" name="v_mid" value="<%=v_mid%>">
	<input type="hidden" name="v_oid" value="<%=v_oid%>">
	<input type="hidden" name="v_rcvname" value="<%=v_rcvname%>">
	<input type="hidden" name="v_rcvaddr" value="<%=v_rcvaddr%>">
	<input type="hidden" name="v_rcvtel" value="<%=v_rcvtel%>">
	<input type="hidden" name="v_ymd" value="<%=v_ymd%>">
	<input type="hidden" name="v_rcvpost" value="<%=v_rcvpost%>">
	<input type="hidden" name="v_amount" value="<%=v_amount%>">
	<input type="hidden" name="v_orderstatus"  value="<%=v_orderstatus%>">
	<input type="hidden" name="v_ordername"  value="<%=v_ordername%>">
	<input type="hidden" name="v_moneytype"  value="<%=v_moneytype%>">
	<input type="hidden" name="v_url" value="<%=v_url%>">
	<input type="hidden" name="style" value="<%=style%>">
	<input type="hidden" name="remark1" value="<%=remark1%>">
	<input type="hidden" name="remark2" value="<%=remark2%>">
	<input type=submit name=v_action value="下一步进入付费流程>>>">
</form>

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

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