Add separate libraries
This commit is contained in:
parent
e62aa9d559
commit
dabe3eca7b
|
@ -0,0 +1,112 @@
|
||||||
|
import pprint
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import stripe
|
||||||
|
from chartjs.views.lines import BaseLineChartView
|
||||||
|
from django.conf import settings
|
||||||
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
|
from django.http import HttpResponse, JsonResponse
|
||||||
|
from django.shortcuts import redirect, render
|
||||||
|
from django.urls import reverse, reverse_lazy
|
||||||
|
from django.utils.decorators import method_decorator
|
||||||
|
from django.views import View
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from django.views.generic.edit import CreateView
|
||||||
|
from rest_framework.parsers import JSONParser
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
from core.forms import NewUserForm
|
||||||
|
from core.lib.products import assemble_plan_map
|
||||||
|
from core.models import Plan, Session, User
|
||||||
|
|
||||||
|
class Callback(APIView):
|
||||||
|
parser_classes = [JSONParser]
|
||||||
|
|
||||||
|
@csrf_exempt
|
||||||
|
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
|
||||||
|
return HttpResponse(status=400)
|
||||||
|
except stripe.error.SignatureVerificationError:
|
||||||
|
# Invalid signature
|
||||||
|
return HttpResponse(status=400)
|
||||||
|
|
||||||
|
pp.pprint(request.data)
|
||||||
|
if request.data is None:
|
||||||
|
return JsonResponse({"success": False}, status=500)
|
||||||
|
if "type" in request.data.keys():
|
||||||
|
rtype = request.data["type"]
|
||||||
|
if rtype == "checkout.session.completed":
|
||||||
|
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
|
||||||
|
session_map.subscription_id = subscription_id
|
||||||
|
session_map.save()
|
||||||
|
|
||||||
|
if rtype == "customer.subscription.updated":
|
||||||
|
stripe_id = request.data["data"]["object"]["customer"]
|
||||||
|
if not stripe_id:
|
||||||
|
print("No user found for customer:", stripe_id)
|
||||||
|
return JsonResponse({"success": False}, status=500)
|
||||||
|
user = User.objects.get(stripe_id=stripe_id)
|
||||||
|
# ssubscription_active
|
||||||
|
subscription_id = request.data["data"]["object"]["id"]
|
||||||
|
sessions = Session.objects.filter(user=user)
|
||||||
|
session = None
|
||||||
|
for session_iter in sessions:
|
||||||
|
if session_iter.subscription_id == subscription_id:
|
||||||
|
session = session_iter
|
||||||
|
if not session:
|
||||||
|
print(f"No session found for subscription id {subscription_id}")
|
||||||
|
return JsonResponse({"success": False}, status=500)
|
||||||
|
# query Session objects
|
||||||
|
# iterate and check against product_id
|
||||||
|
session.request = request.data["request"]["id"]
|
||||||
|
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}")
|
||||||
|
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)
|
||||||
|
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()
|
||||||
|
|
||||||
|
elif rtype == "customer.subscription.deleted":
|
||||||
|
customer = request.data["data"]["object"]["customer"]
|
||||||
|
user = User.objects.get(stripe_id=customer)
|
||||||
|
if not user:
|
||||||
|
print("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)
|
||||||
|
user.plans.remove(plan)
|
||||||
|
user.save()
|
||||||
|
else:
|
||||||
|
return JsonResponse({"success": False}, status=500)
|
||||||
|
return JsonResponse({"success": True})
|
|
@ -0,0 +1,86 @@
|
||||||
|
import pprint
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import stripe
|
||||||
|
from chartjs.views.lines import BaseLineChartView
|
||||||
|
from django.conf import settings
|
||||||
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
|
from django.http import HttpResponse, JsonResponse
|
||||||
|
from django.shortcuts import redirect, render
|
||||||
|
from django.urls import reverse, reverse_lazy
|
||||||
|
from django.utils.decorators import method_decorator
|
||||||
|
from django.views import View
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from django.views.generic.edit import CreateView
|
||||||
|
from rest_framework.parsers import JSONParser
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
from core.forms import NewUserForm
|
||||||
|
from core.lib.products import assemble_plan_map
|
||||||
|
from core.models import Plan, Session, User
|
||||||
|
|
||||||
|
class CSRFExemptMixin(object):
|
||||||
|
@method_decorator(csrf_exempt)
|
||||||
|
def dispatch(self, *args, **kwargs):
|
||||||
|
return super(CSRFExemptMixin, self).dispatch(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class VolumeChartJSONView(CSRFExemptMixin, LoginRequiredMixin, BaseLineChartView):
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
print("POST")
|
||||||
|
context = self.get_context_data(**kwargs)
|
||||||
|
return self.render_to_response(context)
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
data = super(VolumeChartJSONView, self).get_context_data(**kwargs)
|
||||||
|
# data["colors"] = islice(next_color(), 0, 50)
|
||||||
|
print("KWARGS", kwargs)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def get_labels(self):
|
||||||
|
"""Return 7 labels for the x-axis."""
|
||||||
|
return ["January", "February", "March", "April", "May", "June", "July"]
|
||||||
|
|
||||||
|
def get_providers(self):
|
||||||
|
"""Return names of datasets."""
|
||||||
|
return ["Central", "Eastside", "Westside"]
|
||||||
|
|
||||||
|
def get_data(self):
|
||||||
|
"""Return 3 datasets to plot."""
|
||||||
|
|
||||||
|
return [
|
||||||
|
[75, 44, 92, 11, 44, 95, 35],
|
||||||
|
[41, 92, 18, 3, 73, 87, 92],
|
||||||
|
[87, 21, 94, 3, 90, 13, 65],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class SentimentChartJSONView(CSRFExemptMixin, LoginRequiredMixin, BaseLineChartView):
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
print("POST")
|
||||||
|
print(request.POST)
|
||||||
|
context = self.get_context_data(**kwargs)
|
||||||
|
return self.render_to_response(context)
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
data = super(SentimentChartJSONView, self).get_context_data(**kwargs)
|
||||||
|
# data["colors"] = islice(next_color(), 0, 50)
|
||||||
|
print("KWARGS", kwargs)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def get_labels(self):
|
||||||
|
"""Return 7 labels for the x-axis."""
|
||||||
|
return ["January", "February", "March", "April", "May", "June", "July"]
|
||||||
|
|
||||||
|
def get_providers(self):
|
||||||
|
"""Return names of datasets."""
|
||||||
|
return ["Central", "Eastside", "Westside"]
|
||||||
|
|
||||||
|
def get_data(self):
|
||||||
|
"""Return 3 datasets to plot."""
|
||||||
|
|
||||||
|
return [
|
||||||
|
[75, 44, 92, 11, 44, 95, 35],
|
||||||
|
[41, 92, 18, 3, 73, 87, 92],
|
||||||
|
[87, 21, 94, 3, 90, 13, 65],
|
||||||
|
]
|
Loading…
Reference in New Issue