You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
7.1 KiB
PHP
198 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Payment;
|
|
|
|
use App\Models\Configurations;
|
|
use App\Services\Config;
|
|
|
|
class YipayConfig
|
|
{
|
|
public const SECRET_KEY = 'yipay_secret_key';
|
|
|
|
private const DEFAULTS = [
|
|
'yipay_enabled' => 'false',
|
|
'yipay_test_mode' => 'true',
|
|
'yipay_alipay_enabled' => 'true',
|
|
'yipay_wxpay_enabled' => 'true',
|
|
'yipay_gateway_base_url' => 'https://fp.2imc.cn',
|
|
'yipay_callback_base_url' => '',
|
|
'yipay_pid' => '',
|
|
'yipay_order_name' => '账户余额充值',
|
|
'yipay_min_amount' => '1.00',
|
|
'yipay_max_amount' => '10000.00',
|
|
'yipay_request_timeout_seconds' => '15',
|
|
];
|
|
|
|
public static function values()
|
|
{
|
|
$values = self::DEFAULTS;
|
|
if ($values['yipay_callback_base_url'] === '') {
|
|
$values['yipay_callback_base_url'] = rtrim((string) Config::get('baseUrl', ''), '/');
|
|
}
|
|
|
|
try {
|
|
$rows = Configurations::whereIn('name', array_merge(array_keys(self::DEFAULTS), [self::SECRET_KEY]))
|
|
->get()
|
|
->keyBy('name');
|
|
foreach (array_keys(self::DEFAULTS) as $key) {
|
|
if (isset($rows[$key])) {
|
|
$values[$key] = (string) $rows[$key]->value;
|
|
}
|
|
}
|
|
$values['yipay_secret_configured'] = isset($rows[self::SECRET_KEY])
|
|
&& trim((string) $rows[self::SECRET_KEY]->value) !== '';
|
|
} catch (\Throwable $exception) {
|
|
$values['yipay_secret_configured'] = false;
|
|
}
|
|
|
|
foreach (['yipay_enabled', 'yipay_test_mode', 'yipay_alipay_enabled', 'yipay_wxpay_enabled'] as $key) {
|
|
$values[$key] = self::truthy($values[$key]);
|
|
}
|
|
$values['yipay_min_amount'] = PaymentAmount::normalize($values['yipay_min_amount']);
|
|
$values['yipay_max_amount'] = PaymentAmount::normalize($values['yipay_max_amount']);
|
|
$values['yipay_request_timeout_seconds'] = max(3, min(60, (int) $values['yipay_request_timeout_seconds']));
|
|
return $values;
|
|
}
|
|
|
|
public static function runtime($requireEnabled = true)
|
|
{
|
|
$values = self::values();
|
|
if ($requireEnabled && !$values['yipay_enabled']) {
|
|
throw new \RuntimeException('易支付暂未开放');
|
|
}
|
|
foreach (['yipay_gateway_base_url', 'yipay_callback_base_url', 'yipay_pid'] as $key) {
|
|
if (trim((string) $values[$key]) === '') {
|
|
throw new \RuntimeException('易支付配置不完整:' . $key);
|
|
}
|
|
}
|
|
if (!self::isHttpsUrl($values['yipay_gateway_base_url']) || !self::isHttpsUrl($values['yipay_callback_base_url'])) {
|
|
throw new \RuntimeException('易支付网关和回调地址必须使用 HTTPS');
|
|
}
|
|
return array_merge($values, self::callbackCredentials());
|
|
}
|
|
|
|
public static function runtimeForUser($user, $method)
|
|
{
|
|
$values = self::runtime(true);
|
|
if (!self::canUserAccess($user, $values)) {
|
|
throw new \RuntimeException('易支付当前处于测试模式,仅管理员可用');
|
|
}
|
|
$method = self::normalizeMethod($method);
|
|
if (!self::methodEnabled($method, $values)) {
|
|
throw new \RuntimeException('所选易支付方式当前未开放');
|
|
}
|
|
return $values;
|
|
}
|
|
|
|
public static function callbackCredentials()
|
|
{
|
|
$values = self::values();
|
|
if (!preg_match('/^\d{1,20}$/', trim((string) $values['yipay_pid']))) {
|
|
throw new \RuntimeException('易支付商户 PID 尚未正确配置');
|
|
}
|
|
if (!$values['yipay_secret_configured']) {
|
|
throw new \RuntimeException('易支付商户密钥尚未配置');
|
|
}
|
|
$row = Configurations::where('name', self::SECRET_KEY)->first();
|
|
if ($row === null) {
|
|
throw new \RuntimeException('易支付商户密钥尚未配置');
|
|
}
|
|
$secret = YipayCredentialCipher::decrypt((string) $row->value);
|
|
if ($secret === '') {
|
|
throw new \RuntimeException('易支付商户密钥尚未配置');
|
|
}
|
|
return ['yipay_pid' => (string) $values['yipay_pid'], self::SECRET_KEY => $secret];
|
|
}
|
|
|
|
public static function canUserAccess($user, ?array $values = null)
|
|
{
|
|
$values = $values === null ? self::values() : $values;
|
|
if (!self::truthy($values['yipay_enabled'] ?? false)) {
|
|
return false;
|
|
}
|
|
if (!self::truthy($values['yipay_test_mode'] ?? true)) {
|
|
return true;
|
|
}
|
|
return $user !== null && method_exists($user, 'isAdmin') && (bool) $user->isAdmin();
|
|
}
|
|
|
|
public static function visibleMethods($user = null, ?array $values = null)
|
|
{
|
|
$values = $values === null ? self::values() : $values;
|
|
if (!self::canUserAccess($user, $values)) {
|
|
return [];
|
|
}
|
|
$methods = [];
|
|
foreach (['alipay', 'wxpay'] as $method) {
|
|
if (self::methodEnabled($method, $values)) {
|
|
$methods[] = $method;
|
|
}
|
|
}
|
|
return $methods;
|
|
}
|
|
|
|
public static function methodEnabled($method, array $values)
|
|
{
|
|
$method = self::normalizeMethod($method);
|
|
return $method === 'alipay'
|
|
? self::truthy($values['yipay_alipay_enabled'] ?? false)
|
|
: self::truthy($values['yipay_wxpay_enabled'] ?? false);
|
|
}
|
|
|
|
public static function normalizeMethod($method)
|
|
{
|
|
$method = strtolower(trim((string) $method));
|
|
if (!in_array($method, ['alipay', 'wxpay'], true)) {
|
|
throw new \InvalidArgumentException('易支付方式无效');
|
|
}
|
|
return $method;
|
|
}
|
|
|
|
public static function save(array $values, $secret = '')
|
|
{
|
|
$secret = trim((string) $secret);
|
|
$encryptedSecret = $secret === '' ? '' : YipayCredentialCipher::encrypt($secret);
|
|
$connection = (new Configurations())->getConnection();
|
|
$connection->transaction(function () use ($values, $encryptedSecret) {
|
|
foreach (self::DEFAULTS as $key => $default) {
|
|
if (array_key_exists($key, $values)) {
|
|
self::set($key, (string) $values[$key]);
|
|
}
|
|
}
|
|
if ($encryptedSecret !== '') {
|
|
self::set(self::SECRET_KEY, $encryptedSecret);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static function truthy($value)
|
|
{
|
|
return in_array(strtolower(trim((string) $value)), ['1', 'true', 'yes', 'on'], true);
|
|
}
|
|
|
|
public static function isHttpsUrl($value)
|
|
{
|
|
$value = trim((string) $value);
|
|
if (filter_var($value, FILTER_VALIDATE_URL) === false || preg_match('#^https://#i', $value) !== 1) {
|
|
return false;
|
|
}
|
|
$parts = parse_url($value);
|
|
return is_array($parts)
|
|
&& empty($parts['user'])
|
|
&& empty($parts['pass'])
|
|
&& empty($parts['query'])
|
|
&& empty($parts['fragment']);
|
|
}
|
|
|
|
private static function set($name, $value)
|
|
{
|
|
$row = Configurations::where('name', $name)->first();
|
|
if ($row === null) {
|
|
$row = new Configurations();
|
|
$row->name = $name;
|
|
}
|
|
$row->value = $value;
|
|
$row->save();
|
|
}
|
|
}
|