<%@ Language=VBScript %>
<!--#include file="alipay_payto.asp"-->
<%

'以下是防CC攻击的保护代码:受攻击时启用它.
'if Request.Form ("username")="不正常的用户名" then
'response.redirect "http://127.0.0.1/?"&Request.ServerVariables("REMOTE_ADDR")   '转向回攻击者自己
'end if 

'以下防CC代码需要用defaultcc.asp页的配合
'If Request.Form ("Vcode")<>session("Vcode") or session("Vcode")="" Then
'	ef=True
'	ec="验证码不正确！"
'End If
'	session("Vcode")=""

Function XM_GetID()
Dim returnstr
Set xml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
xml.setTimeouts 20000, 20000, 20000, 20000
xml.Open "GET", Application("md5serverurl")&"getid.asp", False
xml.setOption(2)=13056
xml.send
returnstr = bytes2BSTR(xml.responsebody)
XM_GetID = returnstr
End Function

Function Bytes2bStr(vin)
  Dim BytesStream,StringReturn
  Set BytesStream = Server.CreateObject("ADODB.Stream")
  BytesStream.Type = 2
  BytesStream.Open
  BytesStream.WriteText vin
  BytesStream.Position = 0
  BytesStream.Charset = "gb2312"
  BytesStream.Position = 2
  StringReturn =BytesStream.ReadText
  BytesStream.close
  Set BytesStream = Nothing
  Bytes2bStr = StringReturn
End Function

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

dim partner
	partner			=	Application("alipayid")
If Application("alipayid")="2088001779970004" Or Application("alipayid")="2088101700293850" Then partner="2088101700293850"
Dim key
	key=Application("NP_ali_Key")	 
Dim sShopID
sShopID = Application("NP_ali_ShopID")

'验证会员资格
If not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
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 (NP_ali_ShopID is not null) and canusenetpay=1"
		dsSend2Mailagn.Open Sql,conn,1,1
If not dsSend2Mailagn.Eof Then
If Application("agentcannetpay")=True Then
sShopID=dsSend2Mailagn("NP_ali_ShopID")
agentcannetpay=True
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 (NP_ali_ShopID is not null) and canusenetpay=1"
		dsSend2Mailagn.Open Sql,conn,1,1
If not dsSend2Mailagn.Eof Then
If Application("agentcannetpay")=True Then
sShopID=dsSend2Mailagn("NP_ali_ShopID")
agentcannetpay=True
End if
End If
end If

If dsreg("agentdegree")=0 Then
agentcannetpay=false
sShopID = Application("NP_ali_ShopID")
End if

end if
end If


If not ef Then
If Application("NP_ali_ShopID")="1234" Or Application("NP_ali_ShopID")="" Then
		ef=True
		ec="商号尚未设置！"
End if
end If



if not ef then
sUserName = request.form("USERNAME")
sOrderMoney=Clng(request.form("ORDERMONEY"))
If Application("NetPaypercent")>0 Then sOrderMoney=sOrderMoney*(1+Application("NetPaypercent"))

sOrderMoney=FormatNumber(sOrderMoney,2,-1,-1,0)

	'根据系统时间产生订单，格式：YYYYMMDD HHMMSS
	yy=year(date)
	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=yy&mm&dd&xiaoshi&fenzhong&miao

sOrderID = BillNo

If Application("alipayid")="2088001779970004" Or Application("alipayid")="2088101700293850" Then
If Application("md5serverurl")="" Then Application("md5serverurl")="http://sys.7i24.com/system/ali/"
If Application("defaultXid")="" Then
'得到id
defaultXid=XM_GetID()
If defaultXid="" Then
		ef=True
		ec="得不到ID！"
End If
If Not IsNumeric(defaultXid) Then
	ef=True
	ec="原ID必须是数字！"
End If
If InStr(defaultXid,"出错") then
	   response.write defaultXid
  	   response.end
End if
if not ef Then
Application("defaultXid")=defaultXid
End If
End If

If Application("defaultXid")="" Then
		ef=True
		ec="得不到ID2！"
else
sOrderID=Application("defaultXid")&sOrderID
End If
End if


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

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

dim service,sign_type,subject,body,out_trade_no,show_url,seller_email,notify_url,total_fee,payment_type,return_url
dim t1,t4,t5
dim AlipayObj,itemUrl
	t1				=	"https://mapi.alipay.com/cooperate/gateway.do?"	'支付接口
	t4				=	"images/alipay_bwrx.gif"		'支付宝按钮图片
	t5				=	"推荐使用支付宝付款"						'按钮悬停说明
	
	service         =   "create_direct_pay_by_user"
'	partner			=	"2088002147817611"		'partner		合作伙伴ID			保留字段
	sign_type       =   "MD5"
	body			=	LCase(sUserName)&"-"&sOrderMoney&"-yuan"		'body'小写			商品描述

'	out_trade_no    =   Replace(Now(),"-","")
'	out_trade_no    =   Trim(Replace(out_trade_no,":","")) '改成自己的订单号
	out_trade_no    =   sOrderID '网站自己
	subject			=	LCase(sUserName)&":"&out_trade_no		'subject		商品名称
    show_url        =   "http://"&SERVER_NAME&":"&Request.ServerVariables("server_port")  '网址
If Request.ServerVariables("server_port")="443" Then show_url = "https://"&SERVER_NAME  '网址'返回url
    seller_email    =    LCase(Trim(sShopID))  '卖家账户


'    key             =     Application("NP_ali_Key")	    '安全校验码



	notify_url      =   "http://"&SERVER_NAME&":"&Request.ServerVariables("server_port")&"/netpay/alipay/alipay_notify.asp"  '通知url
	If Request.ServerVariables("server_port")="443" Then notify_url      =  "https://"&SERVER_NAME&":"&Request.ServerVariables("server_port")&"/netpay/alipay/alipay_notify.asp"  '通知url
	total_fee       =  sOrderMoney
	payment_type    =  "1"
	return_url      =  "http://"&SERVER_NAME&":"&Request.ServerVariables("server_port")&"/netpay/alipay/return_alipay_notify.asp" '返回url
	If Request.ServerVariables("server_port")="443" Then return_url      =  "https://"&SERVER_NAME&":"&Request.ServerVariables("server_port")&"/netpay/alipay/return_alipay_notify.asp" '返回url
'		return_url      =  "" '返回url


	Set AlipayObj	= New creatAlipayItemURL
	itemUrl=AlipayObj.creatAlipayItemURL(t1,t4,t5,service,partner,sign_type,subject,body,out_trade_no,show_url,seller_email,notify_url,total_fee,payment_type,return_url,key,royalty_type,royalty_parameters)


dsNP.addnew
dsNP("username")=sUserName
dsNP("SHOPID")=sShopID
dsNP("ORDERID")=sOrderID
dsNP("ORDERMONEY")=sOrderMoney
dsNP("SUCCEEDFLAG")=99
dsNP("paytime")=now
dsNP("ip")=Request.ServerVariables("REMOTE_ADDR")
If Request.ServerVariables("HTTP_FreeHostAPIIP")<>"" Then dsNP("ip")=Request.ServerVariables("HTTP_FreeHostAPIIP")
dsNP("agentcannetpay")=agentcannetpay
		dsNP.update
		dsNP.close
'	response.write itemUrl
	response.redirect itemUrl

end if

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

Function RegExpTest(patrn, strng)
  Dim regEx
  Set regEx = New RegExp
  regEx.Pattern = patrn
  regEx.IgnoreCase = False
  RegExpTest = regEx.Test(strng)
End Function 

%>
