<!-- #include file="conn.asp" -->
<!-- #include file="tenpay_md5.asp" -->
<!--#include file="../include/Lebi_Config.asp"-->
<% Response.Buffer = true %>
<meta name="TENCENT_ONELINE_PAYMENT" content="China TENCENT">
<html>
<%
'取返回参数
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")
Pay_tenpay_attach= Request("Pay_tenpay_attach")
If InStr(Pay_tenpay_attach,"|") > 0 Then 
	LangTxt = Split(Pay_tenpay_attach,"|")(0)
	user_id = Split(Pay_tenpay_attach,"|")(1)
Else
	user_id = ""
End If 
'本地参数
spid	= Pay_tenpay(1)
sp_key = Pay_tenpay(2)

'返回值定义
Private Const retOK = 0					' 成功					
Private Const invalidSpid = 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
	
	localSignText = UCase(ASP_MD5(origText)) ' 转换为大写
	verifyMd5Sign = (localSignText = md5_sign)
	
End Function

'返回值
Dim retValue
retValue = retOK

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

If retOK = 0 Then 
		If user_id = "" Then 
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"& cmdno &"'")
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& cmdno &"'",conn,1,3
				If Not rs.eof Then
						orderzt = orderzt & rs(0) &",2,"
						rs(0) = orderzt
						rs(1) = Pay_tenpay(8)
						rs(2) = rs(2) + total_fee
						rs.update 
						conn.execute("insert into [Lebi_Orders_Paylog](p_price,p_type,p_content,p_time,oid)values("& total_fee &",1,'"& Pay_tenpay(8) &"',"& sqlnowstring &","& rs(3) &")")
						If Config_Lebi_Eshop(23) = 1 Then '付款后扣除库存 by 56770.kingdge 2012-08-28
							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='"& cmdno &"' and m_zt=0")
				If not rs.eof Then
						conn.execute("UPDATE [Lebi_money_log] SET m_zt=1 where m_order_num='"& cmdno &"'")
						conn.execute("UPDATE [Lebi_buy] SET orderzt=1 where order_num='"& cmdno &"'")
						conn.execute("UPDATE [Lebi_User] SET usermoney = usermoney + "& total_fee &" where user_id = '"& user_id &"'")
				End If
				rs.close : Set rs = Nothing
				response.redirect Eshop_url & Split(Pay_tenpay_attach,"|")(0) &"user/pay.asp?action=update"
		End If 
End If 
Dim pay_msg
Select Case retValue
	Case retOK			pay_msg = "支付成功!"
	Case invalidSpid	pay_msg = "错误的商户号!"	
	Case invalidSign	pay_msg = "验证MD5签名失败!"
	Case Else			pay_msg = "支付失败!"
End Select
response.redirect"../"
%>
</html>