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> <p> 分享题目:<a href="http://www.hantingmc.com/qtweb/news19/243169.html">PHP语言开发Paypal支付demo的具体实现</a> <br> 文章源于:<a href="http://www.hantingmc.com/qtweb/news19/243169.html">http://www.hantingmc.com/qtweb/news19/243169.html</a> </p> <p> 网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等 </p> <p class="adpic"> <a href="https://www.cdcxhl.com/service/ad.html" target="_blank" class="ad">广告</a> <a href="" target="_blank" class="adimg"><img src=""></a> </p> <p class="copy"> 声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a> </p> </div> <div class="newsmorelb"> <p>猜你还喜欢下面的内容</p> <ul> <li> <a href="/qtweb/news18/243168.html">长春网站优化方式</a> </li><li> <a href="/qtweb/news17/243167.html">Oracle交换空间操作失败排查经验</a> </li><li> <a href="/qtweb/news16/243166.html">精准无误!st60红外测温仪带您体验高效远距离温度测量</a> </li><li> <a href="/qtweb/news15/243165.html">虚拟主机上如何配置文件-虚拟主机/数据库问题</a> </li><li> <a href="/qtweb/news14/243164.html">主流的服务器虚拟化技术包括?(服务器虚拟化技术特点有哪些)</a> </li><li> <a href="/qtweb/news13/243163.html">linux怎么搭建数据库服务器</a> </li><li> <a href="/qtweb/news12/243162.html">ipad更新系统卡住了怎么办?(windows10安装更新卡住了)</a> </li><li> <a href="/qtweb/news11/243161.html">怎么用css实现滚动效果</a> </li><li> <a href="/qtweb/news10/243160.html">监控连接报错10005</a> </li> </ul> </div> </div> <div class="col-lg-3 noneb"> <div class="bkright" style="margin-top: 0"> <p><a href="https://www.cdcxhl.com/news/qiyewangzhan/">企业网站制作知识</a></p> <ul> <li> <a class="text_overflow" href="/qtweb/news27/529877.html">21年windows81专业版怎么激活?(Win81官方终极正式版)</a> </li><li> <a class="text_overflow" href="/qtweb/news37/488287.html">Redis没有数据库名的新方式(Redis没有库名)</a> </li><li> <a class="text_overflow" href="/qtweb/news12/207112.html">虚拟主机可以使用云数据库吗?(虚拟空间数据库)</a> </li><li> <a class="text_overflow" href="/qtweb/news16/98316.html">XML数据库产品:新时代数据存储技术(xml数据库产品)</a> </li><li> <a class="text_overflow" href="/qtweb/news25/158625.html">ubuntu和centos区别大吗</a> </li><li> <a class="text_overflow" href="/qtweb/news46/454446.html">微信如何制作ppt</a> </li><li> <a class="text_overflow" href="/qtweb/news32/430332.html">win10开机一直停留在还原以前版本?(windows10旧版本下载)</a> </li><li> <a class="text_overflow" href="/qtweb/news16/134466.html">双线服务器和多线服务器的优势和劣势是什么</a> </li><li> <a class="text_overflow" href="/qtweb/news10/492660.html">G口带宽是什么概念?G级服务器</a> </li><li> <a class="text_overflow" href="/qtweb/news45/280595.html">html中如何不换行</a> </li><li> <a class="text_overflow" href="/qtweb/news28/18228.html">linux网站项目发布要做的配置有哪些内容</a> </li><li> <a class="text_overflow" href="/qtweb/news34/65584.html">如何关掉后台广告</a> </li><li> <a class="text_overflow" href="/qtweb/news40/141640.html">新加坡服务器(新加坡服务器为什么比较受欢迎?)</a> </li><li> <a class="text_overflow" href="/qtweb/news14/461814.html">生产环境遇到一个Go问题,整组人都懵圈了...</a> </li><li> <a class="text_overflow" href="/qtweb/news16/492216.html">lrf44n参数?(香港大带宽VDS)</a> </li> </ul> </div> <div class="bkright tag"> <p><a href="https://www.cdcxhl.com/hangye/" target="_blank">同城分类信息</a></p> <ul> <li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/huisuosj/" target="_blank">会所设计</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bengche/" target="_blank">混凝土泵车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/xiaojbc/" target="_blank">小搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/weixiufdj/" target="_blank">发电机维修</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/jbgc/" target="_blank">搅拌罐车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/suliaodai/" target="_blank">塑料袋</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/led/" target="_blank">LED显示屏</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/jiagu/" target="_blank">加固</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/shiliangting/" target="_blank">石凉亭</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sljbc/" target="_blank">生料搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sqwhq/" target="_blank">社区文化墙</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sljbgc/" target="_blank">三轮搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/ldjbc/" target="_blank">履带搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bangongkongjian/" target="_blank">办公空间设计</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/banjia/" target="_blank">搬家公司</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/shipaifang/" target="_blank">石牌坊</a> </li> </ul> </div> </div> </div> <div class="carousel-inner linkbg" style="background: #fff"> <div class="container"> <a href="http://m.cdcxhl.com/dingzhi.html" target="_blank">定制网站建设多少钱</a>    <a href="http://www.4006tel.net/mobile/" target="_blank">app开发</a>    <a href="http://www.cdxwcx.cn/tuoguan/xiyun.html" target="_blank">成都移动托管</a>    <a href="http://www.cdxwcx.cn/tuoguan/bgp.html" target="_blank">bgp机房托管</a>    <a href="https://www.xwcx.net/jigui.html" target="_blank">成都机柜租用</a>    <a href="http://www.cdkjz.cn/wangzhan/" target="_blank">网站seo</a>    <a href="http://www.4006tel.net/mobile/" target="_blank">app制作</a>    <a href="http://www.cdxwcx.cn/weihu/" target="_blank">成都网站改版</a>    <a href="http://www.cdhuace.com/biaoshi.html" target="_blank">成都标识标牌设计制作</a>    <a href="http://www.scfushun.com/" target="_blank">富顺网站建设</a>    <a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">响应式网站建设</a>    <a href="http://www.scjiangyou.com/" target="_blank">江油网站建设</a>    <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>    <a href="http://www.cdhcym.com/" target="_blank">鸿程源茂</a>    <a href="http://www.msbanjia.cn/" target="_blank">美美搬家</a>    <a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">绵阳服务器托管</a>    <a href="http://www.hxwzsj.com/" target="_blank">和县翔豪网站</a>    <a href="https://www.cdcxhl.com/" target="_blank">成都做网站</a>    <a href="http://www.cdxwcx.cn/tuoguan/xibuxinxi.html" target="_blank">西信服务器托管</a>    <a href="http://www.scyucang.com/" target="_blank">宇仓仓储</a>     </div> </div> <footer> <div class="carousel-inner footjz"> <div class="container"> <i class="icon iconfont zbw"></i> 高品质定制 <i class="icon iconfont"></i> 跨终端自动兼容 <i class="icon iconfont"></i> 节约开发成本 <i class="icon iconfont"></i> 开发周期短 <i class="icon iconfont"></i> 一体化服务 <button type="button" class="btn btn-default btn-lg" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 立即开始2800定制网站建设</button> <button type="button" class="btn btn-default btn-xs" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 2800定制网站建设</button> </div> </div> <div class="carousel-inner bqsy"> <div class="container"> <div class="lxfs"> <h4 class="yutelnone">028-86922220 13518219792</h4> <h4 class="yutelblock"><a href="tel:02886922220">028-86922220</a> <a href="tel:13518219792">13518219792</a></h4> <a class="btn btn-default" href="tencent://message/?uin=532337155&Site=&Menu=yes" role="button">网站建设<span>QQ</span>:532337155</a> <a class="btn btn-default" href="tencent://message/?uin=631063699&Site=&Menu=yes" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=532337155&version=1&src_type=web&web_src=oicqzone.com" role="button">网站制作<span>QQ</span>:532337155</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=631063699&version=1&src_type=web&web_src=oicqzone.com" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn-default nonea" href="tencent://message/?uin=1683211881&Site=&Menu=yes" role="button">售后QQ:1683211881</a> <div class="dz">创新互联建站专注: <a href="https://www.cdcxhl.com/" target="_blank">网站设计</a> <a href="https://www.cdcxhl.com/" target="_blank">网站制作</a> <a href="https://www.cdcxhl.com/" target="_blank">网站建设</a> <address>地址:成都太升南路288号锦天国际A幢10楼</address> </div> </div> <div class="bzdh dz"><img src="https://www.cdcxhl.com/imges/bottom_logo.png" alt="创新互联"> <p><a href="https://www.cdcxhl.com/menu.html" target="_blank">成都创新互联科技有限公司</a><br> Tel:400-028-6601(7x24h)</p></div> </div> </div> </footer> </body> </html> <script> $.getJSON ("../../qtwebpic.txt", function (data) { var jsonContent = { "featured":data } var random = jsonContent.featured[Math.floor(Math.random() * jsonContent.featured.length)]; $(".adpic .adimg").attr("href",random.link) $(".adpic img").attr("src",random.pic); }) </script>