將alipay檔案夾放在extend目錄下
打開支付寶開發者中心
創建一個應用,點擊密鑰管理,設定介面加簽方式:打開支付寶RSA2安裝,安裝完成后直接生成密鑰,將生成密鑰所對應的公鑰復制到內容配置里,然后保存,支付寶會自動回傳一個支付寶公鑰
alipay檔案地址
使用:
use alipay\aop\AopClient;
use alipay\aop\request\AlipaySystemOauthTokenRequest;
/**
* 支付寶獲取user_id
*/
public function Alipays(){
$auth_code = input('auth_code');
//appId
$appId = '2021001108623781';
if (!$auth_code){
//回呼地址(經過轉義 的url鏈接,url必須以http或者https開頭),這里的回呼地址就是測驗號中的授權回呼地址
$redirect_uri = 'http://qr.cnzhuoyue.cn/index/fubapay/Alipays';
//url轉義
$redirect_uri = urlencode($redirect_uri);
//授權方式(目前只支持auth_userinfo和auth_base兩個值)
$scope = 'auth_base';
//授權地址
$authUrl = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=". $appId ."&scope=". $scope ."&redirect_uri=". $redirect_uri;
return $this->redirect($authUrl);
}else{
//獲取授權回傳的auth_code
$aop = new AopClient();
//網關,正式環境需要將alipaydev改成alipay
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
//應用私鑰
$aop->rsaPrivateKey = '私鑰 由軟體生成';
//支付寶公鑰
$aop->alipayrsaPublicKey = '公鑰';
//簽名演算法型別
$aop->signType = 'RSA2';
$request = new AlipaySystemOauthTokenRequest();
$request->setGrantType("authorization_code");
$request->setCode($auth_code);
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
//獲取access_token
$openId = $result->$responseNode->user_id;
$this->redirect('http://qr.cnzhuoyue.cn/index/fubapay/alipay?openId='.$openId);
}
}
注意:具體配置請到支付寶開發中進行配置,這些都是實際專案所使用,不是沙盒環境
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/232313.html
標籤:其他
上一篇:tp5.1生成二維碼
下一篇:tp5.1+微信點金計劃開發
