支付宝 app应用 授权 php + APICloud

app应用授权 怎么处理 

支付宝官方文档 https://docs.open.alipay.com/218/105325

APICloud 文档 https://docs.apicloud.com/Client-API/Open-SDK/aliPayTradePlus#a5

根据文档整理了 php 的代码 仅供参考 

获取 授权数据信息  

极简版授权请求参数和返回

apiname=com.alipay.account.auth&app_id=2016051801417322&app_name=mc&auth_type=AUTHACCOUNT&biz_type=openservice&method=alipay.open.auth.sdk.code.get&pid=2088221932028920&product_id=APP_FAST_LOGIN&scope=auth_user&sign_type=RSA&target_id=61ef37122e104d148c855d14e9bf90e2&sign=m6K7Dz4CxPAgLn2uwIjGSmgRcOBYtHcqaYqLc85/C6PCqoIu6tUHDmx5/hb0xy+dMCdQoFcQWKRGzBl040g/6avD/PhOUSUi9Cmtd2HxSzEEjk7LuFn9QrpAmcM7/tub+K/G/2rQp9ce8FY2RCbJ/sFDA09M5B+2gqzy9Qkc5fE=

     /**
     *获取授权数据
     */
    public function get_auth_data(){
        $appid =  '1111'; // 商户账号appid
        $sign_type = 'RSA2';
        $targetId = mt_rand(000000,999999);//此处随机获取
        $data = [
            'apiname'=>'com.alipay.account.auth',
            'method'=>'alipay.open.auth.sdk.code.get',
            'app_id'=>$appid,//支付宝分配给开发者的应用ID
            'app_name'=>'mc',
            'biz_type'=>'openservice',
            'pid'=>'11111',//签约的支付宝账号对应的支付宝唯一用户号,以2088开头的16位纯数字组成 登录你的支付宝 用户信息中 可以找到
            'product_id'=>'APP_FAST_LOGIN', //标识授权类型,取值范围:AUTHACCOUNT代表授权;LOGIN代表登录
            'scope'=>'kuaijie',
            'target_id'=>$targetId, //商户标识该次用户授权请求的ID,该值在商户端应保持唯一
            'auth_type'=>'AUTHACCOUNT',//授权类型  AUTHACCOUNT:授权 LOGIN:登录
            'sign_type'=>$sign_type,//签名算法类型
        ];
        //读取配置信息
        $aop = new \AopClient;
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = '1111111';//支付宝分配给开发者的应用ID
        $aop->rsaPrivateKey = '1111111';//支付宝私钥
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = '1111111';//支付宝公钥
        $sign =  $aop->generateSign($data,$sign_type);//生成sing 此处用的 支付宝 demo封装好的
        $sign = urlencode($sign);
        ksort($data);
        $str='';
        foreach($data as $k=>$v) {
            $str.=$k.'='.$v.'&';
        }
        $str.= 'sign='.$sign;
       /* var_dump($str);
        exit;*/
        $str.= 'sign='.$sign;
       /* var_dump($str);
        exit;*/
        return $str;//返给前端 去做授权处理哦!
    }

如果前端 不用上面那种 提供第二种方案

    /**
     *获取授权数据
     *
     */
    public  function get_data(){
        $targetId = mt_rand(000000,999999);
        $data = [
            'appId'=>'1111', // 商户账号appid
            'targetId'=>$targetId , //商户标识
            'partner'=>'1111',//支付宝唯一用户号
            'rsaPriKey'=>'1111',//商户私钥
            'authType'=>'AUTHACCOUNT',//AUTHACCOUNT:授权 LOGIN:登录
            'rsa2'=>true, 签名方式
        ];
        return  $data;
    }

前端 授权成功 会获取到 这样一个回调 数据

返回结果样例

     resultStatus=9000
     memo="处理成功"
     result="success=true&auth_code=d9d1b5acc26e461dbfcb6974c8ff5E64&result_code=200 &user_id=2088003646494707"

 然后就 是 调用 
 alipay.system.oauth.token 

获取授权令牌接口  使用文档  https://docs.open.alipay.com/api_9/alipay.system.oauth.token

         function  auth_user($uid,$authorization_code=''){

            
            $config = $config->toArray()[0];
            $aop = new  \AopClient ();
            $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
            $aop->appId = '111111';//支付宝分配给开发者的应用ID
            $aop->rsaPrivateKey = '11111'; //支付宝私钥
            $aop->alipayrsaPublicKey= '11111'; //支付宝公钥
            $aop->apiVersion = '1.0';
            $aop->signType = 'RSA2';
            $aop->postCharset='UTF-8';
            $aop->format='json';
            $request = new \AlipaySystemOauthTokenRequest();
            $request->setGrantType("authorization_code");// 值为authorization_code时,代表用code换取;值为refresh_token时,代表用refresh_token换取 
            $request->setCode($authorization_code);//前端返回数据 包含的 授权码,用户对应用授权后得到。
//          $request->setRefreshToken("12121212121");//刷刷新令牌,上次换取访问令牌时得到。见出参的refresh_token字段  与code验证 二选一
            $result = $aop->execute($request);
           $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
            if(!empty($resultCode)&&$resultCode == 10000){
                $alipay_user_id = $result['user_id'];//获取用户唯一支付宝标识
                return self::memeber_update($uid,$alipay_user_id);//保存到你的数据里 
            } else {
                return  false;
            }
        }

 然后其他的就是你自由发挥了 

追加一下 研究了一下网页版的 只不h5 授权页只能在支付宝客户端里使用,否则会报错

官方文档 https://docs.alipay.com/fw/api/105942

同样 可以获取  auth_code   

/*
     *获取授权数据
     *
     */
    public  function get_auth_url(){
        $appid = '11111' // 开发者应用的app_id
        $redirect_uri = 'http://www.baidu.com/api/authnotify';//回调页面,是 经过转义 的url链接(url必须以http或者https开头),比如:http%3A%2F%2Fexample.com
在请求之前,开发者需要先到开发者中心对应应用内,配置授权回调地址。 
        $redirect_uri = UrlEncode($redirect_uri);
        $application_type = 'MOBILEAPP';//
        $url ="https://openauth.alipay.com/oauth2/appToAppBatchAuth.htm?app_id={$appid}&application_type={$application_type}&redirect_uri={$redirect_uri}";
        return  $url;
    }

在回调地址页面 获取下面的 结果

{
    "alipay_system_oauth_token_response": {
        "access_token": "111",
        "user_id": "111",
        "alipay_user_id": "1111",
        "expires_in": 300,
        "re_expires_in": 300,
        "refresh_token": "11"
    },
    "sign": "1111"
}
发布了52 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40816144/article/details/103911771
今日推荐