设为首页 收藏本站
查看: 445|回复: 0

[经验分享] PHP集成PayPal

[复制链接]
发表于 2017-3-21 09:25:08 | 显示全部楼层 |阅读模式
  1.注册开发者账号,注册完登陆,然后新建一个Business账号和一个Personal账号,然后选择一个账号登陆,可以看到一些明细之类的东东,
  2.几个地址
  a.return        ---就是付款完成之后返回的页面
  b.notify_url   ---付完款之后PayPal通知你的页面,这个页面会处理逻辑(包括接受IPN信息,验证, 判断是否付款完成以及你付款完成之后的后续逻辑处理)
  c.cancel_return  ----就是在跳到付款页面直接取消回到的页面
  3.你的页面上生成一个form表单,然后把你的需要支付的信息放到表单的hidden里面,例如:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="business email">  <!--接受付款的账号 >
<input type="hidden" name="item_name" value="cash">
<input type="hidden" name="amount" value="0.5">
<input type="hidden" name="currency_code" value="HKD">
<input type="hidden" name="return" value="http://xxx/paypal_return.php">
<input type="hidden" name="invoice" value="82">
<input type="hidden" name="charset" value="utf-8">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="no_note" value="">
<input type="hidden" name="notify_url" value="http://xxx/paypal_notify.php">
<input type="hidden" name="rm" value="82">
<input type="hidden" name="cancel_return"value="http://xxx/paypal_cancel.php">
<input type="submit" value="submit">
</form>
  4.最重要的paypal.notify.php页面
  a.接受Paypal post给你的数据,完全按照收到表单变量时的原样发送所有收到的表单变量。您还需要将一个值为“_notify-validate”的名为“cmd”变量(例如,cmd=_notify-validate)附加到 POST 字符串。
  b.然后把post过来的数据加上标签和修改的cmd请求PayPal页面
  c.PayPal将回复该 POST,并在回复的正文中包含一个单词“VERIFIED”或“INVALID”。当您收到 VERIFIED 回复时,       在实施订单之前执行若干检查:
  @确认“payment_status”为“Completed”,因为系统也会为其他结果(如“Pending”或“Failed”)发送 IPN。
  @检查“txn_id”是否未重复,以防止欺诈者重复使用旧的已完成的交易。
  @验证“receiver_email”是已在您的PayPal账户中注册的电子邮件地址,以防止将付款发送到欺诈者的账户 。
  @检查其他交易详情(如物品号和价格),以确认价格未改变完成了以上检查后,您可以使用 IPN 数据更新您的DB,并      处理购物。
  @如果收到“无效”通知,则应将其视为可疑通知,并应对其进行调查。
  d.最后正式部署的时候需要去掉sandbox

<?php
//reading raw POST data from input stream. reading pot data from $_POST may cause serialization issues since POST data may contain arrays
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval)
{
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc'))
{
$get_magic_quotes_exits = true;
}
foreach ($myPost as $key => $value)
{        
if($get_magic_quotes_exits == true && get_magic_quotes_gpc() == 1)
{
$value = urlencode(stripslashes($value));
}
else
{
$value = urlencode($value);
}
$req .= "&$key=$value";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.paypal.com'));
// In wamp like environment where the root authority certificate doesn't comes in the bundle, you need
// to download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
$res = curl_exec($ch);
curl_close($ch);
/*
file_put_contents(dirname(__FILE__) . '/payresp/rc_req.txt', print_r($req, true));
file_put_contents(dirname(__FILE__) . '/payresp/rc_resp.txt', print_r($res, true));
file_put_contents(dirname(__FILE__) . '/payresp/rc_post.txt', print_r($_POST, true));
*/
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];

if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-352789-1-1.html 上篇帖子: php 效率总结 下篇帖子: php glob()函数的一个实例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表