Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add stripe setupIntent api #1083

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions api/internal/owner/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@
return Response(self.get_serializer(owner).data)


@action(detail=False, methods=["get"])
@stripe_safe
def setup_intent(self, request, *args, **kwargs):
"""
GET a Stripe setupIntent clientSecret for updating payment method
"""
try:
billing = BillingService(requesting_user=request.current_owner)
client_secret = billing.get_setup_intent(self.owner)
return Response({"client_secret": client_secret})
except Exception as e:
log.error(

Check warning on line 127 in api/internal/owner/views.py

View check run for this annotation

Codecov Notifications / codecov/patch

api/internal/owner/views.py#L122-L127

Added lines #L122 - L127 were not covered by tests
f"Error getting setup intent for owner {self.owner.ownerid}",
extra={"error": str(e)},
)
raise ValidationError(detail="Unable to create setup intent")

Check warning on line 131 in api/internal/owner/views.py

View check run for this annotation

Codecov Notifications / codecov/patch

api/internal/owner/views.py#L131

Added line #L131 was not covered by tests

class UsersOrderingFilter(filters.OrderingFilter):
def get_valid_fields(self, queryset, view, context=None):
fields = super().get_valid_fields(queryset, view, context=context or {})
Expand Down
24 changes: 24 additions & 0 deletions services/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
def create_checkout_session(self, owner, plan):
pass

@abstractmethod
def create_setup_intent(self, owner):
pass

Check warning on line 64 in services/billing.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/billing.py#L64

Added line #L64 was not covered by tests

@abstractmethod
def get_subscription(self, owner):
pass
Expand Down Expand Up @@ -539,6 +543,23 @@
)
return session["id"]

@_log_stripe_error
def create_setup_intent(self, owner: Owner):
log.info(

Check warning on line 548 in services/billing.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/billing.py#L548

Added line #L548 was not covered by tests
"Stripe create setup intent for owner",
extra=dict(
owner_id=owner.ownerid,
user_id=self.requesting_user.ownerid,
subscription_id=owner.stripe_subscription_id,
customer_id=owner.stripe_customer_id,
),
)
setup_intent = stripe.SetupIntent.create(

Check warning on line 557 in services/billing.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/billing.py#L557

Added line #L557 was not covered by tests
payment_method_types=['card', 'us_bank_account'],
customer=owner.stripe_customer_id,
)
return setup_intent.client_secret

Check warning on line 561 in services/billing.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/billing.py#L561

Added line #L561 was not covered by tests

@_log_stripe_error
def update_payment_method(self, owner: Owner, payment_method):
log.info(
Expand Down Expand Up @@ -722,6 +743,9 @@
def get_subscription(self, owner):
return self.payment_service.get_subscription(owner)

def get_setup_intent(self, owner):
return self.payment_service.create_setup_intent(owner)

Check warning on line 747 in services/billing.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/billing.py#L747

Added line #L747 was not covered by tests

def get_schedule(self, owner):
return self.payment_service.get_schedule(owner)

Expand Down
10 changes: 10 additions & 0 deletions services/tests/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,15 @@ def test_apply_cancellation_discount_existing_coupon(
assert not customer_modify_mock.called
assert not coupon_create_mock.called

@patch("services.billing.stripe.SetupIntent.create")
def test_get_setup_intent(self, setup_intent_create_mock):
owner = OwnerFactory(stripe_customer_id="test-customer-id")
setup_intent_create_mock.return_value = {"client_secret": "test-client-secret"}
resp = self.stripe.get_setup_intent(owner)
self.stripe.payment_service.get_setup_intent.assert_called_once_with(owner)

assert resp.client_secret == "test-client-secret"


class MockPaymentService(AbstractPaymentService):
def list_filtered_invoices(self, owner, limit=10):
Expand Down Expand Up @@ -2022,3 +2031,4 @@ def test_get_invoice(self, get_invoice_mock):
owner = OwnerFactory()
self.billing_service.get_invoice(owner, "abc")
get_invoice_mock.assert_called_once_with(owner, "abc")

Loading