<!--#include file="conn.asp"-->
<!--#include file="tenpay_md5.asp"-->
<!--#include file="../include/Lebi_db_cls.asp"-->
<!--#include file="../include/Lebi_cache_cls.asp"-->
<!--#include file="../include/Lebi_cache.asp"-->
<!--#include file="bill_fun.asp"-->
<%
'快钱人民币支付网关接口 version 2.0
merchantAcctId=trim(request("merchantAcctId"))
key=Pay_bill(2)
version=trim(request("version"))
language=trim(request("language"))
signType=trim(request("signType"))
payType=trim(request("payType"))
bankId=trim(request("bankId"))
orderId=trim(request("orderId"))
orderTime=trim(request("orderTime"))
orderAmount=trim(request("orderAmount"))
'获取快钱交易号
dealId=trim(request("dealId"))
'获取银行交易号  如果使用银行卡支付时，在银行的交易号。如不是通过银行支付，则为空
bankDealId=trim(request("bankDealId"))
'获取在快钱交易时间
dealTime=trim(request("dealTime"))
'获取实际支付金额
payAmount=trim(request("payAmount"))
'获取交易手续费
fee=trim(request("fee"))
ext1=trim(request("ext1"))
If InStr(ext1,"|") > 0 Then 
	LangTxt = Split(ext1,"|")(0)
	user_id = Split(ext1,"|")(1)
Else
	user_id = ""
End If 
ext2=trim(request("ext2"))
'获取处理结果
''10代表 成功11代表 失败
''00代表 下订单成功（仅对电话银行支付订单返回）;01代表 下订单失败（仅对电话银行支付订单返回）
payResult=trim(request("payResult"))
'获取错误代码
errCode=trim(request("errCode"))
'获取加密签名串
signMsg=trim(request("signMsg"))
'生成加密串。必须保持如下顺序。
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"merchantAcctId",merchantAcctId)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"version",version)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"language",language)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"signType",signType)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"payType",payType)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"bankId",bankId)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"orderId",orderId)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"orderTime",orderTime)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"orderAmount",orderAmount)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"dealId",dealId)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"bankDealId",bankDealId)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"dealTime",dealTime)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"payAmount",payAmount)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"fee",fee)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"ext1",ext1)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"ext2",ext2)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"payResult",payResult)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"errCode",errCode)
	merchantSignMsgVal=appendParam(merchantSignMsgVal,"key",key)
merchantSignMsg= ASP_MD5(merchantSignMsgVal)
'初始化结果及地址
rtnOk=0
rtnUrl=""
'商家进行数据处理，并跳转会商家显示支付结果的页面
''首先进行签名字符串验证
If UCase(signMsg)=UCase(merchantSignMsg) then
	''接着进行支付结果判断
	Select Case payResult	
		  case "10"			
			'*  
			' 商户网站逻辑处理，比方更新订单支付状态为成功
			' 特别注意：只有UCase(signMsg)=UCase(merchantSignMsgVal)，且payResult=10，才表示支付成功！
			'*
			'报告给快钱处理结果，并提供将要重定向的地址。
			If user_id = "" Then 
					conn.execute("UPDATE [Lebi_pay_log] SET l_state=1 where l_ordernum='"& orderId &"'")
					Set rs = server.CreateObject("adodb.recordset")
					rs.open "select orderzt,paymenttype,paidmoney,id from [Lebi_orders] where order_number='"& orderId &"'",conn,1,3
					If Not rs.eof Then
						orderzt = orderzt & rs(0) &",2,"
						rs(0) = orderzt
						rs(1) = Pay_bill(7)
						rs(2) = rs(2) + (orderAmount/100)
						rs.update 
						conn.execute("insert into [Lebi_Orders_Paylog](p_price,p_type,p_content,p_time,oid)values("& orderAmount/100 &",1,'"& Pay_bill(7) &"',"& 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='"& orderId &"' and m_zt=0")
					If not rs.eof Then
							conn.execute("UPDATE [Lebi_money_log] SET m_zt=1 where m_order_num='"& orderId &"'")
							conn.execute("UPDATE [Lebi_buy] SET orderzt=1 where order_num='"& orderId &"'")
							conn.execute("UPDATE [Lebi_User] SET usermoney = usermoney + "& (orderAmount/100) &" where user_id = '"& user_id &"'")
					End If
					rs.close : Set rs = Nothing
					response.redirect Eshop_url & Split(ext1,"|")(0) &"user/pay.asp?action=update"
			End If 
			msg="success!"
		 Case else
			msg="false!"
	End select
else
	msg="error!"
End if
%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
	<head>
		<title>快钱支付结果</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" >
	</head>
<BODY>
	<div align="center">
		<table width="259" border="0" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC" >
			<tr bgcolor="#FFFFFF">
				<td width="80">支付方式:</td>
				<td >快钱[99bill]</td>
			</tr>
			<tr bgcolor="#FFFFFF">
				<td >订单编号:</td>
				<td ><%=orderId %></td>
			</tr>
			<tr bgcolor="#FFFFFF">
				<td>订单金额:</td>
				<td><%=(orderAmount)&"分" %></td>
			</tr>
			<tr bgcolor="#FFFFFF">
				<td>支付结果:</td>
				<td><%=msg %></td>
			</tr>
			<tr>
				<td></td>
				<td></td>
			</tr>
	  </table>
	</div>
</BODY>
</HTML>