代码初始化
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Pay\Traits;
|
||||
|
||||
use Yansongda\Pay\Exception\ContainerException;
|
||||
use Yansongda\Pay\Exception\Exception;
|
||||
use Yansongda\Pay\Exception\InvalidConfigException;
|
||||
use Yansongda\Pay\Exception\InvalidParamsException;
|
||||
use Yansongda\Pay\Exception\InvalidResponseException;
|
||||
use Yansongda\Pay\Exception\ServiceNotFoundException;
|
||||
|
||||
use function Yansongda\Pay\get_wechat_config;
|
||||
use function Yansongda\Pay\reload_wechat_public_certs;
|
||||
|
||||
trait HasWechatEncryption
|
||||
{
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws InvalidConfigException
|
||||
* @throws InvalidParamsException
|
||||
* @throws InvalidResponseException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public function loadSerialNo(array $params): array
|
||||
{
|
||||
$config = get_wechat_config($params);
|
||||
|
||||
if (empty($config['wechat_public_cert_path'])) {
|
||||
reload_wechat_public_certs($params);
|
||||
|
||||
$config = get_wechat_config($params);
|
||||
}
|
||||
|
||||
if (empty($params['_serial_no'])) {
|
||||
mt_srand();
|
||||
$params['_serial_no'] = strval(array_rand($config['wechat_public_cert_path']));
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws InvalidParamsException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public function getPublicKey(array $params, string $serialNo): string
|
||||
{
|
||||
$config = get_wechat_config($params);
|
||||
|
||||
$publicKey = $config['wechat_public_cert_path'][$serialNo] ?? null;
|
||||
|
||||
if (empty($publicKey)) {
|
||||
throw new InvalidParamsException(Exception::WECHAT_SERIAL_NO_NOT_FOUND, 'Wechat serial no not found: '.$serialNo);
|
||||
}
|
||||
|
||||
return $publicKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Pay\Traits;
|
||||
|
||||
use Yansongda\Pay\Exception\ContainerException;
|
||||
use Yansongda\Pay\Exception\ServiceNotFoundException;
|
||||
use Yansongda\Pay\Pay;
|
||||
use Yansongda\Pay\Rocket;
|
||||
|
||||
use function Yansongda\Pay\get_alipay_config;
|
||||
|
||||
trait SupportServiceProviderTrait
|
||||
{
|
||||
/**
|
||||
* @throws ContainerException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
protected function loadAlipayServiceProvider(Rocket $rocket): void
|
||||
{
|
||||
$params = $rocket->getParams();
|
||||
$config = get_alipay_config($params);
|
||||
$serviceProviderId = $config['service_provider_id'] ?? null;
|
||||
|
||||
if (Pay::MODE_SERVICE !== ($config['mode'] ?? Pay::MODE_NORMAL)
|
||||
|| empty($serviceProviderId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$rocket->mergeParams([
|
||||
'extend_params' => array_merge($params['extend_params'] ?? [], ['sys_service_provider_id' => $serviceProviderId]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user