Skip to content

Commit

Permalink
Optimized version Dynamic ICPS from hoosnick/payme-pkg-3.0b
Browse files Browse the repository at this point in the history
  • Loading branch information
hoosnick authored Aug 25, 2023
2 parents aeb6022 + 1844042 commit 2f014a2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
12 changes: 6 additions & 6 deletions lib/payme/methods/generate_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/payme/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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)),
Expand All @@ -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(
Expand Down
32 changes: 20 additions & 12 deletions lib/payme/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}"
Expand All @@ -42,15 +42,15 @@ 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)
package_code = models.CharField(max_length=255)
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):
Expand All @@ -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):
Expand All @@ -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
Expand Down
14 changes: 6 additions & 8 deletions lib/payme/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit 2f014a2

Please sign in to comment.