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

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

这篇文章将为大家详细讲解有关PHP语言开发Paypal支付demo的具体实现是怎样的,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

成都创新互联专注于企业成都全网营销、网站重做改版、睢宁县网站定制设计、自适应品牌网站建设、H5建站商城网站建设、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为睢宁县等各大城市提供网站开发制作服务。

一、开发前准备

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项即可、代码和截图如下:

DOCTYPE html>                        支付页面title>     head>     <body>         <div>             <form action="checkout.php" method="post" autocomplete="off">                 <label for="item">                     产品名称                     <input type="text" name="product">                 label>                 <br>                 <label for="amount">                     价格                     <input type="text" name="price">                 label>                 <br>                 <input type="submit" value="去付款">             form>         div>     body> html></pre><p><img src="/upload/otherpic72/444422.jpg" alt="PHP语言开发Paypal支付demo的具体实现是怎样的"></p><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</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 type="_moz"/></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;<br/><br/>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 type="_moz"/></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';<br/><br/>use PayPal\Api\Payment;<br/>use PayPal\Api\PaymentExecution;<br/><br/>if(!isset($_GET['success'], $_GET['paymentId'], $_GET['PayerID'])){<br/>    die();<br/>}<br/><br/>if((bool)$_GET['success']=== 'false'){<br/><br/>    echo 'Transaction cancelled!';<br/>    die();<br/>}<br/><br/>$paymentID = $_GET['paymentId'];<br/>$payerId = $_GET['PayerID'];<br/><br/>$payment = Payment::get($paymentID, $paypal);<br/><br/>$execute = new PaymentExecution();<br/>$execute->setPayerId($payerId);<br/><br/>try{<br/>    $result = $payment->execute($execute, $paypal);<br/>}catch(Exception $e){<br/>    die($e);<br/>}<br/>echo '支付成功!感谢支持!';<br type="_moz"/></p><p>好了。到这里一个简单的paypal支付的demo其实已经走通了。懂得支付原理之后、想要再你自己的项目里面进行更丰富的扩展、就去paypal的官方文档查看更多具体的开发项设置。包括交易明细的获取等等都是可以实现的。这里就不具体讲下去了。</p><p>关于PHP语言开发Paypal支付demo的具体实现是怎样的就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。</p>            
            
                        <br>
            文章题目:PHP语言开发Paypal支付demo的具体实现是怎样的            <br>
            标题URL:<a href="http://cxhlcq.cn/article/pegshp.html">http://cxhlcq.cn/article/pegshp.html</a>
        </div>
    </div>
</div>
<div class="other container">
    <h3>其他资讯</h3>
    <ul>
        <li>
                <a href="/article/dopdesi.html">sap系统中周期单位价格的简单介绍</a>
            </li><li>
                <a href="/article/dopdese.html">sap财务系统wa是什么的简单介绍</a>
            </li><li>
                <a href="/article/dopdesp.html">开发zblog用什么ui zblog开发教程</a>
            </li><li>
                <a href="/article/dopdeeo.html">mysql5安装怎么下载 mysql56安装教程</a>
            </li><li>
                <a href="/article/dopdsej.html">vb.net检测关机 vbs关机命令</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.wjwzjz.com/" target="_blank">温江网站制作</a><a href="http://chengdu.cdcxhl.com/" target="_blank">成都营销网站制作</a><a href="https://www.cdcxhl.com/zhizuo/chengdu.html" target="_blank">四川成都网站制作</a><a href="https://www.cdxwcx.com/" target="_blank">成都网站制作</a><a href="http://www.kswsj.com/" 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/service/icpxuke.html" target="_blank">互联网信息经营许可证</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站快速收录</a><a href="https://www.cdcxhl.com/link/" target="_blank">链接买卖</a><a href="https://www.cdcxhl.com/service/icpbeian.html" target="_blank">ICP经营性备案</a><a href="https://www.cdcxhl.com/hangyead/" target="_blank">一元广告</a><a href="https://www.cdcxhl.com/service/zzgj.html" 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/mobile.html" target="_blank">移动网站建设</a><a href="http://chengdu.cdcxhl.cn/jianshe/" target="_blank">网站建设公司</a><a href="http://m.cdcxhl.com/" target="_blank">成都网站建设</a><a href="http://www.cdkjz.cn/" target="_blank">成都网站建设</a><a href="http://chengdu.cdcxhl.cn/qiye/" target="_blank">企业网站建设公司</a><a href="http://m.xwcx.net/wangzhan/" 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/gysx.html" target="_blank">贵阳三线机房</a><a href="https://www.xwcx.net/tgxq/cdghjf.html" target="_blank">成都光华机房</a><a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">绵阳机房租用</a><a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">绵阳服务器托管</a><a href="https://www.cdcxhl.com/idc/yaan.html" target="_blank">雅安服务器托管</a><a href="https://www.cdcxhl.com/idc/deyang.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>