Files
boursecu_server/app/Services/Payment/WalletPaymentManager.php
T

98 lines
2.7 KiB
PHP

<?php
namespace App\Services\Payment;
use App\Services\ConfigService;
use App\Services\Payment\Contracts\WalletPaymentGatewayInterface;
use App\Services\Payment\Gateways\MontifyPartnerGateway;
use InvalidArgumentException;
/**
* انتخاب و مدیریت درگاه فعال شارژ کیف پول.
*
* افزودن درگاه جدید:
* 1) کلاس Gateway با WalletPaymentGatewayInterface
* 2) ثبت در map داخل drivers()
* 3) مقدار جدید در setting: wallet_payment_provider
*/
class WalletPaymentManager
{
public const PROVIDER_NONE = 'none';
public const PROVIDER_MONTIFY = 'montify';
public function __construct(private ConfigService $config)
{
}
/**
* @return array<string, class-string<WalletPaymentGatewayInterface>>
*/
public function drivers(): array
{
return [
self::PROVIDER_MONTIFY => MontifyPartnerGateway::class,
// مثال آینده:
// self::PROVIDER_PAYPING => PaypingDirectGateway::class,
];
}
public function configuredProvider(): string
{
$provider = strtolower($this->config->getString('wallet_payment_provider', ''));
if ($provider !== '' && $provider !== self::PROVIDER_NONE) {
return $provider;
}
// سازگاری با سوییچ قدیمی external_payment_enabled
if ($this->config->getBool('external_payment_enabled', false)) {
return self::PROVIDER_MONTIFY;
}
return self::PROVIDER_NONE;
}
public function resolve(?string $provider = null): ?WalletPaymentGatewayInterface
{
$key = strtolower($provider ?: $this->configuredProvider());
if ($key === '' || $key === self::PROVIDER_NONE) {
return null;
}
$drivers = $this->drivers();
if (!isset($drivers[$key])) {
return null;
}
return app($drivers[$key]);
}
public function resolveOrFail(?string $provider = null): WalletPaymentGatewayInterface
{
$gateway = $this->resolve($provider);
if (!$gateway) {
throw new InvalidArgumentException('درگاه پرداخت پیکربندی نشده است');
}
return $gateway;
}
/** فقط برای UI کلاینت — بدون افشای نوع/credential */
public function isOnlineDepositAvailable(): bool
{
$gateway = $this->resolve();
return $gateway !== null && $gateway->isAvailable();
}
/**
* @return array{online_enabled: bool}
*/
public function publicOptions(): array
{
return [
'online_enabled' => $this->isOnlineDepositAvailable(),
];
}
}