| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- declare(strict_types=1);
- namespace Yansongda\Pay\Plugin\Alipay\V2;
- use Closure;
- use Yansongda\Artful\Contract\PluginInterface;
- use Yansongda\Artful\Exception\ContainerException;
- use Yansongda\Artful\Exception\InvalidConfigException;
- use Yansongda\Artful\Exception\ServiceNotFoundException;
- use Yansongda\Artful\Logger;
- use Yansongda\Artful\Rocket;
- use Yansongda\Pay\Exception\Exception;
- use function Yansongda\Pay\get_private_cert;
- use function Yansongda\Pay\get_provider_config;
- class AddPayloadSignaturePlugin implements PluginInterface
- {
- /**
- * @throws ContainerException
- * @throws InvalidConfigException
- * @throws ServiceNotFoundException
- */
- public function assembly(Rocket $rocket, Closure $next): Rocket
- {
- Logger::debug('[Alipay][AddPayloadSignaturePlugin] 插件开始装载', ['rocket' => $rocket]);
- $rocket->mergePayload(['sign' => $this->getSign($rocket)]);
- Logger::info('[Alipay][AddPayloadSignaturePlugin] 插件装载完毕', ['rocket' => $rocket]);
- return $next($rocket);
- }
- /**
- * @throws ContainerException
- * @throws InvalidConfigException
- * @throws ServiceNotFoundException
- */
- protected function getSign(Rocket $rocket): string
- {
- $privateKey = $this->getPrivateKey($rocket->getParams());
- $content = $rocket->getPayload()->sortKeys()->toString();
- openssl_sign($content, $sign, $privateKey, OPENSSL_ALGO_SHA256);
- return base64_encode($sign);
- }
- /**
- * @throws ContainerException
- * @throws InvalidConfigException
- * @throws ServiceNotFoundException
- */
- protected function getPrivateKey(array $params): string
- {
- $privateKey = get_provider_config('alipay', $params)['app_secret_cert'] ?? null;
- if (is_null($privateKey)) {
- throw new InvalidConfigException(Exception::CONFIG_ALIPAY_INVALID, '配置异常: 缺少支付宝配置 -- [app_secret_cert]');
- }
- return get_private_cert($privateKey);
- }
- }
|