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

View File

@@ -0,0 +1,100 @@
<?php
namespace Yeepay\Yop\Sdk\Client;
use Yeepay\Yop\Sdk\Model\BaseRequest;
use Yeepay\Yop\Sdk\Model\Transform\RequestMarshaller;
use Yeepay\Yop\Sdk\Model\Transform\ResponseUnMarshaller;
class ClientExecutionParams
{
/**
* @var BaseRequest
*/
private $request;
/**
* @var RequestMarshaller
*/
private $requestMarshaller;
/**
* @var ResponseUnMarshaller
*/
private $responseUnMarshaller;
/**
* ClientExecutionParams constructor.
* @param $request BaseRequest
* @param RequestMarshaller $requestMarshaller
* @param ResponseUnMarshaller $responseUnMarshaller
*/
public function __construct(
$request,
RequestMarshaller $requestMarshaller,
ResponseUnMarshaller $responseUnMarshaller
) {
$this->request = $request;
$this->requestMarshaller = $requestMarshaller;
$this->responseUnMarshaller = $responseUnMarshaller;
}
/**
* @return BaseRequest
*/
public function getRequest()
{
return $this->request;
}
/**
* @param BaseRequest $request
* @return ClientExecutionParams
*/
public function setRequest(BaseRequest $request)
{
$this->request = $request;
return $this;
}
/**
* @return RequestMarshaller
*/
public function getRequestMarshaller()
{
return $this->requestMarshaller;
}
/**
* @param RequestMarshaller $requestMarshaller
* @return ClientExecutionParams
*/
public function setRequestMarshaller(RequestMarshaller $requestMarshaller)
{
$this->requestMarshaller = $requestMarshaller;
return $this;
}
/**
* @return ResponseUnMarshaller
*/
public function getResponseUnMarshaller()
{
return $this->responseUnMarshaller;
}
/**
* @param ResponseUnMarshaller $responseUnMarshaller
* @return ClientExecutionParams
*/
public function setResponseUnMarshaller(ResponseUnMarshaller $responseUnMarshaller)
{
$this->responseUnMarshaller = $responseUnMarshaller;
return $this;
}
}

View File

@@ -0,0 +1,133 @@
<?php
namespace Yeepay\Yop\Sdk\Client;
use Yeepay\Yop\Sdk\Auth\AuthorizationReqRegistry;
use Yeepay\Yop\Sdk\Auth\Cipher\DefaultEncryptor;
use Yeepay\Yop\Sdk\Auth\SignerFactory;
use Yeepay\Yop\Sdk\Auth\SignOptions;
use Yeepay\Yop\Sdk\Auth\YopCredentialProvider;
use Yeepay\Yop\Sdk\Exception\YopClientException;
use Yeepay\Yop\Sdk\Http\ExecutionContext;
use Yeepay\Yop\Sdk\Http\YopHttpClient;
use Yeepay\Yop\Sdk\Model\Transform\ResponseUnMarshalParams;
class ClientHandler
{
/**
* @var YopCredentialProvider
*/
private $credentialsProvider;
/**
* @var AuthorizationReqRegistry
*/
private $authorizationReqRegistry;
/**
* @var YopHttpClient
*/
private $yopHttpClient;
/**
* @var GateWayRouter
*/
private $gatewayRouter;
/**
* ClientHandler constructor.
* @param ClientParams $clientParams
*/
public function __construct(ClientParams $clientParams)
{
$this->credentialsProvider = $clientParams->getCredentialsProvider();
$this->authorizationReqRegistry = $clientParams->getAuthorizationReqRegistry();
$this->yopHttpClient = new YopHttpClient($clientParams->getClientConfiguration());
$this->gatewayRouter = new GateWayRouter(new ServerRootSpace($clientParams->getEndPoint(),
$clientParams->getYosEndPoint(),
$clientParams->getSandboxEndPoint()),
$clientParams->getModes());
}
/**
* @param ClientExecutionParams $executionParams
* @return mixed
* @throws YopClientException
*/
public function execute(ClientExecutionParams $executionParams)
{
$executionContext = $this->getExecutionContext($executionParams);
$request = $executionParams->getRequestMarshaller()->marshal($executionParams->getRequest());
/** @var ExecutionContext $httpExecutionContext */
$httpExecutionContext = $executionContext[0];
$request->setEndpoint($this->gatewayRouter->route($httpExecutionContext->getCredentials()->getAppKey(),
$request));
$yopHttpResponse = $this->yopHttpClient->execute($request, $httpExecutionContext);
/** @var ResponseUnMarshalParams $ResponseUnMarshalParams */
$ResponseUnMarshalParams = $executionContext[1];
return $executionParams->getResponseUnMarshaller()->unmarshal($yopHttpResponse, $ResponseUnMarshalParams);
}
/**
* @param ClientExecutionParams $executionParams
* @return array
* @throws YopClientException
*/
private function getExecutionContext(ClientExecutionParams $executionParams)
{
$httpExecutionContext = new ExecutionContext();
$ResponseUnMarshalParams = new ResponseUnMarshalParams();
$request = $executionParams->getRequest();
$authorizationReq = $this->authorizationReqRegistry->getAuthorizationReq($request::getOperationId());
if (isset($authorizationReq)) {
$signer = SignerFactory::getSigner($authorizationReq->getSignerType());
$signOptions = new SignOptions($authorizationReq->getDigestAlg(), $authorizationReq->getProtocolPrefix());
$httpExecutionContext->setSigner($signer)->setSignOptions($signOptions);
$ResponseUnMarshalParams->setSigner($signer)->setSignOptions($signOptions)
->setPublicKey($this->credentialsProvider->getYopPublicKey($authorizationReq->getCredentialType()));
$credentials = null;
$requestConfig = $request->getRequestConfig();
$needEncrypt = false;
if (isset($requestConfig)) {
if (!empty($requestConfig->getCredentials())) {
$credentials = $requestConfig->getCredentials();
} else {
$customAppKey = $requestConfig->getAppKey();
if (!empty($customAppKey)) {
$credentials = $this->credentialsProvider->getCredential($customAppKey,
$authorizationReq->getCredentialType());
if (!isset($credentials)) {
throw new YopClientException('no credentials specified, appKey:'.$customAppKey
.', credentialType:'.$authorizationReq->getCredentialType());
}
}
}
$needEncrypt = $requestConfig->isNeedEncrypt();
}
if (!isset($credentials)) {
$credentials = $this->credentialsProvider->getDefaultAppCredential($authorizationReq->getCredentialType());
}
if (!isset($credentials)) {
throw new YopClientException('no credentials specified for defaultAppKey, credentialType:'
.$authorizationReq->getCredentialType());
}
$httpExecutionContext->setCredentials($credentials);
if ($needEncrypt) {
$encryptor = new DefaultEncryptor($credentials->getEncryptKey());
$httpExecutionContext->setNeedEncrypt(true)->setEncryptor($encryptor);
$ResponseUnMarshalParams->setNeedDecrypt(true)->setEncryptor($encryptor);
}
return [$httpExecutionContext, $ResponseUnMarshalParams];
} else {
throw new YopClientException("no securityReq assigned, api:".$request::getOperationId());
}
}
}

181
lib/Client/ClientParams.php Normal file
View File

@@ -0,0 +1,181 @@
<?php
namespace Yeepay\Yop\Sdk\Client;
use GuzzleHttp\Psr7\Uri;
use Yeepay\Yop\Sdk\Auth\AuthorizationReqRegistry;
use Yeepay\Yop\Sdk\Auth\YopCredentialProvider;
use Yeepay\Yop\Sdk\Http\ClientConfiguration;
class ClientParams
{
/**
* @var Uri
*/
private $endPoint;
/**
* @var Uri
*/
private $yosEndPoint;
/**
* @var Uri
*/
private $sandboxEndPoint;
/**
* @var ClientConfiguration
*/
private $clientConfiguration;
/**
* @var AuthorizationReqRegistry
*/
private $authorizationReqRegistry;
/**
* @var YopCredentialProvider
*/
private $credentialsProvider;
/**
* @var array
*/
private $modes;
/**
* @return Uri
*/
public function getEndPoint()
{
return $this->endPoint;
}
/**
* @param Uri $endPoint
* @return ClientParams
*/
public function setEndPoint($endPoint)
{
$this->endPoint = $endPoint;
return $this;
}
/**
* @return Uri
*/
public function getYosEndPoint()
{
return $this->yosEndPoint;
}
/**
* @param Uri $yosEndPoint
* @return ClientParams
*/
public function setYosEndPoint($yosEndPoint)
{
$this->yosEndPoint = $yosEndPoint;
return $this;
}
/**
* @return Uri
*/
public function getSandboxEndPoint()
{
return $this->sandboxEndPoint;
}
/**
* @param Uri $sandboxEndPoint
* @return ClientParams
*/
public function setSandboxEndPoint($sandboxEndPoint)
{
$this->sandboxEndPoint = $sandboxEndPoint;
return $this;
}
/**
* @return ClientConfiguration
*/
public function getClientConfiguration()
{
return $this->clientConfiguration;
}
/**
* @param ClientConfiguration $clientConfiguration
* @return ClientParams
*/
public function setClientConfiguration($clientConfiguration)
{
$this->clientConfiguration = $clientConfiguration;
return $this;
}
/**
* @return AuthorizationReqRegistry
*/
public function getAuthorizationReqRegistry()
{
return $this->authorizationReqRegistry;
}
/**
* @param AuthorizationReqRegistry $authorizationReqRegistry
* @return ClientParams
*/
public function setAuthorizationReqRegistry($authorizationReqRegistry)
{
$this->authorizationReqRegistry = $authorizationReqRegistry;
return $this;
}
/**
* @return YopCredentialProvider
*/
public function getCredentialsProvider()
{
return $this->credentialsProvider;
}
/**
* @param YopCredentialProvider $credentialsProvider
* @return ClientParams
*/
public function setCredentialsProvider($credentialsProvider)
{
$this->credentialsProvider = $credentialsProvider;
return $this;
}
/**
* @return array
*/
public function getModes()
{
return $this->modes;
}
/**
* @param array $modes
* @return ClientParams
*/
public function setModes($modes)
{
$this->modes = $modes;
return $this;
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace Yeepay\Yop\Sdk\Client;
use GuzzleHttp\Psr7\Uri;
use Yeepay\Yop\Sdk\Config\Mode;
use Yeepay\Yop\Sdk\Internal\Request;
class GateWayRouter
{
/**
* @var ServerRootSpace
*/
private $serverRootSpace;
/**
* @var array
*/
private $independentApiGroups;
/**
* @var string
*/
private $defaultMode;
/**
* @var array
*/
private $modes;
/**
* GateWayRouter constructor.
* @param ServerRootSpace $serverRootSpace
* @param $modes
*/
public function __construct(ServerRootSpace $serverRootSpace, $modes = [])
{
$this->serverRootSpace = $serverRootSpace;
$this->independentApiGroups = ["bank-encryption"];
$this->defaultMode = getenv("yop.sdk.mode");
$this->modes = $modes;
}
/**
* @param $appKey string
* @param $request Request
* @return Uri
*/
public function route($appKey, $request)
{
if ($this->isAppInSandboxMode($appKey)) {
return $this->serverRootSpace->getSandboxServerRoot();
} else {
$serverRoot = $request->getYosFlag() ? $this->serverRootSpace->getYosServerRoot() :
$this->serverRootSpace->getServerRoot();
$apiGroup = str_replace('_', '-', strtolower($request->getServiceName()));
if (in_array($apiGroup, $this->independentApiGroups)) {
$result = new Uri();
$result->withScheme($serverRoot->getScheme());
$result->withHost($serverRoot->getHost());
$result->withPort($serverRoot->getPort());
$result->withPath($serverRoot->getPath());
$result->withUserInfo($serverRoot->getUserInfo());
$result->withQuery($serverRoot->getQuery());
$result->withFragment($serverRoot->getFragment());
return $result;
} else {
return $serverRoot;
}
}
}
/**
* @param string $appKey
* @return bool
*/
private function isAppInSandboxMode($appKey)
{
if (empty($this->defaultMode)) {
return isset($this->modes[$appKey]) && $this->modes[$appKey] = Mode::SANDBOX;
}
return $this->defaultMode == Mode::SANDBOX;
}
/**
* @param string $apiGroup
* @param string $originHost
* @param bool $isYosRequest
* @return string
*/
private function getIndependentApiGroupHost($apiGroup, $originHost, $isYosRequest)
{
if ($isYosRequest) {
return $originHost;
}
$index = strpos($originHost, '.');
return substr($originHost, 0, $index).'_'.$apiGroup.substr($originHost, $index, -1);
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Yeepay\Yop\Sdk\Client;
use GuzzleHttp\Psr7\Uri;
class ServerRootSpace
{
private static $defaultServerRoot;
private static $defaultYosServerRoot;
private static $defaultSandboxServerRoot;
public static function __init()
{
self::$defaultServerRoot = new Uri('https://openapi.yeepay.com/yop-center');
self::$defaultYosServerRoot = new Uri('https://yos.yeepay.com/yop-center');
self::$defaultSandboxServerRoot = new Uri('https://sandbox.yeepay.com/yop-center');
}
/**
* @var Uri
*/
private $serverRoot;
/**
* @var Uri
*/
private $yosServerRoot;
/**
* @var Uri
*/
private $sandboxServerRoot;
/**
* ServerRootSpace constructor.
* @param Uri|null $serverRoot
* @param Uri|null $yosServerRoot
* @param Uri|null $sandboxServerRoot
*/
public function __construct(Uri $serverRoot = null, Uri $yosServerRoot = null, Uri $sandboxServerRoot = null)
{
$this->serverRoot = isset($serverRoot) ? $serverRoot : self::$defaultServerRoot;
$this->yosServerRoot = isset($yosServerRoot) ? $yosServerRoot : self::$defaultYosServerRoot;
$this->sandboxServerRoot = isset($sandboxServerRoot) ? $sandboxServerRoot : self::$defaultSandboxServerRoot;
}
/**
* @return Uri
*/
public function getServerRoot()
{
return $this->serverRoot;
}
/**
* @return Uri
*/
public function getYosServerRoot()
{
return $this->yosServerRoot;
}
/**
* @return Uri
*/
public function getSandboxServerRoot()
{
return $this->sandboxServerRoot;
}
}
ServerRootSpace::__init();

View File

@@ -0,0 +1,67 @@
<?php
namespace Yeepay\Yop\Sdk\Client\Support;
use GuzzleHttp\Psr7\Uri;
use Yeepay\Yop\Sdk\Auth\Credential\DefaultCredentialProvider;
use Yeepay\Yop\Sdk\Client\ClientParams;
use Yeepay\Yop\Sdk\Config\AppSdkConfig;
use Yeepay\Yop\Sdk\Config\AppSdkConfigProvider;
use Yeepay\Yop\Sdk\Exception\YopClientException;
use Yeepay\Yop\Sdk\Http\ClientConfiguration;
class ClientParamsSupport
{
/**
* @param AppSdkConfigProvider $appSdkConfigProvider
* @return ClientParams
* @throws YopClientException
*/
public static function generateClientParams(AppSdkConfigProvider $appSdkConfigProvider)
{
$clientParams = new ClientParams();
$clientParams->setCredentialsProvider(new DefaultCredentialProvider($appSdkConfigProvider));
$defaultAppSdkConfig = $appSdkConfigProvider->getDefaultConfig();
if (!empty($defaultAppSdkConfig->getServerRoot())) {
$clientParams->setEndPoint(new Uri($defaultAppSdkConfig->getServerRoot()));
}
if (!empty($defaultAppSdkConfig->getYosServerRoot())) {
$clientParams->setYosEndPoint(new Uri($defaultAppSdkConfig->getYosServerRoot()));
}
if (!empty($defaultAppSdkConfig->getSandboxServerRoot())) {
$clientParams->setSandboxEndPoint(new Uri($defaultAppSdkConfig->getSandboxServerRoot()));
}
$clientConfigurations = new ClientConfiguration();
if (!empty($defaultAppSdkConfig->getHttpClientConfig())) {
$httpClientConfig = $defaultAppSdkConfig->getHttpClientConfig();
$clientConfigurations->setSocketTimeoutInMillis($httpClientConfig['read_timeout']);
$clientConfigurations->setConnectionTimeoutInMillis($httpClientConfig['connect_timeout']);
}
if (!empty($defaultAppSdkConfig->getProxy())) {
$proxyConfig = $defaultAppSdkConfig->getProxy();
$parts = [
'scheme' => $proxyConfig->getScheme(), 'host' => $proxyConfig->getHost(),
'port' => $proxyConfig->getPort(),
];
if (!empty($proxyConfig->getUsername()) && !empty($proxyConfig->getPort())) {
$parts['user'] = $proxyConfig->getUsername();
$parts['pass'] = $proxyConfig->getPassword();
}
$proxyURI = new Uri($parts);
$clientConfigurations->setProxyUrl($proxyURI->__toString());
}
$clientParams->setClientConfiguration($clientConfigurations);
$modes = [];
foreach ($appSdkConfigProvider->getAllConfig() as $appKey => $appKeySdkConfig) {
/* @var $appKeySdkConfig AppSdkConfig */
if (!empty($appKeySdkConfig->getMode())) {
$modes[$appKey] = $appKeySdkConfig->getMode();
}
}
$clientParams->setModes($modes);
return $clientParams;
}
}