How to call Alipay payment interface in php programming

  For any software, the payment function is the core, so the current mainstream payment interfaces are mainly Alipay, WeChat and UnionPay cards, and most developers and technicians still have a lot of troubles about how to call the payment interface of Alipay. Today, I will share with you how to correctly call the implementation of the Alipay payment interface. The code is as follows:

   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){

            // Merchant order number, the unique order number in the order system of the merchant's website, required

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

            // Order name, required

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

            // payment amount, required

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

            $total_amount = 0.01;

            // Product description, can be empty

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

            // construction parameters

            $payRequestBuilder = new AlipayTradePagePayContentBuilder();

            $payRequestBuilder->setBody($body);

            $payRequestBuilder->setSubject($subject);

            $payRequestBuilder->setTotalAmount($total_amount);

            $payRequestBuilder->setOutTradeNo($out_trade_no);

 

            $aop = new AlipayTradeService($config);

 

          /**

           * pagePay computer website payment request

           * @param $builder business parameter, generated using the object in buildmodel .

           * @param $return_url synchronous jump address, accessible from the public network

           * @param $notify_url asynchronous notification address, accessible from the public network

           * @return $response Information returned by Alipay

          */

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

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

          $map['out_trade_no']=$out_trade_no;

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

 

          // output form

          var_dump($response);          

        }

}

 

callback

    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);

        /* In the actual verification process, merchants are advised to add the following verifications.

        1. The merchant needs to verify whether the out_trade_no in the notification data is the order number created in the merchant's system,

        2. Determine whether total_amount is indeed the actual amount of the order (that is, the amount when the merchant's order was created),

        3. Check whether the seller_id (or seller_email) in the notification is the corresponding operator of the out_trade_no document (sometimes, a merchant may have multiple seller_id/seller_email )

        4. Verify that the app_id is the merchant itself.

        */

        if($result) {// Verification succeeded

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

          // Please add the business logic program code of the merchant here

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

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

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

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

 

          

          // ——Please write the program according to your business logic (the following code is for reference only)——

          

            // Get the notification return parameters of Alipay, please refer to the server asynchronous notification parameter list in the technical documentation

          

          // Merchant order number

 

          $out_trade_no = $_POST['out_trade_no'];

 

          // Alipay transaction number

 

          $trade_no = $_POST['trade_no'];

 

          // transaction status

          $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 buy goods 2 recharge balance 3 make order payment

                    if($type==1){

                        // buy item

                        $ 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){

                        // top up the balance

                        $rid=$arr['id'];

                        // Get recharge information

                        $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 order status

                            $update['paystate']=1;

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

                            if($res){

                                // update user balance

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

                            }

                        }                        

                    }elseif($type==3){

                        

                     

                    }

                }

            // Determine whether the order has been processed in the merchant's website

              // If no processing has been done, check the details of the order in the order system of the merchant's website according to the order number ( out_trade_no ), and execute the business procedures of the merchant

              // Please make sure that the total_amount at the time of request is consistent with the total_fee obtained at the time of notification

              // If processing has been done, do not execute the business program of the merchant

                

            // Note:

            // After the refund date exceeds the refundable period (such as three months refundable), the Alipay system sends the transaction status notification

            }

            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 buy goods 2 recharge balance 3 make order payment

                    if($type==1){

                        // buy item

                        $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);

                            

                            // update the point

                        $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){

                        // top up the balance

                        $rid=$arr['id'];

                        // Get recharge information

                        $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 order status

                            $update['paystate']=1;

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

                            if($res){

                                // update user balance

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

                            }

                        }

                        

                    }elseif($type==3){

                      

                    }

                }

            // Determine whether the order has been processed in the merchant's website

              // If no processing has been done, check the details of the order in the order system of the merchant's website according to the order number ( out_trade_no ), and execute the business procedures of the merchant

              // Please make sure that the total_amount at the time of request is consistent with the total_fee obtained at the time of notification

              // If processing has been done, do not execute the business program of the merchant      

            // Note:

            // After the payment is completed, the Alipay system sends the transaction status notification

            }

          // ——Please write the program according to your business logic (the above code is for reference only)——

          echo "success"; // Please do not modify or delete

        }else {

            // validation failed

            echo "fail";

 

        }

    }

  Well, seeing here, I believe that all technical developers already know where their problems are, so you can quickly try it. If there are other problems, you can leave a message to consult us.

  This article is edited and released by Yixuan Technology, a professional app development quotation. If you need to reprint, please indicate the original author and source!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325610793&siteId=291194637