<!--#include file="conn.asp"-->
<!--#include file="md5PAY.asp"-->
<!--#include file="../include/Lebi_config.asp"-->
<%
session.codepage=65001
response.charset="utf-8"
Function viate(itemnumber,paymentid,payamount,payeremail,custom)
		If InStr(custom,"|") > 0 Then 
			LangTxt = Split(custom,"|")(0)
			user_id = Split(custom,"|")(1)
		Else
			user_id = ""
		End If 
		If user_id = "" Then 
				conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"& itemnumber &"'")
				Set rs = server.CreateObject("adodb.recordset")
				rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& itemnumber &"'",conn,1,3
				If Not rs.eof Then
					orderzt = orderzt & rs(0) &",2,"
					rs(0) = orderzt
					rs(1) = Pay_paypal(8)
					rs(2) = rs(2) + payamount
					rs.update 
					conn.execute("insert into [Lebi_Orders_Paylog](p_price,p_type,p_content,p_time,oid)values("& payamount &",1,'"& Pay_paypal(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='"& itemnumber &"' and m_zt=0")
		        If not rs.eof Then
                        conn.execute("UPDATE [Lebi_money_log] SET m_zt=1 where m_order_num='"& itemnumber &"'")
			            conn.execute("UPDATE [Lebi_buy] SET orderzt=1 where order_num='"& itemnumber &"'")
						conn.execute("UPDATE [Lebi_User] SET usermoney = usermoney + "& payamount &" where user_id = '"& rs("user_id") &"'")
		        End If
				rs.close : Set rs = Nothing
				response.redirect Eshop_url & Split(custom,"|")(0) &"user/pay.asp?action=update"
		End If 
End Function

Function urldecode(encodestr)   '这个函数是对paypal返回值的urldecode解码的
		newstr=""
		havechar=False
		lastchar=""
		for i=1 to len(encodestr)
		char_c=mid(encodestr,i,1)
		if char_c="+" then
		newstr=newstr & " "
		elseif char_c="%" then
		next_1_c=mid(encodestr,i+1,2)
		next_1_num=cint("&H" & next_1_c)
		if havechar then
		havechar=false
		newstr=newstr & chr(cint("&H" & lastchar & next_1_c))
		else
		if abs(next_1_num)<=127 then
		newstr=newstr & chr(next_1_num)
		else
		havechar=true
		lastchar=next_1_c
		end if
		end if
		i=i+2
		else
		newstr=newstr & char_c
		end if
		next
		urldecode=newstr
End Function

str1=Trim(request.querystring("tx"))
str2="&at="& Pay_paypal(6)                           '这里是Paypal身份标记    获取方法
str = "?tx="&str1& "&cmd=_notify-synch"&str2
'paypalurl="https://www.sandbox.paypal.com/cgi-bin/webscr"
paypalurl="https://www.paypal.com/cgi-bin/webscr"
paypalurl=paypalurl&str
'response.write "<br>"&paypalurl&"<br>"&"<br>"&"<br>"
Set objHttp=Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
objHttp.setOption 2, 13056
objHttp.open "POST",paypalurl,False,"",""
objHttp.send()
ResponseTxt = objHttp.ResponseText           '示例下面有写
Set objHttp=Nothing
'-------------------------------------------------核对取得值
ResponseTxt=UrlDecode(ResponseTxt)                '将返回值解码并赋给 responsetxt
If Mid(ResponseTxt,1,7) = "SUCCESS" Then          '取得返回值的状态, sucess表示支付成功 ! Fail 表示支付失败 ! 返回值只有这两种情况 !
		ResponseTxt = Mid(ResponseTxt,9)                  '取得除了前9个字符的返回值,并返回给responsetxt
		sParts = Split(ResponseTxt, vbLf)                 '将返回值以vbLf(在vb里面这是回车<换行>的意思)分开,并赋给一个数组sParts
		iParts = UBound(sParts) - 1                      '对这个数组分离取值, 后面的应该都可以看得懂的吧!
		ReDim sResults(iParts, 1)
		For i = 0 To iParts
				aParts = Split(sParts(i), "=")
				sKey = aParts(0)
				sValue = aParts(1)
				sResults(i, 0) = sKey
				sResults(i, 1) = sValue

				Select Case sKey
				Case "first_name"
				firstName = sValue
				Case "last_name"
				lastName = sValue
				Case "item_name"
				itemName = sValue
				Case "item_number"
				item_number = sValue
				Case "mc_gross"
				mcGross = sValue
				Case "mc_currency"
				mcCurrency = sValue
				Case "txn_id"
				liushuihao = Trim(sValue)
				Case "payer_email"
				payeremail=Trim(sValue)
				Case "custom"
				custom=Trim(sValue)
				End Select
		Next
		Call viate(item_number,liushuihao,mcGross,payeremail,custom)        '这里调用viate()这个function 进行认证, 并对这个订单状态进行更新 !
		Msg="Pay for success! Please wait for delivery! "&"\n \n Your Order Number: "&item_number&" !"
Else
		Msg="Sorry ! Your operating error! Please contact website administrator !!"
End If
response.write "<script>alert('"&msg&"');location.href='../'</script>" '支付完毕返回 用户信息页 !
%>