add: add atmos callback api
This commit is contained in:
@@ -28,6 +28,7 @@ INSTALLED_APPS = [
|
||||
'core.apps.accounts',
|
||||
'core.apps.orders',
|
||||
'core.apps.common',
|
||||
'core.apps.payment',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
||||
@@ -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')),
|
||||
]
|
||||
))
|
||||
]
|
||||
|
||||
@@ -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):
|
||||
|
||||
0
core/apps/payment/__init__.py
Normal file
0
core/apps/payment/__init__.py
Normal file
3
core/apps/payment/admin.py
Normal file
3
core/apps/payment/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
core/apps/payment/apps.py
Normal file
6
core/apps/payment/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PaymentConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'core.apps.payment'
|
||||
0
core/apps/payment/migrations/__init__.py
Normal file
0
core/apps/payment/migrations/__init__.py
Normal file
3
core/apps/payment/models.py
Normal file
3
core/apps/payment/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
core/apps/payment/tests.py
Normal file
3
core/apps/payment/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
7
core/apps/payment/urls.py
Normal file
7
core/apps/payment/urls.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import AtmosCallbackApiView
|
||||
|
||||
urlpatterns = [
|
||||
path('callback/', AtmosCallbackApiView.as_view()),
|
||||
]
|
||||
72
core/apps/payment/views.py
Normal file
72
core/apps/payment/views.py
Normal 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
|
||||
)
|
||||
Reference in New Issue
Block a user