aboutsummaryrefslogtreecommitdiffstats
path: root/alphabetlearning/payments
diff options
context:
space:
mode:
Diffstat (limited to 'alphabetlearning/payments')
-rw-r--r--alphabetlearning/payments/admin.py29
-rw-r--r--alphabetlearning/payments/models.py2
-rw-r--r--alphabetlearning/payments/urls.py2
-rw-r--r--alphabetlearning/payments/views.py40
4 files changed, 50 insertions, 23 deletions
diff --git a/alphabetlearning/payments/admin.py b/alphabetlearning/payments/admin.py
index 9588f3b..685af63 100644
--- a/alphabetlearning/payments/admin.py
+++ b/alphabetlearning/payments/admin.py
@@ -9,31 +9,36 @@ from .models import SubscriptionPlan
from alphabetlearning.resources.models import Resource
-#class PriceAdmin(admin.ModelAdmin):
-# list_display =
+# class PriceAdmin(admin.ModelAdmin):
+# list_display =
-#class ProductAdmin(admin.ModelAdmin):
+# class ProductAdmin(admin.ModelAdmin):
# inlines = [PriceInlineAdmin]
+
class SubscriptionPlanAdmin(admin.ModelAdmin):
- list_display = ('name', 'price', 'description', 'allowed_downloads')
+ list_display = ("name", "price", "description", "allowed_downloads")
+
+
+class CartItemAdminInline(admin.TabularInline):
+ model = CartItem
class ShoppingCartAdmin(admin.ModelAdmin):
- list_display = ('user', 'created_at', 'updated_at')
+# list_display = ("user", "created_at", "updated_at")
+ inlines = [CartItemAdminInline,]
-class CartItemAdmin(admin.ModelAdmin):
- list_display = ('cart', 'resource', 'quantity', 'added_at')
+# class CartItemAdmin(admin.ModelAdmin):
+# list_display = ('cart', 'resource', 'quantity', 'added_at')
class SubscriptionAdmin(admin.ModelAdmin):
- list_display = ('user', 'is_active', 'start_date', 'end_date')
+ list_display = ("user", "is_active", "start_date", "end_date")
-#admin.site.register(Product, ProductAdmin)
+# admin.site.register(Product, ProductAdmin)
admin.site.register(Price)
-admin.site.register(SubscriptionPlan)
-admin.site.register(ShoppingCart)
+admin.site.register(ShoppingCart, ShoppingCartAdmin)
admin.site.register(CartItem)
-admin.site.register(Subscription)
+admin.site.register(Subscription, SubscriptionAdmin)
diff --git a/alphabetlearning/payments/models.py b/alphabetlearning/payments/models.py
index 0ad07fb..7bf4164 100644
--- a/alphabetlearning/payments/models.py
+++ b/alphabetlearning/payments/models.py
@@ -14,7 +14,7 @@ class Product(models.Model):
class Price(models.Model):
- resource = models.ForeignKey(Resource, on_delete=models.CASCADE, related_name="price")
+ resource = models.ForeignKey(Resource, on_delete=models.CASCADE, related_name="price_obj")
price = models.IntegerField(default=0)
stripe_price_id = models.CharField(max_length=100)
diff --git a/alphabetlearning/payments/urls.py b/alphabetlearning/payments/urls.py
index b782bdc..d5b0dd5 100644
--- a/alphabetlearning/payments/urls.py
+++ b/alphabetlearning/payments/urls.py
@@ -16,7 +16,7 @@ urlpatterns = [
views.CreateCheckoutSessionView.as_view(),
name="create-checkout-session",
),
- path("add-to-card/<int:resource_id>", views.add_to_cart, name="add_to_cart"),
+ path("add-to-basket/<int:resource_id>", views.add_to_cart, name="add_to_basket"),
path("landing/", views.ProductLandingPageView.as_view(), name="landing"),
path("webhooks/stripe/", views.stripe_webhook, name="stripe-webhook"),
]
diff --git a/alphabetlearning/payments/views.py b/alphabetlearning/payments/views.py
index 6bcb5a5..dae1a9d 100644
--- a/alphabetlearning/payments/views.py
+++ b/alphabetlearning/payments/views.py
@@ -37,19 +37,23 @@ from .models import ShoppingCart
stripe.api_key = settings.STRIPE_SECRET_KEY
+def create_line_items(resources):
+ price_objs = [r.price_obj.first() for r in resources]
+ stripe_price_ids = [p.stripe_price_id for p in price_objs]
+ return [{"quantity": 1, "price": s} for s in stripe_price_ids]
+ pass
class CreateCheckoutSessionView(View):
def post(self, request, *args, **kwargs):
- price = Price.objects.get(id=self.kwargs["pk"])
+ #price = Price.objects.get(id=self.kwargs["pk"])
+ cart = ShoppingCart.objects.get(id=self.kwargs["pk"])
+ resources = [i.resource for i in cart.items.all()]
+ total = sum([r.price_obj.first().price for r in resources])
domain = "http://localhost:8000"
checkout_session = stripe.checkout.Session.create(
payment_method_types=["card"],
- line_items=[
- {
- "price": price.stripe_price_id,
- "quantity": 1,
- },
- ],
+ customer_email=request.user.email,
+ line_items=create_line_items(resources),
mode="payment",
success_url=domain + "/payments/success/",
cancel_url=domain + "/payments/cancel/",
@@ -83,13 +87,20 @@ def add_to_cart(request, resource_id):
cart_item, created = CartItem.objects.get_or_create(cart=cart, resource=resource)
# cart_item.quantity += 1
cart_item.save()
- return render(request, "payments/cart_detail.html", {"cart": cart})
+ return redirect("payments:cart_detail")
@login_required
def cart_detail(request):
cart, created = ShoppingCart.objects.get_or_create(user=request.user)
- return render(request, "payments/cart_detail.html", {"cart": cart})
+ resources = [i.resource for i in cart.items.all()]
+ total = sum([r.price_obj.first().price for r in resources])
+ context = {
+ "cart": cart,
+ "resources": resources,
+ "total": total
+ }
+ return render(request, "payments/cart_detail.html", context)
# def cart_detail(request):
@@ -135,6 +146,17 @@ def stripe_webhook(request):
customer_email = session["customer_details"]["email"]
payment_intent = session["payment_intent"]
+ # TODO Add the items to the users profile page
+ # This should be done before deleting the cart otherwise we have no record of what they bought
+
+ # 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?
+
+
# TODO send an email to the customer
send_mail(
"Thank you for your purchase",