Skip to content

Commit

Permalink
feat: add a portal redirect endpoint (#23375)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Eric Duong <[email protected]>
Co-authored-by: Dylan Martin <[email protected]>
Co-authored-by: Sandy Spicer <[email protected]>
Co-authored-by: Robbie <[email protected]>
Co-authored-by: Xavier Vello <[email protected]>
Co-authored-by: Paul D'Ambra <[email protected]>
Co-authored-by: Marcus Hof <[email protected]>
Co-authored-by: Juraj Majerik <[email protected]>
Co-authored-by: Tom Owers <[email protected]>
Co-authored-by: Phani Raj <[email protected]>
  • Loading branch information
12 people authored Jul 2, 2024
1 parent 0c176a5 commit c62058c
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1955,5 +1955,5 @@
"discount_amount_usd": null,
"amount_off_expires_at": null,
"never_drop_data": null,
"stripe_portal_url": "https://billing.stripe.com/p/session/test_YWNjdF8xSElNRERFdUlhdFJYU2R6LF9PdXhKRW1kYVNRb3BOcFpVVHl6emVCUjdSUDJrQTJs0100akrliUs0"
"stripe_portal_url": "http://localhost:8000/api/billing/portal"
}
2 changes: 1 addition & 1 deletion cypress/fixtures/api/billing/billing-unsubscribed.json
Original file line number Diff line number Diff line change
Expand Up @@ -3657,5 +3657,5 @@
"session_replay": 3,
"product_analytics": 3
},
"stripe_portal_url": "https://billing.stripe.com/p/session/test_YWNjdF8xSElNRERFdUlhdFJYU2R6LF9QaEVaQ0hCTUE0aE8wUFhlVWVqd29MaElGd3lwRjFa010044U4IxJp"
"stripe_portal_url": "http://localhost:8000/api/billing/portal"
}
2 changes: 1 addition & 1 deletion cypress/fixtures/api/billing/billing.json
Original file line number Diff line number Diff line change
Expand Up @@ -3777,5 +3777,5 @@
"session_replay": 3,
"product_analytics": 3
},
"stripe_portal_url": "https://billing.stripe.com/p/session/test_YWNjdF8xSElNRERFdUlhdFJYU2R6LF9QaEVpVHFJNXdKYk9DaG04SVhMaUV4TDlxOTR1WEZi0100SMJCDr2e"
"stripe_portal_url": "http://localhost:8000/api/billing/portal"
}
14 changes: 14 additions & 0 deletions ee/api/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ def deactivate(self, request: Request, *args: Any, **kwargs: Any) -> HttpRespons

return self.list(request, *args, **kwargs)

@action(methods=["GET"], detail=False)
def portal(self, request: Request, *args: Any, **kwargs: Any) -> HttpResponse:
license = get_cached_instance_license()
if not license:
return Response(
{"sucess": True},
status=status.HTTP_200_OK,
)

organization = self._get_org_required()

res = BillingManager(license)._get_stripe_portal_url(organization)
return redirect(res)

@action(methods=["GET"], detail=False)
def get_invoices(self, request: Request, *args: Any, **kwargs: Any) -> HttpResponse:
license = get_cached_instance_license()
Expand Down
16 changes: 14 additions & 2 deletions ee/api/test/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
"available_product_features": [],
"custom_limits_usd": {},
"has_active_subscription": True,
"stripe_portal_url": "https://billing.stripe.com/p/session/test_1234",
"stripe_portal_url": "http://localhost:8000/api/billing/portal",
"current_total_amount_usd": "100.00",
"deactivated": False,
"products": [
Expand Down Expand Up @@ -562,7 +562,7 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
"free_trial_until": None,
"current_total_amount_usd": "0.00",
"deactivated": False,
"stripe_portal_url": "https://billing.stripe.com/p/session/test_1234",
"stripe_portal_url": "http://localhost:8000/api/billing/portal",
}

@patch("ee.api.billing.requests.get")
Expand Down Expand Up @@ -839,6 +839,18 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
assert self.organization.customer_trust_scores == {"recordings": 0, "events": 15, "rows_synced": 0}


class TestPortalBillingAPI(APILicensedTest):
@patch("ee.api.billing.requests.get")
def test_portal_success(self, mock_request):
mock_request.return_value.status_code = 200
mock_request.return_value.json.return_value = {"url": "https://billing.stripe.com/p/session/test_1234"}

response = self.client.get("/api/billing/portal")

self.assertEqual(response.status_code, status.HTTP_302_FOUND)
self.assertIn("https://billing.stripe.com/p/session/test_1234", response.url)


class TestActivateBillingAPI(APILicensedTest):
def test_activate_success(self):
url = "/api/billing/activate"
Expand Down
6 changes: 3 additions & 3 deletions ee/billing/billing_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from datetime import datetime, timedelta
from enum import Enum
from typing import Any, Optional, cast
Expand Down Expand Up @@ -85,8 +86,7 @@ def get_billing(self, organization: Optional[Organization], plan_keys: Optional[
products = self.get_default_products(organization)
response["products"] = products["products"]

stripe_portal_url = self._get_stripe_portal_url(organization)
response["stripe_portal_url"] = stripe_portal_url
response["stripe_portal_url"] = f"{settings.SITE_URL}/api/billing/portal"

# Extend the products with accurate usage_limit info
for product in response["products"]:
Expand Down Expand Up @@ -207,7 +207,7 @@ def _get_billing(self, organization: Organization) -> BillingStatus:

return data

def _get_stripe_portal_url(self, organization: Organization) -> BillingStatus:
def _get_stripe_portal_url(self, organization: Organization) -> str:
"""
Retrieves stripe protal url
"""
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion frontend/src/mocks/fixtures/_billing_unsubscribed.json
Original file line number Diff line number Diff line change
Expand Up @@ -1872,5 +1872,5 @@
"discount_amount_usd": null,
"amount_off_expires_at": null,
"never_drop_data": null,
"stripe_portal_url": "https://billing.stripe.com/p/session/test_YWNjdF8xSElNRERFdUlhdFJYU2R6LF9QN0ltZVQ3RmpLbTZacXgzYWo3Q0FFbFpITHZydlpK0100iKmkfAZi"
"stripe_portal_url": "http://localhost:8000/api/billing/portal"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2769,5 +2769,5 @@
]
}
],
"stripe_portal_url": "https://billing.stripe.com/p/session/XYZ"
"stripe_portal_url": "http://localhost:8000/api/billing/portal"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4683,5 +4683,5 @@
"session_replay": 3,
"product_analytics": 3
},
"stripe_portal_url": "https://billing.stripe.com/p/session/test_YWNjdF8xSElNRERFdUlhdFJYU2R6LF9RNE9kdEg5elRmWE1wZkY1SGlsTFFFQXRKRmVkdnpJ0100M2MwqzHs"
"stripe_portal_url": "http://localhost:8000/api/billing/portal"
}
2 changes: 1 addition & 1 deletion frontend/src/mocks/fixtures/_billing_with_discount.json
Original file line number Diff line number Diff line change
Expand Up @@ -2767,5 +2767,5 @@
]
}
],
"stripe_portal_url": "https://billing.stripe.com/p/session/XYZ"
"stripe_portal_url": "http://localhost:8000/api/billing/portal"
}
2 changes: 1 addition & 1 deletion frontend/src/mocks/fixtures/_billing_with_flat_fee.json
Original file line number Diff line number Diff line change
Expand Up @@ -1447,5 +1447,5 @@
"discount_amount_usd": null,
"amount_off_expires_at": null,
"never_drop_data": false,
"stripe_portal_url": "https://billing.stripe.com/p/session/live_YWNjdF8xSElNRERFdUlhdFJYU2R6LF9PeG9wUmZuUFpmcTlvaWVjeUd0ZlhFN2hHbjJUWDR30100sm6GDMjx"
"stripe_portal_url": "http://localhost:8000/api/billing/portal"
}
1 change: 1 addition & 0 deletions frontend/src/scenes/billing/Billing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export function Billing(): JSX.Element {
htmlType="submit"
to={billing.stripe_portal_url}
disableClientSideRouting
targetBlank
center
>
Manage card details and view past invoices
Expand Down

0 comments on commit c62058c

Please sign in to comment.