<!-- #include file="asp_md5.asp" -->
<!--#include file="../../inc/conn.asp"-->
<% Response.Buffer = true %>
<meta name="TENCENT_ONELINE_PAYMENT" content="China TENCENT">
<html>
<%
response.charset="UTF-8"
'取返回参数
cmdno			= Request("cmdno")				' 财付通支付为"1" (当前只支持 cmdno=1)
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")				' 商户生成的订单号(最多32位)
total_fee		= Request("total_fee")				' 总金额, 分为单位
fee_type		= Request("fee_type")				' 货币类型: 1 – RMB(人民币) (目前本商城系统只支持RMB)
attach			= Request("attach")
md5_sign		= Request("sign")

'以下两向由程序获取
'spid			= "1000000301"					' 这里替换为您的实际商户号
'sp_key  		= "01234567890123456789012345678901"		' sp_key是32位商户密钥, 请替换为您的实际密钥

'获取支付方式ID
payid=GetOneValue("web_orders","uo_recpayway","uo_orderid='"&trim(sp_billno)&"'")
'获取商家ID和KEY
if isnumeric(payid)=true then
	spid=GetOneValue("pay","userkey","payid="&payid)
	sp_key=GetOneValue("pay","userno","payid="&payid)
else
	response.write "获取密钥异常，如果您确定付款成功，请和网站联系，并手动修改定单状态"
	response.end
end if 

'返回值定义
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 retValue=retOK then
	'支付验证成功、修改定单状态
	conndb
	conn.execute("update web_orders set uo_state=2,uo_upstate=1 where uo_orderid='"&trim(sp_billno)&"'")
	closedb
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

Function GetOneValue(tabname,fldname,strwhere)
        conndb	
	'on error resume next
	set ojbrss=server.createobject("adodb.recordset")
	if strwhere="" then
		sql="select ["&fldname&"] from " & tabname
	else
		sql="select ["&fldname&"] from "&tabname& " where " &  strwhere
	end if 
	'response.write sql
	ojbrss.open sql,conn,1,3
	if ojbrss.eof then 
		GetOneValue="-1NISNUL"
	else
		GetOneValue=ojbrss(fldname)
	end if 
	ojbrss.close : set ojbrss=nothing
	closedb
End Function
%>


<!--以下输出返回信息，可以根据返回参数进行调整-->
<table width="93%" border="0">
  <tr> 
    <td> 
	<p><b><font color="#FF0000">提示：</font> 您通过财付通在线支付情况反馈如下：</b><br><br>此次交易单号： <%=sp_billno%></p>
        <p><%=pay_msg%></p>
      <p>
          在线支付结果：<%=pay_msg%>
      </p>
      <p>金额：<%=total_fee%>分</p>
      <p>币种：人民币</p>
      </p></td>
  </tr>
</table>
</html>
