PHP语言开发Paypal支付demo的具体实现-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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://kswsj.cn/article/dhpeese.html">http://kswsj.cn/article/dhpeese.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/coddceh.html">两位后缀域名,cc后缀的域名是哪里的</a> </li><li> <a href="/article/coddcds.html">视觉智能平台小程序文字识别访问域名除了配置这个,还需要配置其他的么?</a> </li><li> <a href="/article/coddceo.html">Nginx配置中运行与启动的详细介绍</a> </li><li> <a href="/article/coddcid.html">凡搜网站宝效果怎么样(搜凡餐饮)</a> </li><li> <a href="/article/coddcic.html">Redis的累加性能优化(redis累加性能为题)</a> </li> </ul> </div> </div> <div class="line"></div> <!--底部--> <footer id="5"> <div class="foot1 container"> <div class="list"> <div class="item"> <a href="javascript:;"> <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span> <strong>关注我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>索要报价</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>我要咨询</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>找到我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>投诉建议</strong> </a> </div> </div> <div class="tel"> <dl> <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br> <span>也许您需要专业的服务,欢迎来电咨询</span> </dl> <dl> <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br> <span>您的需求,是我们前进的动力</span> </dl> </div> </div> <div class="friend"> <div class="container"> <span class="tit">友情链接:</span> <div class="inner"> <a href="https://www.cdcxhl.com/" target="_blank">网站建设公司</a><a href="https://www.cdcxhl.com/qiye.html" target="_blank">成都企业网站建设</a><a href="https://www.cdcxhl.com/google.html" target="_blank">谷歌推广公司</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">成都微信小程序开发公司</a><a href="https://www.cdcxhl.com/weihu/" target="_blank">成都网站维护公司</a><a href="https://www.cdcxhl.com/sosuo.html" target="_blank">搜索引擎优化</a><a href="https://www.cdcxhl.com/gaofang/" target="_blank">成都高防服务器租用</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">成都抖音运营</a><a href="https://www.cdcxhl.com/weihu/" target="_blank">成都网站维护</a><a href="https://www.cdcxhl.com/cloud/" target="_blank">成都云服务器租用</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">发布软文</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站建设公司</a><a href="https://www.cdcxhl.com/link/" target="_blank">卖友情链接</a><a href="https://www.cdcxhl.com/link/" target="_blank">买链接</a><a href="https://www.cdcxhl.com/sosuo.html" target="_blank">网站搜索引擎优化</a><a href="https://www.cdcxhl.com/link/" target="_blank">友情链接购买</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">成都品牌网站建设</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布平台</a> </div> </div> </div> <div class="foot"> <div class="container"> <div class="footNav"> <h3>网站建设</h3> <a href="https://www.cdxwcx.com/" target="_blank">网站建设</a><a href="http://www.cqcxhl.com/service/foreigntrade.html" target="_blank">重庆外贸网站建设</a><a href="http://www.cxhljz.cn/" target="_blank">成都网站建设</a> </div> <div class="footNav"> <h3>服务器托管</h3> <a href="https://www.cdcxhl.com/idc/ziyang.html" target="_blank">资阳天府云计算中心</a><a href="https://www.cdcxhl.com/idc/mianyang.html" target="_blank">绵阳服务器托管</a><a href="https://www.cdcxhl.com/cqtuoguan.html" target="_blank">重庆服务器托管</a> </div> <div class="footNav"> <h3>网站制作</h3> <a href="http://www.cdxwcx.cn/bj/" target="_blank">网站制作报价</a><a href="https://www.cdxwcx.com/wangzhan/shop.html" target="_blank">成都商城网站制作</a><a href="http://www.cdxtjz.com/" target="_blank">网站制作</a> </div> <div class="footNav"> <h3>企业服务</h3> <a href="https://www.cdcxhl.com/mianfei/jianzhan/" target="_blank">成都免费建站</a><a href="https://www.cdcxhl.com/link/" target="_blank">买友情链接</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">免费收录</a> </div> <div class="fr ecode"> <div class="fl"> <img src="/Public/Home/img/ewm.jpg"> <p>关注企业微信</p> </div> <div class="fr slogan"> <p class="icon"> <a class="ph" href=""><i class="iconfont"></i></a> <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a> </p> <p> <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、 <a href="">营销型网站建设</a>、<a href="">网站建设</a>、 <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a> </p> </div> </div> </div> <div class="bottom container"> <p class="fl"> 版权所有:成都创新互联科技有限公司 备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a> 服务热线:028-86922220 </p> <p class="fr"> <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>: <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a> </p> </div> </div> </footer> <!--在线咨询--> <div class="fot"> <ul> <li> <a href="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" target="_blank"> <img src="/Public/Home/img/fot1.png" alt=""> <p>在线咨询</p> </a> </li> <li> <a href="tel:18980820575" target="_blank"> <img src="/Public/Home/img/fot2.png" alt=""> <p>拨打电话</p> </a> </li> </ul> </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>