add: add atmos callback api

This commit is contained in:
behruz-dev
2025-08-27 13:13:19 +05:00
parent 3923644f77
commit e4ff61c4bd
11 changed files with 97 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ INSTALLED_APPS = [
'core.apps.accounts',
'core.apps.orders',
'core.apps.common',
'core.apps.payment',
]
MIDDLEWARE = [

View File

@@ -32,6 +32,7 @@ urlpatterns = [
path('accounts/', include('core.apps.accounts.urls')),
path('common/', include('core.apps.common.urls')),
path('orders/', include('core.apps.orders.urls')),
path('payment/', include('core.apps.payment.urls')),
]
))
]

View File

@@ -2,6 +2,7 @@ from rest_framework import generics
from rest_framework.response import Response
from core.apps.common import models, serializers
from core.apps.payment.views import get_client_ip
class SiteConfigApiView(generics.GenericAPIView):

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class PaymentConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core.apps.payment'

View File

View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,7 @@
from django.urls import path
from .views import AtmosCallbackApiView
urlpatterns = [
path('callback/', AtmosCallbackApiView.as_view()),
]

View File

@@ -0,0 +1,72 @@
import hashlib
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from core.apps.orders.models import Order
API_KEY = "ATMOS_API_KEY"
ALLOWED_ATMOS_IPS = ["185.8.212.47"]
def get_client_ip(request):
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
if x_forwarded_for:
ip = x_forwarded_for.split(",")[0]
else:
ip = request.META.get("REMOTE_ADDR")
return ip
class AtmosCallbackApiView(APIView):
authentication_classes = []
permission_classes = []
def post(self, request):
client_ip = get_client_ip(request)
if client_ip not in ALLOWED_ATMOS_IPS:
return Response({"status": 0, "message": "IP ruxsat etilmagan"}, status=403)
data = request.data
if not data:
return Response(
{'success': 0, "message": "Request body required"},
status=status.HTTP_200_OK
)
store_id = data.get("store_id")
transaction_id = data.get("transaction_id")
invoice = data.get("invoice")
amount = data.get("amount")
sign = data.get("sign")
check_string = f"{store_id}{transaction_id}{invoice}{amount}{API_KEY}"
generated_sign = hashlib.sha256(check_string.encode()).hexdigest()
if generated_sign != sign:
return Response(
{"status": 0, "message": f"Инвойс с номером {invoice} отсутствует в системе"},
status=status.HTTP_200_OK
)
try:
order = Order.objects.get(id=invoice)
except Order.DoesNotExist:
return Response(
{"status": 0, "message": f"Инвойс с номером {invoice} отсутствует в системе"},
status=status.HTTP_200_OK
)
if str(order.total_price) != str(amount):
return Response(
{"status": 0, "message": f"Инвойс с номером {invoice} отсутствует в системе"},
status=status.HTTP_200_OK
)
order.is_paid = True
order.save()
return Response(
{"status": 1, "message": "Успешно"},
status=status.HTTP_200_OK
)