You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ymww_backend/addons/epay/library/v3/Yansongda/Pay/Traits/GetUnipayCerts.php

62 lines
1.8 KiB

1 year ago
<?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;
}
}