PHP中集成PayPal標(biāo)準(zhǔn)支付的實(shí)現(xiàn)方法分享
PayPal支付功能其實(shí)一直在更新文檔和接口,這里說的是一個(gè)簡單的支付功能大概流程如下
1,在網(wǎng)站的結(jié)賬頁面,設(shè)置一個(gè)提交到PayPal網(wǎng)站的form,里面有一些金額,商品名稱,商家收款賬號(hào)、結(jié)賬成功后返回URL等內(nèi)容,
2,用戶結(jié)賬時(shí),通過點(diǎn)擊‘使用PayPal結(jié)賬'的按鈕到達(dá)PayPal的結(jié)賬頁面,輸入自己的PayPal用戶名和密碼并確認(rèn)支付
3,PayPal會(huì)根據(jù)是否支付成功來決定返回網(wǎng)站的哪個(gè)頁面,并在后臺(tái)對網(wǎng)站的某個(gè)頁面發(fā)起post請求,這個(gè)動(dòng)作稱作IPN,告訴網(wǎng)站這筆付款的到賬情況,比如completed即為完成付款
4,網(wǎng)站收到PayPal的notify通知后,即可給用戶發(fā)貨或者其他的處理邏輯
這里有一張圖來解釋
更為簡單的流程圖
我們要完成整個(gè)流程,其實(shí)只需要兩個(gè)頁面來處理
- checkout.php 這個(gè)頁面用來顯示購物車信息,并讓用戶點(diǎn)擊按鈕導(dǎo)航到PayPal進(jìn)行支付
- notify.php 這個(gè)頁面是用來接收PayPal的IPN信息的,判斷用戶的付款是否到賬等狀態(tài),并處理網(wǎng)站收款之后的業(yè)務(wù)邏輯
記錄一下代碼:
checkout.php 這個(gè)頁面其實(shí)可以是HTML
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="ev_csrf" value="9878824eb2cf4f1075dfa43c216d7cec"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="upload" value="1"> <input type="hidden" name="charset" value="utf-8"> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="business" value=sales@test.com> <input type="hidden" name="cancel_return" value=”http://www.test.com/checkout.html”> <input type="hidden" name="return" value=”http://www.test.com/thanks.html”> <input type="hidden" name="notify_url" value="http://www.test.com/notify.php"> <input type="hidden" name="custom" value="userid:31;ip:182.114.240.221"> <input type="hidden" name="item_number" value="ARO0101"> <input type="hidden" name="item_name" value="AD182m"> <input type="hidden" name="quantity" value="1"> <input type="hidden" name="amount" value="70"> <input type="submit" value="Checkout with PayPal"> </form>
這個(gè)form中包含了一些PayPal支付必須要加的項(xiàng),需要注意的是notify.php是PayPal會(huì)在后臺(tái)進(jìn)行調(diào)用的notify.php這個(gè)頁面有兩個(gè)功能,一個(gè)是接收PayPal的post內(nèi)容并加上標(biāo)簽返回,一個(gè)是接收到PayPal的認(rèn)證信息之后進(jìn)行網(wǎng)站內(nèi)部的邏輯處理
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
} else {//HTTP OK
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
//process business of website
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
相關(guān)文章
PHP中addslashes與mysql_escape_string的區(qū)別分析
這篇文章主要介紹了PHP中addslashes與mysql_escape_string的區(qū)別,簡單分析了addslashes與mysql_escape_string在使用過程中的區(qū)別,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-04-04PHP操作MySQL的mysql_fetch_* 函數(shù)的常見用法教程
這篇文章主要介紹了PHP中操作MySQL的mysql_fetch函數(shù)的常見用法教程,文中提到了其下fetch_array和mysql_fetch_row以及mysql_fetch_object函數(shù)的使用,需要的朋友可以參考下2015-12-12Yii 使用intervention/image拓展實(shí)現(xiàn)圖像處理功能
這篇文章主要介紹了Yii 使用intervention/image拓展實(shí)現(xiàn)圖像處理功能,需要的朋友可以參考下2019-06-06PHP最常用的ini函數(shù)分析 針對PHP.ini配置文件
php的配置函數(shù)就是幾個(gè)ini_*的函數(shù),主要是針對配置文件的操作,其實(shí)就四個(gè)函數(shù):ini_get、ini_set、ini_get_all、ini_restore。個(gè)人感覺最有用的就是ini_set和ini_get。2010-04-04PHP基于接口技術(shù)實(shí)現(xiàn)簡單的多態(tài)應(yīng)用完整實(shí)例
這篇文章主要介紹了PHP基于接口技術(shù)實(shí)現(xiàn)簡單的多態(tài)應(yīng)用,結(jié)合完整實(shí)例形式分析了php接口的定義、繼承、調(diào)用及多態(tài)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-04-04采用PHP函數(shù)memory_get_usage獲取PHP內(nèi)存清耗量的方法
PHP性能優(yōu)化過程中需要獲取PHP內(nèi)存消耗,使用memory_get_usage()函數(shù)可獲取當(dāng)前的內(nèi)存消耗情況,函數(shù)使用簡單,這里討論一下memory_get_usage()函數(shù)的用法與實(shí)例2011-12-12PHP版 漢字轉(zhuǎn)碼的實(shí)現(xiàn)詳解
本篇文章是對用php實(shí)現(xiàn)漢字轉(zhuǎn)碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php+mysqli實(shí)現(xiàn)批量替換數(shù)據(jù)庫表前綴的方法
這篇文章主要介紹了php+mysqli實(shí)現(xiàn)批量替換數(shù)據(jù)庫表前綴的方法,涉及針對mysql數(shù)據(jù)庫的遍歷與表名修改等操作技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12