VerifySignaturePlugin.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Pay\Plugin\Unipay\Open;
  4. use Closure;
  5. use Yansongda\Artful\Contract\PluginInterface;
  6. use Yansongda\Artful\Exception\ContainerException;
  7. use Yansongda\Artful\Exception\InvalidConfigException;
  8. use Yansongda\Artful\Exception\ServiceNotFoundException;
  9. use Yansongda\Artful\Logger;
  10. use Yansongda\Artful\Rocket;
  11. use Yansongda\Pay\Exception\InvalidSignException;
  12. use Yansongda\Supports\Collection;
  13. use function Yansongda\Artful\should_do_http_request;
  14. use function Yansongda\Pay\get_provider_config;
  15. use function Yansongda\Pay\verify_unipay_sign;
  16. class VerifySignaturePlugin implements PluginInterface
  17. {
  18. /**
  19. * @throws ContainerException
  20. * @throws InvalidConfigException
  21. * @throws InvalidSignException
  22. * @throws ServiceNotFoundException
  23. */
  24. public function assembly(Rocket $rocket, Closure $next): Rocket
  25. {
  26. /* @var Rocket $rocket */
  27. $rocket = $next($rocket);
  28. Logger::debug('[Unipay][VerifySignaturePlugin] 插件开始装载', ['rocket' => $rocket]);
  29. if (!should_do_http_request($rocket->getDirection())) {
  30. return $rocket;
  31. }
  32. $destination = $rocket->getDestination();
  33. if (!$destination instanceof Collection) {
  34. return $rocket;
  35. }
  36. $params = $rocket->getParams();
  37. $config = get_provider_config('unipay', $params);
  38. verify_unipay_sign(
  39. $config,
  40. $destination->except('signature')->sortKeys()->toString(),
  41. $destination->get('signature', ''),
  42. $destination->get('signPubKeyCert')
  43. );
  44. Logger::info('[Unipay][VerifySignaturePlugin] 插件装载完毕', ['rocket' => $rocket]);
  45. return $rocket;
  46. }
  47. }