成都创新互联网站制作重庆分公司

PHP语言开发Paypal支付demo的具体实现

一、开发前准备

创新互联建站网站建设提供从项目策划、软件开发,软件安全维护、网站优化(SEO)、网站分析、效果评估等整套的建站服务,主营业务为成都网站设计、成都做网站重庆APP开发公司以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。创新互联建站深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

https://developer.paypal.com/  到paypal的开发者官网注册开发者账号。

用账号登录之后、点击导航上面的 dashboard、进入dashboard面版。如下截图、后续的操作都是在这个面板中操作。

上面截图中菜单 Sandbox下面的Accounts里面能看到你的 sandbox测试的买家账号和卖家账号。2个测试账号里面都有profile选项里面有changepassword可以设置虚拟账号的密码。

上面截图中菜单Sandbox下面的Transactions就是你的交易记录。

点击截图页面右上角的 Create App按钮。创建一个应用。创建好后、会给你提供一个Client ID 和 Secret。这两个可以配置为php常量后面开发中会用到。

二、进入支付Demo开发

随便在本地建立一个开发代码根目录、先建立一个index.html里面就放一个简单的产品名称和产品价格两个input项即可、代码和截图如下:

  
 
  1. DOCTYPE html> 
  2.  
  3.      
  4.          
  5.         支付页面title> </li> <li>    head> </li> <li>    <body> </li> <li>        <div> </li> <li>            <form action="checkout.php" method="post" autocomplete="off"> </li> <li>                <label for="item"> </li> <li>                    产品名称 </li> <li>                    <input type="text" name="product"> </li> <li>                label> </li> <li>                <br> </li> <li>                <label for="amount"> </li> <li>                    价格 </li> <li>                    <input type="text" name="price"> </li> <li>                label> </li> <li>                <br> </li> <li>                <input type="submit" value="去付款"> </li> <li>            form> </li> <li>        div> </li> <li>    body> </li> <li>html> </li> </ol></pre><p>输入产品名称 和 价格。点击去付款就会到paypal的付款页面。用你的sandbox测试买家账号去付款。就会发现付款成功。然后登陆你的测试卖家账号。会发现卖家账号已经收到付款。当然这里会扣除paypal收取的手续费。手续费收的是卖家的。</p><p>下面来具体看看php是怎么实现的。首先先要把paypal提供的 php-sdk给弄到你的代码目录中来。这里介绍使用php的包管理器composer来获取***sdk、当然你可以可以从github等其他渠道获取***的paypal php-sdk。</p><p>默认你的电脑已经安装composer了。如果没有自己去度娘或者google下composer安装。</p><p>然后在你的代码根目录写一个composer.json文件来获取包内容。json文件代码如下:</p><p>{<br />     "require" : { "paypal/rest-api-sdk-php" : "1.5.1"<br />     }<br /> }</p><p>这里如果是 linux/unix系统就直接再根目录执行composer install来获取包内容。</p><p>安装好之后。根目录下面会产生一个vendor目录。里面有composer 和 paypal两个子目录。composer里面实现了自动加载、paypal则是你的sdk内容。</p><p>接 下来我们来写一个公共文件(这里默认用 app/start.php、你的项目中可以自定义)、其实里面就只是实现了 sdk的autoload.php自动加载 和 创建刚才上面的的client id  和 secret生成的paypal支付对象实例。start.php代码如下:</p><p> php<br /></p><p>require "vendor/autoload.php"; //载入sdk的自动加载文件 define('SITE_URL', 'http://www.paydemo.com'); //网站url自行定义 //创建支付对象实例 $paypal = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( '你的Client ID' '你的secret'<br />     )<br /> );<br /></p><p>接下来就来实现表单中提交的处理文件 checkout.php。代码内容如下:</p><p> php</p><p>/**<br /> * @author xxxxxxxx<br /> * @brief 简介:<br /> * @date 15/9/2<br /> * @time 下午5:00<br /> */<br /> use \PayPal\Api\Payer;<br /> use \PayPal\Api\Item;<br /> use \PayPal\Api\ItemList;<br /> use \PayPal\Api\Details;<br /> use \PayPal\Api\Amount;<br /> use \PayPal\Api\Transaction;<br /> use \PayPal\Api\RedirectUrls;<br /> use \PayPal\Api\Payment;<br /> use \PayPal\Exception\PayPalConnectionException;</p><p> require "app/start.php"; if (!isset($_POST['product'], $_POST['price'])) { die("lose some params"); } $product = $_POST['product']; $price = $_POST['price']; $shipping = 2.00; //运费 $total = $price + $shipping; $payer = new Payer(); $payer->setPaymentMethod('paypal'); $item = new Item(); $item->setName($product) ->setCurrency('USD') ->setQuantity(1) ->setPrice($price); $itemList = new ItemList(); $itemList->setItems([$item]); $details = new Details(); $details->setShipping($shipping) ->setSubtotal($price); $amount = new Amount(); $amount->setCurrency('USD') ->setTotal($total) ->setDetails($details); $transaction = new Transaction(); $transaction->setAmount($amount) ->setItemList($itemList) ->setDescription("支付描述内容") ->setInvoiceNumber(uniqid()); $redirectUrls = new RedirectUrls(); $redirectUrls->setReturnUrl(SITE_URL . '/pay.php?success=true') ->setCancelUrl(SITE_URL . '/pay.php?success=false'); $payment = new Payment(); $payment->setIntent('sale') ->setPayer($payer) ->setRedirectUrls($redirectUrls) ->setTransactions([$transaction]); try { $payment->create($paypal); } catch (PayPalConnectionException $e) { echo $e->getData(); die(); } $approvalUrl = $payment->getApprovalLink(); header("Location: {$approvalUrl}");<br /></p><p>checkout.php通过表单提交上来的参数对支付具体细节和参数进行初始化和设置。这里只列出了常用的部分。paypal提供了很多参数设置。具体更丰富的可以自己参考paypal官方开发者文档。</p><p>checkout.php设置完参数之后。会生成一个支付链接。用header跳转到这个支付链接(就是paypal的支付页面)到这个支付页面上面就可以用你的sandbox提供的buyer账号去支付了。截图如下:</p><p>用buyer账号支付完成之后。去看看你的sandbox的商家账户余额吧。就会发现已经收到了扣除手续费外的钱了。</p><p>这里支付成功 或者 失败后还有一个回调的处理。回调处理的php文件再上面的checkout.php里面的setReturnUrl处设置。这里设置的是/pay.php?success=true</p><p>接下来我们来看看pay.php是怎么简单处理回调的。先贴上pay.php的代码:</p><p>php</p><p>require 'app/start.php';</p><p> use PayPal\Api\Payment;<br /> use PayPal\Api\PaymentExecution;</p><p> if(!isset($_GET['success'], $_GET['paymentId'], $_GET['PayerID'])){<br />     die();<br /> }</p><p> if((bool)$_GET['success']=== 'false'){</p><p>     echo 'Transaction cancelled!';<br />     die();<br /> }</p><p> $paymentID = $_GET['paymentId'];<br /> $payerId = $_GET['PayerID'];</p><p> $payment = Payment::get($paymentID, $paypal);</p><p> $execute = new PaymentExecution();<br /> $execute->setPayerId($payerId);</p><p> try{<br />     $result = $payment->execute($execute, $paypal);<br /> }catch(Exception $e){<br />     die($e);<br /> }<br /> echo '支付成功!感谢支持!';<br /></p><p>好了。到这里一个简单的paypal支付的demo其实已经走通了。懂得支付原理之后、想要再你自己的项目里面进行更丰富的扩展、就去paypal的官方文档查看更多具体的开发项设置。包括交易明细的获取等等都是可以实现的。这里就不具体讲下去了。</p> <br> 当前题目:PHP语言开发Paypal支付demo的具体实现 <br> 网页路径:<a href="http://www.cxhlcq.cn/article/dhpeese.html">http://www.cxhlcq.cn/article/dhpeese.html</a> </div> </div> </div> <div class="other container"> <h3>其他资讯</h3> <ul> <li> <a href="/article/coepocj.html">服务器fc卡是什么意思</a> </li><li> <a href="/article/coepodp.html">轻松掌握Linux脚本编程——方法($()linux脚本)</a> </li><li> <a href="/article/coepooh.html">服务器电信好不好?(服务器电信好不好用)</a> </li><li> <a href="/article/coepoco.html">如何解决mysql数据库输入汉字报错问题?(mysql数据库输入汉字报错)</a> </li><li> <a href="/article/coepodj.html">WCF消息模式基本内容简述</a> </li> </ul> </div> <div class="footer"> <div class="foota container"> <div class="foot_nav fl col-lg-8 col-md-8 col-sm-12 col-xs-12"> <ul> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"> <h3>网站制作</h3> <a href="http://www.cqcxhl.com/" target="_blank">网站制作</a><a href="http://www.cxjianzhan.com/mobile/" target="_blank">wap网站制作</a><a href="https://www.cdxwcx.com/wangzhan/mbqiye.html" target="_blank">成都企业网站制作</a><a href="http://www.scyanting.com/" target="_blank">盐亭网站制作公司</a><a href="http://chengdu.cdxwcx.cn/" target="_blank">成都网站制作</a><a href="http://www.kswsj.com/" target="_blank">成都网站制作</a> </li> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"> <h3>企业服务</h3> <a href="https://www.cdcxhl.com/ruanwen/yingxiao/" target="_blank">软文平台</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">分类目录</a><a href="https://www.cdcxhl.com/service/guangdianxuke.html" target="_blank">广播电视节目制作许可证</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站收录</a><a href="https://www.cdcxhl.com/mianfei/jianzhan/" target="_blank">免费建站</a><a href="https://www.cdcxhl.com/mianfei/jianshe/chengdu.html" target="_blank">免费网站建设</a> </li> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"> <h3>网站建设</h3> <a href="http://m.cdxwcx.com/" target="_blank">成都网站建设</a><a href="https://www.cdxwcx.com/city/pengzhou/" target="_blank">彭州网站建设</a><a href="http://chengdu.cdcxhl.cn/" target="_blank">成都网站建设</a><a href="http://www.scjiangyou.cn/" target="_blank">江油网站建设</a><a href="https://www.cdxwcx.com/city/jiangyou/" target="_blank">江油网站建设</a><a href="https://www.cdcxhl.com/" target="_blank">网站建设</a> </li> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"> <h3>服务器托管</h3> <a href="https://www.cdcxhl.com/cqtuoguan.html" target="_blank">重庆服务器托管</a><a href="http://www.cqfuwuqi.com/" target="_blank">重庆服务器托管</a><a href="https://www.cdcxhl.com/idc/ershu.html" target="_blank">二枢服务器托管</a><a href="https://www.cdcxhl.com/idc/zongshu.html" target="_blank">棕树服务器托管</a><a href="https://www.cdcxhl.com/idc/xixin.html" target="_blank">西信服务器托管</a><a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">绵阳主机托管</a> </li> </ul> </div> <div class="footar fl col-lg-4 col-md-4 col-sm-12 col-xs-12"> <p>全国免费咨询:</p> <b>400-028-6601</b> <p>业务咨询:028-86922220 / 13518219792</p> <p>节假值班:18980820575 / 13518219792</p> <p>联系地址:成都市太升南路288号锦天国际A幢1002号</p> </div> </div> <div class="footb"> <div class="copy container"> <div class="fl">Copyright © 成都创新互联科技有限公司重庆分公司 <a href="https://beian.miit.gov.cn/" target="_blank">渝ICP备2021005571号</a></div> <!--<div class="fr"><a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:<a href="https://www.cdcxhl.com/" target="_blank">创新互联</a></div>--> </div> </div> <div class="link"> <div class="container"> 友情链接:: <a href="https://www.cdcxhl.com/" target="_blank">成都网站建设</a> <a href="https://www.cdcxhl.com/city/chongqing.html" target="_blank">重庆网站建设</a> <a href="">四川网站建设</a> <a href="">重庆建设网站</a> <a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">移动服务器托管</a> <a href="http://www.cdfuwuqi.com/" target="_blank">成都服务器托管</a> <a href="https://www.cdcxhl.cn/" target="_blank">云服务器</a> <a href="http://www.cdhuace.com/" target="_blank">广告设计制作</a> <a href="https://www.cdcxhl.com/sheji/chongqing.html" target="_blank">重庆网页设计</a> <a href="https://www.cdcxhl.com/zuo/chongqing.html" target="_blank">重庆做网站</a> <a href="https://www.cdcxhl.com/zhizuo/chongqing.html" target="_blank">重庆网站制作</a> <a href="">重庆网站建设</a> <a href="">重庆网站公司</a> <a href="">渝中网站制作</a> <a href="">重庆网站设计</a> </div> </div> </div> <div class="foot"> <ul class="public-celan"> <li> <a href="https://p.qiao.baidu.com/cps3/chatIndex?siteToken=6ce441ff9e2d6bedbdfc2a4138de449e&speedLogId=162260383240185e3_1622603832401_02407&eid=6256368&reqParam=%7B%22from%22%3A1%2C%22sessionid%22%3A%22-100%22%2C%22siteId%22%3A%2211284691%22%2C%22tid%22%3A%22-1%22%2C%22userId%22%3A%226256368%22%2C%22ttype%22%3A1%2C%22pageId%22%3A0%7D" target="_blank" class="a1 db tc"> <img src="/Public/Home/img/icon-23.png" alt="" class="db auto"> <span class="span-txt">在线咨询</span> </a> </li> <li> <a href="tel:18980820575" class="a1 db tc"> <img src="/Public/Home/img/icon-24.png" alt="" class="db auto"> <span class="span-txt">电话咨询</span> </a> </li> <li> <a target="_blank" href="tencent://message/?uin=1683211881&Site=&Menu=yes" class="a1 db tc"> <img src="/Public/Home/img/icon-25.png" alt="" class="db auto"> <span class="span-txt">QQ咨询</span> </a> </li> <li> <a target="_blank" href="tencent://message/?uin=532337155&Site=&Menu=yes" class="a1 db tc public-yuyue-up"> <img src="/Public/Home/img/icon-26.png" alt="" class="db auto"> <span class="span-txt">预约顾问</span> </a> </li> </ul> </div> <div class="customer"> <dl class="icon1"> <dt> <a href="tencent://message/?uin=1683211881&Site=&Menu=yes"> <i class="iconT"><img src="/Public/Home/img/QQ.png" alt=""></i> <p>在线咨询</p> </a> </dt> </dl> <dl class="icon2"> <dt><i><img src="/Public/Home/img/weixin.png" alt=""></i><p>微信咨询</p></dt> <dd><img src="/Public/Home/img/ewm.png"></dd> </dl> <dl class="icon3"> <dt><i><img src="/Public/Home/img/dianhua.png" alt=""></i><p>电话咨询</p></dt> <dd> <p>028-86922220(工作日)</p> <p>18980820575(7×24)</p> </dd> </dl> <dl class="icon4"> <dt class="sShow"> <a href="tencent://message/?uin=244261566&Site=&Menu=yes"> <i><img src="/Public/Home/img/dengji.png" alt=""></i><p>提交需求</p> </a> </dt> </dl> <dl class="icon5"> <dt class="gotop"> <a href="#top"> <i><img src="/Public/Home/img/top.png" alt=""></i><p>返回顶部</p> </a> </dt> </dl> </div> </body> </html> <script> $(".con img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>