laravel 框架接入第三方支付宝扫码支付(万众云平台)

首先在控制器方法,去请求第三方的扫码支付接口

  1. //去充值的界面  
  2.     public function recharge(Request $request){  
  3.   
  4.         $data['money']=$request->input('money');//钱数  
  5.         $data['order_id']=$request->input('order_id');//订单号  
  6.         $data['card_num']=$request->input('card_num');//元宝数量  
  7.         $data['username']=$request->session()->get("username");//用户昵称  
  8.   
  9.         $url = 'http://epay.taidupay.com/api/pay/unifiedOrder';  
  10.         $key = '';//<span style="color:#33ff33;">平台申请的app_key</span>  
  11.         $datas = [  
  12.             'appId' =>'',//<span style="background-color: rgb(255, 153, 0);">平台申请的app_id</span>  
  13.             'timestamp' => (string)time(),  
  14.             'nonce' => time().rand(),  
  15.             'service' => 'alipay.native',  
  16.             'orderNo' => $data['order_id'],  
  17.             'totalAmount' =>1,  
  18.             'clientIp' => '116.62.214.81',  
  19.             'attach' => 'ces',  
  20.             'notifyUrl' => 'http://116.62.214.81/city.php',//异步回调地址(必须是外网访问到的地址)  
  21.             'body' => '元宝',  
  22.         ];  
  23.         ksort($datas);  
  24.   
  25.         $signOStr = '';  
  26.         foreach ($datas as $k => $val) {  
  27.             if ($signOStr != '') {  
  28.                 $signOStr .= '&';  
  29.             }  
  30.             $signOStr .= $k . '=' . $val;  
  31.         }  
  32.         $signOStr = $signOStr . '&key=' . $key;  
  33.         $datas['sign'] = strtoupper(MD5($signOStr));  
  34.   
  35.         $data['rs'] = json_decode($this->json_post($url, json_encode($datas)), true);  
  36.         //print_r($data['rs']);die;  
  37.         if($data['rs']['success'] == true && $data['rs']['status'] == 0){  
  38.               
  39.             echo json_decode($data['rs']['payInfo'], true);  
  40.         }else{  
  41.             echo "<script> alert('订单编号重复!');parent.location.href='http://116.62.214.81/userInfo'; </script>";   
  42.   
  43.         }  
  44.           
  45.         return view('recharge')->with('data'$data);  
  46.   
  47.     }  
    1. public function json_post($url$data){  
    2.         $ch = curl_init();  
    3.         $header = [  
    4.             'Accept-Charset: utf-8',  
    5.             'Content-Type: application/json'  
    6.         ];  
    7.         curl_setopt($ch, CURLOPT_URL, $url);  
    8.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
    9.         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
    10.         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');  
    11.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
    12.         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
    13.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
    14.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    15.         $tmpInfo = curl_exec($ch);  
    16.         $errorno=curl_errno($ch);  
    17.   
    18.         return $tmpInfo;  
    19.     }  
    20. city.php异步处理文件内容
      1. <?php  
      2. header("Content-type:text/html;charset=utf-8");  
      3.     $get_data = file_get_contents('php://input''r');  
      4.     //file_put_contents('a.txt',$get_data);  
      5.   
      6.         $get_data = json_decode($get_data, true);  
      7.         $key = '1bdc6c80b3cb8987e2720bd965748593f07bb3e9';  
      8.         $data = $get_data;  
      9.         unset($data['sign']);  
      10.         ksort($data);  
      11.   
      12.   
      13.         $signOStr = '';  
      14.         foreach ($data as $k => $val) {  
      15.             if ($signOStr != '') {  
      16.                 $signOStr .= '&';  
      17.             }  
      18.             $signOStr .= $k . '=' . $val;  
      19.         }  
      20.         $signOStr = $signOStr . '&key=' . $key;  
      21.         $sign = strtoupper(MD5($signOStr));  
      22.   
      23.         if($sign == $get_data['sign'] && $data['status'] == 3){  
      24.             $dbms='mysql';//数据库类型  
      25.             $dbName='youleapp';//使用的数据库  
      26.             $users='root';//数据库连接用户名  
      27.             $pwd='root';//数据库连接密码  
      28.             $host='127.0.0.1';//数据库主机名  
      29.             $dsn="$dbms:host=$host;port=3306;dbname=$dbName";  
      30.             $pdo=new PDO($dsn,$users,$pwd);//初始化一个PDO对象,就是创建了数据库连接对象$pdo  
      31.            //成功  
      32.             $out_trade_no=$data['orderNo'];//订单号  
      33.             $trade_no=$data['platformOrderNo'];  
      34.             $pay_time=$data['finishTime'];  
      35.             //通过订单号修改订单id  
      36.   
      37.             $updatesql="UPDATE pay_order set trade_no='$trade_no',pay_time='$pay_time',status=1 WHERE order_id='$out_trade_no'";  
      38.             $res=$pdo->exec($updatesql);  
      39.                 if($res==1){  
      40.                     //修改完状态之后要给用户加上元宝  
      41.                     //$order_user=$model_payorder->getorder($out_trade_no);  
      42.                     //查询修改完订单的信息  
      43.                     $query1="select * from `pay_order` where order_id='$out_trade_no'";//需要执行的sql语句  
      44.                     $res1=$pdo->prepare($query1);//准备查询语句  
      45.                     $res1->execute();//  
      46.                     $arr=$res1->fetch(PDO::FETCH_ASSOC);  
      47.   
      48.                       
      49.                     $uid=$arr['uid'];//用户的id  
      50.                     $querys="select * from `user` where id='$uid'";//需要执行的sql语句  
      51.                     $res2=$pdo->prepare($querys);//准备查询语句  
      52.                     $res2->execute();//  
      53.                     $user=$res2->fetch(PDO::FETCH_ASSOC);  
      54.   
      55.                     $acer_money=$user['acer_money'];  
      56.                     $card_num=$arr['card_num'];  
      57.                     $updatesq="UPDATE `user` set acer_money='$acer_money'+'$card_num' WHERE id='$uid'";  
      58.                     $result=$pdo->exec($updatesq);  
      59.                     if($result==1){  
      60.                           
      61.                         echo "success";  
      62.                     }  
      63.                 }else{  
      64.                     echo "fail";  
      65.                 }  
      66.   
      67.         }else{  
      68.   
      69.           echo "fail";  
      70.         }  
      71. 这就完成了。还有一个问题就是  扫码之后,页面跳转问题,别着急,一一给你解答。

        注意放到充值的页面


        1. $(document).ready(function () {  
        2.         setInterval("ajaxstatus()", 17000);      
        3.     });  
        4.       
        5.     function ajaxstatus() {  
        6.         var order_id=$("#out_trade_no").val();  
        7.         //alert(order_id);  
        8.         if ($("#out_trade_no").val() != 0) {  
        9.   
        10.             $.ajax({  
        11.             url: "pay/order",  
        12.             type: "GET",  
        13.             dataType:'jsonp',  
        14.             async: false,  
        15.             data: {order_id:order_id},  
        16.             success: function(msg) {  
        17.                 if(msg.status == 1) {  
        18.                     //注册成功调到登录页面  
        19.                   
        20.                     window.location.href = "/userInfo";  
        21.                 } else {  
        22.                     layer.msg('请求订单状态出错!', {icon: 7, time: 2000});  
        23.                     return false;                     
        24.                 }  
        25.   
        26.             }  
        27.             });  
        28.         }  
        29.       
        30.     }  
          1. ajax请求的地址pay/order控制器操作  
            1. //跳转页面  
            2.      public  function paysorder(Request $request){  
            3.          $order_id=$request->input('order_id','');  
            4.          $callback=$_GET['callback'];  
            5.          $model_payorder=new PayOrder();  
            6.          $info=$model_payorder->getorder($order_id);  
            7.          if($info){  
            8.              $response = array(  
            9.                 'state'  => 200,  
            10.                 'message' => '成功',  
            11.                 'order_id' => $info['order_id'],  
            12.                 'status' => $info['status'],  
            13.                   
            14.             );  
            15.             exit$callback.'('.json_encode($response).')');  
            16.          }else{  
            17.              $response = array(  
            18.                 'state'  => 202,  
            19.   
            20.             );  
            21.             exit$callback.'('.json_encode($response).')');  
            22.          }  
            23.            
            24.            
            25.      }  



      72.           




猜你喜欢

转载自blog.csdn.net/tianjingang1/article/details/79035165