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

40
lib/Model/BaseRequest.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
namespace Yeepay\Yop\Sdk\Model;
abstract class BaseRequest
{
/**
* @var RequestConfig
*/
private $requestConfig;
/**
* @return RequestConfig
*/
public function getRequestConfig()
{
return $this->requestConfig;
}
/**
* @param RequestConfig $requestConfig
* @return BaseRequest
*/
public function setRequestConfig($requestConfig)
{
$this->requestConfig = $requestConfig;
return $this;
}
/**
* @return string
*/
public static function getOperationId()
{
return '';
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Yeepay\Yop\Sdk\Model;
abstract class BaseResponse
{
/**
* @var YopResponseMetadata
*/
private $metadata;
/**
* BaseResponse constructor.
*/
public function __construct()
{
$this->metadata = $this->getMetaDataInstance();
}
/**
* @return YopResponseMetadata
*/
public function getMetadata()
{
return $this->metadata;
}
/**
* @return YopResponseMetadata
*/
protected function getMetaDataInstance()
{
return new YopResponseMetadata();
}
abstract function getResultClass();
/**
* @param mixed $result
*/
abstract function setResult($result);
}

View File

@@ -0,0 +1,57 @@
<?php
namespace Yeepay\Yop\Sdk\Model;
interface ModelInterface
{
/**
* The original name of the model.
* @return string
*/
public function getModelName();
/**
* Array of property to type mappings. Used for (de)serialization
* @return array
*/
public static function swaggerTypes();
/**
* Array of property to format mappings. Used for (de)serialization
* @return array
*/
public static function swaggerFormats();
/**
* Array of attributes where the key is the local name, and the value is the original name
* @return array
*/
public static function attributeMap();
/**
* Array of attributes to setter functions (for deserialization of responses)
* @return array
*/
public static function setters();
/**
* Array of attributes to getter functions (for serialization of requests)
* @return array
*/
public static function getters();
/**
* Show all the invalid properties with reasons.
* @return array
*/
public function listInvalidProperties();
/**
* Validate all the properties in the model
* return true if all passed
* @return bool
*/
public function valid();
}

125
lib/Model/RequestConfig.php Normal file
View File

@@ -0,0 +1,125 @@
<?php
namespace Yeepay\Yop\Sdk\Model;
use Yeepay\Yop\Sdk\Auth\YopRsaCredentials;
class RequestConfig
{
/**
* @var string
*/
private $appKey;
/**
* @var YopRsaCredentials
*/
private $credentials;
/**
* @var array
*/
private $customRequestHeaders;
/**
* @var bool
*/
private $needEncrypt;
/**
* RequestConfig constructor.
* @param null $appKey
* @param YopRsaCredentials|null $credentials
* @param array|null $customRequestHeaders
* @param bool $needEncrypt
*/
public function __construct(
$appKey = null,
YopRsaCredentials $credentials = null,
array $customRequestHeaders = null,
$needEncrypt = false
) {
$this->appKey = $appKey;
$this->credentials = $credentials;
$this->customRequestHeaders = $customRequestHeaders;
$this->needEncrypt = $needEncrypt;
}
/**
* @return string
*/
public function getAppKey()
{
return $this->appKey;
}
/**
* @param string $appKey
* @return RequestConfig
*/
public function setAppKey($appKey)
{
$this->appKey = $appKey;
return $this;
}
/**
* @return YopRsaCredentials
*/
public function getCredentials()
{
return $this->credentials;
}
/**
* @param YopRsaCredentials $credentials
* @return RequestConfig
*/
public function setCredentials($credentials)
{
$this->credentials = $credentials;
return $this;
}
/**
* @return array
*/
public function getCustomRequestHeaders()
{
return $this->customRequestHeaders;
}
/**
* @param array $customRequestHeaders
* @return RequestConfig
*/
public function setCustomRequestHeaders($customRequestHeaders)
{
$this->customRequestHeaders = $customRequestHeaders;
return $this;
}
/**
* @return bool
*/
public function isNeedEncrypt()
{
return $this->needEncrypt;
}
/**
* @param bool $needEncrypt
* @return RequestConfig
*/
public function setNeedEncrypt($needEncrypt)
{
$this->needEncrypt = $needEncrypt;
return $this;
}
}

View File

@@ -0,0 +1,150 @@
<?php
namespace Yeepay\Yop\Sdk\Model\Transform;
use Psr\Log\LoggerInterface;
use Yeepay\Yop\Sdk\Exception\YopClientException;
use Yeepay\Yop\Sdk\Exception\YopServiceException;
use Yeepay\Yop\Sdk\Http\ContentType;
use Yeepay\Yop\Sdk\Http\Headers;
use Yeepay\Yop\Sdk\Http\HttpStatus;
use Yeepay\Yop\Sdk\Http\YopHttpResponse;
use Yeepay\Yop\Sdk\Log\LogFactory;
use Yeepay\Yop\Sdk\Model\BaseResponse;
use Yeepay\Yop\Sdk\Model\YopResponseMetadata;
use Yeepay\Yop\Sdk\Utils\ObjectSerializer;
use Yeepay\Yop\Sdk\Utils\YopConstants;
abstract class BaseResponseUnMarshaller implements ResponseUnMarshaller
{
/**
* @var LoggerInterface
*/
private static $logger;
public static function __init()
{
self::$logger = LogFactory::getLogger(self::class);
}
public function unmarshal(YopHttpResponse $yopHttpResponse, ResponseUnMarshalParams $params)
{
$response = $this->getResponseInstance();
$this->handleResponseMetaData($yopHttpResponse, $response->getMetadata());
$this->checkSignature($yopHttpResponse, $response->getMetadata()->getYopSign(), $params);
$this->handleContent($yopHttpResponse, $response->getMetadata()->getContentType(), $response, $params);
return $response;
}
/**
* @return BaseResponse
*/
protected abstract function getResponseInstance();
/**
* @param YopHttpResponse $yopHttpResponse
* @param YopResponseMetadata $responseMetadata
*/
public function handleResponseMetaData($yopHttpResponse, YopResponseMetadata $responseMetadata)
{
$responseMetadata->setYopRequestId($yopHttpResponse->getHeader(Headers::YOP_REQUEST_ID));
$responseMetadata->setYopContentSha256($yopHttpResponse->getHeader(Headers::YOP_CONTENT_SHA256));
$responseMetadata->setYopSign($yopHttpResponse->getHeader(Headers::YOP_SIGN));
$responseMetadata->setYopVia($yopHttpResponse->getHeader(Headers::YOP_VIA));
$responseMetadata->setContentDisposition($yopHttpResponse->getHeader(Headers::CONTENT_DISPOSITION));
$responseMetadata->setContentEncoding($yopHttpResponse->getHeader(Headers::CONTENT_ENCODING));
$responseMetadata->setContentLength($yopHttpResponse->getHeaderAsLong(Headers::CONTENT_LENGTH));
$responseMetadata->setContentType($yopHttpResponse->getHeader(Headers::CONTENT_TYPE));
$responseMetadata->setDate($yopHttpResponse->getHeaderAsRFC822Date(Headers::DATE));
$responseMetadata->setTransferEncoding($yopHttpResponse->getHeader(Headers::TRANSFER_ENCODING));
$etag = $yopHttpResponse->getHeader(Headers::ETAG);
if (isset($etag)) {
//TODO 此处对比java版有所简略
$responseMetadata->setEtag($etag);
}
$responseMetadata->setExpires($yopHttpResponse->getHeaderAsRFC822Date(Headers::EXPIRES));
$responseMetadata->setLastModified($yopHttpResponse->getHeaderAsRFC822Date(Headers::LAST_MODIFIED));
$responseMetadata->setServer($yopHttpResponse->getHeader(Headers::SERVER));
if ($responseMetadata->getYopVia() == YopConstants::DEFAULT_SANDBOX_VIA) {
self::$logger->info('response from sandbox-gateway');
}
}
private function checkSignature(YopHttpResponse $yopHttpResponse, $signature, ResponseUnMarshalParams $params)
{
if (isset($signature)) {
$params->getSigner()
->checkSignature($yopHttpResponse, $signature, $params->getPublicKey(), $params->getSignOptions());
}
}
/**
* @param YopHttpResponse $yopHttpResponse
* @param $contentType
* @param BaseResponse $response
* @param ResponseUnMarshalParams $params
* @throws YopClientException
*/
protected function handleContent(
YopHttpResponse $yopHttpResponse,
$contentType,
BaseResponse $response,
ResponseUnMarshalParams $params
) {
$content = $yopHttpResponse->readContent();
if ($params->isNeedDecrypt() && $contentType == ContentType::APPLICATION_JSON) {
$content = $params->getEncryptor()->decrypt($content);
}
$statusCode = $yopHttpResponse->getStatusCode();
if ($statusCode / 100 == HttpStatus::SC_OK / 100) {
if ($statusCode != HttpStatus::SC_NO_CONTENT) {
$data = json_decode($content);
$response->setResult(ObjectSerializer::deserialize($data->{'result'}, $response->getResultClass()));
}
} elseif ($statusCode >= HttpStatus::SC_INTERNAL_SERVER_ERROR && $statusCode != HttpStatus::SC_BAD_GATEWAY) {
$this->handleErrorResponse($content, $response->getMetadata(), $yopHttpResponse);
} else {
throw new YopClientException("Unexpected http statusCode:".$statusCode);
}
}
/**
* @param $content
* @param YopResponseMetadata $responseMetadata
* @param YopHttpResponse $yopHttpResponse
* @throws YopServiceException
*/
protected function handleErrorResponse(
$content,
YopResponseMetadata $responseMetadata,
YopHttpResponse $yopHttpResponse
) {
$yopServiceException = null;
if (!empty($content)) {
try {
$data = json_decode($content, true);
dd($data);
$yopServiceException = new YopServiceException($data['message'], $data['code']);
$yopServiceException->setRequestId($data['requestId']);
$yopServiceException->setSubErrorCode($data['subCode']);
$yopServiceException->setSubErrorMessage($data['subMessage']);
} catch (\Exception $e) {
self::$logger->error("unable to parse error response, content".$content);
}
}
if (!isset($yopServiceException)) {
$yopServiceException = new YopServiceException($yopHttpResponse->getStatusText());
$yopServiceException->setRequestId($responseMetadata->getYopRequestId());
}
$yopServiceException->setStatusCode($yopHttpResponse->getStatusCode());
$yopServiceException->setErrorType(YopServiceException::ERROR_TYPE_SERVICE);
throw $yopServiceException;
}
}
BaseResponseUnMarshaller::__init();

View File

@@ -0,0 +1,17 @@
<?php
namespace Yeepay\Yop\Sdk\Model\Transform;
use Yeepay\Yop\Sdk\Internal\Request;
use Yeepay\Yop\Sdk\Model\BaseRequest;
interface RequestMarshaller
{
/**
* @param BaseRequest $request
* @return Request
*/
public function marshal($request);
}

View File

@@ -0,0 +1,132 @@
<?php
namespace Yeepay\Yop\Sdk\Model\Transform;
use Yeepay\Yop\Sdk\Auth\Encryptor;
use Yeepay\Yop\Sdk\Auth\Signer;
use Yeepay\Yop\Sdk\Auth\SignOptions;
class ResponseUnMarshalParams
{
/**
* @var resource
*/
private $publicKey;
/**
* @var Signer
*/
private $signer;
/**
* @var SignOptions
*/
private $signOptions;
/**
* @var bool
*/
private $needDecrypt;
/**
* @var Encryptor
*/
private $encryptor;
/**
* @return resource
*/
public function getPublicKey()
{
return $this->publicKey;
}
/**
* @param resource $publicKey
* @return ResponseUnMarshalParams
*/
public function setPublicKey($publicKey)
{
$this->publicKey = $publicKey;
return $this;
}
/**
* @return Signer
*/
public function getSigner()
{
return $this->signer;
}
/**
* @param Signer $signer
* @return ResponseUnMarshalParams
*/
public function setSigner($signer)
{
$this->signer = $signer;
return $this;
}
/**
* @return SignOptions
*/
public function getSignOptions()
{
return $this->signOptions;
}
/**
* @param SignOptions $signOptions
* @return ResponseUnMarshalParams
*/
public function setSignOptions($signOptions)
{
$this->signOptions = $signOptions;
return $this;
}
/**
* @return bool
*/
public function isNeedDecrypt()
{
return $this->needDecrypt;
}
/**
* @param bool $needDecrypt
* @return ResponseUnMarshalParams
*/
public function setNeedDecrypt($needDecrypt)
{
$this->needDecrypt = $needDecrypt;
return $this;
}
/**
* @return Encryptor
*/
public function getEncryptor()
{
return $this->encryptor;
}
/**
* @param Encryptor $encryptor
* @return ResponseUnMarshalParams
*/
public function setEncryptor($encryptor)
{
$this->encryptor = $encryptor;
return $this;
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Yeepay\Yop\Sdk\Model\Transform;
use Yeepay\Yop\Sdk\Http\YopHttpResponse;
interface ResponseUnMarshaller
{
/**
* @param $yopHttpResponse YopHttpResponse
* @param ResponseUnMarshalParams $params
* @return mixed
*/
public function unmarshal(YopHttpResponse $yopHttpResponse, ResponseUnMarshalParams $params);
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Yeepay\Yop\Sdk\Model\Transform;
use Yeepay\Yop\Sdk\Exception\YopClientException;
use Yeepay\Yop\Sdk\Http\ContentType;
use Yeepay\Yop\Sdk\Http\HttpStatus;
use Yeepay\Yop\Sdk\Http\YopHttpResponse;
use Yeepay\Yop\Sdk\Model\BaseResponse;
use Yeepay\Yop\Sdk\Model\YosDownloadResponse;
class YosDownloadResponseUnMarshaller extends BaseResponseUnMarshaller
{
/**
* @return YosDownloadResponse
*/
protected function getResponseInstance()
{
return new YosDownloadResponse();
}
protected function handleContent(
YopHttpResponse $yopHttpResponse,
$contentType,
BaseResponse $response,
ResponseUnMarshalParams $params
) {
$statusCode = $yopHttpResponse->getStatusCode();
if ($statusCode / 100 == HttpStatus::SC_OK / 100 && $statusCode != HttpStatus::SC_NO_CONTENT) {
$response->setResult($yopHttpResponse->getContent());
} elseif ($statusCode >= HttpStatus::SC_INTERNAL_SERVER_ERROR && $statusCode != HttpStatus::SC_BAD_GATEWAY) {
$content = $yopHttpResponse->readContent();
if ($params->isNeedDecrypt() && $contentType == ContentType::APPLICATION_JSON) {
$content = $params->getEncryptor()->decrypt($content);
}
$this->handleErrorResponse($content, $response->getMetadata(), $yopHttpResponse);
} else {
throw new YopClientException("Unexpected http statusCode:".$statusCode);
}
}
}

View File

@@ -0,0 +1,128 @@
<?php
namespace Yeepay\Yop\Sdk\Model;
class YopErrorResponse
{
/**
* @var string
*/
private $requestId;
/**
* @var string
*/
private $code;
/**
* @var string
*/
private $message;
/**
* @var string
*/
private $subCode;
/**
* @var string
*/
private $subMessage;
/**
* @return string
*/
public function getRequestId()
{
return $this->requestId;
}
/**
* @param string $requestId
* @return YopErrorResponse
*/
public function setRequestId($requestId)
{
$this->requestId = $requestId;
return $this;
}
/**
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* @param string $code
* @return YopErrorResponse
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @param string $message
* @return YopErrorResponse
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* @return string
*/
public function getSubCode()
{
return $this->subCode;
}
/**
* @param string $subCode
* @return YopErrorResponse
*/
public function setSubCode($subCode)
{
$this->subCode = $subCode;
return $this;
}
/**
* @return string
*/
public function getSubMessage()
{
return $this->subMessage;
}
/**
* @param string $subMessage
* @return YopErrorResponse
*/
public function setSubMessage($subMessage)
{
$this->subMessage = $subMessage;
return $this;
}
}

View File

@@ -0,0 +1,418 @@
<?php
namespace Yeepay\Yop\Sdk\Model;
use DateTime;
class YopResponseMetadata
{
/**
* @var string
*/
private $yopRequestId;
/**
* @var string
*/
private $yopSign;
/**
* @var string
*/
private $yopContentSha256;
/**
* @var string
*/
private $yopVia;
/**
* @var string
*/
private $contentDisposition;
/**
* @var string
*/
private $transferEncoding;
/**
* @var string
*/
private $contentEncoding;
/**
* @var float
*/
private $contentLength = -1;
/**
* @var string
*/
private $contentMd5;
/**
* @var string
*/
private $contentRange;
/**
* @var string
*/
private $contentType;
/**
* @var DateTime
*/
private $date;
/**
* @var string
*/
private $etag;
/**
* @var DateTime
*/
private $expires;
/**
* @var DateTime
*/
private $lastModified;
/**
* @var string
*/
private $server;
/**
* @var string
*/
private $location;
/**
* @return string
*/
public function getYopRequestId()
{
return $this->yopRequestId;
}
/**
* @param string $yopRequestId
* @return YopResponseMetadata
*/
public function setYopRequestId($yopRequestId)
{
$this->yopRequestId = $yopRequestId;
return $this;
}
/**
* @return string
*/
public function getYopSign()
{
return $this->yopSign;
}
/**
* @param string $yopSign
* @return YopResponseMetadata
*/
public function setYopSign($yopSign)
{
$this->yopSign = $yopSign;
return $this;
}
/**
* @return string
*/
public function getYopContentSha256()
{
return $this->yopContentSha256;
}
/**
* @param string $yopContentSha256
* @return YopResponseMetadata
*/
public function setYopContentSha256($yopContentSha256)
{
$this->yopContentSha256 = $yopContentSha256;
return $this;
}
/**
* @return string
*/
public function getYopVia()
{
return $this->yopVia;
}
/**
* @param string $yopVia
* @return YopResponseMetadata
*/
public function setYopVia($yopVia)
{
$this->yopVia = $yopVia;
return $this;
}
/**
* @return string
*/
public function getContentDisposition()
{
return $this->contentDisposition;
}
/**
* @param string $contentDisposition
* @return YopResponseMetadata
*/
public function setContentDisposition($contentDisposition)
{
$this->contentDisposition = $contentDisposition;
return $this;
}
/**
* @return string
*/
public function getTransferEncoding()
{
return $this->transferEncoding;
}
/**
* @param string $transferEncoding
* @return YopResponseMetadata
*/
public function setTransferEncoding($transferEncoding)
{
$this->transferEncoding = $transferEncoding;
return $this;
}
/**
* @return string
*/
public function getContentEncoding()
{
return $this->contentEncoding;
}
/**
* @param string $contentEncoding
* @return YopResponseMetadata
*/
public function setContentEncoding($contentEncoding)
{
$this->contentEncoding = $contentEncoding;
return $this;
}
/**
* @return float
*/
public function getContentLength()
{
return $this->contentLength;
}
/**
* @param float $contentLength
* @return YopResponseMetadata
*/
public function setContentLength($contentLength)
{
$this->contentLength = $contentLength;
return $this;
}
/**
* @return string
*/
public function getContentMd5()
{
return $this->contentMd5;
}
/**
* @param string $contentMd5
* @return YopResponseMetadata
*/
public function setContentMd5($contentMd5)
{
$this->contentMd5 = $contentMd5;
return $this;
}
/**
* @return string
*/
public function getContentRange()
{
return $this->contentRange;
}
/**
* @param string $contentRange
* @return YopResponseMetadata
*/
public function setContentRange($contentRange)
{
$this->contentRange = $contentRange;
return $this;
}
/**
* @return string
*/
public function getContentType()
{
return $this->contentType;
}
/**
* @param string $contentType
* @return YopResponseMetadata
*/
public function setContentType($contentType)
{
$this->contentType = $contentType;
return $this;
}
/**
* @return DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* @param DateTime $date
* @return YopResponseMetadata
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* @return string
*/
public function getEtag()
{
return $this->etag;
}
/**
* @param string $etag
* @return YopResponseMetadata
*/
public function setEtag($etag)
{
$this->etag = $etag;
return $this;
}
/**
* @return DateTime
*/
public function getExpires()
{
return $this->expires;
}
/**
* @param DateTime $expires
* @return YopResponseMetadata
*/
public function setExpires($expires)
{
$this->expires = $expires;
return $this;
}
/**
* @return DateTime
*/
public function getLastModified()
{
return $this->lastModified;
}
/**
* @param DateTime $lastModified
* @return YopResponseMetadata
*/
public function setLastModified($lastModified)
{
$this->lastModified = $lastModified;
return $this;
}
/**
* @return string
*/
public function getServer()
{
return $this->server;
}
/**
* @param string $server
* @return YopResponseMetadata
*/
public function setServer($server)
{
$this->server = $server;
return $this;
}
/**
* @return string
*/
public function getLocation()
{
return $this->location;
}
/**
* @param string $location
* @return YopResponseMetadata
*/
public function setLocation($location)
{
$this->location = $location;
return $this;
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Yeepay\Yop\Sdk\Model;
use GuzzleHttp\Psr7\LazyOpenStream;
use Psr\Http\Message\StreamInterface;
class YosDownloadResponse extends BaseResponse
{
/**
* @var StreamInterface
*/
private $result;
/**
* @return YopResponseMetadata
*/
public function getMetadata()
{
return parent::getMetadata();
}
protected function getMetaDataInstance()
{
return new YosDownloadResponseMetadata();
}
/**
* @param $file string|resource
*/
public function save($file)
{
$sink = is_string($file)
? new LazyOpenStream($file, 'w+')
: \GuzzleHttp\Psr7\stream_for($file);
$contentLength = $this->getMetadata()->getContentLength();
\GuzzleHttp\Psr7\copy_to_stream(
$this->result,
$sink,
(strlen($contentLength) > 0 && (int) $contentLength > 0) ? (int) $contentLength : -1
);
$sink->seek(0);
$this->result->close();
}
function getResultClass()
{
return StreamInterface::class;
}
/**
* @param StreamInterface $result
*/
function setResult($result)
{
$this->result = $result;
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace Yeepay\Yop\Sdk\Model;
class YosDownloadResponseMetadata extends YopResponseMetadata
{
/**
* @var float
*/
private $instanceLength = -1;
/**
* @var string
*/
private $cacheControl;
/**
* @var float
*/
private $appendOffset;
/**
* @return float
*/
public function getInstanceLength()
{
return $this->instanceLength;
}
/**
* @param float $instanceLength
* @return YosDownloadResponseMetadata
*/
public function setInstanceLength($instanceLength)
{
$this->instanceLength = $instanceLength;
return $this;
}
/**
* @return string
*/
public function getCacheControl()
{
return $this->cacheControl;
}
/**
* @param string $cacheControl
* @return YosDownloadResponseMetadata
*/
public function setCacheControl($cacheControl)
{
$this->cacheControl = $cacheControl;
return $this;
}
/**
* @return float
*/
public function getAppendOffset()
{
return $this->appendOffset;
}
/**
* @param float $appendOffset
* @return YosDownloadResponseMetadata
*/
public function setAppendOffset($appendOffset)
{
$this->appendOffset = $appendOffset;
return $this;
}
}