文件
yjyf/server/addons/epay/library/v3/Yansongda/Pay/Traits/GetUnipayCerts.php
YuanWen 78d45eea74 chore: 新增epay和cos插件相关类库及配置文件
- 新增epay v2、v3、v3new版本的支付类库及证书文件
- 新增cos云存储类库文件
- 调整文件上传保存格式为按分类存储
- 修复会员、规则、类目等语言翻译键名
- 新增小程序图标等静态资源文件
2026-11-04 09:33:57 +08:00

62 行
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Yansongda\Pay\Traits;
use Yansongda\Pay\Contract\ConfigInterface;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidConfigException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Pay;
trait GetUnipayCerts
{
/**
* @throws ContainerException
* @throws InvalidConfigException
* @throws ServiceNotFoundException
*/
public function getCertId(string $tenant, array $config): string
{
if (!empty($config['certs']['cert_id'])) {
return $config['certs']['cert_id'];
}
$certs = $this->getCerts($config);
$ssl = openssl_x509_parse($certs['cert'] ?? '');
if (false === $ssl) {
throw new InvalidConfigException(Exception::UNIPAY_CONFIG_ERROR, 'Parse `mch_cert_path` Error');
}
$certs['cert_id'] = $ssl['serialNumber'] ?? '';
Pay::get(ConfigInterface::class)->set('unipay.'.$tenant.'.certs', $certs);
return $certs['cert_id'];
}
/**
* @return array ['cert' => 公钥, 'pkey' => 私钥, 'extracerts' => array]
*
* @throws InvalidConfigException
*/
protected function getCerts(array $config): array
{
$path = $config['mch_cert_path'] ?? null;
$password = $config['mch_cert_password'] ?? null;
if (is_null($path) || is_null($password)) {
throw new InvalidConfigException(Exception::UNIPAY_CONFIG_ERROR, 'Missing Unipay Config -- [mch_cert_path] or [mch_cert_password]');
}
if (false === openssl_pkcs12_read(file_get_contents($path), $certs, $password)) {
throw new InvalidConfigException(Exception::UNIPAY_CONFIG_ERROR, 'Read `mch_cert_path` Error');
}
return $certs;
}
}