PayPal即时付款通知IPN,PDT,支付接口,附代码,請有用过PayPal的朋友进去看看,謝謝
PayPal即时付款通知IPN,PDT,支付接口,附代码,請有用过PayPal的朋友进去看看,謝謝
問題點如下:
When customer have been paid, I didn't have receive store transaction information in our own database, Our own payment_status field of database was blank.
Could you please possibly help me check the code of ipn.asp and pdt.asp? and tell me how to solve the problem. thanks a lot.
ipn.asp代碼如下:
<%@LANGUAGE="VBScript" codepage="936"%>
<!--#include file="conn.asp"-->
<%
' dim some variables
Dim Item_name, Item_number, Payment_status, Payment_amount
Dim Txn_id, Receiver_email, Payer_email
Dim objHttp, str
'begin IPN handling
' read post from PayPal system and add 'cmd'
str = Request.Form & "&cmd=_notify-validate"
' post back to PayPal system to validate
set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
' set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.4.0")
' set objHttp = Server.CreateObject("Microsoft.XMLHTTP")
objHttp.open "POST", "https://www.paypal.com/cgi-bin/webscr", false
objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHttp.Send str
' assign posted variables to local variables
Item_name = Request.Form("item_name")
Item_number = Request.Form("item_number")
Payment_status = Request.Form("payment_status")
Payment_amount = Request.Form("mc_gross")
Payment_currency = Request.Form("mc_currency")
Txn_id = Request.Form("txn_id")
Receiver_email = Request.Form("receiver_email")
Payer_email = Request.Form("payer_email")
firstName = Request.Form("cust_firstname")
lastName = Request.Form("cust_lastname")
set rs=Server.CreateObejct("ADODB.Recordset")
sql="select * from [shop_action]
rs.open sql,conn,2,3
rs("cust_firstname")=first_name
rs("cust_lastname")=last_name
rs("Payer_email")=payer_email
rs("payment_status")=payment_status
rs.update
rs.close
set rs=nothing
' Check notification validation
if (objHttp.status <> 200 ) then
' HTTP error handling
elseif (objHttp.responseText = "VERIFIED") then
' check that Payment_status=Completed
' check that Txn_id has not been previously processed
' check that Receiver_email is your Primary PayPal email
' check that Payment_amount/Payment_currency are correct
' process payment
elseif (objHttp.responseText = "INVALID") then
' log for manual investigation
' add code to handle the INVALID scenario
else
' error
end if
set objHttp = nothing
%>
pdt.asp代码如下:
<%@LANGUAGE="VBScript" codepage="936"%>
<!--#include file="conn.asp"-->
<%
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=ueV5o5HCx2Wvqyc3RBi2SIhSizleX_or8LWDx_OJ9EeEbkHryuayx9qDma45" '这里是Paypal身份标记 获取方法
str = "?tx="&str1& "&cmd=_notify-synch"&str2
'
paypalurl="https://www.sandbox.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 "mc_gross"
mcGross = sValue
Case "mc_currency"
mcCurrency = sValue
Case "receiver_email"
receiver_email = Trim(receiver_email)
Case "payment_status"
payment_status=Trim(sValue)
End Select
Next
Else
response.write Mid(ResponseTxt,1,7)
response.end
End IF
response.write "从PAYPAL返回的数据为:" & firstName & "<br>" & lastName & "<br>" & itemName & "<br>" & mcGross & "<br>" & mcCurrency & "<br>" & receiver_email & "<br>" & payment_status & "<br>" '
'response.End
set rs=server.createobject("adodb.recordset")
sql="select cust_firstname,cust_lastname,payer_email,payment_status,mc_gross from shop_action"
rs.open sql,conn,1,3
rs.addnew
firstName=firstName
lastName=lastName
Payer_email=receiver_email
payment_status=payment_status
rs("cust_firstname")=firstName
rs("cust_lastname")=lastName
rs("payer_email")=Payer_email
rs("payment_status")=payment_status
rs.UPDATE
rs.close
set rs=nothing
conn.close
set rs=nothing
%>
<meta http-equiv="refresh" content="5;URL=index.asp">
<link href="css/layout.css" rel="stylesheet" type="text/css">
<br>
Thank you for your payment. Your transaction has been completed, and a receipt for your purchase has been emailed to you.<br>
You may log into your account at <A
href="https://www.paypal.com/us/">www.paypal.com/us</A> to view details of this transaction.
<br>
If this page backs for more than 5 seconds, click <a href="index.asp">here</a> to back home.
支付接口代码如下:
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal
is the safer, easier way to pay - PayPal">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="jane@cool.com">
<input type="hidden" name="item_number_1" value=" <%=dingdan%>">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="cancel_return" value="http://www.coll.com/?state=null">
<input type="hidden" name="return" value="http://www.coll.com/pdt.asp">
<input type="hidden" name="shipping" value=" <%=feiyong%>">
<input type="hidden" name="charset" value="UTF-8">
<input type="hidden" name="notify_url" value="http://www.coll.com/ipn.asp">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="bn" value="PP-BuyNowBF">
</form>