tp获取支付宝用户user_id唯一

app获取支付宝user_id 唯一

支付宝流程

tp获取支付宝用户user_id唯一

咱们开始

第一步 下载demo

请自行注册开发者账号。

  1. 进入支付宝开放平台,文档中心》APP支付》下载SDK&Demo,放到tp框架的Vendor中
    在这里插入图片描述

4.配置文件
在tp框架中配置接入支付宝需要的配置的参数

 //支付宝 支付配置
    'ALI_CONFIG'  => array(
        'gatewayUrl'            => 'https://openapi.alipay.com/gateway.do',//支付宝网关(固定)'
        'appId'                 => 'appIDappIDappIDappIDappIDappIDappID',//APPID即创建应用后生成
        //由开发者自己生成: 请填写开发者私钥去头去尾去回车,一行字符串
        'rsaPrivateKey'         =>  'AfewqFEWfefedwfewfewfewfewfewfewfwefewfewfeefwef',
        //支付宝公钥,由支付宝生成: 请填写支付宝公钥,一行字符串
        'alipayrsaPublicKey'    =>  'ewqdwqfwqdfwqfwqfwqfqwfwqfwqfwqfwqfwqferagrGERW',
        )
  1. 控制器代码

$id 需要修改的用户的, code是用户需要接受到前端的授权码

public function getUserId($id,$code)
    {
        require_once root_path() .'/vendor/Alipay/aop/AopClient.php';
        require_once root_path() .'/vendor/Alipay/aop/request/AlipaySystemOauthTokenRequest.php';
        $aliConfig = Config('app.ALI_CONFIG'); //加载配置项
        $aop = new \AopClient ();
        $aop->gatewayUrl = $aliConfig['gatewayUrl'];
        $aop->appId = $aliConfig['appId'];
        $aop->rsaPrivateKey = $aliConfig['rsaPrivateKey'];
        $aop->alipayrsaPublicKey = $aliConfig['alipayrsaPublicKey'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $request = new \AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $result = $aop->execute ($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		//这样判断和官方文档不一致是因为这里成功不反回code,返回user_id,所以我们使用user_id来判断
        if(isset($result->$responseNode->user_id)){
            $user_id = $result->$responseNode->user_id;
            //保存支付宝用户user_id
            $arr = [
                'user_id' => $user_id
            ];
            $res = $this->User->where('id',$id)->update($arr);
            if (!$res){
                return error_msg();
            }else{
                $this->SetRedis($id,'user_id',$user_id);
                return success_msg();
            }
        } else {
            return error_msg();
        }
    }
  1. 码字不易,求点赞
  2. 如果任何侵权行为请联系管理员删除
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值