Skip to content

Commit

Permalink
Currency backend
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtbn committed May 25, 2024
1 parent 69c09da commit a94e6af
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
4 changes: 3 additions & 1 deletion nxtbn/core/currency/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import importlib
from django.conf import settings


currency_Backend = importlib.import_module('nxtbn.core.currency.currency_backend')
if settings.CURRENCY_BACKEND:
currency_Backend = importlib.import_module(settings.CURRENCY_BACKEND) # Import the currency backend module specified in settings
23 changes: 22 additions & 1 deletion nxtbn/core/currency/abstract_base_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,25 @@ def refresh_rate(self, data):
base_currency=self.base_currency,
target_currency=fetch_data['target_currency'],
defaults={'exchange_rate': fetch_data['exchange_rate']}
)
)

def convert_to_customer_currency(self, target_currency: str, amount: float) -> float:
"""
Convert the given amount from the base currency to the target currency.
Args:
- target_currency: str
- amount: float
Returns:
- float: Amount in the target currency.
"""
try:
exchange_rate = CurrencyExchange.objects.get(
base_currency=self.base_currency,
target_currency=target_currency
).exchange_rate
return amount * exchange_rate
except CurrencyExchange.DoesNotExist:
raise ValueError(f"Exchange rate for {target_currency} not found.")

11 changes: 0 additions & 11 deletions nxtbn/core/currency/utils.py

This file was deleted.

2 changes: 1 addition & 1 deletion nxtbn/payment/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def to_unit(self, amount, **kwargs) -> Decimal:
"""
Adjust the decimal amount by multiplying it by 100 and return as an integer.
Args:
amount (Decimal): The amount to be adjusted.
amount (int): The amount to be adjusted.
Returns:
int: The adjusted amount as an integer.
"""
Expand Down
4 changes: 3 additions & 1 deletion nxtbn/product/api/storefront/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from nxtbn.filemanager.api.dashboard.serializers import ImageSerializer
from nxtbn.product.models import Product, Collection, Category, ProductVariant


class CategorySerializer(RecursiveCategorySerializer):
pass

Expand All @@ -27,8 +28,9 @@ def get_price_in_customer_currency(self, obj): # TODO: Implement logic for taxcl
if not settings.IS_MULTI_CURRENCY: # If site is in single currency, no currenyc required
return obj.price
else:
from nxtbn.core.currency import currency_Backend
currency_code = self.context.get('request').currency
rate = CurrencyExchange.objects.get(base_currency=settings.BASE_CURRENCY, target_currency=currency_code).exchange_rate
rate = currency_Backend().convert_to_customer_currency(currency_code, obj.price)
return rate

class ProductSerializer(serializers.ModelSerializer):
Expand Down

0 comments on commit a94e6af

Please sign in to comment.