Browse Source

RU IP transactions get payment-links now

master
Mohsen Taba 5 months ago
parent
commit
94ab37b097
  1. 26
      apps/transaction/views.py
  2. 32
      utils/ip_helper.py

26
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):
@ -65,9 +67,33 @@ 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}')
# =======================================================
# 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)

32
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
Loading…
Cancel
Save