Files
2026-09-15 10:35:56 +08:00

123 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Oceanpayment\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Store\Model\ScopeInterface;
class Config
{
public const METHOD_CODE = 'esun_oceanpayment';
public const ONEPAGE_METHOD_CODE = 'esun_oceanpayment_onepage';
public const LEGACY_METHOD_CODE = 'evebit_paymentqianhai';
public const SANDBOX_ENDPOINT = 'https://test-secure.oceanpayment.com/gateway/service/sendTrade';
public const PRODUCTION_ENDPOINT = 'https://secure.oceanpayment.com/gateway/service/sendTrade';
public static function isSupportedMethod(string $method): bool
{
return in_array(
$method,
[self::METHOD_CODE, self::ONEPAGE_METHOD_CODE, self::LEGACY_METHOD_CODE],
true
);
}
private const PATH_PREFIX = 'payment/';
public function __construct(
private readonly ScopeConfigInterface $scopeConfig,
private readonly EncryptorInterface $encryptor
) {
}
public function getValue(
string $field,
?int $storeId = null,
string $method = self::METHOD_CODE
): string {
return trim((string)$this->scopeConfig->getValue(
$this->getPath($method, $field),
ScopeInterface::SCOPE_STORE,
$storeId
));
}
public function getSecret(
string $field,
?int $storeId = null,
string $method = self::METHOD_CODE
): string {
$value = $this->getValue($field, $storeId, $method);
if ($value === '') {
return '';
}
// Magento's current encrypted config format starts with key/cipher version segments.
// Values supplied through environment deployment configuration remain plaintext.
if (!preg_match('/^\d+:\d+:/', $value)) {
return $value;
}
try {
$decrypted = $this->encryptor->decrypt($value);
return $decrypted !== '' ? $decrypted : $value;
} catch (\Throwable) {
// Environment-provided sensitive values may already be plaintext.
return $value;
}
}
public function isActive(?int $storeId = null, string $method = self::METHOD_CODE): bool
{
return $this->scopeConfig->isSetFlag(
$this->getPath($method, 'active'),
ScopeInterface::SCOPE_STORE,
$storeId
);
}
public function isSandbox(?int $storeId = null, string $method = self::METHOD_CODE): bool
{
return $this->scopeConfig->isSetFlag(
$this->getPath($method, 'sandbox'),
ScopeInterface::SCOPE_STORE,
$storeId
);
}
public function isDebug(?int $storeId = null, string $method = self::METHOD_CODE): bool
{
return $this->scopeConfig->isSetFlag(
$this->getPath($method, 'debug'),
ScopeInterface::SCOPE_STORE,
$storeId
);
}
public function getEndpoint(?int $storeId = null, string $method = self::METHOD_CODE): string
{
return $this->isSandbox($storeId, $method) ? self::SANDBOX_ENDPOINT : self::PRODUCTION_ENDPOINT;
}
public function isConfigured(?int $storeId = null, string $method = self::METHOD_CODE): bool
{
return $this->getSecret('account', $storeId, $method) !== ''
&& $this->getSecret('terminal', $storeId, $method) !== ''
&& $this->getSecret('secure_code', $storeId, $method) !== ''
&& ($method !== self::ONEPAGE_METHOD_CODE
|| $this->getSecret('public_key', $storeId, $method) !== '');
}
private function getPath(string $method, string $field): string
{
$method = $method === self::LEGACY_METHOD_CODE ? self::METHOD_CODE : $method;
if (!in_array($method, [self::METHOD_CODE, self::ONEPAGE_METHOD_CODE], true)) {
throw new \InvalidArgumentException('Unsupported Oceanpayment method code.');
}
return self::PATH_PREFIX . $method . '/' . $field;
}
}