StartPlugin.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Pay\Plugin\Unipay\Open;
  4. use Closure;
  5. use Yansongda\Artful\Contract\ConfigInterface;
  6. use Yansongda\Artful\Contract\PluginInterface;
  7. use Yansongda\Artful\Exception\ContainerException;
  8. use Yansongda\Artful\Exception\InvalidConfigException;
  9. use Yansongda\Artful\Exception\ServiceNotFoundException;
  10. use Yansongda\Artful\Logger;
  11. use Yansongda\Artful\Rocket;
  12. use Yansongda\Pay\Exception\Exception;
  13. use Yansongda\Pay\Pay;
  14. use function Yansongda\Pay\get_provider_config;
  15. use function Yansongda\Pay\get_tenant;
  16. class StartPlugin implements PluginInterface
  17. {
  18. /**
  19. * @throws ContainerException
  20. * @throws ServiceNotFoundException
  21. * @throws InvalidConfigException
  22. */
  23. public function assembly(Rocket $rocket, Closure $next): Rocket
  24. {
  25. Logger::debug('[Unipay][StartPlugin] 插件开始装载', ['rocket' => $rocket]);
  26. $params = $rocket->getParams();
  27. $config = get_provider_config('unipay', $params);
  28. $tenant = get_tenant($params);
  29. $rocket->mergePayload(array_merge($params, [
  30. '_unpack_raw' => true,
  31. 'certId' => $this->getCertId($tenant, $config),
  32. ]));
  33. Logger::info('[Unipay][StartPlugin] 插件装载完毕', ['rocket' => $rocket]);
  34. return $next($rocket);
  35. }
  36. /**
  37. * @throws ContainerException
  38. * @throws InvalidConfigException
  39. * @throws ServiceNotFoundException
  40. */
  41. public function getCertId(string $tenant, array $config): string
  42. {
  43. if (!empty($config['certs']['cert_id'])) {
  44. return $config['certs']['cert_id'];
  45. }
  46. $certs = $this->getCerts($config);
  47. $ssl = openssl_x509_parse($certs['cert'] ?? '');
  48. if (false === $ssl) {
  49. throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 解析银联 `mch_cert_path` 失败,请检查参数是否正确');
  50. }
  51. $certs['cert_id'] = $ssl['serialNumber'] ?? '';
  52. Pay::get(ConfigInterface::class)->set('unipay.'.$tenant.'.certs', $certs);
  53. return $certs['cert_id'];
  54. }
  55. /**
  56. * @return array ['cert' => 公钥, 'pkey' => 私钥, 'extracerts' => array]
  57. *
  58. * @throws InvalidConfigException
  59. */
  60. protected function getCerts(array $config): array
  61. {
  62. $path = $config['mch_cert_path'] ?? null;
  63. $password = $config['mch_cert_password'] ?? null;
  64. if (is_null($path) || is_null($password)) {
  65. throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 缺少银联配置 -- [mch_cert_path] or [mch_cert_password]');
  66. }
  67. if (false === openssl_pkcs12_read(file_get_contents($path), $certs, $password)) {
  68. throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 读取银联 `mch_cert_path` 失败,请确认参数是否正确');
  69. }
  70. return $certs;
  71. }
  72. }