Skip to content

Commit

Permalink
fixed mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChogirmaliYigit committed Jan 21, 2024
1 parent e8c7ef6 commit 8eecd86
Show file tree
Hide file tree
Showing 31 changed files with 401 additions and 430 deletions.
3 changes: 0 additions & 3 deletions .env.example

This file was deleted.

11 changes: 7 additions & 4 deletions lib/payme/admin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from django.contrib import admin
from payme.models import (
FiscalData, MerchantTransactionsModel, PaymeItem,
PaymeOrder, ShippingDetail, OrderItem
FiscalData,
MerchantTransactionsModel,
PaymeItem,
PaymeOrder,
ShippingDetail,
OrderItem,
)

admin.site.register(MerchantTransactionsModel)
admin.site.register(
[
MerchantTransactionsModel,
FiscalData,
PaymeItem,
PaymeOrder,
OrderItem,
ShippingDetail
ShippingDetail,
]
)
5 changes: 3 additions & 2 deletions lib/payme/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ class PaymeConfig(AppConfig):
PaymeConfig AppConfig \
That is used to configure the payme application with django settings.
"""
default_auto_field = 'django.db.models.BigAutoField'
name = 'payme'

default_auto_field = "django.db.models.BigAutoField"
name = "payme"
21 changes: 8 additions & 13 deletions lib/payme/cards/subscribe_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ class PaymeSubscribeCards:
-------------------------
https://developer.help.paycom.uz/metody-subscribe-api/
"""

def __init__(
self,
base_url: str,
paycom_id: str,
timeout=5
self, base_url: str, paycom_id: str, timeout=5
) -> "PaymeSubscribeCards":
self.base_url: str = base_url
self.timeout: int = timeout
Expand Down Expand Up @@ -78,7 +76,7 @@ def cards_create(self, number: str, expire: str, save: bool = True) -> dict:
"expire": expire,
},
"save": save,
}
},
}
return self.__request(to_json(**data))

Expand All @@ -95,10 +93,10 @@ def card_get_verify_code(self, token: str) -> dict:
https://developer.help.paycom.uz/metody-subscribe-api/cards.get_verify_code
"""
data: dict = {
"method": self.__methods.get('cards_get_verify_code'),
"method": self.__methods.get("cards_get_verify_code"),
"params": {
"token": token,
}
},
}
return self.__request(to_json(**data))

Expand All @@ -117,10 +115,7 @@ def cards_verify(self, verify_code: str, token: str) -> dict:
"""
data: dict = {
"method": self.__methods.get("cards_verify"),
"params": {
"token": token,
"code": verify_code
}
"params": {"token": token, "code": verify_code},
}
return self.__request(to_json(**data))

Expand All @@ -140,7 +135,7 @@ def cards_check(self, token: str) -> dict:
"method": self.__methods.get("cards_check"),
"params": {
"token": token,
}
},
}

return self.__request(to_json(**data))
Expand All @@ -161,6 +156,6 @@ def cards_remove(self, token: str) -> dict:
"method": self.__methods.get("cards_remove"),
"params": {
"token": token,
}
},
}
return self.__request(to_json(**data))
1 change: 1 addition & 0 deletions lib/payme/decorators/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def payme_request(func):
"""
Payme request decorator.
"""

@functools.wraps(func)
def wrapper(self, data):
response = None
Expand Down
20 changes: 13 additions & 7 deletions lib/payme/errors/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class BasePaymeException(APIException):
"""
BasePaymeException it's APIException.
"""

status_code = 200
error_code = None
message = None
Expand All @@ -15,7 +16,7 @@ def __init__(self, error_message: str = None):
"error": {
"code": self.error_code,
"message": self.message,
"data": error_message
"data": error_message,
}
}
self.detail = detail
Expand All @@ -26,6 +27,7 @@ class PermissionDenied(BasePaymeException):
PermissionDenied APIException \
That is raised when the client is not allowed to server.
"""

status_code = 200
error_code = -32504
message = "Permission denied"
Expand All @@ -36,22 +38,24 @@ class MethodNotFound(BasePaymeException):
MethodNotFound APIException \
That is raised when the method does not exist.
"""

status_code = 405
error_code = -32601
message = 'Method not found'
message = "Method not found"


class TooManyRequests(BasePaymeException):
"""
TooManyRequests APIException \
That is raised when the request exceeds the limit.
"""

status_code = 200
error_code = -31099
message = {
"uz": "Buyurtma tolovni amalga oshirish jarayonida",
"ru": "Транзакция в очереди",
"en": "Order payment status is queued"
"en": "Order payment status is queued",
}


Expand All @@ -60,12 +64,13 @@ class IncorrectAmount(BasePaymeException):
IncorrectAmount APIException \
That is raised when the amount is not incorrect.
"""

status_code = 200
error_code = -31001
message = {
'ru': 'Неверная сумма',
'uz': "Noto'g'ri qiymat",
'en': 'Incorrect amount',
"ru": "Неверная сумма",
"uz": "Noto'g'ri qiymat",
"en": "Incorrect amount",
}


Expand All @@ -74,12 +79,13 @@ class PerformTransactionDoesNotExist(BasePaymeException):
PerformTransactionDoesNotExist APIException \
That is raised when a transaction does not exist or deleted.
"""

status_code = 200
error_code = -31050
message = {
"uz": "Buyurtma topilmadi",
"ru": "Заказ не существует",
"en": "Order does not exists"
"en": "Order does not exists",
}


Expand Down
9 changes: 4 additions & 5 deletions lib/payme/methods/cancel_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ class CancelTransaction:

@transaction.atomic
def __call__(self, params: dict):
clean_data: dict = MTMS.get_validated_data(
params=params
)
clean_data: dict = MTMS.get_validated_data(params=params)
try:
with transaction.atomic():
transactions: MerchantTransactionsModel = \
transactions: MerchantTransactionsModel = (
MerchantTransactionsModel.objects.filter(
_id=clean_data.get('_id'),
_id=clean_data.get("_id"),
).first()
)
if transactions.cancel_time == 0:
transactions.cancel_time = int(time.time() * 1000)
if transactions.perform_time == 0:
Expand Down
16 changes: 4 additions & 12 deletions lib/payme/methods/check_perform_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@ class CheckPerformTransaction:
-------------------------
https://developer.help.paycom.uz/metody-merchant-api/checktransaction
"""

def __call__(self, params: dict) -> tuple:
serializer = MerchantTransactionsModelSerializer(
data=get_params(params)
)
serializer = MerchantTransactionsModelSerializer(data=get_params(params))
serializer.is_valid(raise_exception=True)

order = Order.objects.get(
pk=serializer.validated_data.get('order_id')
)
order = Order.objects.get(pk=serializer.validated_data.get("order_id"))
detail = clean_empty(order.to_detail())

response = {
"result": {
"allow": True,
"detail": detail
}
}
response = {"result": {"allow": True, "detail": detail}}

return None, response
12 changes: 5 additions & 7 deletions lib/payme/methods/check_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ class CheckTransaction:
-------------------------
https://developer.help.paycom.uz/metody-merchant-api/checkperformtransaction
"""

def __call__(self, params: dict) -> tuple:
clean_data: dict = MTMS.get_validated_data(
params=params
)
clean_data: dict = MTMS.get_validated_data(params=params)

try:
transaction = \
MerchantTransactionsModel.objects.get(
_id=clean_data.get("_id"),
)
transaction = MerchantTransactionsModel.objects.get(
_id=clean_data.get("_id"),
)
response = {
"result": {
"create_time": int(transaction.created_at_ms),
Expand Down
23 changes: 10 additions & 13 deletions lib/payme/methods/create_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ class CreateTransaction:
-------------------------
https://developer.help.paycom.uz/metody-merchant-api/createtransaction
"""

def __call__(self, params: dict) -> tuple:
serializer = MerchantTransactionsModelSerializer(
data=get_params(params)
)
serializer = MerchantTransactionsModelSerializer(data=get_params(params))
serializer.is_valid(raise_exception=True)
order_id = serializer.validated_data.get("order_id")

Expand All @@ -39,14 +38,13 @@ def __call__(self, params: dict) -> tuple:
raise TooManyRequests() from error

if transaction is None:
transaction, _ = \
MerchantTransactionsModel.objects.get_or_create(
_id=serializer.validated_data.get('_id'),
order_id=serializer.validated_data.get('order_id'),
transaction_id=uuid.uuid4(),
amount=serializer.validated_data.get('amount'),
created_at_ms=int(time.time() * 1000),
)
transaction, _ = MerchantTransactionsModel.objects.get_or_create(
_id=serializer.validated_data.get("_id"),
order_id=serializer.validated_data.get("order_id"),
transaction_id=uuid.uuid4(),
amount=serializer.validated_data.get("amount"),
created_at_ms=int(time.time() * 1000),
)

if transaction:
response: dict = {
Expand All @@ -61,8 +59,7 @@ def __call__(self, params: dict) -> tuple:

@staticmethod
def _convert_ms_to_datetime(time_ms: int) -> datetime:
"""Use this format to convert from time ms to datetime format.
"""
"""Use this format to convert from time ms to datetime format."""
readable_datetime = datetime.datetime.fromtimestamp(time_ms / 1000)

return readable_datetime
Loading

0 comments on commit 8eecd86

Please sign in to comment.