aboutsummaryrefslogtreecommitdiffstats
path: root/alphabetlearning/payments/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-10-19 21:35:20 +0100
committerMatthew Lemon <y@yulqen.org>2024-10-19 21:35:20 +0100
commit9b06fa6596def3505af076d6f8807a251c4162e4 (patch)
tree55f15a16aa4ef00f9050eaefac413933d203c3e3 /alphabetlearning/payments/views.py
parent1e1a2f6ac2cadfbc57cc855a67498af097391caf (diff)
Add multiple items to the cart and Stripe process
- Very rough but it works - Multiple items can be added to cart - Can be bought with stripe - Shopping cart is deleted after successful transaction
Diffstat (limited to 'alphabetlearning/payments/views.py')
-rw-r--r--alphabetlearning/payments/views.py40
1 files changed, 31 insertions, 9 deletions
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",