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通知后,即可给用户发货或者其他的处理逻辑
这里有一张图来解释
更为简单的流程图
我们要完成整个流程,其实只需要两个页面来处理
记录一下代码:
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>">
<span>hidden</span>" name="<span>cmd</span>" <span>value</span>="<span>_cart</span>">
<span>hidden</span>" name="<span>upload</span>" <span>value</span>="<span>1</span>">
<span>hidden</span>" name="<span>charset</span>" <span>value</span>="<span>utf-8</span>">
<span>hidden</span>" name="<span>currency_co<wbr>de</wbr></span>" <span>value</span>="<span>USD</span>">
<span>hidden</span>" name="<span>business</span>" <span>value</span>=<span>sales@test.com</span>>
<span>hidden</span>" name="<span>cancel_return</span>" <span>value</span>=”<span>http://www.test.com/checkout.html”</span>>
<span>hidden</span>" name="<span>return</span>" <span>value</span>=”<span>http://www.test.com/thanks.html”</span>>
<span>hidden</span>" name="<span>notify_url</span>" <span>value</span>="<span>http://www.test.com/notify.php"></span>
<span>hidden</span>" name="<span>custom</span>" <span>value</span>="<span>userid:31;ip:182.114.240.221</span>">
<span>hidden</span>" name="<span>item_number</span>" <span>value</span>="<span>ARO0101</span>">
<span>hidden</span>" name="<span>item_name</span>" <span>value</span>="<span>AD182m</span>">
<span>hidden</span>" name="<span>quantity</span>" <span>value</span>="<span>1</span>">
<span>hidden</span>" name="<span>amount</span>" <span>value</span>="<span>70</span>">
<span>submit</span>" <span>value</span>="<span>Checkout with PayPal</span>">
这个form中包含了一些PayPal支付必须要加的项,需要注意的是notify.php是PayPal会在后台进行调用的
$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);
}