支付宝获取用户信息

我们项目通过扫码支付,HTML5与后台交互,先获取到用户的信息,在通过信息进行一些支付操作

首先:获取用户信息 

$appid=config('alipay_id');
            $redirect_uri = urlencode ('你的回调地址');
            $url ="https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id={$appid}&scope=auth_base&redirect_uri={$redirect_uri}";
            header("Location:".$url);//此处进行地址跳转

//app_id  支付宝分给商家的标示 
//scope   授权方式 auth_base 静默授权(不需要用户点击直接获取权限)auth_userinfo(需要用户手动授权) 
//redirect_uri  授权回调地址 

支付宝获取商家信息文档地址:https://docs.open.alipay.com/53/104114

支付宝回调地址在开发者配置里边设置 有一个授权回调地址 改为自己可以访问的服务器可以访问路径

/**
     * 授权回调地址 支付宝
     * @return [type] [description]
     */
public function backAlipayUrl()
    {
        //支付宝授权 获取用户信息
        $code = $_REQUEST['auth_code'];
        //APPID
        $appid = config('alipay_id');
        //私钥  文件名(rsa_private_key.pem)
        $rsaPrivateKey = config('private_key');
         
        //公钥  文件名 (rsa_public_key.pem)
        $alipayrsaPublicKey = config('public_key');
        //config[public_key]
         
        //初始化
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $appid;
        $aop->rsaPrivateKey = $rsaPrivateKey;
        $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        //获取access_token
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);//这里传入 code
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        // $access_token = $result->$responseNode->access_token;
        
        $result = json_decode(json_encode($result),true);
        $user_id = $result[$responseNode]['user_id'];//用户信息 userID
        
        Cache::set('alipayUserid',$user_id);
        return $this->redirect('Payback/pay');
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饭饭咿呀呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值