主頁 > 知識庫 > 詳解PHP實現支付寶小程序用戶授權的工具類

詳解PHP實現支付寶小程序用戶授權的工具類

熱門標簽:Mysql連接數設置 銀行業(yè)務 服務器配置 阿里云 Linux服務器 電子圍欄 科大訊飛語音識別系統(tǒng) 團購網站

背景

最近項目需要上線支付寶小程序,同時需要走用戶的授權流程完成用戶信息的存儲,以前做過微信小程序的開發(fā),本以為實現授權的過程是很簡單的事情,但是再實現的過程中還是遇到了不少的坑,因此記錄一下實現的過程

學到的知識

  • 支付寶開放接口的調用模式以及實現方式
  • 支付寶小程序授權的流程
  • RSA加密方式

吐槽點

支付寶小程序的入口隱藏的很深,沒有微信小程序那么直接了當
支付寶小程序的開發(fā)者工具比較難用,編譯時候比較卡,性能有很大的問題
每提交一次代碼,支付寶小程序的體驗碼都要進行更換,比較繁瑣,而且localStorage的東西不知道要如何刪除

事先準備

  • 到支付寶開放平臺注冊一個開發(fā)者賬號,并做好相應的認證等工作
  • 創(chuàng)建一個小程序,并記錄好相關的小程序信息,包括支付寶公鑰,私鑰,app公鑰等,可以借鑒支付寶官方提供的相應的公鑰生成工具來生成公鑰和私鑰,工具的下載地址:傳送門
  • 了解下支付寶小程序的簽名機制,詳細見https://docs.open.alipay.com/291/105974
  • 熟悉下支付寶小程序獲取用戶信息的過程,詳細見支付寶小程序用戶授權指引

授權的步驟

授權時序圖

實現流程

  1. 客戶端通過my.getAuthCode接口獲取code,傳給服務端
  2. 服務端通過code,調用獲取token接口獲取access_token,alipay.system.oauth.token(換取授權訪問令牌)
  3. 通過token接口調用支付寶會員查詢接口獲取會員信息,alipay.user.info.share(支付寶會員授權信息查詢接口)
  4. 將獲取的用戶信息保存到數據庫

AmpHelper工具類

?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2018/8/16
 * Time: 17:45
 */

namespace App\Http\Helper;

use App\Http\Helper\Sys\BusinessHelper;
use Illuminate\Support\Facades\Log;

class AmpHelper
{

  const API_DOMAIN = "https://openapi.alipay.com/gateway.do?";
  const API_METHOD_GENERATE_QR = 'alipay.open.app.qrcode.create';
  const API_METHOD_AUTH_TOKEN = 'alipay.system.oauth.token';
  const API_METHOD_GET_USER_INFO = 'alipay.user.info.share';

  const SIGN_TYPE_RSA2 = 'RSA2';
  const VERSION = '1.0';
  const FILE_CHARSET_UTF8 = "UTF-8";
  const FILE_CHARSET_GBK = "GBK";
  const RESPONSE_OUTER_NODE_QR = 'alipay_open_app_qrcode_create_response';
  const RESPONSE_OUTER_NODE_AUTH_TOKEN = 'alipay_system_oauth_token_response';
  const RESPONSE_OUTER_NODE_USER_INFO = 'alipay_user_info_share_response';
  const RESPONSE_OUTER_NODE_ERROR_RESPONSE = 'error_response';

  const STATUS_CODE_SUCCESS = 10000;
  const STATUS_CODE_EXCEPT = 20000;


  /**
   * 獲取用戶信息接口,根據token
   * @param $code 授權碼
   * 通過授權碼獲取用戶的信息
   */
  public static function getAmpUserInfoByAuthCode($code){
    $aliUserInfo = [];
    $tokenData = AmpHelper::getAmpToken($code);
    //如果token不存在,這種主要是為了處理支付寶的異常記錄
    if(isset($tokenData['code'])){
      return $tokenData;
    }
    $token = formatArrValue($tokenData,'access_token');
    if($token){
      $userBusiParam = self::getAmpUserBaseParam($token);
      $url = self::buildRequestUrl($userBusiParam);
      $resonse = self::getResponse($url,self::RESPONSE_OUTER_NODE_USER_INFO);
      if($resonse['code'] == self::STATUS_CODE_SUCCESS){
        //有效的字段列
        $userInfoColumn = ['user_id','avatar','province','city','nick_name','is_student_certified','user_type','user_status','is_certified','gender'];
        foreach ($userInfoColumn as $column){
          $aliUserInfo[$column] = formatArrValue($resonse,$column,'');
        }

      }else{
        $exceptColumns = ['code','msg','sub_code','sub_msg'];
        foreach ($exceptColumns as $column){
          $aliUserInfo[$column] = formatArrValue($resonse,$column,'');
        }
      }
    }
    return $aliUserInfo;
  }


  /**
   * 獲取小程序token接口
   */
  public static function getAmpToken($code){
    $param = self::getAuthBaseParam($code);
    $url = self::buildRequestUrl($param);
    $response = self::getResponse($url,self::RESPONSE_OUTER_NODE_AUTH_TOKEN);
    $tokenResult = [];
    if(isset($response['code'])  $response['code'] != self::STATUS_CODE_SUCCESS){
      $exceptColumns = ['code','msg','sub_code','sub_msg'];
      foreach ($exceptColumns as $column){
        $tokenResult[$column] = formatArrValue($response,$column,'');
      }
    }else{
      $tokenResult = $response;
    }
    return $tokenResult;
  }

  /**
   * 獲取二維碼鏈接接口
   * 433ac5ea4c044378826afe1532bcVX78
   * https://openapi.alipay.com/gateway.do?timestamp=2013-01-01 08:08:08method=alipay.open.app.qrcode.createapp_id=2893sign_type=RSA2sign=ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEEversion=1.0biz_content=
  {"url_param":"/index.html?name=aliloc=hz", "query_param":"name=1age=2", "describe":"二維碼描述"}
  */
  public static function generateQrCode($mpPage = 'pages/index',$queryParam = [],$describe){
    $param = self::getQrcodeBaseParam($mpPage,$queryParam,$describe );
    $url = self::buildRequestUrl($param);
    $response = self::getResponse($url,self::RESPONSE_OUTER_NODE_QR);
    return $response;
  }


  /**
   * 獲取返回的數據,對返回的結果做進一步的封裝和解析,因為支付寶的每個接口的返回都是由一個特定的  
   * key組成的,因此這里直接封裝了而一個通用的方法,對于不同的接口只需要更改相應的node節(jié)點就可以了
   */
  public static function getResponse($url,$responseNode){
    $json = curlRequest($url);
    $response = json_decode($json,true);
    $responseContent = formatArrValue($response,$responseNode,[]);
    $errResponse = formatArrValue($response,self::RESPONSE_OUTER_NODE_ERROR_RESPONSE,[]);
    if($errResponse){
      return $errResponse;
    }
    return $responseContent;
  }

  /**
   * 獲取請求的鏈接
   */
  public static function buildQrRequestUrl($mpPage = 'pages/index',$queryParam = []){
    $paramStr = http_build_query(self::getQrBaseParam($mpPage,$queryParam));
    return self::API_DOMAIN . $paramStr;
  }



  /**
   * 構建請求鏈接
   */
  public static function buildRequestUrl($param){
    $paramStr = http_build_query($param);
    return self::API_DOMAIN . $paramStr;
  }


  /**
   * 獲取用戶的基礎信息接口
   */
  public static function getAmpUserBaseParam($token){
    $busiParam = [
      'auth_token' => $token,
    ];
    $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_GET_USER_INFO);
    return $param;

  }

  /**
   *獲取二維碼的基礎參數
   */
  public static function getQrcodeBaseParam($page= 'pages/index/index',$queryParam = [],$describe = ''){
    $busiParam = [
      'biz_content' => self::getQrBizContent($page,$queryParam,$describe)
    ];
    $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_GENERATE_QR);
    return $param;

  }

  /**
   *獲取授權的基礎參數
   */
  public static function getAuthBaseParam($code,$refreshToken = ''){
    $busiParam = [
      'grant_type' => 'authorization_code',
      'code' => $code,
      'refresh_token' => $refreshToken,
    ];
    $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_AUTH_TOKEN);
    return $param;
  }


  /**
   * 構建業(yè)務參數
   */
  public static function buildApiBuisinessParam($businessParam,$apiMethod){
    $pubParam = self::getApiPubParam($apiMethod);
    $businessParam = array_merge($pubParam,$businessParam);
    $signContent = self::getSignContent($businessParam);
    error_log('sign_content ===========>'.$signContent);
    $rsaHelper = new RsaHelper();
    $sign = $rsaHelper->createSign($signContent);
    error_log('sign ===========>'.$sign);
    $businessParam['sign'] = $sign;
    return $businessParam;
  }


  /**
   * 公共參數
   *
   */
  public static function getApiPubParam($apiMethod){
    $ampBaseInfo = BusinessHelper::getAmpBaseInfo();
    $param = [
      'timestamp' => date('Y-m-d H:i:s') ,
      'method' => $apiMethod,
      'app_id' => formatArrValue($ampBaseInfo,'appid',config('param.amp.appid')),
      'sign_type' =>self::SIGN_TYPE_RSA2,
      'charset' =>self::FILE_CHARSET_UTF8,
      'version' =>self::VERSION,
    ];
    return $param;
  }


  /**
   * 獲取簽名的內容
   */
  public static function getSignContent($params) {
    ksort($params);
    $stringToBeSigned = "";
    $i = 0;
    foreach ($params as $k => $v) {
      if (!empty($v)  "@" != substr($v, 0, 1)) {
        if ($i == 0) {
          $stringToBeSigned .= "$k" . "=" . "$v";
        } else {
          $stringToBeSigned .= "" . "$k" . "=" . "$v";
        }
        $i++;
      }
    }
    unset ($k, $v);
    return $stringToBeSigned;
  }


  public static function convertArrToQueryParam($param){
    $queryParam = [];
    foreach ($param as $key => $val){
      $obj = $key.'='.$val;
      array_push($queryParam,$obj);
    }
    $queryStr = implode('',$queryParam);
    return $queryStr;
  }

  /**
   * 轉換字符集編碼
   * @param $data
   * @param $targetCharset
   * @return string
   */
  public static function characet($data, $targetCharset) {
    if (!empty($data)) {
      $fileType = self::FILE_CHARSET_UTF8;
      if (strcasecmp($fileType, $targetCharset) != 0) {
        $data = mb_convert_encoding($data, $targetCharset, $fileType);
      }
    }
    return $data;
  }

  /**
   * 獲取業(yè)務參數內容
   */
  public static function getQrBizContent($page, $queryParam = [],$describe = ''){
    if(is_array($queryParam)){
      $queryParam = http_build_query($queryParam);
    }
    $obj = [
      'url_param' => $page,
      'query_param' => $queryParam,
      'describe' => $describe
    ];
    $bizContent = json_encode($obj,JSON_UNESCAPED_UNICODE);
    return $bizContent;
  }

}

AmpHeler工具類關鍵代碼解析相關常量

//支付寶的api接口地址
const API_DOMAIN = "https://openapi.alipay.com/gateway.do?";
//獲取支付寶二維碼的接口方法
const API_METHOD_GENERATE_QR = 'alipay.open.app.qrcode.create';
//獲取token的接口方法
const API_METHOD_AUTH_TOKEN = 'alipay.system.oauth.token';
//獲取用戶信息的接口方法
const API_METHOD_GET_USER_INFO = 'alipay.user.info.share';
//支付寶的簽名方式,由RSA2和RSA兩種
const SIGN_TYPE_RSA2 = 'RSA2';
//版本號,此處固定挑那些就可以了
const VERSION = '1.0';
//UTF8編碼
const FILE_CHARSET_UTF8 = "UTF-8";
//GBK編碼
const FILE_CHARSET_GBK = "GBK";
//二維碼接口調用成功的 返回節(jié)點
const RESPONSE_OUTER_NODE_QR = 'alipay_open_app_qrcode_create_response';
//token接口調用成功的 返回節(jié)點
const RESPONSE_OUTER_NODE_AUTH_TOKEN = 'alipay_system_oauth_token_response';
//用戶信息接口調用成功的 返回節(jié)點
const RESPONSE_OUTER_NODE_USER_INFO = 'alipay_user_info_share_response';
//錯誤的返回的時候的節(jié)點
const RESPONSE_OUTER_NODE_ERROR_RESPONSE = 'error_response';

const STATUS_CODE_SUCCESS = 10000;
const STATUS_CODE_EXCEPT = 20000;

getAmpUserInfoByAuthCode方法

這個方法是獲取用戶信息的接口方法,只需要傳入客戶端傳遞的code,就可以獲取到用戶的完整信息

getAmpToken方法

這個方法是獲取支付寶接口的token的方法,是一個公用方法,后面所有的支付寶的口調用,都可以使用這個方法先獲取token

getResponse方法

考慮到會調用各個支付寶的接口,因此這里封裝這個方法是為了方便截取接口返回成功之后的信息,提高代碼的閱讀性

getApiPubParam方法

這個方法是為了獲取公共的參數,包括版本號,編碼,appid,簽名類型等基礎業(yè)務參數

getSignContent方法

這個方法是獲取簽名的內容,入參是一個數組,最后輸出的是參數的拼接字符串

buildApiBuisinessParam($businessParam,$apiMethod)

這個是構建api獨立的業(yè)務參數部分方法,businessParam參數是支付寶各個接口的業(yè)務參數部分(出去公共參數),$apiMethod是對應的接口的方法名稱,如獲取token的方法名為alipay.system.oauth.token

簽名幫助類

?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2018/12/4
 * Time: 15:37
 */

namespace App\Http\Helper;

/**
 *$rsa2 = new Rsa2();
 *$data = 'mydata'; //待簽名字符串
 *$strSign = $rsa2->createSign($data);   //生成簽名
 *$is_ok = $rsa2->verifySign($data, $strSign); //驗證簽名
 */
class RsaHelper
{

  private static $PRIVATE_KEY;
  private static $PUBLIC_KEY;


  function __construct(){
    self::$PRIVATE_KEY = config('param.amp.private_key');
    self::$PUBLIC_KEY = config('param.amp.public_key');
  }

  /**
   * 獲取私鑰
   * @return bool|resource
   */
  private static function getPrivateKey()
  {
    $privKey = self::$PRIVATE_KEY;
    $privKey = "-----BEGIN RSA PRIVATE KEY-----".PHP_EOL.wordwrap($privKey, 64, PHP_EOL, true).PHP_EOL."-----END RSA PRIVATE KEY-----";
    ($privKey) or die('您使用的私鑰格式錯誤,請檢查RSA私鑰配置');
    error_log('private_key is ===========>: '.$privKey);
    return openssl_pkey_get_private($privKey);
  }
  /**
   * 獲取公鑰
   * @return bool|resource
   */
  private static function getPublicKey()
  {
    $publicKey = self::$PUBLIC_KEY;
    $publicKey = "-----BEGIN RSA PRIVATE KEY-----".PHP_EOL.wordwrap($publicKey, 64, PHP_EOL, true).PHP_EOL."-----END RSA PRIVATE KEY-----";
    error_log('public key is : ===========>'.$publicKey);
    return openssl_pkey_get_public($publicKey);
  }
  /**
   * 創(chuàng)建簽名
   * @param string $data 數據
   * @return null|string
   */
  public function createSign($data = '')
  {
    // var_dump(self::getPrivateKey());die;
    if (!is_string($data)) {
      return null;
    }
    return openssl_sign($data, $sign, self::getPrivateKey(),OPENSSL_ALGO_SHA256 ) ? base64_encode($sign) : null;
  }
  /**
   * 驗證簽名
   * @param string $data 數據
   * @param string $sign 簽名
   * @return bool
   */
  public function verifySign($data = '', $sign = '')
  {
    if (!is_string($sign) || !is_string($sign)) {
      return false;
    }
    return (bool)openssl_verify(
      $data,
      base64_decode($sign),
      self::getPublicKey(),
      OPENSSL_ALGO_SHA256
    );
  }
}

調用

$originUserData = AmpHelper::getAmpUserInfoByAuthCode($code);
echo $originUserData;

注意getAmpUserInfoByAuthCode方法,調用接口成功,會返回支付寶用戶的正確信息,示例如下

{
  "alipay_user_info_share_response": {
    "code": "10000",
    "msg": "Success",
    "user_id": "2088102104794936",
    "avatar": "http://tfsimg.alipay.com/images/partner/T1uIxXXbpXXXXXXXX",
    "province": "安徽省",
    "city": "安慶",
    "nick_name": "支付寶小二",
    "is_student_certified": "T",
    "user_type": "1",
    "user_status": "T",
    "is_certified": "T",
    "gender": "F"
  },
  "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}

踩坑點

  1. 在開發(fā)之前一定要仔細閱讀用戶的授權流程指引文檔,否則很容出錯
  2. 對于用戶信息接口,在獲取授權信息接口并沒有做明確的說明,所以需要先梳理清楚
  3. 支付寶的簽名機制和微信的有很大不同,對于習慣了微信小程序開發(fā)的人來說,剛開始可能有點不適應,所以需要多看看sdk里面的實現

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP:微信小程序 微信支付服務端集成實例詳解及源碼下載
  • 微信小程序 支付功能實現PHP實例詳解
  • 微信小程序與php 實現微信支付的簡單實例
  • php實現小程序支付完整版
  • 微信小程序支付功能 php后臺對接完整代碼分享
  • 微信小程序支付PHP代碼
  • 微信小程序 PHP后端form表單提交實例詳解
  • 微信小程序調用PHP后臺接口 解析純html文本
  • 微信小程序request請求后臺接口php的實例詳解
  • PHP小程序支付功能完整版【基于thinkPHP】

標簽:江蘇 蚌埠 棗莊 廣元 衡水 萍鄉(xiāng) 衢州 大理

巨人網絡通訊聲明:本文標題《詳解PHP實現支付寶小程序用戶授權的工具類》,本文關鍵詞  ;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266