diff --git a/lib/payme/methods/generate_link.py b/lib/payme/methods/generate_link.py index 5590adf..404f985 100644 --- a/lib/payme/methods/generate_link.py +++ b/lib/payme/methods/generate_link.py @@ -27,7 +27,7 @@ class GeneratePayLink: Parameters ---------- order_id: int — The order_id for paying - amount: float — The amount belong to the order + amount: int — The amount belong to the order Returns str — pay link ---------------------- @@ -37,7 +37,7 @@ class GeneratePayLink: https://developer.help.paycom.uz/initsializatsiya-platezhey/ """ order_id: str - amount: float + amount: int def generate_link(self) -> str: """ @@ -131,23 +131,23 @@ def to_qrcode(self, path: str = 'qr-codes', filename: str = None, **kwargs): return image_output_path @staticmethod - def to_tiyin(amount: float) -> float: + def to_tiyin(amount: int) -> int: """ Convert from soum to tiyin. Parameters ---------- - amount: float -> order amount + amount: int -> order amount """ return amount * 100 @staticmethod - def to_soum(amount: float) -> float: + def to_soum(amount: int) -> int: """ Convert from tiyin to soum. Parameters ---------- - amount: float -> order amount + amount: int -> order amount """ return amount / 100 diff --git a/lib/payme/migrations/0001_initial.py b/lib/payme/migrations/0001_initial.py index 7c7c6f9..fba08cd 100644 --- a/lib/payme/migrations/0001_initial.py +++ b/lib/payme/migrations/0001_initial.py @@ -18,7 +18,7 @@ class Migration(migrations.Migration): )), ('discount', models.FloatField(blank=True, null=True)), ('title', models.CharField(max_length=255)), - ('price', models.FloatField(blank=True, null=True)), + ('price', models.BigIntegerField(default=0)), ('count', models.IntegerField(default=1)), ('code', models.CharField(max_length=17)), ('units', models.IntegerField(blank=True, null=True)), @@ -36,7 +36,7 @@ class Migration(migrations.Migration): ('_id', models.CharField(max_length=255, null=True)), ('transaction_id', models.CharField(max_length=255, null=True)), ('order_id', models.BigIntegerField(blank=True, null=True)), - ('amount', models.FloatField(blank=True, null=True)), + ('amount', models.BigIntegerField(blank=True, null=True)), ('time', models.BigIntegerField(blank=True, null=True)), ('perform_time', models.BigIntegerField(default=0, null=True)), ('cancel_time', models.BigIntegerField(default=0, null=True)), @@ -55,7 +55,7 @@ class Migration(migrations.Migration): serialize=False, verbose_name='ID' )), ('title', models.CharField(max_length=255)), - ('price', models.FloatField(default=0)), + ('price', models.BigIntegerField(default=0)), ], ), migrations.CreateModel( diff --git a/lib/payme/models.py b/lib/payme/models.py index 75c2c99..2f7d8c9 100644 --- a/lib/payme/models.py +++ b/lib/payme/models.py @@ -9,7 +9,7 @@ class MerchatTransactionsModel(models.Model): _id = models.CharField(max_length=255, null=True, blank=False) transaction_id = models.CharField(max_length=255, null=True, blank=False) order_id = models.BigIntegerField(null=True, blank=True) - amount = models.FloatField(null=True, blank=True) + amount = models.BigIntegerField(null=True, blank=True) time = models.BigIntegerField(null=True, blank=True) perform_time = models.BigIntegerField(null=True, default=0) cancel_time = models.BigIntegerField(null=True, default=0) @@ -29,7 +29,7 @@ class ShippingDetail(models.Model): That's used for managing shipping """ title = models.CharField(max_length=255) - price = models.FloatField(default=0) + price = models.BigIntegerField(default=0) def __str__(self) -> str: return f"[{self.pk}] {self.title} {self.price}" @@ -42,7 +42,7 @@ class Item(models.Model): """ discount = models.FloatField(null=True, blank=True) title = models.CharField(max_length=255) - price = models.FloatField(null=True, blank=True) + price = models.BigIntegerField(default=0) count = models.IntegerField(default=1) code = models.CharField(max_length=17) units = models.IntegerField(null=True, blank=True) @@ -50,7 +50,7 @@ class Item(models.Model): vat_percent = models.IntegerField(default=0, null=True, blank=True) def __str__(self) -> str: - return f"[{self.id}] {self.title} #{self.code}" + return f"[{self.id}] {self.title} ({self.count} pc.) x {self.price}" class OrderDetail(models.Model): @@ -66,12 +66,22 @@ class OrderDetail(models.Model): ) items = models.ManyToManyField(Item) + @property def get_items_display(self): # pylint: disable=missing-function-docstring - return ', '.join([items.title for items in self.items.all()]) + return ', '.join([items.title for items in self.items.all()[:2]]) + + @property + def get_total_items_price(self) -> int: + # pylint: disable=missing-function-docstring + return self.items.all().aggregate( + total_price=models.Sum( + models.F('price') * models.F('count') + ) + )['total_price'] def __str__(self) -> str: - return f"[{self.pk}] {self.get_items_display()}" + return f"{self.get_items_display}" class DisallowOverrideMetaclass(models.base.ModelBase): @@ -96,18 +106,16 @@ class BaseOrder(models.Model, metaclass=DisallowOverrideMetaclass): Order class \ That's used for managing order process """ - created_at = models.DateTimeField(auto_now_add=True) - updated_at = models.DateTimeField(auto_now=True) - - amount = models.FloatField(null=True, blank=True) detail = models.ForeignKey( OrderDetail, null=True, blank=True, on_delete=models.CASCADE ) - def __str__(self): - return f"ORDER ID: {self.id} - AMOUNT: {self.amount}" + @property + def amount(self): + # pylint: disable=missing-function-docstring + return self.detail.get_total_items_price class Meta: # pylint: disable=missing-class-docstring diff --git a/lib/payme/serializers.py b/lib/payme/serializers.py index 3d36ed4..82fb5fa 100644 --- a/lib/payme/serializers.py +++ b/lib/payme/serializers.py @@ -27,7 +27,7 @@ class Meta: fields: str = "__all__" extra_fields = ['start_date', 'end_date'] - def validate(self, attrs) -> dict: + def validate(self, attrs: dict) -> dict: """ Validate the data given to the MerchatTransactionsModel. """ @@ -36,7 +36,7 @@ def validate(self, attrs) -> dict: order = Order.objects.get( id=attrs['order_id'] ) - if order.amount != float(attrs['amount']): + if order.amount != int(attrs['amount']): raise IncorrectAmount() except IncorrectAmount as error: @@ -45,15 +45,13 @@ def validate(self, attrs) -> dict: return attrs - def validate_amount(self, amount: float) -> float: + def validate_amount(self, amount: int) -> int: """ Validator for Transactions Amount. """ - if amount is None: - raise IncorrectAmount() - - if amount <= float(settings.PAYME.get("PAYME_MIN_AMOUNT", 0)): - raise IncorrectAmount("Payment amount is less than allowed.") + if amount is not None: + if amount <= int(settings.PAYME.get("PAYME_MIN_AMOUNT")): + raise IncorrectAmount("Payment amount is less than allowed.") return amount