Implement cancelling subscriptions
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import pprint
|
||||
|
||||
import stripe
|
||||
@@ -13,6 +14,7 @@ from core.forms import NewUserForm
|
||||
from core.lib.products import assemble_plan_map
|
||||
from core.models import ContentBlock, Plan, Session
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
pp = pprint.PrettyPrinter(indent=4)
|
||||
|
||||
# Create your views here
|
||||
@@ -55,6 +57,25 @@ class Order(LoginRequiredMixin, View):
|
||||
return JsonResponse({"error": str(e)}, status=500)
|
||||
|
||||
|
||||
class Cancel(LoginRequiredMixin, View):
|
||||
def get(self, request, plan_name):
|
||||
plan = Plan.objects.get(name=plan_name)
|
||||
try:
|
||||
subscriptions = stripe.Subscription.list(
|
||||
customer=request.user.stripe_id, price=plan.product_id
|
||||
)
|
||||
for subscription in subscriptions["data"]:
|
||||
items = subscription["items"]["data"]
|
||||
for item in items:
|
||||
stripe.Subscription.delete(item["subscription"])
|
||||
return render(request, "subscriptioncancel.html", {"plan": plan})
|
||||
# return JsonResponse({'id': session.id})
|
||||
except Exception as e:
|
||||
# Raise a server error
|
||||
logging.error(f"Error cancelling subscription for user: {e}")
|
||||
return JsonResponse({"error": "True"}, status=500)
|
||||
|
||||
|
||||
class Signup(CreateView):
|
||||
form_class = NewUserForm
|
||||
success_url = reverse_lazy("login")
|
||||
|
||||
Reference in New Issue
Block a user