From 39cfdd6a0eb75898172ae6eb1e6aa0d34da265e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 2 Dec 2024 11:22:44 +0100 Subject: [PATCH] feat: select backends by supported currency --- weblate_web/payments/backends.py | 9 ++++++++- weblate_web/views.py | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/weblate_web/payments/backends.py b/weblate_web/payments/backends.py index e1e0c55b4..a2691400f 100644 --- a/weblate_web/payments/backends.py +++ b/weblate_web/payments/backends.py @@ -241,11 +241,14 @@ def get_backend(name: str) -> type[Backend]: return backend -def list_backends(exclude_names: set[str] | None = None) -> list[type[Backend]]: +def list_backends( + *, exclude_names: set[str] | None = None, currency: str = "EUR" +) -> list[type[Backend]]: result = [ backend for backend in BACKENDS.values() if (not backend.debug or settings.PAYMENT_DEBUG) + and currency in backend.supported_currencies and (exclude_names is None or backend.name not in exclude_names) ] return sorted(result, key=lambda x: x.name) @@ -270,6 +273,7 @@ class Backend: verbose: StrOrPromise = "" description: str = "" recurring: bool = False + supported_currencies: set[str] = {"EUR", "CZK", "USD", "GBP"} def __init__(self, payment: Payment): select = Payment.objects.filter(pk=payment.pk).select_for_update() @@ -636,6 +640,8 @@ class ThePay2Card(Backend): description = "Payment Card (The Pay)" recurring = True thepay_method_code = "card" + # TODO: consider allowing GBP and USD + supported_currencies: set[str] = {"EUR", "CZK"} @staticmethod def get_headers() -> dict[str, str]: @@ -826,3 +832,4 @@ class ThePay2Bitcoin(ThePay2Card): description = "Bitcoin (The Pay)" recurring = False thepay_method_code = "bitcoin" + supported_currencies: set[str] = {"EUR", "CZK"} diff --git a/weblate_web/views.py b/weblate_web/views.py index a554089d3..0f31d463b 100644 --- a/weblate_web/views.py +++ b/weblate_web/views.py @@ -365,7 +365,10 @@ def get_context_data(self, **kwargs): kwargs["can_pay"] = self.can_pay kwargs["backends"] = [ backend(self.object) - for backend in list_backends(self.object.extra.get("exclude_backends")) + for backend in list_backends( + exclude_names=self.object.extra.get("exclude_backends"), + currency=self.object.get_currency_display(), + ) ] return kwargs