Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions web/tests/test_donation_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
from unittest.mock import Mock, patch

from django.contrib.auth.models import User
from django.test import Client, TestCase
from django.urls import reverse

from ..models import MembershipPlan, Profile, UserMembership


class CreateDonationSubscriptionViewTests(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create_user(username="test", email="test@example.com", password="pass")
self.profile, _ = Profile.objects.get_or_create(user=self.user, defaults={"is_teacher": False})
self.plan = MembershipPlan.objects.create(
name="Basic", slug="basic", description="Basic plan", price_monthly=0, price_yearly=0
)
Comment thread
Ananya44444 marked this conversation as resolved.

@patch("stripe.PaymentIntent.create")
@patch("stripe.Customer.retrieve")
def test_reuses_existing_stripe_customer(self, mock_retrieve, mock_payment_intent):
UserMembership.objects.create(user=self.user, plan=self.plan, stripe_customer_id="cus_existing")
mock_retrieve.return_value = Mock(id="cus_existing")
mock_payment_intent.return_value = Mock(client_secret="secret", id="pi_123")
self.client.login(username="test", password="pass")
response = self.client.post(
reverse("create_donation_subscription"),
data=json.dumps({"amount": "10.00", "email": "test@example.com"}),
content_type="application/json",
)
self.assertEqual(response.status_code, 200)
mock_retrieve.assert_called_once_with("cus_existing")
Comment thread
Ananya44444 marked this conversation as resolved.
22 changes: 16 additions & 6 deletions web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
TeamInvite,
TimeSlot,
UserBadge,
UserMembership,
VideoRequest,
VirtualClassroom,
VirtualClassroomCustomization,
Expand Down Expand Up @@ -5617,11 +5618,15 @@ def create_donation_subscription(request: HttpRequest) -> JsonResponse:
# Create or get customer
customer = None
# Try to retrieve existing customer for authenticated users
if request.user.is_authenticated and hasattr(request.user, "stripe_customer_id"):
from contextlib import suppress
if request.user.is_authenticated:
try:
membership = request.user.membership
if membership.stripe_customer_id:
customer = stripe.Customer.retrieve(membership.stripe_customer_id)
except (UserMembership.DoesNotExist, stripe.error.InvalidRequestError):
# No membership or invalid customer ID
customer = None

with suppress(stripe.error.InvalidRequestError):
customer = stripe.Customer.retrieve(request.user.stripe_customer_id)
# Create new customer if needed
if not customer:
customer = stripe.Customer.create(
Expand All @@ -5631,8 +5636,13 @@ def create_donation_subscription(request: HttpRequest) -> JsonResponse:
},
)
if request.user.is_authenticated:
request.user.stripe_customer_id = customer.id
request.user.save()
# Store customer ID in user membership
membership, _ = UserMembership.objects.get_or_create(
user=request.user, defaults={"plan_id": 1, "stripe_customer_id": customer.id}
)
Comment thread
Ananya44444 marked this conversation as resolved.
if not membership.stripe_customer_id:
membership.stripe_customer_id = customer.id
membership.save()
Comment thread
Ananya44444 marked this conversation as resolved.

# Create a PaymentIntent for the first payment with setup_future_usage
payment_intent = stripe.PaymentIntent.create(
Expand Down
Loading