Implement cancelling subscriptions
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
import stripe
|
||||
@@ -9,6 +10,8 @@ from rest_framework.views import APIView
|
||||
|
||||
from core.models import Plan, Session, User
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Callback(APIView):
|
||||
parser_classes = [JSONParser]
|
||||
@@ -17,16 +20,17 @@ class Callback(APIView):
|
||||
def post(self, request):
|
||||
payload = request.body
|
||||
sig_header = request.META["HTTP_STRIPE_SIGNATURE"]
|
||||
|
||||
try:
|
||||
stripe.Webhook.construct_event(
|
||||
payload, sig_header, settings.STRIPE_ENDPOINT_SECRET
|
||||
)
|
||||
except ValueError:
|
||||
# Invalid payload
|
||||
logger.error("Invalid payload")
|
||||
return HttpResponse(status=400)
|
||||
except stripe.error.SignatureVerificationError:
|
||||
# Invalid signature
|
||||
logger.error("Invalid signature")
|
||||
return HttpResponse(status=400)
|
||||
|
||||
if request.data is None:
|
||||
@@ -37,7 +41,6 @@ class Callback(APIView):
|
||||
session = request.data["data"]["object"]["id"]
|
||||
subscription_id = request.data["data"]["object"]["subscription"]
|
||||
session_map = Session.objects.get(session=session)
|
||||
print("querying session", session)
|
||||
if not session_map:
|
||||
return JsonResponse({"success": False}, status=500)
|
||||
user = session_map.user
|
||||
@@ -47,7 +50,7 @@ class Callback(APIView):
|
||||
if rtype == "customer.subscription.updated":
|
||||
stripe_id = request.data["data"]["object"]["customer"]
|
||||
if not stripe_id:
|
||||
print("No user found for customer:", stripe_id)
|
||||
logging.error("No stripe id")
|
||||
return JsonResponse({"success": False}, status=500)
|
||||
user = User.objects.get(stripe_id=stripe_id)
|
||||
# ssubscription_active
|
||||
@@ -58,7 +61,9 @@ class Callback(APIView):
|
||||
if session_iter.subscription_id == subscription_id:
|
||||
session = session_iter
|
||||
if not session:
|
||||
print(f"No session found for subscription id {subscription_id}")
|
||||
logging.error(
|
||||
f"No session found for subscription id {subscription_id}"
|
||||
)
|
||||
return JsonResponse({"success": False}, status=500)
|
||||
# query Session objects
|
||||
# iterate and check against product_id
|
||||
@@ -66,24 +71,20 @@ class Callback(APIView):
|
||||
product_id = request.data["data"]["object"]["plan"]["id"]
|
||||
plan = Plan.objects.get(product_id=product_id)
|
||||
if not plan:
|
||||
print(f"Plan not found: {product_id}")
|
||||
logging.error(f"Plan not found: {product_id}")
|
||||
return JsonResponse({"success": False}, status=500)
|
||||
session.plan = plan
|
||||
session.save()
|
||||
|
||||
elif rtype == "payment_intent.succeeded":
|
||||
customer = request.data["data"]["object"]["customer"]
|
||||
print("customer", customer)
|
||||
user = User.objects.get(stripe_id=customer)
|
||||
if not user:
|
||||
print("No user found for customer:", customer)
|
||||
logging.error(f"No user found for customer: {customer}")
|
||||
return JsonResponse({"success": False}, status=500)
|
||||
print("got", user.email)
|
||||
session = Session.objects.get(request=request.data["request"]["id"])
|
||||
print("Got session", session)
|
||||
|
||||
user.plans.add(session.plan)
|
||||
print("ADDING PLAN TO USER PLANS")
|
||||
user.last_payment = datetime.utcnow()
|
||||
user.save()
|
||||
|
||||
@@ -91,7 +92,7 @@ class Callback(APIView):
|
||||
customer = request.data["data"]["object"]["customer"]
|
||||
user = User.objects.get(stripe_id=customer)
|
||||
if not user:
|
||||
print("No user found for customer:", customer)
|
||||
logging.error(f"No user found for customer {customer}")
|
||||
return JsonResponse({"success": False}, status=500)
|
||||
product_id = request.data["data"]["object"]["plan"]["id"]
|
||||
plan = Plan.objects.get(product_id=product_id)
|
||||
|
||||
Reference in New Issue
Block a user