欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP中集成PayPal標(biāo)準(zhǔn)支付的實(shí)現(xiàn)方法分享

 更新時(shí)間:2012年02月06日 14:27:06   作者:  
前兩天一個(gè)客戶需要在網(wǎng)站上集成PayPal支付功能,查了一下資料,簡單記錄如下

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ā)貨或者其他的處理邏輯

這里有一張圖來解釋

paypal_process_thumb[1]

更為簡單的流程圖

download_thumb[3]

 我們要完成整個(gè)流程,其實(shí)只需要兩個(gè)頁面來處理
  1. checkout.php 這個(gè)頁面用來顯示購物車信息,并讓用戶點(diǎn)擊按鈕導(dǎo)航到PayPal進(jìn)行支付
  2. notify.php 這個(gè)頁面是用來接收PayPal的IPN信息的,判斷用戶的付款是否到賬等狀態(tài),并處理網(wǎng)站收款之后的業(yè)務(wù)邏輯

記錄一下代碼:
checkout.php 這個(gè)頁面其實(shí)可以是HTML

復(fù)制代碼 代碼如下:

<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)部的邏輯處理
復(fù)制代碼 代碼如下:

$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)文章

最新評論