<%
'/***************************************************************************************************************
	'* FileName:	alipay_Notify.asp
	'* Description:	本Asp文件为Asp网店集成支付宝最新payto接口的示例代码,用于接收支付宝传递过来反馈通知（买家已付款请卖家发货、买家已收到货物交易结束、支付宝测试商户接口是否正常）
'****************************************************************************************************************/
%>
<!--#include file="conn.asp"-->

<!--#include file="myAlipay.asp"-->
<%
	'定义支付宝通知接口传递过来的参数
	Dim msg_id,order_no,gross,buyer_email,buyer_name,buyer_address,buyer_zipcode,buyer_tel,buyer_mobile,action,s_date,ac
	
	Dim returnTxt		'返回给支付宝通知接口的结果
	Dim alipayNotifyURL	'支付宝查询接口URL
	Dim myalipayEmail	'商户的支付宝Email
	Dim myAlipayObj,ResponseTxt
	
	returnTxt			= "Error"
	alipayNotifyURL		= "http://notify.alipay.com/trade/notify_query.do?"
	myalipayEmail		= "liasn@126.com"

	'创建一个myAlipay实例,并初始化myAlipay_Initialize()
	Set myAlipayObj		= New myAlipay
	myAlipayObj.myAlipay_Initialize()

	'检查支付宝通知接口传递过来的参数是否合法
	msg_id			= DelStr(Request("msg_id"))
	order_no		= DelStr(Request("order_no"))
	gross			= DelStr(Request("gross"))
	buyer_email		= DelStr(Request("buyer_email"))
	buyer_name		= DelStr(Request("buyer_name"))
	buyer_address	= DelStr(Request("buyer_address"))
	buyer_zipcode	= DelStr(Request("buyer_zipcode"))
	buyer_tel		= DelStr(Request("buyer_tel"))
	buyer_mobile	= DelStr(Request("buyer_mobile"))
	action			= DelStr(Request("action"))
	s_date			= DelStr(Request("date"))
	ac				= DelStr(Request("ac"))

	alipayNotifyURL	= alipayNotifyURL & "msg_id=" & msg_id & "&email=" & myalipayEmail & "&order_no=" & order_no
	ResponseTxt		= myAlipayObj.getHTTPURL(alipayNotifyURL)

	'事务处理流程如下：
	'1、支付宝通知接口请求该链接，并把各参数传递过来
	'2、商户URL页面接收各参数并做简单处理后，调用支付宝查询接口，获得支付宝返回来的true或false结果，用于判断是否是支付宝发过来的请求
	'3、根据支付宝返回来的true结果，进行后台数据处理
	'4、处理完成后，把处理结果反馈给支付宝通知接口

	'测试商户网站URL是否正确安装
	If action = "test" Then
		returnTxt	= "Y"

	'发货通知
	ElseIf action = "sendOff" Then
		returnTxt		= "N"
		If (ResponseTxt = "true") Or (ResponseTxt = "false") Then
			'更新数据在商户系统里的订单数据；如果已经发货，则将returnTxt置为Y，否则为N
			'检查定单状态
			set status=server.CreateObject("adodb.recordset")
			status.open "select state from orders where goods='"&trim(order_no)&"' ",conn,1,3
			if status("state")=4 then 
			returnTxt= "Y"
			else
			returnTxt= "N"
			end if
			set status=nothing
		Else
			'非法数据，不做更新
		End If
	
	'交易结束通知
	ElseIf action = "checkOut" Then
		returnTxt	= "Y"
		If (ResponseTxt = "true") Or (ResponseTxt = "false") Then
			'更新数据在商户系统里的订单数据；如果数据更新成功，则将returnTxt置为Y，否则为N
			'更新数据
			
			set shopv8=server.CreateObject("adodb.recordset")
			shopv8.open "update  orders set state=5 where goods='"&trim(order_no)&"' ",conn,1,3
			returnTxt= "Y"
			
			set shopv8=nothing
		Else
			'非法数据，不做更新
			returnTxt	= "N"
		End If

	End If

	Response.Write returnTxt
	

	'调用方法 DelStr(值)，防止SQL注入攻击
	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
%>