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

关于创新互联

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

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

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://kswsj.cn/article/pegshp.html">http://kswsj.cn/article/pegshp.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/ddhhpgh.html">c语言中解方程根的函数 c语言求方程式的根</a>
                </li><li>
                    <a href="/article/ddhhpii.html">腾讯云轻量无忧服务器升级 腾讯云轻量级服务器怎么用</a>
                </li><li>
                    <a href="/article/ddhhpje.html">智能聊天ai怎么用 chatpic怎么打开</a>
                </li><li>
                    <a href="/article/ddhhpgc.html">阿里云服务器预装环境 阿里云服务器安装yum</a>
                </li><li>
                    <a href="/article/ddhhpjg.html">包含SAP系统批量清AR的词条</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/ruanwen/" target="_blank">软文投放</a><a href="https://www.cdcxhl.com/tuoguan/" target="_blank">成都IDC机房托管</a><a href="https://www.cdcxhl.com/" target="_blank">建站公司</a><a href="https://www.cdcxhl.com/zuyong/" target="_blank">成都租用服务器</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站建设</a><a href="https://www.cdcxhl.com/gaiban/" target="_blank">成都网站改版公司</a><a href="https://www.cdcxhl.com/app.html" target="_blank">成都app软件开发公司</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">品牌网站建设公司</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器机柜租赁</a><a href="https://www.cdcxhl.com/security/" target="_blank">成都等保咨询</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">营销型网站建设</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">抖音运营公司</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">营销网站建设公司</a><a href="https://www.cdcxhl.com/wangzhandingzhi.html" target="_blank">成都企业网站定制</a><a href="https://www.cdcxhl.com/tuoguan/yaan/" target="_blank">川西大数据中心</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">小程序开发公司</a><a href="https://www.cdcxhl.com/gaofang/" target="_blank">高防服务器</a><a href="https://www.cdcxhl.com/yunying.html" target="_blank">成都网站托管</a>            </div>
        </div>
    </div>
    <div class="foot">
        <div class="container">
            <div class="footNav">
                <h3>网站建设</h3>
                <a href="http://m.cdcxhl.cn/mobile/" target="_blank">移动网站建设</a><a href="http://www.kswcd.com/solution/" target="_blank">网站建设方案</a><a href="http://www.abwzjs.com/" target="_blank">阿坝网站建设</a>            </div>
            <div class="footNav">
                <h3>服务器托管</h3>
                <a href="https://www.cdcxhl.com/idc/gysx.html" target="_blank">贵阳三线机房</a><a href="https://www.cdcxhl.com/tuoguan/" target="_blank">IDC机房托管</a><a href="https://www.cdcxhl.com/idc/zongshu.html" target="_blank">棕树服务器托管</a>            </div>
            <div class="footNav">
                <h3>网站制作</h3>
                <a href="http://www.cxjianzhan.com/" target="_blank">网站制作公司</a><a href="http://www.36103.cn/" target="_blank">成都网站制作</a><a href="http://chengdu.cdxwcx.cn/wangzhan/" target="_blank">手机网站制作</a>            </div>
            <div class="footNav">
                <h3>企业服务</h3>
                <a href="https://www.cdcxhl.com/service/wenwangwen.html" 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>            </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>