config thwani
This commit is contained in:
parent
8ab62e0598
commit
a36477ad32
Binary file not shown.
@ -20,6 +20,7 @@ from django.core.mail import send_mail
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
from django.contrib.sites.shortcuts import get_current_site
|
||||||
import json
|
import json
|
||||||
|
|
||||||
def home(request):
|
def home(request):
|
||||||
@ -546,11 +547,54 @@ def thawani_checkout(request, plan):
|
|||||||
messages.error(request, _("Failed to initiate payment. Please try again later."))
|
messages.error(request, _("Failed to initiate payment. Please try again later."))
|
||||||
return redirect('dashboard')
|
return redirect('dashboard')
|
||||||
|
|
||||||
|
def _activate_subscription_and_notify(transaction, plan, request=None):
|
||||||
|
"""
|
||||||
|
Helper to activate subscription, send WhatsApp/Email, and handle receipt link.
|
||||||
|
"""
|
||||||
|
# Activate Subscription
|
||||||
|
profile = transaction.user.profile
|
||||||
|
profile.subscription_plan = plan
|
||||||
|
profile.is_subscription_active = True
|
||||||
|
|
||||||
|
if plan == 'MONTHLY':
|
||||||
|
profile.subscription_expiry = timezone.now().date() + timedelta(days=30)
|
||||||
|
elif plan == 'ANNUAL':
|
||||||
|
profile.subscription_expiry = timezone.now().date() + timedelta(days=365)
|
||||||
|
|
||||||
|
profile.save()
|
||||||
|
|
||||||
|
# Get receipt URL
|
||||||
|
if request:
|
||||||
|
receipt_url = request.build_absolute_uri(reverse('transaction_receipt', args=[transaction.receipt_number]))
|
||||||
|
else:
|
||||||
|
# Fallback for webhook or when request is missing
|
||||||
|
host = os.getenv('HOST_FQDN', 'masarcargo.com')
|
||||||
|
receipt_url = f"https://{host}{reverse('transaction_receipt', args=[transaction.receipt_number])}"
|
||||||
|
|
||||||
|
# Notifications
|
||||||
|
expiry_date = profile.subscription_expiry.strftime('%Y-%m-%d')
|
||||||
|
msg = _("Your subscription for MASAR CARGO has been successfully renewed! Your new expiry date is %(date)s. You can view your receipt here: %(url)s") % {
|
||||||
|
"date": expiry_date,
|
||||||
|
"url": receipt_url
|
||||||
|
}
|
||||||
|
|
||||||
|
if profile.full_phone_number:
|
||||||
|
send_whatsapp_message(profile.full_phone_number, msg)
|
||||||
|
|
||||||
|
if transaction.user.email:
|
||||||
|
send_mail(
|
||||||
|
_("Subscription Activated - MASAR CARGO"),
|
||||||
|
msg,
|
||||||
|
settings.DEFAULT_FROM_EMAIL,
|
||||||
|
[transaction.user.email],
|
||||||
|
fail_silently=True,
|
||||||
|
)
|
||||||
|
return receipt_url
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def thawani_success(request):
|
def thawani_success(request):
|
||||||
session_id = request.GET.get('session_id')
|
session_id = request.GET.get('session_id')
|
||||||
if not session_id:
|
if not session_id:
|
||||||
# Fallback to looking for the last pending transaction for this user
|
|
||||||
transaction = Transaction.objects.filter(user=request.user, status='PENDING', payment_method='Thawani').first()
|
transaction = Transaction.objects.filter(user=request.user, status='PENDING', payment_method='Thawani').first()
|
||||||
else:
|
else:
|
||||||
transaction = get_object_or_404(Transaction, session_id=session_id, user=request.user)
|
transaction = get_object_or_404(Transaction, session_id=session_id, user=request.user)
|
||||||
@ -566,37 +610,12 @@ def thawani_success(request):
|
|||||||
transaction.status = 'COMPLETED'
|
transaction.status = 'COMPLETED'
|
||||||
transaction.save()
|
transaction.save()
|
||||||
|
|
||||||
# Activate Subscription
|
plan = session_data['data']['metadata'].get('plan', transaction.user.profile.subscription_plan)
|
||||||
profile = transaction.user.profile
|
_activate_subscription_and_notify(transaction, plan, request)
|
||||||
plan = session_data['data']['metadata'].get('plan', profile.subscription_plan)
|
|
||||||
profile.subscription_plan = plan
|
|
||||||
profile.is_subscription_active = True
|
|
||||||
|
|
||||||
if plan == 'MONTHLY':
|
|
||||||
profile.subscription_expiry = timezone.now().date() + timedelta(days=30)
|
|
||||||
elif plan == 'ANNUAL':
|
|
||||||
profile.subscription_expiry = timezone.now().date() + timedelta(days=365)
|
|
||||||
|
|
||||||
profile.save()
|
|
||||||
|
|
||||||
# Notifications
|
|
||||||
expiry_date = profile.subscription_expiry.strftime('%Y-%m-%d')
|
|
||||||
msg = _("Your subscription for MASAR CARGO has been successfully renewed! Your new expiry date is %(date)s. Thank you for using our service.") % {"date": expiry_date}
|
|
||||||
|
|
||||||
if profile.full_phone_number:
|
|
||||||
send_whatsapp_message(profile.full_phone_number, msg)
|
|
||||||
|
|
||||||
if transaction.user.email:
|
|
||||||
send_mail(
|
|
||||||
_("Subscription Activated - MASAR CARGO"),
|
|
||||||
msg,
|
|
||||||
settings.DEFAULT_FROM_EMAIL,
|
|
||||||
[transaction.user.email],
|
|
||||||
fail_silently=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
messages.success(request, _("Payment successful! Your subscription is now active."))
|
messages.success(request, _("Payment successful! Your subscription is now active."))
|
||||||
return redirect('dashboard')
|
# Redirect directly to the receipt for "Automated Invoice" feel
|
||||||
|
return redirect('transaction_receipt', receipt_number=transaction.receipt_number)
|
||||||
else:
|
else:
|
||||||
transaction.status = 'FAILED'
|
transaction.status = 'FAILED'
|
||||||
transaction.save()
|
transaction.save()
|
||||||
@ -613,9 +632,6 @@ def thawani_cancel(request):
|
|||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def thawani_webhook(request):
|
def thawani_webhook(request):
|
||||||
"""
|
|
||||||
Handle asynchronous status updates from Thawani.
|
|
||||||
"""
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
try:
|
try:
|
||||||
data = json.loads(request.body)
|
data = json.loads(request.body)
|
||||||
@ -632,16 +648,8 @@ def thawani_webhook(request):
|
|||||||
transaction.status = 'COMPLETED'
|
transaction.status = 'COMPLETED'
|
||||||
transaction.save()
|
transaction.save()
|
||||||
|
|
||||||
# Activate Subscription
|
plan = data['data']['metadata'].get('plan', transaction.user.profile.subscription_plan)
|
||||||
profile = transaction.user.profile
|
_activate_subscription_and_notify(transaction, plan, request)
|
||||||
plan = data['data']['metadata'].get('plan', profile.subscription_plan)
|
|
||||||
profile.subscription_plan = plan
|
|
||||||
profile.is_subscription_active = True
|
|
||||||
if plan == 'MONTHLY':
|
|
||||||
profile.subscription_expiry = timezone.now().date() + timedelta(days=30)
|
|
||||||
elif plan == 'ANNUAL':
|
|
||||||
profile.subscription_expiry = timezone.now().date() + timedelta(days=365)
|
|
||||||
profile.save()
|
|
||||||
else:
|
else:
|
||||||
transaction.status = 'FAILED'
|
transaction.status = 'FAILED'
|
||||||
transaction.save()
|
transaction.save()
|
||||||
@ -714,4 +722,4 @@ def admin_app_settings(request):
|
|||||||
else:
|
else:
|
||||||
form = AppSettingForm(instance=settings_obj)
|
form = AppSettingForm(instance=settings_obj)
|
||||||
|
|
||||||
return render(request, 'core/app_settings.html', {'form': form})
|
return render(request, 'core/app_settings.html', {'form': form})
|
||||||
Loading…
x
Reference in New Issue
Block a user