<!-- #include file="conn.asp" -->
<!-- #include file="tenpay_md5.asp" -->
<!--#include file="../include/Lebi_config.asp"-->
<!--#include file="../union/config.asp"-->
<html>
<head>
<META content="text/html; charset=UTF-8" http-equiv="content-type">
</head>
<body>
<%
dim billno
dim amount 
dim mydate
dim succ
dim msg
dim attach
dim ipsbillno
dim encodetype
dim signature
'接收数据
billno=Request.QueryString("billno")
amount=Request.QueryString("amount")
currency_type=Request.QueryString("Currency_type")
mydate=request.querystring("date")
succ=Request.QueryString("succ")
msg=request.querystring("msg")
attach=request.querystring("attach")
ipsbillno=Request.QueryString("ipsbillno")
retEncodeType=Request.QueryString("retencodetype")
signature=request.querystring("signature")	
If InStr(attach,"|") > 0 Then 
	LangTxt = Split(attach,"|")(0)
	user_id = Split(attach,"|")(1)
Else
	user_id = ""
End If 
'签名原文
'content=billno & amount & mydate & succ & ipsbillno & currency_type
'签名原文billno+订单编号+currencytype+币种+amount+订单金额+date+订单日期+succ+成功标志+ipsbillno+IPS订单编号+retencodetype +交易返回签名方式
content="billno"&billno&"currencytype"& currency_type&"amount" & amount &"date"& mydate&"succ" & succ&"ipsbillno" & ipsbillno &"retencodetype"&retEncodeType
'签名是否正确
verify=false
'验证方式：16-md5withRSA  17-md5
If retEncodeType = "16" Then
	set secre = Server.CreateObject("IpsVerify.RSAMd5")
	pubfilename="C:\PubKey\publickey.txt"
	result = secre.VerifyMessage(pubfilename, content, signature)
	set secre=nothing
	'result=0   表示签名验证成功
	'result=-1  表示系统错误
	'result=-2  表示文件绑定错误
	'result=-3  表示读取公钥失败
	'result=-4  表示签名长度错
	'result=-5  表示签名验证失败
	'result=-99 表示系统锁定失败
	If result=0 Then
		verify=true
	End If
ElseIf retEncodeType = "17" Then
	merchant_key=Pay_IPS(2)
	signature1=LCase(ASP_MD5(content & merchant_key))
	If signature1=signature Then
		verify=true
	End If
End If

'判断签名验证是否通过
If verify=true Then
	'判断交易是否成功
	If succ<>"Y" Then
		Response.Write "交易失败！"
	Else
		If user_id = "" Then 
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"&billno&"'")
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& billno &"'",conn,1,3
				If Not rs.eof Then
					orderzt = orderzt & rs(0) &",2,"
					rs(0) = orderzt
					rs(1) = Pay_IPS(8)
					rs(2) = rs(2) + amount
					rs.update 
					conn.execute("insert into [Lebi_Orders_Paylog](p_price,p_type,p_content,p_time,oid)values("& amount &",1,'"& Pay_IPS(8) &"',"& sqlnowstring &","& rs(3) &")")
					If Config_Lebi_Eshop(23) = 1 Then '付款后扣除库存 by 56770.kingdge 2012-05-14
						Set rsbasket = conn.execute("select cpid,basket_count,spvalue from [lebi_basket] where order_number = "& rs(3) &"")
						Do While Not rsbasket.eof 
							conn.execute("update [Lebi_product] set buys = buys + "& rsbasket("basket_count")&",kucun = kucun - "& rsbasket("basket_count") &",html = 0 where id = " & rsbasket("cpid") &"")
							conn.execute("update [Lebi_Product_Stock] set p_buys = p_buys + "& rsbasket("basket_count") &",p_stock = p_stock - "& rsbasket("basket_count") &" where p_id = " & rsbasket("cpid") &" and s_value"& LangTxt &" = '"& rsbasket("spvalue") &"'")
						rsbasket.movenext 
						Loop
						rsbasket.close : Set rsbasket = Nothing 
					End If 
				End If
				rs.close : Set rs = Nothing
		Else
				set Rs = conn.execute("select * from [Lebi_money_log] where m_order_num='"& billno &"' and m_zt=0")
				If not rs.eof Then
						conn.execute("UPDATE [Lebi_money_log] SET m_zt=1 where m_order_num='"& billno &"'")
						conn.execute("UPDATE [Lebi_buy] SET orderzt=1 where order_num='"& billno &"'")
						conn.execute("UPDATE [Lebi_User] SET usermoney = usermoney + "& amount &" where user_id = '"& rs("user_id") &"'")
				End If
				rs.close : Set rs = Nothing
				response.redirect Eshop_url & Split(attach,"|")(0) &"user/pay.asp?action=update"
		End If 
		Response.Write "交易成功！"
	End If
Else
	Response.Write "签名不正确!"
End If
%>
<div align="center"><font color=red>(<span id="backtime">0</span>)</font> <%=msg%></div>
<script language="JavaScript">Url="../";if (!Url){Url=document.referrer}function countDown(secs){document.getElementById("backtime").innerHTML=secs;if(--secs>0) {setTimeout("countDown("+secs+")",1000);}else {window.location.href=Url;}}countDown(5);</script>
</body>
</html>