my subscriptions page 3
This commit is contained in:
@@ -92,6 +92,7 @@ urlpatterns = [
|
||||
),
|
||||
path("stripe/", views.SubscriptionPageView.as_view(), name="stripe"),
|
||||
path("active/", views.ActiveSubscriptionView.as_view(), name="active"),
|
||||
path("active/", views.CancelSubscriptionView.as_view(), name="cancel_subscription"),
|
||||
path("success/", views.SuccessView.as_view(), name="success"),
|
||||
path("cancel/", views.CancelView.as_view(), name="cancel"),
|
||||
# path("join-now/", views.IndexView.as_view(), name="index"),
|
||||
|
||||
@@ -38,6 +38,7 @@ from django.views.decorators.http import require_POST
|
||||
from django.conf import settings
|
||||
from django.views.generic.base import TemplateView
|
||||
from django.db.models import Q
|
||||
from django.db import transaction
|
||||
|
||||
# Create your views here.
|
||||
|
||||
@@ -670,6 +671,7 @@ class ActiveSubscriptionView(TemplateView):
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
request = self.request
|
||||
today = timezone.now().date()
|
||||
if request.user.is_authenticated:
|
||||
active_subscription = (
|
||||
PrincipalSubscription.objects.filter(
|
||||
@@ -684,7 +686,7 @@ class ActiveSubscriptionView(TemplateView):
|
||||
.first()
|
||||
)
|
||||
|
||||
if active_subscription:
|
||||
if active_subscription and active_subscription.end_date > today:
|
||||
context["active_subscription"] = active_subscription
|
||||
else:
|
||||
# If no active subscription is found, redirect to the SubscriptionPageView
|
||||
@@ -693,6 +695,40 @@ class ActiveSubscriptionView(TemplateView):
|
||||
return context
|
||||
|
||||
|
||||
class CancelSubscriptionView(LoginRequiredMixin, generic.View):
|
||||
def post(self, request, *args, **kwargs):
|
||||
subscription_id = request.POST.get("subscription_id")
|
||||
|
||||
try:
|
||||
subscription = PrincipalSubscription.objects.get(
|
||||
id=subscription_id, principal=request.user
|
||||
)
|
||||
except PrincipalSubscription.DoesNotExist:
|
||||
messages.error(request, "Subscription not found.")
|
||||
return redirect("manage_subscriptions:cancel")
|
||||
|
||||
try:
|
||||
with transaction.atomic():
|
||||
if subscription.is_stripe_subscription:
|
||||
# Cancel Stripe subscription
|
||||
stripe.Subscription.modify(
|
||||
subscription.stripe_subscription_id, cancel_at_period_end=True
|
||||
)
|
||||
|
||||
# Updating subscription status in the local database
|
||||
subscription.status = SubscriptionStatus.INACTIVE
|
||||
subscription.cancelled = True
|
||||
subscription.active = False
|
||||
subscription.cancelled_date_time = timezone.now()
|
||||
subscription.save()
|
||||
|
||||
messages.success(request, "Subscription cancelled successfully.")
|
||||
return redirect("manage_subscriptions:success")
|
||||
except stripe.error.InvalidRequestError as e:
|
||||
messages.error(request, f"Stripe error: {str(e)}")
|
||||
return redirect("manage_subscriptions:cancel")
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
def stripe_config(request):
|
||||
if request.method == "GET":
|
||||
|
||||
@@ -1,70 +1,125 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Active Subscription</title>
|
||||
<!-- Bootstrap CSS -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');
|
||||
|
||||
body {
|
||||
background-color: var(--black);
|
||||
color: var(--white);
|
||||
font-family: 'Poppins', sans-serif;
|
||||
}
|
||||
|
||||
.container {
|
||||
padding: 40px 15px;
|
||||
}
|
||||
|
||||
.card {
|
||||
background-color: var(--light-black);
|
||||
border: 1px solid var(--main-yellow);
|
||||
border-radius: 8px;
|
||||
margin-top: 20px;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
background-color: transparent;
|
||||
border-bottom: 1px solid var(--main-yellow);
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 1.5rem;
|
||||
color: var(--main-yellow);
|
||||
}
|
||||
|
||||
.card-body {
|
||||
font-size: 1rem;
|
||||
color: var(--white-mix);
|
||||
}
|
||||
|
||||
.btn {
|
||||
background: linear-gradient(90.02deg, #CDA34C 0.02%, #F1D6A0 52%, #D1A956 98.68%);
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--black);
|
||||
border-radius: 5px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.btn-cancel {
|
||||
background-color: #dc3545;
|
||||
color: var(--white);
|
||||
}
|
||||
|
||||
.cancel-details {
|
||||
background-color: #111;
|
||||
color: #bbb;
|
||||
padding: 15px;
|
||||
margin-top: 20px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.card-title {
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container my-5">
|
||||
|
||||
<header class="text-center py-3">
|
||||
<h1 class="text-gold">Your Active Subscription</h1>
|
||||
</header>
|
||||
|
||||
<div class="container">
|
||||
<div class="card">
|
||||
<div class="card-header text-center bg-primary text-white">
|
||||
<h3>Your Active Subscription</h3>
|
||||
<div class="card-header">
|
||||
<h2 class="card-title">{{ active_subscription.subscription.name }}</h2>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<!-- Subscription Details -->
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<h5>Subscription:</h5>
|
||||
<p>{{ active_subscription.subscription.name }}</p>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<h5>Principal:</h5>
|
||||
<p>{{ active_subscription.principal.first_name }} {{ active_subscription.principal.last_name }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<p><strong>Status:</strong> {{ active_subscription.status }}</p>
|
||||
<p><strong>Start Date:</strong> {{ active_subscription.start_date }}</p>
|
||||
<p><strong>End Date:</strong> {{ active_subscription.end_date }}</p>
|
||||
<p><strong>Auto Renew:</strong> {{ active_subscription.auto_renew }}</p>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<h5>Status:</h5>
|
||||
<p>{{ active_subscription.get_status_display }}</p>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<h5>Start Date:</h5>
|
||||
<p>{{ active_subscription.start_date }}</p>
|
||||
</div>
|
||||
{% if active_subscription.cancelled %}
|
||||
<div class="cancel-details">
|
||||
<h3>Cancellation Details</h3>
|
||||
<p><strong>Cancelled:</strong> Yes</p>
|
||||
<p><strong>Cancellation Date:</strong> {{ active_subscription.cancelled_date_time }}</p>
|
||||
<p><strong>Grace Period Ends:</strong> {{ active_subscription.grace_period_end_date }}</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<h5>End Date:</h5>
|
||||
<p>{{ active_subscription.end_date }}</p>
|
||||
{% if active_subscription.auto_renew and not active_subscription.cancelled %}
|
||||
<div class="cancel-details">
|
||||
<h3>Cancel Subscription</h3>
|
||||
<form method="POST" action="{% url 'manage_subscriptions:cancel_subscription' %}">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="subscription_id" value="{{ active_subscription.stripe_subscription_id }}">
|
||||
<button type="submit" class="btn btn-cancel">Cancel Subscription</button>
|
||||
</form>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<h5>Auto Renew:</h5>
|
||||
<p>{{ active_subscription.auto_renew|yesno:"Yes,No" }}</p>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<!-- Payment Details -->
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<h5>Coupon Code:</h5>
|
||||
<p>{{ active_subscription.coupon_code }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Stripe Links -->
|
||||
<div class="text-center mt-4">
|
||||
<a href="" class="btn btn-success me-2">Cancel Subscription</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Bootstrap JS (optional) -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user