From 94ab37b09790e8a2f434605aed9553c2b627f770 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Sun, 28 Dec 2025 10:14:46 +0330 Subject: [PATCH] RU IP transactions get payment-links now --- apps/transaction/views.py | 30 ++++++++++++++++++++++++++++-- utils/ip_helper.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 utils/ip_helper.py diff --git a/apps/transaction/views.py b/apps/transaction/views.py index 7232d9e..8b7d7f6 100644 --- a/apps/transaction/views.py +++ b/apps/transaction/views.py @@ -21,6 +21,8 @@ from apps.transaction.doc import ( doc_create_transaction ) +from utils.ip_helper import get_client_ip, get_country_code + class TransactionParticipantCreateView(generics.CreateAPIView): @@ -31,7 +33,7 @@ class TransactionParticipantCreateView(generics.CreateAPIView): @swagger_auto_schema( operation_description=doc_create_transaction() ) - def create(self, request, *args, **kwargs): + def create(self, request, *args, **kwargs): user = request.user course_slug = self.kwargs.get('slug') # Get the slug from the URL try: @@ -64,10 +66,34 @@ class TransactionParticipantCreateView(generics.CreateAPIView): transaction_participant = serializer.save(user=user, course=course, price=course.final_price, status=statis) - print(f'---> {type(transaction_participant)}/ {transaction_participant}') + print(f'---> {type(transaction_participant)}/ {transaction_participant}') + + # ======================================================= + # NEW LOGIC: GEOLOCATION CHECK + # ======================================================= + + payment_link = None # Default link + + # Only check IP if transaction is PENDING (needs payment) + if statis == TransactionParticipant.TransactionStatus.PENDING: + + client_ip = get_client_ip(request) + # "188.93.104.1" + #get_client_ip(request) + country_code = get_country_code(client_ip) + + if country_code == 'RU': + # Generate Russia-specific link (e.g., YooMoney, Mir card, etc.) + payment_link = f"https://russia-payment.com/pay/{transaction_participant.id}" + # else: + # # Standard Global Link (e.g., PayPal, Stripe) + # payment_link = f"https://global-payment.com/pay/{transaction_participant.id}" + + # ======================================================= return Response({ 'message': 'Transaction Participant created successfully.', 'transaction_id': transaction_participant.id, + 'payment_link': payment_link, # <--- Return the dynamic link 'participant_infos': serializer.data['participant_infos'] }, status=status.HTTP_201_CREATED) diff --git a/utils/ip_helper.py b/utils/ip_helper.py new file mode 100644 index 0000000..5a1434a --- /dev/null +++ b/utils/ip_helper.py @@ -0,0 +1,32 @@ +# utils/ip_helper.py +import geoip2.database +from django.conf import settings +import os + + +def get_client_ip(request): + """Retrieves the real IP address from the request.""" + x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') + if x_forwarded_for: + # The header contains a list of IPs, the first one is the real client + ip = x_forwarded_for.split(',')[0] + else: + ip = request.META.get('REMOTE_ADDR') + return ip + +def get_country_code(ip_address): + """ + Returns the ISO country code (e.g., 'RU', 'US', 'IR') for an IP. + Returns None if IP is invalid or not found. + """ + # Path to your .mmdb file + db_path = os.path.join(settings.BASE_DIR, 'utils', 'country_city_db', 'GeoLite2-Country.mmdb') + + try: + with geoip2.database.Reader(db_path) as reader: + response = reader.country(ip_address) + return response.country.iso_code + except Exception as e: + # Log error in production + print(f"GeoIP Error: {e}") + return None \ No newline at end of file