赞
踩
要在您的应用中使用支付宝开放产品的接口能力:
需要详细了解开放平台创建应用步骤请参考《开放平台应用创建指南》。
开发者调用接口前需要先生成RSA密钥,RSA密钥包含应用私钥(APP_PRIVATE_KEY)、应用公钥(APP_PUBLIC_KEY)。生成密钥后在开放平台开发者中心进行密钥配置,配置完成后可以获取支付宝公钥(ALIPAY_PUBLIC_KEY)。详情请参考《配置应用环境》。
为了帮助开发者调用开放接口,我们提供了开放平台服务端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.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);//清除定时器
- }
- }
-
- }
- });
- }
2.3 php 后台处理代码
直接看php代码
- <?php
- namespace app\service\controller;
- use think\Loader;
- use think\Session;
- use alipay\jssdk;
- use think\Db;
- use alipayapi\aop\AopClient;
- use alipayapi\aop\request\AlipaySystemOauthTokenRequest;
- use alipayapi\aop\request\AlipayUserInfoShareRequest;
- use alipayapi\aop\request\AlipayUserUserinfoShareRequest;
-
-
- /**
- * Class Alipay
- * @package app\service\controller
- * @note 该控制器是做支付宝第三方登录的
- */
- class Alipay extends Base{
-
- private $appid;
- private $rsaPrivateKey; //应用密钥
- private $alipayrsaPublicKey; //支付宝公钥
- private $grantRedirect; //授权后回调地址
- private $aop; //操作第三方登录的类对象
-
-
- public function __construct()
- {
- require("./extend/alipayapi/AopSdk.php");
- $this->appid='2018111362173186';
- $this->alipayrsaPublicKey='您的公钥';
- $this->rsaPrivateKey='您的私钥';
-
- //该回调地址必须和支付宝开发者平台对应的应用设置的回调地址一致
- $this->grantRedirect='https://www.baidu.com/Alipay/getAlipayUseInfo';
-
- /**
- * 该类是支付宝官方sdk方法
- */
- $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);//返回二维码地址
- }
-
- /**
- * @note 扫二维码跳转到的方法
- */
- 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拉起授权页面
- }
-
- /**
- *@note 根据传过来的授权码换取,授权的accessToken
- */
- 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);
- }
- }
-
- /**
- * @param $code 根据传过来的授权码换取授权的用户信息
- * @return array 返回用户的信息
- * @throws \Exception
- */
- 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
- );
- }
-
-
- /**
- *
- *@note 授权后成功后回调的地址方法
- */
- 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 '<h1 style="margin-top: 5rem; margin-bottom: 5rem; text-align: center;">绑定支付宝成功</h1>';
- }else{
- $msg['code'] = 405;
- $msg['msg'] = '服务器繁忙,请稍后再绑定';
- exit(json_encode($msg));
- }
-
- }
-
- }
- ?>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。