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