272 lines
9.4 KiB
PHP
272 lines
9.4 KiB
PHP
<?php
|
||
|
||
namespace addon\aliapp\model;
|
||
|
||
use app\model\BaseModel;
|
||
use Alipay\EasySDK\Kernel\Factory;
|
||
use Alipay\EasySDK\Kernel\Config as AliCon;
|
||
|
||
class MinCode extends BaseModel
|
||
{
|
||
public $app;
|
||
|
||
protected $config;
|
||
|
||
public $appAuthToken = '';//'202301BB74ac1b3e3b5f4541b174febbaf07bX84';
|
||
|
||
public function __construct($site_id = '')
|
||
{
|
||
// 获取支付宝支付参数(统一支付到平台账户)
|
||
$config_model = new Config();
|
||
if ($site_id) {
|
||
$config_info = $config_model->getAppConfig($site_id)['data']['value'];
|
||
if (isset($config_info['line_type']) && $config_info['line_type'] == 'auth') {
|
||
$this->appAuthToken = $config_info['app_auth_token'];
|
||
}
|
||
}
|
||
$config_info = config('alipay.platform');
|
||
$this->config = $config_info;
|
||
$options = new AliCon();
|
||
$options->protocol = 'https';
|
||
$options->gatewayHost = 'openapi.alipay.com';
|
||
$options->signType = 'RSA2';
|
||
$options->appId = $config_info['appid'];
|
||
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
||
$options->merchantPrivateKey = $config_info['private_key'] ?? ""; //'<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->';
|
||
$options->alipayCertPath = ''; //'<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
|
||
$options->alipayRootCertPath = ''; //'<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
|
||
$options->merchantCertPath = ''; //'<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';
|
||
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
|
||
$options->alipayPublicKey = $config_info['alipay_public_key'] ?? ""; // '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
|
||
//可设置异步通知接收服务地址(可选)
|
||
$options->notifyUrl = ''; //"<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->";
|
||
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
|
||
$options->encryptKey = $config_info['encryptKey']; // 'du/cSQ7P4YPw4d8+3jUc3w=='; //"<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
|
||
Factory::setOptions($options);
|
||
}
|
||
|
||
|
||
/**
|
||
* 消息解密
|
||
* @param array $param
|
||
*/
|
||
public function decryptData($encryptedData)
|
||
{
|
||
try {
|
||
// $cache = Cache::get('weapp_' . $param['weapp_openid']);
|
||
// $session_key = $cache['session_key'] ?? '';
|
||
// $result = $this->app->encryptor->decryptData($session_key, $param['iv'], $param['encryptedData']);
|
||
// if (isset($result['errcode']) && $result['errcode'] != 0) {
|
||
// return $this->handleError($result['errcode'], $result['errmsg']);
|
||
// }
|
||
//2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
|
||
$result = Factory::util()
|
||
->AES()
|
||
->agent($this->appAuthToken)
|
||
->decrypt($encryptedData);
|
||
return json_decode($result, true);
|
||
} catch (\Exception $e) {
|
||
return $this->error([], $e->getMessage());
|
||
}
|
||
}
|
||
|
||
/***
|
||
* @param $authCode
|
||
* @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
|
||
* @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
|
||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||
* @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
|
||
*/
|
||
public function handleAuthorize($authCode, $grant_type = 'authorization_code')
|
||
{
|
||
try {
|
||
$textParams = [];
|
||
$bizParams = [
|
||
'grant_type' => $grant_type,
|
||
'code' => $authCode,
|
||
];
|
||
//2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
|
||
$result = Factory::util()
|
||
->generic()->execute("alipay.open.auth.token.app", $textParams, $bizParams);
|
||
return json_decode($result->httpBody, true)['alipay_open_auth_token_app_response'];
|
||
} catch (\Exception $e) {
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取授权地址
|
||
* @param $urls //回调地址
|
||
* @param string $type
|
||
* @return string
|
||
*/
|
||
public function getPreAuthorizationUrl($urls, $type = 'WEBAPP,MOBILEAPP,PUBLICAPP,TINYAPP')
|
||
{
|
||
$urls = urlencode($urls);
|
||
$auth_url = "https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id={$this->config['appid']}&redirect_uri={$urls}";
|
||
return $auth_url;
|
||
}
|
||
|
||
|
||
public function getminiAuthorizationUrl($urls, $state)
|
||
{
|
||
$bizDataObj = [
|
||
'platformCode' => 'O',
|
||
'taskType' => 'INTERFACE_AUTH',
|
||
'agentOpParam' => [
|
||
'redirectUri' => $urls,
|
||
'appTypes' => ["TINYAPP"],
|
||
'isvAppId' => $this->config['appid'],
|
||
'state' => $state,
|
||
],
|
||
];
|
||
$bizData = json_encode($bizDataObj);
|
||
$auth_url = "alipays://platformapi/startapp?appId=2021003130652097&page=pages%2Fauthorize%2Findex%3FbizData%3D{$bizData}";
|
||
return $auth_url;
|
||
}
|
||
|
||
|
||
/**
|
||
* Common: 支付宝用户相关请求
|
||
* Author: wu-hui
|
||
* Time: 2023/01/05 13:44
|
||
* @param $authToken
|
||
* @param $api
|
||
* @param array $params
|
||
* @param array $textParams
|
||
* @return mixed|string
|
||
*/
|
||
public function requestMemberApi($authToken, $api, $params = [], $textParams = [])
|
||
{
|
||
try {
|
||
$result = Factory::util()
|
||
->generic()
|
||
->agent($this->appAuthToken)
|
||
->auth($authToken)
|
||
->execute($api, $textParams, $params);
|
||
return json_decode($result->httpBody, true);
|
||
} catch (\Exception $e) {
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Common: 发起支付宝请求
|
||
* Author: wu-hui
|
||
* Time: 2022/12/28 17:31
|
||
* @param $api
|
||
* @param array $params
|
||
* @param array $textParams
|
||
* @return mixed|string
|
||
*/
|
||
public function requestApi($api, $params = [], $textParams = [])
|
||
{
|
||
try {
|
||
$result = Factory::util()
|
||
->generic()
|
||
->agent($this->appAuthToken)
|
||
->execute($api, $textParams, $params);
|
||
return json_decode($result->httpBody, true);
|
||
} catch (\Exception $e) {
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
|
||
/***
|
||
* 验签
|
||
* @param $param
|
||
* @return bool
|
||
*/
|
||
public function verifySgin($param)
|
||
{
|
||
$res = Factory::payment()->common()->verifyNotify($param);
|
||
return $res;
|
||
}
|
||
|
||
|
||
/***
|
||
* 查询小程序信息
|
||
* @param $order_no
|
||
* @param $appAuthToken
|
||
* @return mixed|string
|
||
*/
|
||
public function miniQuery($order_no, $appAuthToken)
|
||
{
|
||
try {
|
||
$biz_content['order_no'] = $order_no;
|
||
$result = Factory::util()->generic()
|
||
->agent($appAuthToken)
|
||
->execute('alipay.open.mini.isv.query', [], $biz_content);
|
||
return json_decode($result->httpBody, true)['alipay_open_mini_isv_query_response'];
|
||
} catch (\Exception $e) {
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
|
||
/***
|
||
* 查询订购订单
|
||
* @param $commodity_order_id
|
||
* @param int $start_page
|
||
* @return mixed|string
|
||
*/
|
||
public function queryServiceOrder($commodity_order_id, $start_page = 1)
|
||
{
|
||
try {
|
||
$biz_content['commodity_order_id'] = $commodity_order_id;
|
||
$biz_content['start_page'] = $start_page;
|
||
$result = Factory::util()->generic()
|
||
->execute('alipay.open.servicemarket.order.query', [], $biz_content);
|
||
return json_decode($result->httpBody, true)['alipay_open_servicemarket_order_query_response'];
|
||
} catch (\Exception $e) {
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
/***
|
||
* 查询订购订单
|
||
* @param $commodity_order_id
|
||
* @param int $start_page
|
||
* @return mixed|string
|
||
*/
|
||
public function MiniCreate($biz_content)
|
||
{
|
||
try {
|
||
$result = Factory::util()
|
||
->generic()
|
||
->execute('alipay.open.mini.isv.create', [], $biz_content);
|
||
return json_decode($result->httpBody, true)['alipay_open_mini_isv_create_response'];
|
||
} catch (\Exception $e) {
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
/***
|
||
* 设置小程序基础信息
|
||
* @param $textParams
|
||
* @return mixed|string
|
||
*/
|
||
public function miniSetBaseinfo($textParams,$app_logo)
|
||
{
|
||
$fileParams = [
|
||
'app_logo' => $app_logo,
|
||
];
|
||
try {
|
||
$result = Factory::util()
|
||
->generic()
|
||
->agent($this->appAuthToken)
|
||
->fileExecute("alipay.open.mini.baseinfo.modify", $textParams, [],$fileParams);
|
||
return json_decode($result->httpBody, true)['alipay_open_mini_baseinfo_modify_response'];
|
||
} catch (\Exception $e) {
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
|
||
public function miniCategoryQuery()
|
||
{
|
||
|
||
}
|
||
|
||
} |