feat: enable payment provider for admin only
This commit is contained in:
@@ -23,6 +23,7 @@ from .base import (
|
||||
ServiceFactoryContext,
|
||||
WebAppPaymentContext,
|
||||
provider_env_file,
|
||||
provider_runtime_enabled,
|
||||
)
|
||||
from .shared import (
|
||||
HttpClientMixin,
|
||||
@@ -66,7 +67,9 @@ class PlategaConfig(ProviderEnvConfig):
|
||||
SECRET: Optional[str] = None
|
||||
PAYMENT_METHOD: int = Field(default=2)
|
||||
SBP_ENABLED: bool = Field(default=False)
|
||||
SBP_ADMIN_ONLY_ENABLED: bool = Field(default=False)
|
||||
CRYPTO_ENABLED: bool = Field(default=False)
|
||||
CRYPTO_ADMIN_ONLY_ENABLED: bool = Field(default=False)
|
||||
SBP_METHOD: int = Field(default=2)
|
||||
CRYPTO_METHOD: int = Field(default=13)
|
||||
RETURN_URL: Optional[str] = None
|
||||
@@ -161,7 +164,15 @@ class PlategaService(HttpClientMixin):
|
||||
|
||||
@property
|
||||
def configured(self) -> bool:
|
||||
return bool(self.config.ENABLED and self.merchant_id and self.secret)
|
||||
return bool(
|
||||
provider_runtime_enabled(
|
||||
self.config,
|
||||
"SBP_ADMIN_ONLY_ENABLED",
|
||||
"CRYPTO_ADMIN_ONLY_ENABLED",
|
||||
)
|
||||
and self.merchant_id
|
||||
and self.secret
|
||||
)
|
||||
|
||||
@property
|
||||
def base_url(self) -> str:
|
||||
@@ -395,17 +406,23 @@ def _resolve_platega_variant(
|
||||
) -> Optional[Tuple[str, int]]:
|
||||
"""Map the callback prefix to (variant, payment_method_id) or ``None`` if disabled."""
|
||||
if callback_prefix == "pay_platega_crypto":
|
||||
if not config.CRYPTO_ENABLED:
|
||||
if not (config.CRYPTO_ENABLED or config.CRYPTO_ADMIN_ONLY_ENABLED):
|
||||
return None
|
||||
return "crypto", config.CRYPTO_METHOD
|
||||
if callback_prefix == "pay_platega_sbp":
|
||||
if not config.SBP_ENABLED:
|
||||
if not (config.SBP_ENABLED or config.SBP_ADMIN_ONLY_ENABLED):
|
||||
return None
|
||||
return "sbp", config.sbp_method_resolved
|
||||
# Legacy "pay_platega:" callback — keep working as SBP.
|
||||
return "sbp", config.sbp_method_resolved
|
||||
|
||||
|
||||
def _platega_spec_for_callback_prefix(callback_prefix: str) -> PaymentProviderSpec:
|
||||
if callback_prefix == "pay_platega_crypto":
|
||||
return CRYPTO_SPEC
|
||||
return SBP_SPEC
|
||||
|
||||
|
||||
@router.callback_query(
|
||||
F.data.startswith("pay_platega_sbp:")
|
||||
| F.data.startswith("pay_platega_crypto:")
|
||||
@@ -429,6 +446,15 @@ async def pay_platega_callback_handler(
|
||||
return
|
||||
|
||||
callback_prefix, _, _ = (callback.data or "").partition(":")
|
||||
spec = _platega_spec_for_callback_prefix(callback_prefix)
|
||||
if not spec.is_available_to_user(
|
||||
settings,
|
||||
user_id=callback.from_user.id,
|
||||
require_configured=False,
|
||||
):
|
||||
await notify_service_unavailable(callback, translator)
|
||||
return
|
||||
|
||||
variant = (
|
||||
_resolve_platega_variant(callback_prefix, platega_service.config)
|
||||
if platega_service
|
||||
@@ -538,11 +564,13 @@ async def _create_webapp_payment(ctx: WebAppPaymentContext, variant: str) -> web
|
||||
if not service or not service.configured:
|
||||
return payment_unavailable()
|
||||
if variant == "platega_crypto":
|
||||
if not service.config.CRYPTO_ENABLED:
|
||||
if not (service.config.CRYPTO_ENABLED or service.config.CRYPTO_ADMIN_ONLY_ENABLED):
|
||||
return payment_unavailable()
|
||||
platega_method_id = service.config.CRYPTO_METHOD
|
||||
else:
|
||||
if variant == "platega_sbp" and not service.config.SBP_ENABLED:
|
||||
if variant == "platega_sbp" and not (
|
||||
service.config.SBP_ENABLED or service.config.SBP_ADMIN_ONLY_ENABLED
|
||||
):
|
||||
return payment_unavailable()
|
||||
platega_method_id = service.config.sbp_method_resolved
|
||||
|
||||
@@ -738,6 +766,8 @@ SBP_SPEC = PaymentProviderSpec(
|
||||
enabled=lambda config: bool(
|
||||
getattr(config, "ENABLED", False) and getattr(config, "SBP_ENABLED", False)
|
||||
),
|
||||
admin_only_enabled=lambda config: bool(getattr(config, "SBP_ADMIN_ONLY_ENABLED", False)),
|
||||
admin_only_config_attr="SBP_ADMIN_ONLY_ENABLED",
|
||||
service_key="platega_service",
|
||||
callback_prefix="pay_platega_sbp",
|
||||
aliases=("platega",),
|
||||
@@ -767,6 +797,8 @@ CRYPTO_SPEC = PaymentProviderSpec(
|
||||
enabled=lambda config: bool(
|
||||
getattr(config, "ENABLED", False) and getattr(config, "CRYPTO_ENABLED", False)
|
||||
),
|
||||
admin_only_enabled=lambda config: bool(getattr(config, "CRYPTO_ADMIN_ONLY_ENABLED", False)),
|
||||
admin_only_config_attr="CRYPTO_ADMIN_ONLY_ENABLED",
|
||||
service_key="platega_service",
|
||||
callback_prefix="pay_platega_crypto",
|
||||
create_webapp_payment=create_crypto_webapp_payment,
|
||||
|
||||
Reference in New Issue
Block a user