Skip to content

Commit

Permalink
feat: select backends by supported currency
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Dec 2, 2024
1 parent 55c5749 commit a87c067
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion weblate_web/payments/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -826,3 +832,4 @@ class ThePay2Bitcoin(ThePay2Card):
description = "Bitcoin (The Pay)"
recurring = False
thepay_method_code = "bitcoin"
supported_currencies: set[str] = {"EUR", "CZK"}
5 changes: 4 additions & 1 deletion weblate_web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a87c067

Please sign in to comment.