首页

如何调用支付宝支付接口的实现

支付宝支付 2018-05-07 5307

对于任何一款软件来说,支付功能都是核心的,那么目前的主流支付接口主要是支付宝、微信和银联卡,而大多数开发技术人员对于如何调用支付宝的支付接口还存在很大的困扰,今天app开发报价燚轩科技就来跟大家分享一下如何正确调用支付宝支付接口的实现,代码如下所示:

   public function zfbpay1(){

        require_once './ThinkPHP/Extend/Vendor/alipay1/config.php';

        require_once './ThinkPHP/Extend/Vendor/alipay1/pagepay/service/AlipayTradeService.php';

        require_once './ThinkPHP/Extend/Vendor/alipay1/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';

        $model=M('zfbpay');

        $add['oid']=I('nid');

        $add['type']=1;

        $add['WIDout_trade_no']=trim($_POST['WIDout_trade_no']);

        $add['WIDtotal_amount']=trim($_POST['WIDtotal_amount']);

        $add['WIDsubject']=trim($_POST['WIDsubject']);

        // dump($_POST);die;

        $re=$model->add($add);

        if($re){

            //商户订单号,商户网站订单系统中唯一订单号,必填

            $out_trade_no = trim($_POST['WIDout_trade_no']);

            //订单名称,必填

            $subject = trim($_POST['WIDsubject']);

            //付款金额,必填

            // $total_amount = trim($_POST['WIDtotal_amount']);

            $total_amount = 0.01;

            //商品描述,可空

            $body = trim($_POST['WIDbody']);

            //构造参数

            $payRequestBuilder = new AlipayTradePagePayContentBuilder();

            $payRequestBuilder->setBody($body);

            $payRequestBuilder->setSubject($subject);

            $payRequestBuilder->setTotalAmount($total_amount);

            $payRequestBuilder->setOutTradeNo($out_trade_no);

 

            $aop = new AlipayTradeService($config);

 

          /**

           * pagePay 电脑网站支付请求

           * @param $builder 业务参数,使用buildmodel中的对象生成。

           * @param $return_url 同步跳转地址,公网可以访问

           * @param $notify_url 异步通知地址,公网可以访问

           * @return $response 支付宝返回的信息

          */

          $response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);

          $map['nid']=I('nid');

          $map['out_trade_no']=$out_trade_no;

            M('need')->save($map);

 

          //输出表单

          var_dump($response);          

        }

}

 

回调

    public function updatezfb(){

        require_once './ThinkPHP/Extend/Vendor/alipay1/config.php';

        require_once './ThinkPHP/Extend/Vendor/alipay1/pagepay/service/AlipayTradeService.php';

        $arr=$_POST;

        $alipaySevice = new AlipayTradeService($config);

 

        $alipaySevice->writeLog(var_export($_POST,true));

        $result = $alipaySevice->check($arr);

        /* 实际验证过程建议商户添加以下校验。

        1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,

        2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),

        3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email

        4、验证app_id是否为该商户本身。

        */

        if($result) {//验证成功

          /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

          //请在这里加上商户的业务逻辑程序代

          // $add['content']=$_POST['out_trade_no'];

          // M('a')->add($add);

          // $add['content']=json_encode($_POST);

          // M('a')->add($add);

 

          

          //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——

          

            //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表

          

          //商户订单号

 

          $out_trade_no = $_POST['out_trade_no'];

 

          //支付宝交易号

 

          $trade_no = $_POST['trade_no'];

 

          //交易状态

          $trade_status = $_POST['trade_status'];

 

 

            if($_POST['trade_status'] == 'TRADE_FINISHED') {

                $zfbmodel=M('zfbpay');

                $arr=$zfbmodel->where("WIDout_trade_no=$out_trade_no")->find();

                if($arr){

                    $type=$arr['type'];//1购买商品 2充值余额 3做单支付

                    if($type==1){

                        //购买商品

                        $nid=$arr['id'];

                        $model=M('need');

                        $orderinfo=$model->where("nid=$nid")->find();

                       $tuoguan=$orderinfo['tuoguan'];

                        $province=$orderinfo['province'];

                        $finish_state=$orderinfo['finish_state'];

                        $arr1['tuoguan']=1;

                        if($finish_state==1){

                           $arr1['finish_state']=2;

                        }

                        if ($orderinfo['money']!=0) {

                           $arr1['money']=$orderinfo['money'];

                           

                        }else{

                           $arr1['money']=intval($orderinfo['money'])+intval($arr['WIDtotal_amount']);

                        }

                        $arr1['paytype']=1;

                        $res=$model->save($arr1);

                    }elseif($type==2){

                        //余额充值

                        $rid=$arr['id'];

                        //获取充值信息

                        $model=M('recharge');

                        $usermodel=M('user');

                        $rechargeinfo=$model->where("rid=$rid")->find();

                        $paystate=$rechargeinfo['paystate'];

                        if($paystate==0){

                            $uid=$rechargeinfo['uid'];

                            $num=$rechargeinfo['amoney'];      

                              //更新订单状态

                            $update['paystate']=1;

                            $res=$model->where("rid=$rid")->save($update);            

                            if($res){

                                //更新用户余额

                                $re2=$usermodel->where("id=$uid")->setInc('yue',$num);

                            }

                        }                        

                    }elseif($type==3){

                        

                     

                    }

                }

            //判断该笔订单是否在商户网站中已经做过处理

              //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序

              //请务必判断请求时的total_amount与通知时获取的total_fee为一致的

              //如果有做过处理,不执行商户的业务程序

                

            //注意:

            //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知

            }

            else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {

                $zfbmodel=M('zfbpay');

                $arr=$zfbmodel->where("WIDout_trade_no=$out_trade_no")->find();

                if($arr){

                    $type=$arr['type'];//1购买商品 2充值余额 3做单支付

                    if($type==1){

                        //购买商品

                        $oid=$arr['id'];

                        $model=M('order_goods');

                        $orderinfo=$model->where("oid=$oid")->find();

                        $paystate=$orderinfo['paystate'];

                        if($paystate==0){

                            $update['paystate']=1;

                            $res=$model->where("oid=$oid")->save($update);

                            

                            //更新惠点

                        $umod=M('user');

                        $ordersone=$model->where("oid=$oid")->find();

                        $uid=$ordersone['uid'];

                        $usertel=$umod->where("id=$uid")->find();

                        $sys=M('sys')->find();     

                        $mmp['id']=$usertel['id'];      

                        $mmp['money']=$usertel['money']+$ordersone['oprice'];

                        $mmp['huidian']=floor($mmp['money']/$sys['zshd']);

                        $userSave=$umod->save($mmp);

                        }

                    }elseif($type==2){

                        //余额充值

                        $rid=$arr['id'];

                        //获取充值信息

                        $model=M('recharge');

                        $usermodel=M('user');

                        $rechargeinfo=$model->where("rid=$rid")->find();

                        $paystate=$rechargeinfo['paystate'];

                        if($paystate==0){

                            $uid=$rechargeinfo['ui

分享到微信朋友圈 +
打开微信,点击底部的“发现”,使用 “扫一扫” 即可将网页分享到我的朋友圈。 如何使用?
推荐文章

热贴More +

服务范围More +

联系我们

请扫二维码联系客服

854221200@qq.com

185-3825-9583

QQ客服

关于  ·  招聘  ·  案例中心  ·  网站地图

©@2018 燚轩科技版权所有 豫ICP备16015002号-4

百度提供搜索支持