ASP做paypal支付介面詳細代碼和執行個體

來源:互聯網
上載者:User

ASP做貝寶(paypal)支付介面 !!

今天網速很慢,想想整理一下前些天做的介面吧! 整理 ! 整理一下 !

事實上,paypal可以說分兩個 :

國際www.paypal.com
中國www.paypal.com/cn

國際的這個只支付 美元 收付 ! 而中國的這個支援 人民幣 收付 !

這個先清楚了 !

流程思路:

事實上貝寶帳號上直接就可以得到一個支付介面的: 如下:

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">

<input type="image" src="https://www.paypal.com/zh_CN/i/btn/x-click-but22.gif" border="0" name="submit" alt="貝寶— 最安全便捷的線上支付方式!"> <input type="hidden" name="add" value="1"> <input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="zwfec@163.com">
<input type="hidden" name="item_name" value="asss">
<input type="hidden" name="item_number" value="df">
<input type="hidden" name="amount" value="10.00">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="CNY">
<input type="hidden" name="weight" value="1">
<input type="hidden" name="weight_unit" value="kgs">
<input type="hidden" name="bn" value="PP-ShopCartBF">
</form>

直接使用這種代碼有些不方便! 要一個個添加才可以, 不過想一想, 這段代碼, 也可以動態產生的, 用php,asp都可以,只要裡面授的一些值改變一下,你的商品的支付代碼就可以通過讀取資料庫來產生了,也不用一個個來寫入了!!

我是這樣寫的: 讀取資料庫:rs 這個大家應該都知道的吧

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="<%=rs("email")%>">'你的paypal帳號
    <input type="hidden" name="item_name" value="<%=rs("ordernum")%>">  '你的網站上的購物車上的訂單號
    <input type="hidden" name="currency_code" value="USD">              'USD 美元 ! CNY 人民幣 !
    <input type="hidden" name="amount" value="<%=rs("price")%>">        '產品價格
<input type="hidden" name="return" value="http://<%                 '這裡是客戶支付成功, 返回資訊的地址!
theurl=LCase(Request.ServerVariables("HTTP_HOST"))&Request.ServerVariables("URL") theurl=Left(theurl,InstrRev(theurl,"/")) response.write theurl&"inc/paypalreturn.asp" %>">   
<input type="submit" name="Submit3" value="Use Paypal Payment"> </form>

這個支付肯定是可以的? 至於是否支付成功, 做一個認證然後更改訂單狀態 !

這個認證就是在paypalreturn.asp這個頁裡來實現: 代碼如下 : paypalreturn.asp:

<%
mainpath="paypal.mdb"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(mainpath)
conn.Open connstr

Function viate(itemnumber,paymentid,payamount,payeremail)
set rs=server.CreateObject("adodb.recordset") s
ql="select * from orders where ordernum='"&itemnumber&"'"   '這裡是訂單表 如果會員支付成功, 更改訂單狀態為已付款 !
rs.open sql,conn,1,3

If Not(rs.bof Or rs.eof) Then   
rs("orderline")="3"   
rs("paymentid")=paymentid   
rs("payamount")=payamount   
rs("payeremail")=payeremail   
rs("paydate")=Now()   
rs.update
Else   
response.write "<script>alert('NO this order number ! Please contact website administrator !!');</script>"
End If
rs.close
Set rs=Nothing
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=RDqubRmDD5AWgXJh5q2HMHKmcetP4Q8Ulj9AaPIx0B1l3f9aQiv9EPN1084"                            
'這裡是Paypal身份標記    擷取方法
str = "?tx="&str1& "&cmd=_notify-synch"&str2
'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 "mc_gross"
mcGross = sValue
Case "mc_currency"
mcCurrency = sValue
Case "txn_id"
liushuihao = Trim(sValue)
Case "payer_email"
payeremail=Trim(sValue)
End Select
Next
Call viate(itemName,liushuihao,mcGross,payeremail)       '這裡調用viate()這個function 進行認證, 並對這個訂單狀態進行更新 !
Msg="Pay for success! Please wait for delivery! "&"\n \n Your Order Number: "&itemName&" !"
Else
Msg="Sorry ! Your operating error! Please contact website administrator !!"
End If
response.write "<script>alert('"&msg&"');
location.href='user.asp'</script>" '支付完畢返回 使用者資訊頁 !
%>

ResponseTxt 樣本:

SUCCESS mc_gross=44.00 protection_eligibility=Eligible address_status=confirmed payer_id=TMTNG8HATR5Y6 tax=0.00 address_street=1+Main+St payment_date=07%3A32%3A55+Dec+22%2C+2008+PST payment_status=Completed charset=windows-1252 address_zip=95131 first_name=Test mc_fee=1.58 address_country_code=US address_name=Test+User custom= payer_status=verified business=q2_1227410340_biz%40163.com address_country=United+States address_city=San+Jose quantity=1 payer_email=q1_1227410229_per%40163.com contact_phone= txn_id=7U833557W75672524 payment_type=instant last_name=User address_state=CA receiver_email=q2_1227410340_biz%40163.com payment_fee=1.58 receiver_id=5Q2AS9DBJGWLC txn_type=web_accept item_name=20081222225300 mc_currency=USD item_number= residence_country=US handling_amount=0.00 transaction_subject=20081222225300 payment_gross=44.00 shipping=0.00

說明一下: 可能你的paypal帳號也沒錢來做測試,所在paypal想的還是很周到的, 做一個與paypal一樣功能的次層網域的網站:

https://developer.paypal.com/

介面:https://www.sandbox.paypal.com/cgi-bin/webscr

在這裡註冊一個帳號後, 在裡面可以設定兩個測試帳號, 這兩個測試帳號裡的錢是用不完的, 你可以拿來做測試 ! 相關資料:   查看

 

注:

Paypal的提交時的欄位與返回的欄位含義

’========================================================
’mc_gross 交易收入   
’address_status 地址資訊狀態  
’paypal_address_id Paypal地址資訊ID  
’payer_id 付款人的Paypal ID  
’tax 稅收  
’address_street 通訊地址  
’payment_date 交易時間  
’payment_status 交易狀態  
’charset 語言編碼  
’address_zip 郵編  
’first_name 付款人姓氏  
’address_country_code 國別  
’address_name 收件者姓名  
’custom 自訂值   ’
payer_status 付款人賬戶狀態  
’business 收款人Paypal賬戶  
’address_country 通訊地址國家  
’address_city 通訊地址城市  
’quantity 貨物數量  
’payer_email 付款人email  
’txn_id 交易ID  
’payment_type 交易類型  
’last_name 付款人名  
’address_state 通訊地址省份  
’receiver_email 收款人email  
’address_owner 尚未公布/正式啟用  
’receiver_id 收款人ID  
’ebay_address_id 易趣使用者地址ID  
’txn_type 交易通告方式  
’item_name 貨品名稱  
’mc_currency 貨幣種類  
’item_number 貨品編號  
’payment_gross 交易總額[只適用於美元情況]  
’shipping 運送費

’========================================================
’主要參數:
’add:一次只能購買單種商品
’upload:可以購買多種商品
’購物車 ’<input type="hidden" name="cmd" value="_cart">
’賣家的帳戶 ’<input type="hidden" name="business" value=ziyunyang1981@sina.com>
’商品名 ’<input type="hidden" name="item_name" value="Java">
’商品編號 ’<input type="hidden" name="item_number" value="1001">
’商品價格 ’<input type="hidden" name="amount" value="1.00">
’商品數量-針對每一種商品 ’<input type="hidden" name="quantity" value="2">
’貨幣類型 CNY:人民幣,USD:美元 ’<input type="hidden" name="currency_code" value="CNY"> ’取消交易並要返回的位置 ’<input type="hidden" name="cancel_return" value=" http://www.updateweb.cn ;">
’交易完後自動返回的位置 ’<input type="hidden" name="return" value="http://www.updateweb.cn ">
’=============================================================

 

 

----------完畢, 寫了那麼多天的程式,尋找了那麼多相關資料, 就是這麼點兒總結了, 和大家分享 !-----------

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.