<!-- #include file="conn.asp" -->
<!-- #include file="tenpay_md5.asp" -->
<!--#include file="../include/Lebi_config.asp"-->
<% Response.Buffer = true %>
<html>
<head>
	<meta name="TENCENT_ONLINE_PAYMENT" content="China TENCENT">
</head>
<%
'获取参数
attach					= Request("attach")
buyer_id				= Request("buyer_id")
cft_tid					= Request("cft_tid")
chnid					= Request("chnid")
cmdno					= Request("cmdno")
mch_vno					= Request("mch_vno")
retcode					= Request("retcode")
seller					= Request("seller")
status					= Request("status")
total_fee				= Request("total_fee")
trade_price				= Request("trade_price")
transport_fee			= Request("transport_fee")
sign					= Request("sign")
dim buffer
buffer = AddParameter(buffer, "attach", 		attach)
buffer = AddParameter(buffer, "buyer_id", 		buyer_id)
buffer = AddParameter(buffer, "cft_tid", 		cft_tid)
buffer = AddParameter(buffer, "chnid", 			chnid)
buffer = AddParameter(buffer, "cmdno", 			cmdno)
buffer = AddParameter(buffer, "mch_vno", 		mch_vno)
buffer = AddParameter(buffer, "retcode", 		retcode)
buffer = AddParameter(buffer, "seller", 		seller)
buffer = AddParameter(buffer, "status", 		status)
buffer = AddParameter(buffer, "total_fee", 		total_fee)
buffer = AddParameter(buffer, "trade_price", 	trade_price)
buffer = AddParameter(buffer, "transport_fee", 	transport_fee)
buffer = AddParameter(buffer, "key",			sp_key)
If InStr(attach,"|") > 0 Then 
	LangTxt = Split(attach,"|")(0)
	user_id = Split(attach,"|")(1)
Else
	user_id = ""
End If 
'生成签名
localSignText = UCase(ASP_MD5(buffer))
dim msg
'签名判断
if localSignText = sign then
	'认证签名成功
	if retcode = "0" then
		msg = "OK"
		msg = msg & status
		Select case status
			case "1":
				'交易创建
				
			case "2":
				'收获地址填写完毕
			case "3":
				'买家付款成功，注意判断订单是否重复的逻辑
				msg = "买家付款成功"
				If user_id = "" Then 
						conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"& mch_vno &"'")
						Set rs = server.CreateObject("adodb.recordset")
						rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& mch_vno &"'",conn,1,3
						If Not rs.eof Then
								orderzt = orderzt & rs(0) &",2,"
								rs(0) = orderzt
								rs(1) = "财付通_买家已付款"
								rs(2) = rs(2) + (total_fee/100)
								rs.update 
								conn.execute("insert into [Lebi_Orders_Paylog](p_price,p_type,p_content,p_time,oid)values("& total_fee/100 &",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='"& mch_vno &"' and m_zt=0")
						If not rs.eof Then
								conn.execute("UPDATE [Lebi_money_log] SET m_zt=1 where m_order_num='"& mch_vno &"'")
								conn.execute("UPDATE [Lebi_buy] SET orderzt=1 where order_num='"& mch_vno &"'")
								conn.execute("UPDATE [Lebi_User] SET usermoney = usermoney + "& total_fee/100 &" where user_id = '"& user_id &"'")
						End If
						rs.close : Set rs = Nothing
						response.redirect Eshop_url & LangTxt &"user/pay.asp?action=update"
				End If 
			case "4":
				'卖家发货成功
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,id from [Lebi_orders] where order_number='"& mch_vno &"'",conn,1,3
				If Not rs.eof Then
						orderzt = orderzt & rs(0) &",3,"
						rs(0) = orderzt
						rs(1) = "财付通_卖家已发货"
						rs.update 
						If Config_Lebi_Eshop(23) = 2 Then '发货后扣除库存 by 56770.kingdge 2012-08-28
							Set rsbasket = conn.execute("select cpid,basket_count,spvalue from [lebi_basket] where order_number = "& rs(2) &"")
							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
			case "5":
				'买家收货确认，交易成功
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype from [Lebi_orders] where order_number='"& mch_vno &"'",conn,1,3
				If Not rs.eof Then
						orderzt = orderzt & rs(0) &",4,"
						rs(0) = orderzt
						rs(1) = "财付通_交易完成"
						rs.update 
				End If
				rs.close : Set rs = Nothing
			case "6":
				'交易关闭，未完成超时关闭
			case "7":
				'修改交易价格成功
			case "8":
				'买家发起退款
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype from [Lebi_orders] where order_number='"& mch_vno &"'",conn,1,3
				If Not rs.eof Then
						orderzt = orderzt & rs(0) &",2,"
						rs(0) = orderzt
						rs(1) = "财付通_买家发起退款"
						rs.update 
				End If
				rs.close : Set rs = Nothing
			case "9":
				'退款成功
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,id from [Lebi_orders] where order_number='"& mch_vno &"'",conn,1,3
				If Not rs.eof Then
						orderzt = orderzt & rs(0) &",5,"
						rs(0) = orderzt
						rs(1) = "财付通_已退款"
						rs.update 
						conn.execute("insert into [Lebi_Orders_Paylog](p_price,p_type,p_content,p_time,oid)values("& total_fee/100 &",0,'财付通退款',"& sqlnowstring &","& rs(2) &")")
						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(2) &"")
							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 
						If Config_Lebi_Eshop(23) = 2 Then '发货后扣除库存，退款后还原库存 by 56770.kingdge 2012-08-28
							Set rsbasket = conn.execute("select cpid,basket_count,spvalue from [lebi_basket] where order_number = "& rs(2) &"")
							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
			case "10":
				'退款关闭
			case else
				'error
				msg = "Error"
		end Select

	else
		'支付失败，请不要按成功处理
		msg = "支付失败"
	end if
else
	'认证签名失败
	msg = "认证签名失败"
end if
%>
<body>
	<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>