first commit

This commit is contained in:
2024-04-01 09:54:43 +08:00
commit 899d816bc3
795 changed files with 130040 additions and 0 deletions

467
lib/Config/AppSdkConfig.php Normal file
View File

@@ -0,0 +1,467 @@
<?php
namespace Yeepay\Yop\Sdk\Config;
use Yeepay\Yop\Sdk\Config\Support\ConfigUtils;
use Yeepay\Yop\Sdk\Exception\YopClientException;
class AppSdkConfig
{
/**
* @var string
*/
private $appKey;
/**
* @var string
*/
private $serverRoot;
/**
* @var string
*/
private $yosServerRoot;
/**
* @var string
*/
private $sandboxServerRoot;
/**
* @var string
*/
private $aesSecretKey;
/**
* @var string | resource
*/
private $defaultPublicKey;
/**
* @var string | resource
*/
private $defaultIsvPrivateKey;
/**
* @var array
*/
private $httpClientConfig;
/**
* @var string
*/
private $encryptKey;
/**
* @var array
*/
private $yopPublicKeys;
/**
* @var array
*/
private $isvPrivateKeys;
/**
* @var ProxyConfig
*/
private $proxy;
/**
* @var string
*/
private $region;
/**
* @var string
*/
private $mode;
/**
* @var bool
*/
private $trustAllCerts;
/**
* @var bool
*/
private $default;
/**
* AppSdkConfig constructor.
* @param array $data
* @throws YopClientException
*/
public function __construct(array $data = [])
{
if (isset($data['app_key'])) {
$this->appKey = $data['app_key'];
}
if (isset($data['server_root'])) {
$this->serverRoot = $data['server_root'];
}
if (isset($data['yos_server_root'])) {
$this->yosServerRoot = $data['yos_server_root'];
}
if (isset($data['sandbox_server_root'])) {
$this->sandboxServerRoot = $data['sandbox_server_root'];
}
if (isset($data['aesSecretKey'])) {
$this->aesSecretKey = $data['aesSecretKey'];
}
if (isset($data['yop_public_key'])) {
$publicKeys = [];
for ($i = 0; $i < count($data['yop_public_key']); $i++) {
$publicKeyConfig = $data['yop_public_key'][$i];
$certs = ConfigUtils::loadPublicKey($publicKeyConfig);
$publicKeys[$certs[0]] = $certs[1];
if ($i == 0) {
$this->defaultPublicKey = $certs[1];
}
}
$this->yopPublicKeys = $publicKeys;
}
if (isset($data['isv_private_key'])) {
$privateKeys = [];
for ($i = 0; $i < count($data['isv_private_key']); $i++) {
$privateKeyConfig = $data['isv_private_key'][$i];
$certs = ConfigUtils::loadPrivateKey($privateKeyConfig);
$privateKeys[$certs[0]] = $certs[1];
if ($i == 0) {
$this->defaultIsvPrivateKey = $certs[1];
}
}
$this->isvPrivateKeys = $privateKeys;
}
if (isset($data['encrypt_key'])) {
$this->encryptKey = $data['encrypt_key'];
}
if (isset($data['http_client'])) {
$this->httpClientConfig = $data['http_client'];
}
if (isset($data['trust_all_certs'])) {
$this->trustAllCerts = $data['trust_all_certs'];
}
if (isset($data['proxy'])) {
$proxyConfig = $data['proxy'];
$this->proxy = new ProxyConfig($proxyConfig['host'], $proxyConfig['port'], $proxyConfig['scheme'],
$proxyConfig['username'],
$proxyConfig['password'], $proxyConfig['domain'], $proxyConfig['workstation']);
}
if (isset($data['region'])) {
$this->region = $data['region'];
}
if (isset($data['default'])) {
$this->default = $data['default'];
}
if (isset($data['mode'])) {
$this->mode = $data['mode'];
}
}
/**
* @return string
*/
public function getAppKey()
{
return $this->appKey;
}
/**
* @param string $appKey
* @return AppSdkConfig
*/
public function setAppKey($appKey)
{
$this->appKey = $appKey;
return $this;
}
/**
* @return string
*/
public function getServerRoot()
{
return $this->serverRoot;
}
/**
* @param string $serverRoot
* @return AppSdkConfig
*/
public function setServerRoot($serverRoot)
{
$this->serverRoot = $serverRoot;
return $this;
}
/**
* @return string
*/
public function getYosServerRoot()
{
return $this->yosServerRoot;
}
/**
* @param string $yosServerRoot
* @return AppSdkConfig
*/
public function setYosServerRoot($yosServerRoot)
{
$this->yosServerRoot = $yosServerRoot;
return $this;
}
/**
* @return string
*/
public function getSandboxServerRoot()
{
return $this->sandboxServerRoot;
}
/**
* @param string $sandboxServerRoot
* @return AppSdkConfig
*/
public function setSandboxServerRoot($sandboxServerRoot)
{
$this->sandboxServerRoot = $sandboxServerRoot;
return $this;
}
/**
* @return string
*/
public function getAesSecretKey()
{
return $this->aesSecretKey;
}
/**
* @param string $aesSecretKey
* @return AppSdkConfig
*/
public function setAesSecretKey($aesSecretKey)
{
$this->aesSecretKey = $aesSecretKey;
return $this;
}
/**
* @return resource|string
*/
public function getDefaultPublicKey()
{
return $this->defaultPublicKey;
}
/**
* @param resource|string $defaultPublicKey
* @return AppSdkConfig
*/
public function setDefaultPublicKey($defaultPublicKey)
{
$this->defaultPublicKey = $defaultPublicKey;
return $this;
}
/**
* @return resource|string
*/
public function getDefaultIsvPrivateKey()
{
return $this->defaultIsvPrivateKey;
}
/**
* @param resource|string $defaultIsvPrivateKey
* @return AppSdkConfig
*/
public function setDefaultIsvPrivateKey($defaultIsvPrivateKey)
{
$this->defaultIsvPrivateKey = $defaultIsvPrivateKey;
return $this;
}
/**
* @return array
*/
public function getHttpClientConfig()
{
return $this->httpClientConfig;
}
/**
* @param array $httpClientConfig
* @return AppSdkConfig
*/
public function setHttpClientConfig($httpClientConfig)
{
$this->httpClientConfig = $httpClientConfig;
return $this;
}
/**
* @return string
*/
public function getEncryptKey()
{
return $this->encryptKey;
}
/**
* @param string $encryptKey
* @return AppSdkConfig
*/
public function setEncryptKey($encryptKey)
{
$this->encryptKey = $encryptKey;
return $this;
}
/**
* @return array
*/
public function getYopPublicKeys()
{
return $this->yopPublicKeys;
}
/**
* @param array $yopPublicKeys
* @return AppSdkConfig
*/
public function setYopPublicKeys($yopPublicKeys)
{
$this->yopPublicKeys = $yopPublicKeys;
return $this;
}
/**
* @return array
*/
public function getIsvPrivateKeys()
{
return $this->isvPrivateKeys;
}
/**
* @param array $isvPrivateKeys
* @return AppSdkConfig
*/
public function setIsvPrivateKeys($isvPrivateKeys)
{
$this->isvPrivateKeys = $isvPrivateKeys;
return $this;
}
/**
* @return ProxyConfig
*/
public function getProxy()
{
return $this->proxy;
}
/**
* @param ProxyConfig $proxy
* @return AppSdkConfig
*/
public function setProxy($proxy)
{
$this->proxy = $proxy;
return $this;
}
/**
* @return string
*/
public function getRegion()
{
return $this->region;
}
/**
* @param string $region
* @return AppSdkConfig
*/
public function setRegion($region)
{
$this->region = $region;
return $this;
}
/**
* @return string
*/
public function getMode()
{
return $this->mode;
}
/**
* @param string $mode
* @return AppSdkConfig
*/
public function setMode($mode)
{
$this->mode = $mode;
return $this;
}
/**
* @return bool
*/
public function isTrustAllCerts()
{
return $this->trustAllCerts;
}
/**
* @param bool $trustAllCerts
* @return AppSdkConfig
*/
public function setTrustAllCerts($trustAllCerts)
{
$this->trustAllCerts = $trustAllCerts;
return $this;
}
/**
* @return bool
*/
public function isDefault()
{
return $this->default;
}
/**
* @param bool $default
* @return AppSdkConfig
*/
public function setDefault($default)
{
$this->default = $default;
return $this;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Yeepay\Yop\Sdk\Config;
interface AppSdkConfigProvider
{
/**
* @param $appKey string
* @return AppSdkConfig
*/
public function getConfig($appKey);
/**
* @return array
*/
public function getAllConfig();
/**
* @return AppSdkConfig
*/
public function getDefaultConfig();
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Yeepay\Yop\Sdk\Config;
use Yeepay\Yop\Sdk\Exception\YopClientException;
class DefaultAppSdkConfigProvider implements AppSdkConfigProvider
{
/**
* @var array
*/
private $configContainer;
/**
* @var AppSdkConfig
*/
private $default;
/**
* DefaultAppSdkConfigProvider constructor.
* @param $config AppSdkConfig | AppSdkConfig[]
* @throws YopClientException
*/
public function __construct($config)
{
if ($config instanceof AppSdkConfig) {
$this->default = $config;
$this->configContainer = [$config->getAppKey() => $config];
} else {
if (empty($config)) {
throw new YopClientException("config is empty.");
}
$this->configContainer = [];
$this->default = $config[0];
foreach ($config as $item) {
$this->configContainer[$item->getAppKey()] = $item;
if ($item->isDefault()) {
$this->default = $item;
}
}
}
}
/**
* @param $appKey string
* @return AppSdkConfig
*/
public function getConfig($appKey)
{
return $this->configContainer[$appKey];
}
/**
* @return AppSdkConfig
*/
public function getDefaultConfig()
{
return $this->default;
}
/**
* @return array
*/
public function getAllConfig()
{
return $this->configContainer;
}
}

12
lib/Config/Mode.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
namespace Yeepay\Yop\Sdk\Config;
class Mode
{
const PROD = 'prod';
const SANDBOX = 'sandbox';
}

204
lib/Config/ProxyConfig.php Normal file
View File

@@ -0,0 +1,204 @@
<?php
namespace Yeepay\Yop\Sdk\Config;
class ProxyConfig
{
/**
* @var string
*/
private $host;
/**
* @var int
*/
private $port;
/**
* @var string
*/
private $scheme;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $password;
/**
* @var string
*/
private $domain;
/**
* @var string
*/
private $workstation;
/**
* ProxyConfig constructor.
* @param $host
* @param $port
* @param $scheme
* @param null $username
* @param null $password
* @param null $domain
* @param null $workstation
*/
public function __construct(
$host,
$port,
$scheme,
$username = null,
$password = null,
$domain = null,
$workstation = null
) {
$this->host = $host;
$this->port = $port;
$this->scheme = $scheme;
$this->username = $username;
$this->password = $password;
$this->domain = $domain;
$this->workstation = $workstation;
}
/**
* @return string
*/
public function getHost()
{
return $this->host;
}
/**
* @param string $host
* @return ProxyConfig
*/
public function setHost($host)
{
$this->host = $host;
return $this;
}
/**
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* @param int $port
* @return ProxyConfig
*/
public function setPort($port)
{
$this->port = $port;
return $this;
}
/**
* @return string
*/
public function getScheme()
{
return $this->scheme;
}
/**
* @param string $scheme
* @return ProxyConfig
*/
public function setScheme($scheme)
{
$this->scheme = $scheme;
return $this;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $username
* @return ProxyConfig
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* @param string $password
* @return ProxyConfig
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* @return string
*/
public function getDomain()
{
return $this->domain;
}
/**
* @param string $domain
* @return ProxyConfig
*/
public function setDomain($domain)
{
$this->domain = $domain;
return $this;
}
/**
* @return string
*/
public function getWorkstation()
{
return $this->workstation;
}
/**
* @param string $workstation
* @return ProxyConfig
*/
public function setWorkstation($workstation)
{
$this->workstation = $workstation;
return $this;
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Yeepay\Yop\Sdk\Config\Support;
use Yeepay\Yop\Sdk\Exception\YopClientException;
class ConfigUtils
{
/**
* @param array $config
* @return array
* @throws YopClientException
*/
public static function loadPublicKey(array $config)
{
$certType = $config['cert_type'];
if ($config['store_type'] == 'string') {
return [$certType, self::getPublicKey($config['value'])];
} else {
throw new YopClientException('unsupported publicKey storeType:'.$config['store_type']);
}
}
/**
* @param array $config
* @return array
* @throws YopClientException
*/
public static function loadPrivateKey(array $config)
{
$certType = $config['cert_type'];
if ($config['store_type'] == 'string') {
return [$certType, self::getPrivateKey($config['value'])];
} else {
if ($config['store_type'] == 'file_12') {
$certs = [];
$pkcs12 = file_get_contents($config['value']);
openssl_pkcs12_read($pkcs12, $certs, $config['password']);
return [$certType, $certs['pkey']];
} else {
throw new YopClientException('unsupported publicKey storeType:'.$config['store_type']);
}
}
}
/**
* @param $publicKey
* @return resource
* @throws YopClientException
*/
public static function getPublicKey($publicKey)
{
$publicKey = "-----BEGIN PUBLIC KEY-----\n".
wordwrap($publicKey, 64, "\n", true).
"\n-----END PUBLIC KEY-----";
$result = openssl_pkey_get_public($publicKey);
if (empty($result)) {
throw new YopClientException("illegal publicKey");
}
return $result;
}
/**
* @param $privateKey
* @return resource
* @throws YopClientException
*/
public static function getPrivateKey($privateKey)
{
$privateKey = "-----BEGIN RSA PRIVATE KEY-----\n".
wordwrap($privateKey, 64, "\n", true).
"\n-----END RSA PRIVATE KEY-----";
$result = openssl_pkey_get_private($privateKey);
if (empty($result)) {
throw new YopClientException("illegal privateKey");
}
return $result;
}
}