Home > php教程 > php手册 > PayPal接口集成之:PHP集成PayPal标准支付接口

PayPal接口集成之:PHP集成PayPal标准支付接口

WBOY
Release: 2016-06-06 20:01:38
Original
1746 people have browsed it

1,在网站的结账页面,设置一个提交到PayPal网站的form,里面有一些金额,商品名称,商家收款账号、结账成功后返回URL等内容, 2,用户结账时,通过点击‘使用PayPal结账’的按钮到达PayPal的结账页面,输入自己的PayPal用户名和密码并确认支付 3,PayPal会

1,在网站的结账页面,设置一个提交到PayPal网站的form,里面有一些金额,商品名称,商家收款账号、结账成功后返回URL等内容,

2,用户结账时,通过点击‘使用PayPal结账’的按钮到达PayPal的结账页面,输入自己的PayPal用户名和密码并确认支付

3,PayPal会根据是否支付成功来决定返回网站的哪个页面,并在后台对网站的某个页面发起post请求,这个动作称作IPN,告诉网站这笔付款的到账情况,比如completed即为完成付款

4,网站收到PayPal的notify通知后,即可给用户发货或者其他的处理逻辑

这里有一张图来解释

PayPal接口集成之:PHP集成PayPal标准支付接口

更为简单的流程图

PayPal接口集成之:PHP集成PayPal标准支付接口

我们要完成整个流程,其实只需要两个页面来处理
Copy after login
  1. checkout.php 这个页面用来显示购物车信息,并让用户点击按钮导航到PayPal进行支付
  2. notify.php 这个页面是用来接收PayPal的IPN信息的,判断用户的付款是否到账等状态,并处理网站收款之后的业务逻辑

记录一下代码:

checkout.php 这个页面其实可以是HTML

https://www.paypal.com/cgi-bin/webscr" method="post">
<span>hidden</span>" name="<span>ev_csrf</span>" <span>value</span>="<span>9878824eb2cf4f1075dfa43c<wbr><wbr>216d7cec</wbr></wbr></span>">
Copy after login
<span>hidden</span>" name="<span>cmd</span>" <span>value</span>="<span>_cart</span>">
Copy after login
<span>hidden</span>" name="<span>upload</span>" <span>value</span>="<span>1</span>">
Copy after login
<span>hidden</span>" name="<span>charset</span>" <span>value</span>="<span>utf-8</span>">
Copy after login
<span>hidden</span>" name="<span>currency_co<wbr>de</wbr></span>" <span>value</span>="<span>USD</span>">
Copy after login
<span>hidden</span>" name="<span>business</span>" <span>value</span>=<span>sales@test.com</span>>
Copy after login
<span>hidden</span>" name="<span>cancel_return</span>" <span>value</span>=”<span>http://www.test.com/checkout.html”</span>>
Copy after login
<span>hidden</span>" name="<span>return</span>" <span>value</span>=”<span>http://www.test.com/thanks.html”</span>>
Copy after login
<span>hidden</span>" name="<span>notify_url</span>" <span>value</span>="<span>http://www.test.com/notify.php"></span>
Copy after login
<span>hidden</span>" name="<span>custom</span>" <span>value</span>="<span>userid:31;ip:182.114.240.221</span>">
Copy after login
<span>hidden</span>" name="<span>item_number</span>" <span>value</span>="<span>ARO0101</span>">
Copy after login
<span>hidden</span>" name="<span>item_name</span>" <span>value</span>="<span>AD182m</span>">
Copy after login
<span>hidden</span>" name="<span>quantity</span>" <span>value</span>="<span>1</span>">
Copy after login
<span>hidden</span>" name="<span>amount</span>" <span>value</span>="<span>70</span>">
Copy after login
<span>submit</span>" <span>value</span>="<span>Checkout with PayPal</span>">
Copy after login

这个form中包含了一些PayPal支付必须要加的项,需要注意的是notify.php是PayPal会在后台进行调用的
Copy after login
Copy after login

$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);
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template