- 框架初始化
 - 安装插件
 - 修复PHP8.4报错
This commit is contained in:
2025-04-19 17:21:20 +08:00
commit c6a4e1f5f6
5306 changed files with 967782 additions and 0 deletions

View File

@@ -0,0 +1,159 @@
<?php
declare(strict_types=1);
namespace Yansongda\Pay\Provider;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\MessageInterface;
use Throwable;
use Yansongda\Pay\Contract\HttpClientInterface;
use Yansongda\Pay\Contract\PluginInterface;
use Yansongda\Pay\Contract\ProviderInterface;
use Yansongda\Pay\Contract\ShortcutInterface;
use Yansongda\Pay\Direction\ArrayDirection;
use Yansongda\Pay\Event;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidConfigException;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Logger;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Pipeline;
use function Yansongda\Pay\should_do_http_request;
abstract class AbstractProvider implements ProviderInterface
{
/**
* @return null|array|Collection|MessageInterface
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function call(string $plugin, ?array $params = [])
{
if (!class_exists($plugin) || !in_array(ShortcutInterface::class, class_implements($plugin))) {
throw new InvalidParamsException(Exception::SHORTCUT_NOT_FOUND, "[{$plugin}] is not incompatible");
}
/* @var ShortcutInterface $money */
$money = Pay::get($plugin);
$plugins = $money->getPlugins($params);
if (empty($params['_no_common_plugins'])) {
$plugins = $this->mergeCommonPlugins($plugins);
}
return $this->pay($plugins, $params);
}
/**
* @return null|array|Collection|MessageInterface
*
* @throws ContainerException
* @throws InvalidParamsException
*/
public function pay(array $plugins, ?array $params)
{
Logger::info('[AbstractProvider] 即将进行 pay 操作', func_get_args());
Event::dispatch(new Event\PayStarted($plugins, $params, null));
$this->verifyPlugin($plugins);
/* @var Pipeline $pipeline */
$pipeline = Pay::make(Pipeline::class);
/* @var Rocket $rocket */
$rocket = $pipeline
->send((new Rocket())->setParams($params)->setPayload(new Collection()))
->through($plugins)
->via('assembly')
->then(fn ($rocket) => $this->ignite($rocket))
;
Event::dispatch(new Event\PayFinish($rocket));
$destination = $rocket->getDestination();
if (ArrayDirection::class === $rocket->getDirection() && $destination instanceof Collection) {
return $destination->toArray();
}
return $destination;
}
/**
* @throws ContainerException
* @throws ServiceNotFoundException
* @throws InvalidResponseException
* @throws InvalidConfigException
*/
public function ignite(Rocket $rocket): Rocket
{
if (!should_do_http_request($rocket->getDirection())) {
return $rocket;
}
/* @var HttpClientInterface $http */
$http = Pay::get(HttpClientInterface::class);
if (!$http instanceof ClientInterface) {
throw new InvalidConfigException(Exception::HTTP_CLIENT_CONFIG_ERROR);
}
Logger::info('[AbstractProvider] 准备请求支付服务商 API', $rocket->toArray());
Event::dispatch(new Event\ApiRequesting($rocket));
try {
$response = $http->sendRequest($rocket->getRadar());
$contents = (string) $response->getBody();
$rocket->setDestination($response->withBody(Utils::streamFor($contents)))
->setDestinationOrigin($response->withBody(Utils::streamFor($contents)))
;
} catch (Throwable $e) {
Logger::error('[AbstractProvider] 请求支付服务商 API 出错', ['message' => $e->getMessage(), 'rocket' => $rocket->toArray(), 'trace' => $e->getTrace()]);
throw new InvalidResponseException(Exception::REQUEST_RESPONSE_ERROR, $e->getMessage(), [], $e);
}
Logger::info('[AbstractProvider] 请求支付服务商 API 成功', ['response' => $response, 'rocket' => $rocket->toArray()]);
Event::dispatch(new Event\ApiRequested($rocket));
return $rocket;
}
abstract public function mergeCommonPlugins(array $plugins): array;
/**
* @throws InvalidParamsException
*/
protected function verifyPlugin(array $plugins): void
{
foreach ($plugins as $plugin) {
if (is_callable($plugin)) {
continue;
}
if ((is_object($plugin)
|| (is_string($plugin) && class_exists($plugin)))
&& in_array(PluginInterface::class, class_implements($plugin))) {
continue;
}
throw new InvalidParamsException(Exception::PLUGIN_ERROR, "[{$plugin}] is not incompatible");
}
}
}

View File

@@ -0,0 +1,178 @@
<?php
declare(strict_types=1);
namespace Yansongda\Pay\Provider;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yansongda\Pay\Event;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Alipay\CallbackPlugin;
use Yansongda\Pay\Plugin\Alipay\LaunchPlugin;
use Yansongda\Pay\Plugin\Alipay\PreparePlugin;
use Yansongda\Pay\Plugin\Alipay\RadarSignPlugin;
use Yansongda\Pay\Plugin\ParserPlugin;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Str;
/**
* @method ResponseInterface app(array $order) APP 支付
* @method Collection pos(array $order) 刷卡支付
* @method Collection scan(array $order) 扫码支付
* @method Collection transfer(array $order) 帐户转账
* @method ResponseInterface wap(array $order) 手机网站支付
* @method ResponseInterface web(array $order) 电脑支付
* @method Collection mini(array $order) 小程序支付
*/
class Alipay extends AbstractProvider
{
public const URL = [
Pay::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
Pay::MODE_SANDBOX => 'https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8',
Pay::MODE_SERVICE => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
];
/**
* @return null|array|Collection|MessageInterface
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function __call(string $shortcut, ?array $params)
{
$plugin = '\\Yansongda\\Pay\\Plugin\\Alipay\\Shortcut\\'.
Str::studly($shortcut).'Shortcut';
return $this->call($plugin, ...$params);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function find($order)
{
$order = is_array($order) ? $order : ['out_trade_no' => $order];
Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
return $this->__call('query', [$order]);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function cancel($order)
{
$order = is_array($order) ? $order : ['out_trade_no' => $order];
Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
return $this->__call('cancel', [$order]);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function close($order)
{
$order = is_array($order) ? $order : ['out_trade_no' => $order];
Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
return $this->__call('close', [$order]);
}
/**
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function refund(array $order)
{
Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
return $this->__call('refund', [$order]);
}
/**
* @param null|array|ServerRequestInterface $contents
*
* @throws ContainerException
* @throws InvalidParamsException
*/
public function callback($contents = null, ?array $params = null): Collection
{
$request = $this->getCallbackParams($contents);
Event::dispatch(new Event\CallbackReceived('alipay', $request->all(), $params, null));
return $this->pay(
[CallbackPlugin::class],
$request->merge($params)->all()
);
}
public function success(): ResponseInterface
{
return new Response(200, [], 'success');
}
public function mergeCommonPlugins(array $plugins): array
{
return array_merge(
[PreparePlugin::class],
$plugins,
[RadarSignPlugin::class],
[LaunchPlugin::class, ParserPlugin::class],
);
}
/**
* @param null|array|ServerRequestInterface $contents
*/
protected function getCallbackParams($contents = null): Collection
{
if (is_array($contents)) {
return Collection::wrap($contents);
}
if ($contents instanceof ServerRequestInterface) {
return Collection::wrap('GET' === $contents->getMethod() ? $contents->getQueryParams() :
$contents->getParsedBody());
}
$request = ServerRequest::fromGlobals();
return Collection::wrap(
array_merge($request->getQueryParams(), $request->getParsedBody())
);
}
}

View File

@@ -0,0 +1,168 @@
<?php
declare(strict_types=1);
namespace Yansongda\Pay\Provider;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yansongda\Pay\Event;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\ParserPlugin;
use Yansongda\Pay\Plugin\Unipay\CallbackPlugin;
use Yansongda\Pay\Plugin\Unipay\LaunchPlugin;
use Yansongda\Pay\Plugin\Unipay\PreparePlugin;
use Yansongda\Pay\Plugin\Unipay\RadarSignPlugin;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Str;
/**
* @method ResponseInterface web(array $order) 电脑支付
*/
class Unipay extends AbstractProvider
{
public const URL = [
Pay::MODE_NORMAL => 'https://gateway.95516.com/',
Pay::MODE_SANDBOX => 'https://gateway.test.95516.com/',
Pay::MODE_SERVICE => 'https://gateway.95516.com',
];
/**
* @return null|array|Collection|MessageInterface
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function __call(string $shortcut, ?array $params)
{
$plugin = '\\Yansongda\\Pay\\Plugin\\Unipay\\Shortcut\\'.
Str::studly($shortcut).'Shortcut';
return $this->call($plugin, ...$params);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function find($order)
{
if (!is_array($order)) {
throw new InvalidParamsException(Exception::UNIPAY_FIND_STRING_NOT_SUPPORTED);
}
Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
return $this->__call('query', [$order]);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function cancel($order)
{
if (!is_array($order)) {
throw new InvalidParamsException(Exception::UNIPAY_CANCEL_STRING_NOT_SUPPORTED);
}
Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
return $this->__call('cancel', [$order]);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws InvalidParamsException
*/
public function close($order)
{
throw new InvalidParamsException(Exception::METHOD_NOT_SUPPORTED, 'Unipay does not support close api');
}
/**
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function refund(array $order)
{
Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
return $this->__call('refund', [$order]);
}
/**
* @param null|array|ServerRequestInterface $contents
*
* @throws ContainerException
* @throws InvalidParamsException
*/
public function callback($contents = null, ?array $params = null): Collection
{
$request = $this->getCallbackParams($contents);
Event::dispatch(new Event\CallbackReceived('unipay', $request->all(), $params, null));
return $this->pay(
[CallbackPlugin::class],
$request->merge($params)->all()
);
}
public function success(): ResponseInterface
{
return new Response(200, [], 'success');
}
public function mergeCommonPlugins(array $plugins): array
{
return array_merge(
[PreparePlugin::class],
$plugins,
[RadarSignPlugin::class],
[LaunchPlugin::class, ParserPlugin::class],
);
}
/**
* @param null|array|ServerRequestInterface $contents
*/
protected function getCallbackParams($contents = null): Collection
{
if (is_array($contents)) {
return Collection::wrap($contents);
}
if ($contents instanceof ServerRequestInterface) {
return Collection::wrap($contents->getParsedBody());
}
$request = ServerRequest::fromGlobals();
return Collection::wrap($request->getParsedBody());
}
}

View File

@@ -0,0 +1,178 @@
<?php
declare(strict_types=1);
namespace Yansongda\Pay\Provider;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yansongda\Pay\Event;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\ParserPlugin;
use Yansongda\Pay\Plugin\Wechat\CallbackPlugin;
use Yansongda\Pay\Plugin\Wechat\LaunchPlugin;
use Yansongda\Pay\Plugin\Wechat\PreparePlugin;
use Yansongda\Pay\Plugin\Wechat\RadarSignPlugin;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Str;
/**
* @method Collection app(array $order) APP 支付
* @method Collection mini(array $order) 小程序支付
* @method Collection mp(array $order) 公众号支付
* @method Collection scan(array $order) 扫码支付
* @method Collection wap(array $order) H5 支付
* @method Collection transfer(array $order) 帐户转账
* @method Collection papay(array $order) 支付时签约(委托代扣)
* @method Collection papayApply(array $order) 申请代扣(委托代扣)
* @method Collection papayContract(array $order) 申请代扣(委托代扣)
*/
class Wechat extends AbstractProvider
{
public const AUTH_TAG_LENGTH_BYTE = 16;
public const MCH_SECRET_KEY_LENGTH_BYTE = 32;
public const URL = [
Pay::MODE_NORMAL => 'https://api.mch.weixin.qq.com/',
Pay::MODE_SANDBOX => 'https://api.mch.weixin.qq.com/sandboxnew/',
Pay::MODE_SERVICE => 'https://api.mch.weixin.qq.com/',
];
/**
* @return null|array|Collection|MessageInterface
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function __call(string $shortcut, ?array $params)
{
$plugin = '\\Yansongda\\Pay\\Plugin\\Wechat\\Shortcut\\'.
Str::studly($shortcut).'Shortcut';
return $this->call($plugin, ...$params);
}
/**
* @param array|string $order
*
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function find($order)
{
$order = is_array($order) ? $order : ['transaction_id' => $order];
Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null));
return $this->__call('query', [$order]);
}
/**
* @param array|string $order
*
* @throws InvalidParamsException
*/
public function cancel($order): void
{
throw new InvalidParamsException(Exception::METHOD_NOT_SUPPORTED, 'Wechat does not support cancel api');
}
/**
* @param array|string $order
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function close($order): void
{
$order = is_array($order) ? $order : ['out_trade_no' => $order];
Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null));
$this->__call('close', [$order]);
}
/**
* @return array|Collection
*
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function refund(array $order)
{
Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null));
return $this->__call('refund', [$order]);
}
/**
* @param null|array|ServerRequestInterface $contents
*
* @throws ContainerException
* @throws InvalidParamsException
*/
public function callback($contents = null, ?array $params = null): Collection
{
$request = $this->getCallbackParams($contents);
Event::dispatch(new Event\CallbackReceived('wechat', clone $request, $params, null));
return $this->pay(
[CallbackPlugin::class],
['request' => $request, 'params' => $params]
);
}
public function success(): ResponseInterface
{
return new Response(
200,
['Content-Type' => 'application/json'],
json_encode(['code' => 'SUCCESS', 'message' => '成功']),
);
}
public function mergeCommonPlugins(array $plugins): array
{
return array_merge(
[PreparePlugin::class],
$plugins,
[RadarSignPlugin::class],
[LaunchPlugin::class, ParserPlugin::class],
);
}
/**
* @param null|array|ServerRequestInterface $contents
*/
protected function getCallbackParams($contents = null): ServerRequestInterface
{
if (is_array($contents) && isset($contents['body'], $contents['headers'])) {
return new ServerRequest('POST', 'http://localhost', $contents['headers'], $contents['body']);
}
if (is_array($contents)) {
return new ServerRequest('POST', 'http://localhost', [], json_encode($contents));
}
if ($contents instanceof ServerRequestInterface) {
return $contents;
}
return ServerRequest::fromGlobals();
}
}