新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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>
                网站网址:<a href="http://www.wjwzjz.com/article/pegshp.html">http://www.wjwzjz.com/article/pegshp.html</a>
            </div>
        </div>
        <div class="othernews">
            <h3>其他资讯</h3>
            <div class="othernews_list">
                <ul>
                    <li>
                            <a href="/article/dsddejp.html">ios开发中文,ios开发版下载</a>
                        </li><li>
                            <a href="/article/dsddees.html">ios字体开发,iOS 字体</a>
                        </li><li>
                            <a href="/article/dsddehe.html">html5后退,html前进后退</a>
                        </li><li>
                            <a href="/article/dsddeje.html">flutter云开发,Flutter开发</a>
                        </li><li>
                            <a href="/article/dsddepo.html">鸿蒙os3.0开发者入口,鸿蒙os20开发</a>
                        </li>                </ul>
            </div>
        </div>
    </div>
</div>

<div class="footer">
    <div class="footer_content">
        <div class="footer_content_top clear">
            <div class="content_top_share fl">
                <div><img src="/Public/Home/img/logo.png"></div>
                <div class="top_share_content">
                    <dd>分享至:</dd>
                    <dt class="bdsharebuttonbox clear" id="share">
                        <a href="#" class="bds_tsina iconfont fl" data-cmd="tsina" title="分享到新浪微博"></a>
                        <a href="#" class="bds_sqq iconfont fl" data-cmd="sqq" title="分享到QQ好友"></a>
                        <a href="#" class="bds_weixin iconfont fl" data-cmd="weixin" title="分享到微信"></a>
                        <a href="#" class="bds_weixin iconfont fl" data-cmd="tieba" title="分享到贴吧"></a>
                    </dt>
                    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
                </div>
            </div>
            <div class="content_top_left fl clear">
                <div class="top_left_list fl">
                    <dd><a href="/about/">关于我们</a></dd>
                    <dt>
                        <a href="/about/#gsjj">公司简介</a>
                        <a href="/about/#fzlc">发展历程</a>
                    </dt>
                </div>
                <div class="top_left_list fl">
                    <dd><a href="/service/">服务项目</a></dd>
                    <dt>
                        <a href="/service/">高端网站建设</a>
                        <a href="/miniprogram/">小程序开发</a>
                        <a href="/service/app.html">APP开发</a>
                        <a href="/service/yingxiao.html">网络营销</a>
                    </dt>
                </div>
                <div class="top_left_list fl">
                    <dd><a href="/jianzhan/">建站知识</a></dd>
                    <dt>
                        <a href="/jianzhan/">行业新闻</a>
                        <a href="/jianzhan/">建站学堂</a>
                        <a href="/jianzhan/">常见问题</a>
                    </dt>
                </div>
                <div class="top_left_list fl">
                    <dd><a href="/contact/">联系我们</a></dd>
                    <dt>
                        <a href="/contact/#lxwm">公司地址</a>
                        <a href="/contact/#rczp">人才招聘</a>
                    </dt>
                </div>
            </div>
            <div class="content_top_right addressR fr">
                <div class="top_right_title addressf_title">
                    <a href="javascript:;" class="on">成都</a>
                </div>
                <div class="top_right_content addressf">
                    <div class="right_content_li on">
                        <div class="right_content_list clear">
                            <dd class="fl iconfont"></dd>
                            <dt class="fl">电话:028-86922220</dt>
                        </div>
                        <div class="right_content_list clear">
                            <dd class="fl iconfont"></dd>
                            <dt class="fl">地址:成都市太升南路288号锦天国际A幢1002号</dt>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="footer_content_copyright clear">版权所有:成都新网创想广告设计中心(普通合伙)
        <a href="http://beian.miit.gov.cn/" rel="nofollow" target="_blank">蜀ICP备11025516号-13</a>
    </div>
</div>

<!--浮窗-->
<div class="FloatingWindow clear">
    <a href="tencent://message/?uin=1683211881&Site=&Menu=yes" class="FloatingWindow_list fr">
        <div class="FloatingWindow_list_title">
            <dd class="iconfont"></dd>
            <dt><span>在线</span>咨询</dt>
        </div>
    </a>
    <a href="javascript:;" class="FloatingWindow_list fr">
        <div class="FloatingWindow_list_title">
            <dd class="iconfont"></dd>
            <dt>服务热线</dt>
        </div>
        <div class="FloatingWindow_list_down fadeInRight animated">服务热线:028-86922220</div>
    </a>
    <a href="javascript:;" class="FloatingWindow_list fr STop">
        <div class="FloatingWindow_list_title">
            <dd class="iconfont"></dd>
            <dt>TOP</dt>
        </div>
    </a>
</div>

<script src="/Public/Home/js/jquery-1.8.3.min.js"></script>
<script src="/Public/Home/js/comm.js"></script>
<script src="/Public/Home/js/wow.js"></script>
<script src="/Public/Home/js/common.js"></script>
</body>
</html>
<script>
    $(".cont 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>