一:创建应用
要在您的应用中使用支付宝开放产品的接口能力:
- 您需要先去蚂蚁金服开放平台(open.alipay.com),在开发者中心创建登记您的应用,此时您将获得应用唯一标识(APPID);
- 请在【功能信息】中点击【添加功能】,选择【获取会员信息】;
- 提交审核,等待审核通过,该应用正式可以使用。
需要详细了解开放平台创建应用步骤请参考《开放平台应用创建指南》。
二:配置密钥
开发者调用接口前需要先生成RSA密钥,RSA密钥包含应用私钥(APP_PRIVATE_KEY)、应用公钥(APP_PUBLIC_KEY)。生成密钥后在开放平台开发者中心进行密钥配置,配置完成后可以获取支付宝公钥(ALIPAY_PUBLIC_KEY)。详情请参考《配置应用环境》。
三:搭建和配置开发环境
1. 下载服务端SDK
为了帮助开发者调用开放接口,我们提供了开放平台服务端SDK,包含JAVA、PHP和.NET三个语言版本,封装了签名&验签、HTTP接口请求等基础功能。请先下载对应语言版本的SDK并引入您的开发工程。
各语言版本服务端SDK详细使用说明,请参考《服务端SDK说明》,本文需要下载的就是PHP的SDK包了。
2. 接口调用配置
先看前端代码:
1 先从后台获取二维码,并且展示出来
function show(){ var parmss = { phone:userName }; $.ajax({ url: model.base_url + "/Alipay/getQRcode", data: parms, type: "post", dataType: "text", success: function(res, status, xhr) { if(res != '' || res != null){ //将二维码显示出来 var url = res.body.url; popupUrl.find(".pm-left").find("img").attr("src",url); } }, error: function(data) { } }); $("#popup-bind").show(); int = setInterval(model.bindQuery,"1000");//通过定时器,判断是否绑定过第三方支付}
2 定时器,判断是否绑定成功
//根据请求后台看是否数据已经绑定function bindQuery(){ var parmss = { phone:userName }; $.ajax({ url: model.base_url+"Authorland/getBindsucess", data: parmss, type: "post", dataType: "text", success: function(res, status, xhr) { if(res != '' || res != null){ if(bind_num == 0){ var zfb_id = res.body.zfb_id; bind_num = 1; }else{ var wx_ids = res.body.wx_id; } 如果支付宝已经绑定,就把已经绑定的图片换上去,并且把二维码关闭掉 if(zfb_id != undefined || zfb_ids != undefined){ $("#bind-zfb").unbind(); $("#bind-zfb").addClass("on"); $("#bind-zfb").find("p").text("已绑定支付宝"); $("#bind-zfb").find("img").attr("src","/public/Content/Images/wallet/zfb_on.png"); } if(wx_id != wx_ids || zfb_id != zfb_ids){ clearInterval(int);//清除定时器 } } } });}
3 php 后台处理代码
直接看php代码
appid='2018111362173186'; $this->alipayrsaPublicKey='您的公钥'; $this->rsaPrivateKey='您的私钥'; //该回调地址必须和支付宝开发者平台对应的应用设置的回调地址一致 $this->grantRedirect='https://www.baidu.com/Alipay/getAlipayUseInfo'; $this->aop = new AopClient(); $aop=$this->aop; $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $this->appid; $aop->rsaPrivateKey = $this->rsaPrivateKey; $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset='UTF-8'; $aop->format='json'; parent::__construct(); } public function alipayCreateQRcode(){ $phone = input("param.phone");//接收电话号码 $codeObj = new \qrcode\QRcode(); $url='https://www.baidu.com/Alipay/goQRcodefunc?mobile='.$phone;//url拼接电话号码参数,准备只做二维码 $dir = './public/qrcode/'; $size = 10; if(!is_dir($dir)){ @mkdir($dir,0777); } $createDirImg = $dir.$moblie.'_alipay.jpg';//生成的二维码图片的地址 $codeObj::png($url,$createDirImg,'L',$size,2);//生成二维码 $msg['code'] = 10000; $msg['url'] = 'https://www.baidu.com'.substr($createDirImg,1,strlen($createDirImg));; $msg['mobile'] = $moblie; echo json_encode($msg);//返回二维码地址 } public function goQRcodefunc(){ $mobile=input("param.mobile");//接收二维码传过来的电话 $this->grantRedirect=urlencode($this->grantRedirect."?mobile=$mobile");//拼接授权回调的地址,吧mobile传过去 $url ="https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id={$this->appid}&scope=auth_user&redirect_uri={$this->grantRedirect}"; header("Location:".$url);//跳转到下面的url拉起授权页面 } public function getAccess_token($code){ $request = new AlipaySystemOauthTokenRequest(); $request->setGrantType("authorization_code"); $request->setCode($code);//这里传入 code $result = $this->aop->execute($request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; if(isset($result->$responseNode->access_token)&& !empty($result->$responseNode->access_token)){ return $result->$responseNode->access_token; }else{ $files = 'ali/'.date("Ymd").".txt"; $log = 'time:'.date('Y-m-d H:i:s').'---errsssssss code:'.$code.'---response:'.json_encode($result)."\n\n"; filePutContents($files,$log); } } public function getAlipayUserdata($code){ $access_token=$this->getAccess_token($code); $request_a = new AlipayUserInfoShareRequest(); $result_a = $this->aop->execute ($request_a,$access_token); //这里传入获取的access_token $user_id = $result_a->alipay_user_info_share_response->user_id; //用户唯一id $city = $result_a->alipay_user_info_share_response->city; //用户城市 $province = $result_a->alipay_user_info_share_response->province; //用户省份 $avatar = $result_a->alipay_user_info_share_response->avatar; //用户头像 $is_student_certified = $result_a->alipay_user_info_share_response->is_student_certified; $gender = $result_a->alipay_user_info_share_response->gender; //用户性别 $user_type = $result_a->alipay_user_info_share_response->user_type; $user_status = $result_a->alipay_user_info_share_response->user_status; $is_certified = $result_a->alipay_user_info_share_response->is_certified; return array( 'user_id'=>$user_id, 'gender'=>$gender, 'city'=>$city, 'avatar'=>$avatar, 'nickname'=>'', 'province'=>$province, 'is_student_certified'=>$is_student_certified, 'user_type'=>$user_type, 'user_status'=>$user_status, 'is_certified'=>$is_certified ); } public function getAlipayUseInfo(){ $input = input('param.');//获取传过来的参数 $code = $input['auth_code']; $param=$this->getAlipayUserdata($code); //第三方信息if(isset($param['user_id'])){ //此处是将数据存储到数据库的逻辑 //删除二维码图片 $createDelImg = './public/qrcode/'.$moblie.'_alipay.jpg'; @unlink($createDelImg); echo '绑定支付宝成功
'; }else{ $msg['code'] = 405; $msg['msg'] = '服务器繁忙,请稍后再绑定'; exit(json_encode($msg)); } }}?>
来源地址:https://blog.csdn.net/weixin_46742102/article/details/130650138