<!--#include file="conn.asp"-->
<!--#include file="alipay_payto.asp"-->
<!--#include file="../include/Lebi_config.asp"-->
<%
Dim alipay, mystr, trade_status, returnTxt, out_trade_no
Set alipay = New Qlwz_AliPay
out_trade_no	=DelStr(Request.Form("out_trade_no"))        '获取定单号
total_fee		=DelStr(Request.Form("total_fee"))           '获取支付的总价格
receive_name    = DelStr(Request.Form("receive_name"))       '获取收货人姓名
receive_address = DelStr(Request.Form("receive_address"))    '获取收货人地址
receive_zip     = DelStr(Request.Form("receive_zip"))        '获取收货人邮编
receive_phone   = DelStr(Request.Form("receive_phone"))      '获取收货人电话
receive_mobile  = DelStr(Request.Form("receive_mobile"))     '获取收货人手机
trade_status    = DelStr(Request.Form("trade_status"))       '获取交易状态
refund_status   = DelStr(Request.Form("refund_status"))
extra_common_param   = Request.Form("extra_common_param")
If InStr(extra_common_param,"|") > 0 Then 
	LangTxt = Split(extra_common_param,"|")(0)
	user_id = Split(extra_common_param,"|")(1)
Else
	user_id = ""
End If 
If Not IsNumeric(acpid) Or IsEmpty(acpid) Then acpid = 0
'如需获取其它参数，可填写 参数 =DelStr(Request.Form("获取参数名"))
If Pay_alipay(14) <> "2" Then
		If alipay.Notify_Url Then
				UpdateData()
		Else
				Response.Write "fail"
		End If
Else
'		***********************判断消息是不是支付宝发出*************************
		alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?"
		alipayNotifyURL = alipayNotifyURL &"partner=" & Pay_alipay(3) & "&notify_id=" & request.Form("notify_id")
		Set Retrieval = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
		Retrieval.setOption 2, 13056
		Retrieval.open "GET", alipayNotifyURL, False, "", ""
		Retrieval.send()
		ResponseTxt = Retrieval.ResponseText
		Set Retrieval = Nothing
		'***********************************************************************

		'*********************获取支付宝POST过来通知消息************************
		For Each varItem in Request.Form
				mystr=varItem&"="&Request.Form(varItem)&"^"&mystr
		Next
		If mystr<>"" Then
				mystr=Left(mystr,Len(mystr)-1)
		End If
		mystr = SPLIT(mystr, "^")
		Count=ubound(mystr)
		'对参数排序
		For i = Count TO 0 Step -1
				minmax = mystr( 0 )
				minmaxSlot = 0
				For j = 1 To i
						mark = (mystr( j ) > minmax)
						If mark Then
								minmax = mystr( j )
								minmaxSlot = j
						End If
				Next
				If minmaxSlot <> i Then
						temp = mystr( minmaxSlot )
						mystr( minmaxSlot ) = mystr( i )
						mystr( i ) = temp
				End If
		Next
		'构造md5摘要字符串
		For j = 0 To Count Step 1
				value = SPLIT(mystr( j ), "=")
				If  value(1)<>"" And value(0)<>"sign" And value(0)<>"sign_type"  Then
						If j=Count Then
								md5str= md5str&mystr( j )
						Else
								md5str= md5str&mystr( j )&"&"
						End If
				End If
		Next
		md5str=md5str&Pay_alipay(2)
		mysign=Alipay_MD5(md5str,"UTF-8")
'		If mysign=request.Form("sign") And ResponseTxt="true" Then
		If ResponseTxt="true" Then
				UpdateData()
		Else
				response.write "fail"
		End If
End If
Set alipay = Nothing
''*******************文本写入功能************************************
 '写文本，方便测试（看网站需求，也可以改成存入数据库）
'TOEXCELLR=TOEXCELLR&md5str&"MD5结果:"&mysign&"="&request.Form("sign")&"--ResponseTxt:"&ResponseTxt
'TOEXCELLR=TOEXCELLR&"--trade_status:"&trade_status&"--refund_status:"&refund_status
'Savehtml replace(now(),":","")&".txt",TOEXCELLR
'*******************************************************************
Function DelStr(Str)
		If IsNull(Str) Or IsEmpty(Str) Then
			Str	= ""
		End If
		DelStr	= Replace(Str,";","")
		DelStr	= Replace(DelStr,"'","")
		DelStr	= Replace(DelStr,"&","")
		DelStr	= Replace(DelStr," ","")
		DelStr	= Replace(DelStr,"　","")
		DelStr	= Replace(DelStr,"%20","")
		DelStr	= Replace(DelStr,"--","")
		DelStr	= Replace(DelStr,"==","")
		DelStr	= Replace(DelStr,"<","")
		DelStr	= Replace(DelStr,">","")
		DelStr	= Replace(DelStr,"%","")
End Function
Sub UpdateData()
		Set rs = conn.execute("select orderzt from [Lebi_orders] where order_number='"& out_trade_no &"'")
		If Not rs.eof Then
				orderzt = orderzt & rs(0) &",2,"
		End If
		rs.close : Set rs = Nothing
		If trade_status = "WAIT_BUYER_PAY" And refund_status = "" Then
				'等待买家付款
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_等待买家付款' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		ElseIf trade_status = "WAIT_SELLER_SEND_GOODS" And refund_status = "" Then
				'买家付款成功,等待卖家发货
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"& out_trade_no &"'")
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& out_trade_no &"'",conn,1,3
				If Not rs.eof Then
						orderzt = orderzt & rs(0) &",2,"
						rs(0) = orderzt
						rs(1) = "支付宝_买家已付款_等待卖家发货"
						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,'支付宝_买家已付款_等待卖家发货',"& 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(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
				Response.Write "success"
		ElseIf trade_status = "WAIT_BUYER_CONFIRM_GOODS" And refund_status = "" Then
				'卖家已发货等待买家确认
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"& out_trade_no &"'")
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& out_trade_no &"'",conn,1,3
				If Not rs.eof Then
						orderzt = orderzt & rs(0) &",3,"
						rs(0) = orderzt
						rs(1) = "支付宝_卖家已发货_等待买家确认"
						rs(2) = rs(2) - total_fee
						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
				Response.Write "success"
		ElseIf (trade_status = "TRADE_FINISHED" Or trade_status = "TRADE_SUCCESS") And refund_status = "" Then
				'交易成功结束
				If user_id = "" Then 
						conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"& out_trade_no &"'")
						Set rs = server.CreateObject("adodb.recordset")
						rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& out_trade_no &"'",conn,1,3
						If Not rs.eof Then
								orderzt = orderzt & rs(0) &",2,"
								rs(0) = orderzt
								rs(1) = "支付宝_交易成功"
								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,'支付宝付款',"& sqlnowstring &","& rs(3) &")")
						End If
						rs.close : Set rs = Nothing
				Else
						set Rs = conn.execute("select * from [Lebi_money_log] where m_order_num='"& out_trade_no &"' and m_zt=0")
						If not rs.eof Then
								conn.execute("UPDATE [Lebi_money_log] SET m_zt=1 where m_order_num='"& out_trade_no &"'")
								conn.execute("UPDATE [Lebi_buy] SET orderzt=1 where order_num='"& out_trade_no &"'")
								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(extra_common_param,"|")(0) &"user/pay.asp?action=update"
				End If 
				Response.Write "success"
		ElseIf trade_status = "TRADE_CLOSED" And refund_status = "" Then
				'交易成功结束
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& out_trade_no &"'",conn,1,3
				If Not rs.eof Then
						rs(0) = "5"
						rs(1) = "支付宝_已退款"
						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 &",0,'支付宝退款',"& 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(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
				Response.Write "success"
		ElseIf refund_status = "TRADE_CLOSED" And trade_status = "WAIT_SELLER_SEND_GOODS" Then
				'买家申请退款,进入退款流程(买家已经付款，等待卖家发货)
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_申请退款[等待卖家发货]',orderzt='1,2,5' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		ElseIf refund_status = "WAIT_SELLER_AGREE" And trade_status = "WAIT_BUYER_CONFIRM_GOODS" Then
				'买家申请退款,进入退款流程(卖家已发货，等待买家收货)
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_申请退款[等待买家收货]',orderzt='1,2,3,5' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		ElseIf refund_status = "SELLER_REFUSE_BUYER" And trade_status = "WAIT_SELLER_SEND_GOODS" Then
				'卖家拒绝退款,卖家拒绝买家的申请退款，此时买家可再申请退款也可继续走正常的交易流程（买家已付款，等待卖家发货）
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_申请退款[卖家拒绝]',orderzt='1,2,5' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		ElseIf refund_status = "SELLER_REFUSE_BUYER" And trade_status = "WAIT_BUYER_CONFIRM_GOODS" Then
				'卖家拒绝退款,卖家拒绝买家的申请退款，此时买家可再申请退款也可继续走正常的交易流程（卖家已发货，等待买家确认收货）
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_申请退款[卖家拒绝]',orderzt='1,2,3,5' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		ElseIf refund_status = "WAIT_BUYER_RETURN_GOODS" And trade_status = "WAIT_SELLER_SEND_GOODS" Then
				'卖家同意退款，等待买家退货.等待买家把货寄回给卖家，当买家选择有收到货时（买家已付款，等待卖家发货）
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_申请退款[等待买家寄回]',orderzt='1,2,3,5' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		ElseIf refund_status = "WAIT_BUYER_RETURN_GOODS" And trade_status = "WAIT_BUYER_CONFIRM_GOODS" Then
				'卖家同意退款，等待买家退货.等待买家把货寄回给卖家，当买家选择有收到货时（卖家已发货，等待买家收货）
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_申请退款[等待买家寄回商品]',orderzt='1,2,3,5' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		ElseIf refund_status = "WAIT_SELLER_CONFIRM_GOODS" And trade_status = "WAIT_SELLER_SEND_GOODS" Then
				'买家已退货，等待卖家收到退货（买家已付款，等待卖家发货）
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_申请退款[等待卖家确认商品]',orderzt='1,2,3,5' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		ElseIf refund_status = "REFUND_SUCCESS" And trade_status = "TRADE_CLOSED" Then
				'退款成功,交易完成
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& out_trade_no &"'",conn,1,3
				If Not rs.eof Then
						rs(0) = "5"
						rs(1) = "支付宝_退款成功"
						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 &",0,'支付宝退款',"& 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(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
				Response.Write "success"
		ElseIf refund_status = "REFUND_CLOSED" And (trade_status = "TRADE_FINISHED" Or trade_status = "WAIT_BUYER_CONFIRM_GOODS") Then
				'退款关闭,买卖双方终止了退款操作，并走正常交易流程完成了交易（卖家拒绝退款，买家确认收货，交易完成）
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"& out_trade_no &"'")
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& out_trade_no &"'",conn,1,3
				If Not rs.eof Then
						rs(0) = "1,2,3,4"
						rs(1) = "支付宝_交易完成"
						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,'支付宝付款',"& sqlnowstring &","& rs(3) &")")
						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
				Response.Write "success"
		Else
				'其他交易状态通知情况
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=0 where l_ordernum='"& out_trade_no &"'")
				conn.execute("UPDATE [Lebi_orders] SET paymenttype='支付宝_其他交易状态',orderzt='0' where order_number='"& out_trade_no &"'")
				Response.Write "success"
		End If
End Sub 
%>