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: pass through billing query params #26714

Merged
merged 4 commits into from
Dec 6, 2024
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
5 changes: 4 additions & 1 deletion ee/api/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def list(self, request: Request, *args: Any, **kwargs: Any) -> Response:

plan_keys = request.query_params.get("plan_keys", None)
billing_manager = self.get_billing_manager()
response = billing_manager.get_billing(org, plan_keys)
query = {}
if "include_forecasting" in request.query_params:
query["include_forecasting"] = request.query_params.get("include_forecasting")
response = billing_manager.get_billing(org, plan_keys, query)

return Response(response)

Expand Down
33 changes: 32 additions & 1 deletion ee/api/test/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,37 @@ 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}

@patch("ee.api.billing.requests.get")
def test_billing_with_supported_params(self, mock_get):
"""Test that the include_forecasting param is passed through to the billing service."""

def mock_implementation(url: str, headers: Any = None, params: Any = None) -> MagicMock:
mock = MagicMock()
mock.status_code = 200

if "api/billing/portal" in url:
mock.json.return_value = {"url": "https://billing.stripe.com/p/session/test_1234"}
elif "api/billing" in url:
mock.json.return_value = create_billing_response(
customer=create_billing_customer(has_active_subscription=True)
)

return mock

mock_get.side_effect = mock_implementation

response = self.client.get("/api/billing/?include_forecasting=true")
assert response.status_code == 200

# Verify the billing service was called with the correct query param
billing_calls = [
call
for call in mock_get.call_args_list
if "api/billing" in call[0][0] and "api/billing/portal" not in call[0][0]
]
assert len(billing_calls) == 1
assert billing_calls[0].kwargs["params"] == {"include_forecasting": "true"}


class TestPortalBillingAPI(APILicensedTest):
@patch("ee.api.billing.requests.get")
Expand Down Expand Up @@ -932,7 +963,7 @@ def test_deactivate_success(self, mock_get_billing, mock_deactivate_products):

self.assertEqual(response.status_code, status.HTTP_200_OK)
mock_deactivate_products.assert_called_once_with(self.organization, "product_1")
mock_get_billing.assert_called_once_with(self.organization, None)
mock_get_billing.assert_called_once_with(self.organization, None, {})

def test_deactivate_failure(self):
url = "/api/billing/deactivate"
Expand Down
12 changes: 9 additions & 3 deletions ee/billing/billing_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ def __init__(self, license, user: Optional[User] = None):
self.license = license or get_cached_instance_license()
self.user = user

def get_billing(self, organization: Optional[Organization], plan_keys: Optional[str]) -> dict[str, Any]:
def get_billing(
self,
organization: Optional[Organization],
plan_keys: Optional[str],
query_params: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
if organization and self.license and self.license.is_v2_license:
billing_service_response = self._get_billing(organization)
billing_service_response = self._get_billing(organization, query_params)

# Ensure the license and org are updated with the latest info
if billing_service_response.get("license"):
Expand Down Expand Up @@ -225,7 +230,7 @@ def update_license_details(self, billing_status: BillingStatus) -> License:

return self.license

def _get_billing(self, organization: Organization) -> BillingStatus:
def _get_billing(self, organization: Organization, query_params: Optional[dict[str, Any]] = None) -> BillingStatus:
"""
Retrieves billing info and updates local models if necessary
"""
Expand All @@ -235,6 +240,7 @@ def _get_billing(self, organization: Organization) -> BillingStatus:
res = requests.get(
f"{BILLING_SERVICE_URL}/api/billing",
headers=self.get_auth_headers(organization),
params=query_params,
)
handle_billing_service_error(res)

Expand Down
Loading