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

Add disable stripe option #497

Merged
merged 1 commit into from
May 27, 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
6 changes: 3 additions & 3 deletions services/enterprise/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ DEFAULT_ENGINE_TIMEOUT=120
S3_AWS_ACCESS_KEY_ID=
S3_AWS_SECRET_ACCESS_KEY=


# Optional posthog analytics if disabled
POSTHOG_DISABLED=True
# Optional posthog analytics if disabled
POSTHOG_API_KEY=
POSTHOG_HOST=

Expand All @@ -34,7 +33,8 @@ POSTHOG_HOST=
SSH_PRIVATE_KEY_PASSWORD=
SSH_PATH_TO_CREDENTIAL_FILE=

# Optional stripe env vars if organizations are only on ENTERPRISE plan (organization.invoice_details.plan = "ENTERPRISE")
STRIPE_DISABLED=True
# Optional stripe env vars if STRIPE_DISABLED is set to False
# Otherwise you would need to create a new stripe account and fillout the env vars below
STRIPE_API_KEY=
STRIPE_WEBHOOK_SECRET=
Expand Down
1 change: 1 addition & 0 deletions services/enterprise/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __getitem__(self, key: str) -> Any:
class InvoiceSettings(BaseSettings):
load_dotenv()

stripe_disabled: bool = os.environ.get("STRIPE_DISABLED", False)
stripe_api_key: str = os.environ.get("STRIPE_API_KEY", None)
stripe_webhook_secret: str = os.environ.get("STRIPE_WEBHOOK_SECRET", None)

Expand Down
12 changes: 11 additions & 1 deletion services/enterprise/modules/organization/invoice/controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from fastapi import APIRouter, Security, status
from fastapi import APIRouter, Depends, Security, status
from starlette.requests import Request

from config import invoice_settings
from modules.organization.invoice.models.exceptions import StripeDisabledError
from modules.organization.invoice.models.requests import (
CreditRequest,
PaymentMethodRequest,
Expand All @@ -16,9 +18,17 @@
from modules.organization.invoice.webhook import InvoiceWebhook
from utils.auth import Authorize, User, authenticate_user


def check_stripe_disabled(request: Request):
if invoice_settings.stripe_disabled:
raise StripeDisabledError()
return request


router = APIRouter(
prefix="/organizations",
responses={404: {"description": "Not found"}},
dependencies=[Depends(check_stripe_disabled)],
)

authorize = Authorize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


class InvoiceErrorCode(BaseErrorCode):
stripe_disabled = ErrorCodeData(
status_code=HTTP_400_BAD_REQUEST, message="Stripe is disabled"
)
no_payment_method = ErrorCodeData(
status_code=HTTP_402_PAYMENT_REQUIRED,
message="No payment method on file",
Expand Down Expand Up @@ -59,6 +62,11 @@ class InvoiceError(BaseError):
ERROR_CODES: BaseErrorCode = InvoiceErrorCode


class StripeDisabledError(InvoiceError):
def __init__(self) -> None:
super().__init__(error_code=InvoiceErrorCode.stripe_disabled.name)


class NoPaymentMethodError(InvoiceError):
def __init__(self, organization_id: str) -> None:
super().__init__(
Expand Down
5 changes: 4 additions & 1 deletion services/enterprise/modules/organization/invoice/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ def record_usage(
quantity: int = 0,
description: str = None,
):
if invoice_settings.stripe_disabled:
return
organization = self.org_repo.get_organization(org_id)
if organization.invoice_details.plan == PaymentPlan.ENTERPRISE:
return
Expand Down Expand Up @@ -283,7 +285,8 @@ def check_usage(
type: UsageType,
quantity: int = 0,
):
# check if organization has payment method
if invoice_settings.stripe_disabled:
return
organization = self.org_repo.get_organization(org_id)
if not organization.invoice_details:
raise MissingInvoiceDetailsError(org_id)
Expand Down
2 changes: 1 addition & 1 deletion services/enterprise/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ select = [
"UP",
"W",
]
ignore = ["A001", "A002", "A003", "B008", "UP006", "UP035", "PLR0913", "N805"]
ignore = ["A001", "A002", "A003", "B008", "UP006", "UP035", "PLR0913", "N805", "C901"]
target-version = "py310"

[tool.ruff.per-file-ignores]
Expand Down
Loading