|
@ -1,17 +1,18 @@ |
|
|
import braintree |
|
|
import braintree |
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
|
|
from django.contrib import messages |
|
|
from django.contrib import messages |
|
|
from django.http import HttpResponseForbidden, JsonResponse |
|
|
|
|
|
|
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
|
|
from django.http import (HttpResponseBadRequest, HttpResponseForbidden, |
|
|
|
|
|
JsonResponse) |
|
|
from django.shortcuts import redirect, render |
|
|
from django.shortcuts import redirect, render |
|
|
from django.views.decorators.http import require_POST |
|
|
from django.views.decorators.http import require_POST |
|
|
|
|
|
|
|
|
from hc.accounts.models import Profile |
|
|
from hc.accounts.models import Profile |
|
|
|
|
|
|
|
|
from .models import Subscription |
|
|
from .models import Subscription |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required |
|
|
@login_required |
|
|
def get_client_token(request): |
|
|
def get_client_token(request): |
|
|
sub = Subscription.objects.get(user=request.user) |
|
|
|
|
|
|
|
|
sub = Subscription.objects.for_user(request.user) |
|
|
client_token = braintree.ClientToken.generate({ |
|
|
client_token = braintree.ClientToken.generate({ |
|
|
"customer_id": sub.customer_id |
|
|
"customer_id": sub.customer_id |
|
|
}) |
|
|
}) |
|
@ -22,21 +23,12 @@ def get_client_token(request): |
|
|
def pricing(request): |
|
|
def pricing(request): |
|
|
sub = None |
|
|
sub = None |
|
|
if request.user.is_authenticated(): |
|
|
if request.user.is_authenticated(): |
|
|
try: |
|
|
|
|
|
sub = Subscription.objects.get(user=request.user) |
|
|
|
|
|
except Subscription.DoesNotExist: |
|
|
|
|
|
sub = Subscription(user=request.user) |
|
|
|
|
|
sub.save() |
|
|
|
|
|
|
|
|
|
|
|
first_charge = False |
|
|
|
|
|
if "first_charge" in request.session: |
|
|
|
|
|
first_charge = True |
|
|
|
|
|
del request.session["first_charge"] |
|
|
|
|
|
|
|
|
sub = Subscription.objects.for_user(request.user) |
|
|
|
|
|
|
|
|
ctx = { |
|
|
ctx = { |
|
|
"page": "pricing", |
|
|
"page": "pricing", |
|
|
"sub": sub, |
|
|
"sub": sub, |
|
|
"first_charge": first_charge |
|
|
|
|
|
|
|
|
"first_charge": request.session.pop("first_charge", False) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return render(request, "payments/pricing.html", ctx) |
|
|
return render(request, "payments/pricing.html", ctx) |
|
@ -55,9 +47,10 @@ def log_and_bail(request, result): |
|
|
@require_POST |
|
|
@require_POST |
|
|
def create_plan(request): |
|
|
def create_plan(request): |
|
|
plan_id = request.POST["plan_id"] |
|
|
plan_id = request.POST["plan_id"] |
|
|
assert plan_id in ("P5", "P20") |
|
|
|
|
|
|
|
|
if plan_id not in ("P5", "P20"): |
|
|
|
|
|
return HttpResponseBadRequest() |
|
|
|
|
|
|
|
|
sub = Subscription.objects.get(user=request.user) |
|
|
|
|
|
|
|
|
sub = Subscription.objects.for_user(request.user) |
|
|
|
|
|
|
|
|
# Cancel the previous plan |
|
|
# Cancel the previous plan |
|
|
if sub.subscription_id: |
|
|
if sub.subscription_id: |
|
@ -133,9 +126,10 @@ def cancel_plan(request): |
|
|
def billing(request): |
|
|
def billing(request): |
|
|
sub = Subscription.objects.get(user=request.user) |
|
|
sub = Subscription.objects.get(user=request.user) |
|
|
|
|
|
|
|
|
transactions = braintree.Transaction.search(braintree.TransactionSearch.customer_id == sub.customer_id) |
|
|
|
|
|
ctx = {"transactions": transactions} |
|
|
|
|
|
|
|
|
transactions = braintree.Transaction.search( |
|
|
|
|
|
braintree.TransactionSearch.customer_id == sub.customer_id) |
|
|
|
|
|
|
|
|
|
|
|
ctx = {"transactions": transactions} |
|
|
return render(request, "payments/billing.html", ctx) |
|
|
return render(request, "payments/billing.html", ctx) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|