LaunchPlugin.php 1.5 KB

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