app获取支付宝user_id 唯一
支付宝流程
tp获取支付宝用户user_id唯一
咱们开始
第一步 下载demo
请自行注册开发者账号。
- 进入支付宝开放平台,文档中心》APP支付》下载SDK&Demo,放到tp框架的Vendor中
4.配置文件
在tp框架中配置接入支付宝需要的配置的参数
//支付宝 支付配置
'ALI_CONFIG' => array(
'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',//支付宝网关(固定)'
'appId' => 'appIDappIDappIDappIDappIDappIDappID',//APPID即创建应用后生成
//由开发者自己生成: 请填写开发者私钥去头去尾去回车,一行字符串
'rsaPrivateKey' => 'AfewqFEWfefedwfewfewfewfewfewfewfwefewfewfeefwef',
//支付宝公钥,由支付宝生成: 请填写支付宝公钥,一行字符串
'alipayrsaPublicKey' => 'ewqdwqfwqdfwqfwqfwqfqwfwqfwqfwqfwqfwqferagrGERW',
)
- 控制器代码
$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();
}
}
- 码字不易,求点赞
- 如果任何侵权行为请联系管理员删除