diff options
Diffstat (limited to 'alphabetlearning/payments')
-rw-r--r-- | alphabetlearning/payments/urls.py | 1 | ||||
-rw-r--r-- | alphabetlearning/payments/views.py | 22 |
2 files changed, 20 insertions, 3 deletions
diff --git a/alphabetlearning/payments/urls.py b/alphabetlearning/payments/urls.py index d5b0dd5..9bedaf7 100644 --- a/alphabetlearning/payments/urls.py +++ b/alphabetlearning/payments/urls.py @@ -18,5 +18,6 @@ urlpatterns = [ ), path("add-to-basket/<int:resource_id>", views.add_to_cart, name="add_to_basket"), path("landing/", views.ProductLandingPageView.as_view(), name="landing"), + path("delete-cart-item/<int:pk>", views. DeleteCartItem.as_view(), name="delete_cart_item"), path("webhooks/stripe/", views.stripe_webhook, name="stripe-webhook"), ] diff --git a/alphabetlearning/payments/views.py b/alphabetlearning/payments/views.py index cf77e5b..92751c9 100644 --- a/alphabetlearning/payments/views.py +++ b/alphabetlearning/payments/views.py @@ -1,13 +1,14 @@ import stripe from django.http import HttpResponse, HttpResponseBadRequest from django.core.mail import send_mail +from django.urls import reverse_lazy from django.conf import settings from django.contrib.auth.decorators import login_required from django.shortcuts import get_object_or_404 from django.shortcuts import redirect from django.shortcuts import render from django.views import View -from django.views.generic import TemplateView +from django.views.generic import TemplateView, DeleteView from django.views.decorators.csrf import csrf_exempt from alphabetlearning.resources.models import Resource @@ -155,7 +156,6 @@ def stripe_webhook(request): # TODO clear their shopping cart - we need to obtain their user ID user = User.objects.get(email=customer_email) ShoppingCart.objects.get(user=user).delete() - print(f"Deleted cart for {user}") # TODO add the transaction to our local history? Or just use Stripe? @@ -167,5 +167,21 @@ def stripe_webhook(request): [customer_email], fail_silently=False, ) - return HttpResponse(status=200) + +class DeleteCartItem(DeleteView): + model = CartItem + success_url = reverse_lazy("payments:cart_detail") + + # delete the cart item if there is more than one + # delete the item and the cart itself if it is the last thing in the cart + def post(self, request, *args, **kwargs): + cart_items = request.user.shoppingcart.items.all() + if len(cart_items) > 1: + return self.delete(request, *args, **kwargs) + else: + request.user.shoppingcart.delete() + return redirect("resources:resource_list") + + + |