147 lines
6.3 KiB
PHP
147 lines
6.3 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
use think\Loader;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 支付宝插件 使用方法请查看同文件夹下的demo
|
|||
|
|
* 目前已经支持电脑网站支付,手机APP支付,支付回调校验,用户提现等功能,如需拓展请联系作者
|
|||
|
|
* @author Jack_YanTC <627495692@qq.com>
|
|||
|
|
*/
|
|||
|
|
class AliPay
|
|||
|
|
{
|
|||
|
|
private $appId;
|
|||
|
|
private $rsaPrivateKey;
|
|||
|
|
private $notifyUrl;
|
|||
|
|
private $alipayPublicKey;
|
|||
|
|
private $appCertPath;
|
|||
|
|
private $alipayCertPath;
|
|||
|
|
private $rootCertPath;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 初始化参数
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
public function __construct()
|
|||
|
|
{
|
|||
|
|
$this->appCertPath = ADDON_PATH . 'epay/certs/appCertPublicKey_2021005162648430.crt';
|
|||
|
|
$this->alipayCertPath = ADDON_PATH . 'epay/certs/alipayCertPublicKey_RSA2.crt';
|
|||
|
|
$this->rootCertPath = ADDON_PATH . 'epay/certs/alipayRootCert.crt';
|
|||
|
|
|
|||
|
|
$configs = get_system_config();
|
|||
|
|
$this->appId = $configs['alipay_app_id'];
|
|||
|
|
$this->rsaPrivateKey = $configs['alipay_private_key'];
|
|||
|
|
$this->alipayPublicKey = $configs['alipay_public_key'];
|
|||
|
|
$this->notifyUrl = $configs['web_site']."/api/Payment/notify_ali";
|
|||
|
|
|
|||
|
|
$aliPayPath = EXTEND_PATH.'/AliPayV2/alipay-sdk/aopV2/';
|
|||
|
|
require_once $aliPayPath . 'AopCertClient.php';
|
|||
|
|
require_once $aliPayPath . 'AopClient.php';
|
|||
|
|
require_once $aliPayPath . 'AopCertification.php';
|
|||
|
|
require_once $aliPayPath . 'AlipayConfig.php';
|
|||
|
|
require_once $aliPayPath . 'request/AlipaySystemOauthTokenRequest.php';
|
|||
|
|
require_once $aliPayPath . 'request/AlipayUserInfoShareRequest.php';
|
|||
|
|
require_once $aliPayPath . 'request/AlipayTradeAppPayRequest.php';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 支付宝手机网站支付
|
|||
|
|
* @param string $orderid 订单号
|
|||
|
|
* @param string $money 金额
|
|||
|
|
* @param string $subject 标题
|
|||
|
|
* @return string
|
|||
|
|
*/
|
|||
|
|
public function aliAppPays($orderid, $money, $subject)
|
|||
|
|
{
|
|||
|
|
$aop = new AopCertClient ();
|
|||
|
|
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
|
|||
|
|
$aop->appId = $this->appId;
|
|||
|
|
$aop->rsaPrivateKey = $this->rsaPrivateKey;
|
|||
|
|
$aop->alipayrsaPublicKey = $aop->getPublicKey($this->alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
|
|||
|
|
$aop->apiVersion = '1.0';
|
|||
|
|
$aop->signType = 'RSA2';
|
|||
|
|
$aop->postCharset = 'utf-8';
|
|||
|
|
$aop->format = 'json';
|
|||
|
|
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
|
|||
|
|
$aop->appCertSN = $aop->getCertSN($this->appCertPath);//调用getCertSN获取证书序列号
|
|||
|
|
$aop->alipayRootCertSN = $aop->getRootCertSN($this->rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
|
|||
|
|
|
|||
|
|
$request = new AlipayTradeAppPayRequest ();
|
|||
|
|
|
|||
|
|
$bizcontent = json_encode([
|
|||
|
|
'subject' => $subject,
|
|||
|
|
'out_trade_no' => $orderid,//此订单号为商户唯一订单号
|
|||
|
|
'total_amount' => $money//保留两位小数
|
|||
|
|
], JSON_UNESCAPED_UNICODE);
|
|||
|
|
|
|||
|
|
$request->setNotifyUrl($this->notifyUrl);
|
|||
|
|
$request->setBizContent($bizcontent);
|
|||
|
|
|
|||
|
|
$result = $aop->sdkExecute($request);
|
|||
|
|
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function verify($post)
|
|||
|
|
{
|
|||
|
|
$aop = new AopCertClient ();
|
|||
|
|
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
|
|||
|
|
$aop->appId = $this->appId;
|
|||
|
|
$aop->rsaPrivateKey = $this->rsaPrivateKey;
|
|||
|
|
$aop->alipayrsaPublicKey = $aop->getPublicKey($this->alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
|
|||
|
|
$aop->apiVersion = '1.0';
|
|||
|
|
$aop->signType = 'RSA2';
|
|||
|
|
$aop->postCharset = 'utf-8';
|
|||
|
|
$aop->format = 'json';
|
|||
|
|
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
|
|||
|
|
$aop->appCertSN = $aop->getCertSN($this->appCertPath);//调用getCertSN获取证书序列号
|
|||
|
|
$aop->alipayRootCertSN = $aop->getRootCertSN($this->rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
|
|||
|
|
|
|||
|
|
return $aop->rsaCheckV1($post, NULL, "RSA2");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
//支付宝换取访问令牌
|
|||
|
|
public function login($auth_code)
|
|||
|
|
{
|
|||
|
|
$aop = new AopCertClient ();
|
|||
|
|
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
|
|||
|
|
$aop->appId = $this->appId;
|
|||
|
|
$aop->rsaPrivateKey = $this->rsaPrivateKey;
|
|||
|
|
$aop->alipayrsaPublicKey = $aop->getPublicKey($this->alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
|
|||
|
|
$aop->apiVersion = '1.0';
|
|||
|
|
$aop->signType = 'RSA2';
|
|||
|
|
$aop->postCharset = 'utf-8';
|
|||
|
|
$aop->format = 'json';
|
|||
|
|
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
|
|||
|
|
$aop->appCertSN = $aop->getCertSN($this->appCertPath);//调用getCertSN获取证书序列号
|
|||
|
|
$aop->alipayRootCertSN = $aop->getRootCertSN($this->rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
|
|||
|
|
|
|||
|
|
|
|||
|
|
// 创建请求对象
|
|||
|
|
$request = new AlipaySystemOauthTokenRequest();//AlipaySystemOauthTokenRequest
|
|||
|
|
$request->setGrantType("authorization_code");
|
|||
|
|
$request->setCode( $auth_code);
|
|||
|
|
|
|||
|
|
// 执行请求,获取access_token
|
|||
|
|
$result = $aop->execute($request);
|
|||
|
|
|
|||
|
|
if (isset( $result->alipay_system_oauth_token_response->access_token)) {
|
|||
|
|
// 获取用户信息
|
|||
|
|
$userinfo_request = new AlipayUserInfoShareRequest();
|
|||
|
|
$userinfo_result = $aop->execute( $userinfo_request, $result->alipay_system_oauth_token_response->access_token);
|
|||
|
|
if ($userinfo_result->alipay_user_info_share_response->code == '10000') {
|
|||
|
|
// 处理用户信息,例如保存到数据库
|
|||
|
|
$user_info = $userinfo_result->alipay_user_info_share_response;
|
|||
|
|
return ['code'=>'1','msg'=>'success','data'=>$user_info];
|
|||
|
|
} else {
|
|||
|
|
// 处理错误
|
|||
|
|
return ['code'=>$result->error_response->code,'msg'=>$result->error_response->msg.'/'.$result->error_response->sub_code,'data'=>$result->error_response->sub_msg];
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
// 处理错误
|
|||
|
|
return ['code'=>$result->error_response->code,'msg'=>$result->error_response->msg.'/'.$result->error_response->sub_code,'data'=>$result->error_response->sub_msg];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|