澄梦希喜广告网站制作遂宁站

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.hfanp.com/article/dhpeese.html">http://www.hfanp.com/article/dhpeese.html</a> </div> </div> </div> <div class="other container"> <h3>其他资讯</h3> <ul> <li> <a href="/article/cdjgeog.html">Linux下如何编译执行.c文件(linux运行.c文件)</a> </li><li> <a href="/article/cdjgsdh.html">windows无法完成格式化?windows无法完成格式化</a> </li><li> <a href="/article/cdjgspg.html">香港网络延迟解决方法?(香港服务器很卡怎么处理掉)</a> </li><li> <a href="/article/cdjgsig.html">为什么阿里云域名申请实名认证老是显示失败?(阿里云域名怎么批量实名认证)</a> </li><li> <a href="/article/cdjgeod.html">低价虚拟主机租用怎么搭建网站</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://seo.cdkjz.cn/wangzhan/" target="_blank">网站制作公司</a><a href="http://www.cdxwcx.cn/bj/" target="_blank">网站制作价格</a><a href="http://www.cxjianzhan.com/" target="_blank">网站制作公司</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">手机网站制作设计</a><a href="https://www.cdxwcx.com/wangzhan/mbqiye.html" target="_blank">成都企业网站制作</a><a href="http://www.myzwz.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/link/" target="_blank">卖友情链接</a><a href="https://www.cdcxhl.com/service/ypfwzgz.html" target="_blank">互联网药品信息服务资格证</a><a href="https://www.cdcxhl.com/link/" target="_blank">买友情链接</a><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/ruanwen/yingxiao/" 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/" target="_blank">成都网站建设</a><a href="http://www.cdkjz.cn/fangan/yiyao/" target="_blank">医药医疗网站建设方案</a><a href="http://chengdu.cdcxhl.cn/shop/" target="_blank">成都商城网站建设</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">成都品牌网站建设</a><a href="http://m.cdcxhl.cn/seo/" target="_blank">营销网站建设</a><a href="http://www.cdkjz.cn/fangan/response/" 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/idc/cqlt.html" target="_blank">重庆联通机房托管</a><a href="https://www.cdcxhl.com/idc/ershu.html" target="_blank">二枢服务器托管</a><a href="https://www.cdcxhl.com/tuoguan/yidong/" target="_blank">移动服务器托管</a><a href="https://www.cdcxhl.com/idc/meishan.html" target="_blank">眉山服务器托管</a><a href="https://www.xwcx.net/tgxq/cdghjf.html" target="_blank">成都光华机房</a><a href="https://www.cdcxhl.com/idc/guanghua.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>联系地址:四川省遂宁市船山区德水北路56号</p> </div> </div> <div class="footb"> <div class="copy container"> <div class="fl">Copyright © 四川澄梦希喜广告有限公司 <a href="https://beian.miit.gov.cn/" target="_blank">蜀ICP备2025175960号-6</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/suining.html" target="_blank">遂宁网站建设</a> <a href="https://www.cdcxhl.com/idc/meishan.html">眉山服务器托管</a> <a href="https://www.cdcxhl.com/paiming/suining.html">遂宁seo排名</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/suining.html" target="_blank">遂宁网页设计</a> <a href="https://www.cdcxhl.com/zuo/suining.html" target="_blank">遂宁做网站</a> <a href="https://www.cdcxhl.com/zhizuo/suining.html" target="_blank">遂宁网站制作</a> <a href="https://www.cdcxhl.com/seo/suining.html">遂宁网站推广</a> <a href="https://www.cdcxhl.com/idc/deyang.html">德阳服务器托管</a> <a href="https://www.cdcxhl.com/zuyong/meishan.html">眉山服务器租用</a> <a href="https://www.cdcxhl.com/tuoguan/">成都服务器托管</a> </div> </div> </div> <div class="foot"> <ul class="public-celan"> <li> <a href="#" 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>