关于支付宝授权用户信息

最近做的一个项目授权支付宝信息 进行报名支付

以下是流程

1、一个首先引进阿里相关配置信息

 2、因为我这边项目是支持 小程序、H5、支付宝  登录 报名的,我这边只展示支付宝代码哦

  对啦 微信不同应用下   unionid  是一样的,所以可以将小程序/H5下的视为同一用户,好啦 接下来说说支付宝吧

3、

 elseif ($data['type'] == ActivityUser::TYPE_ALI) {
    list($res1, $info1) = $this->getALIToken($data['code']);
    if ($res1) {
        list($tres, $info) = $this->getALIUserInfo($info1);
        if ($tres) {
            $info['am_id'] = $data['am_id'];
        }
    }

4、

 public function getALIToken($code)
    {
        //用到其他配置时候自加命名空间
        $this->ali = new AopClient ();
        $this->ali->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $this->ali->appId = env('YUDAO_APP_GZH_ALIAPPID');
        $this->ali->rsaPrivateKey = env('YUDAO_APP_GZH_ALIPRI');
        $this->ali->alipayrsaPublicKey = env('YUDAO_APP_GZH_ALIPUB');
        $this->ali->apiVersion = '1.0';
        $this->ali->signType = 'RSA2';
        $this->ali->postCharset = 'UTF-8';
        $this->ali->format = 'json';

        $request = new AlipaySystemOauthTokenRequest();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $result = $this->ali->execute($request);

        if (empty($result->alipay_system_oauth_token_response) || empty($result->alipay_system_oauth_token_response->alipay_user_id)) {
            Log::info('ali_gettoken_error', [$result]);
            return [false, ''];
        } else {
            return [true, $result->alipay_system_oauth_token_response->access_token];
        }
    }


    public function getALIUserInfo($accessToken)
    {
        $this->ali = new AopClient ();
        $this->ali->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $this->ali->appId = env('YUDAO_APP_GZH_ALIAPPID');
        $this->ali->rsaPrivateKey = env('YUDAO_APP_GZH_ALIPRI');  //私钥 
        $this->ali->alipayrsaPublicKey = env('YUDAO_APP_GZH_ALIPUB');   //支付宝公钥
        $this->ali->apiVersion = '1.0';
        $this->ali->signType = 'RSA2';
        $this->ali->postCharset = 'UTF-8';
        $this->ali->format = 'json';
        $request = new AlipayUserInfoShareRequest();
        $result = $this->ali->execute($request, $accessToken);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        if (empty($result->$responseNode->code) || $result->$responseNode->code != 10000) {
            Log::info('ali_getinfo_error', [$result]);
            return [false, ''];
        }
        $response = $result->$responseNode;
//        ["[object] (stdClass: {\"code\":\"10000\",\"msg\":\"Success\",\"avatar\":\"https://tfs.alipayobjects.com/images/partner/XXXXXXXX\",\
//        "city\":\"上海市\",\"gender\":\"m\",\"nick_name\":\"豪\",\"province\":\"上海\",\"user_id\":\"208870243-------\"})"]

        $data['openid'] = $response->user_id;
        $data['headimgurl'] = $response->avatar ?? '';
        $data['nickname'] = $response->nick_name ?? '';
        $data['sex'] = isset($response->gender) ? ($response->gender == 'm' ? 1 : ($response->gender == 'f' ? 2 : 0)) : 0;
        $data['type'] = ActivityUser::TYPE_ALI;
        $data['country'] = '';
        $data['province'] = $response->province ?? '';
        $data['city'] = $response->city ?? '';

        return [true, $data];
    }

对啦获取用户信息,一定要在支付宝应用平台签约 获取会员信息功能,还有这个新申请的支付宝应用一定是  审核通过了  已经上线的,否则会报没有权限之类的错误。

通过以上方法就可以获取到 支付宝的信息,接下来就进行相关业务处理入库就好

猜你喜欢

转载自blog.csdn.net/qq_42082023/article/details/127921850