2025-11-20 15:58:05 +08:00
|
|
|
import base64
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2025-11-20 15:58:05 +08:00
|
|
|
from flask_restx import Resource, fields, reqparse
|
|
|
|
|
from werkzeug.exceptions import BadRequest
|
|
|
|
|
|
|
|
|
|
from controllers.console import api, console_ns
|
2024-11-01 15:51:22 +08:00
|
|
|
from controllers.console.wraps import account_initialization_required, only_edition_cloud, setup_required
|
2025-11-03 11:51:09 +08:00
|
|
|
from enums.cloud_plan import CloudPlan
|
2025-10-16 15:45:51 +09:00
|
|
|
from libs.login import current_account_with_tenant, login_required
|
2023-12-03 20:59:29 +08:00
|
|
|
from services.billing_service import BillingService
|
|
|
|
|
|
|
|
|
|
|
2025-09-28 13:37:06 +08:00
|
|
|
@console_ns.route("/billing/subscription")
|
2023-12-03 20:59:29 +08:00
|
|
|
class Subscription(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
@only_edition_cloud
|
|
|
|
|
def get(self):
|
2025-10-16 15:45:51 +09:00
|
|
|
current_user, current_tenant_id = current_account_with_tenant()
|
2025-10-19 12:54:41 +09:00
|
|
|
parser = (
|
|
|
|
|
reqparse.RequestParser()
|
2025-11-03 11:51:09 +08:00
|
|
|
.add_argument(
|
|
|
|
|
"plan",
|
|
|
|
|
type=str,
|
|
|
|
|
required=True,
|
|
|
|
|
location="args",
|
|
|
|
|
choices=[CloudPlan.PROFESSIONAL, CloudPlan.TEAM],
|
|
|
|
|
)
|
2025-10-19 12:54:41 +09:00
|
|
|
.add_argument("interval", type=str, required=True, location="args", choices=["month", "year"])
|
|
|
|
|
)
|
2023-12-03 20:59:29 +08:00
|
|
|
args = parser.parse_args()
|
2024-01-26 18:26:15 +08:00
|
|
|
BillingService.is_tenant_owner_or_admin(current_user)
|
2025-10-16 15:45:51 +09:00
|
|
|
return BillingService.get_subscription(args["plan"], args["interval"], current_user.email, current_tenant_id)
|
2023-12-03 20:59:29 +08:00
|
|
|
|
|
|
|
|
|
2025-09-28 13:37:06 +08:00
|
|
|
@console_ns.route("/billing/invoices")
|
2023-12-03 20:59:29 +08:00
|
|
|
class Invoices(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
@only_edition_cloud
|
|
|
|
|
def get(self):
|
2025-10-16 15:45:51 +09:00
|
|
|
current_user, current_tenant_id = current_account_with_tenant()
|
2024-01-26 18:26:15 +08:00
|
|
|
BillingService.is_tenant_owner_or_admin(current_user)
|
2025-10-16 15:45:51 +09:00
|
|
|
return BillingService.get_invoices(current_user.email, current_tenant_id)
|
2025-11-20 15:58:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@console_ns.route("/billing/partners/<string:partner_key>/tenants")
|
|
|
|
|
class PartnerTenants(Resource):
|
|
|
|
|
@api.doc("sync_partner_tenants_bindings")
|
|
|
|
|
@api.doc(description="Sync partner tenants bindings")
|
|
|
|
|
@api.doc(params={"partner_key": "Partner key"})
|
|
|
|
|
@api.expect(
|
|
|
|
|
api.model(
|
|
|
|
|
"SyncPartnerTenantsBindingsRequest",
|
|
|
|
|
{"click_id": fields.String(required=True, description="Click Id from partner referral link")},
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
@api.response(200, "Tenants synced to partner successfully")
|
|
|
|
|
@api.response(400, "Invalid partner information")
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
@only_edition_cloud
|
|
|
|
|
def put(self, partner_key: str):
|
|
|
|
|
current_user, _ = current_account_with_tenant()
|
|
|
|
|
parser = reqparse.RequestParser().add_argument("click_id", required=True, type=str, location="json")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
click_id = args["click_id"]
|
|
|
|
|
decoded_partner_key = base64.b64decode(partner_key).decode("utf-8")
|
|
|
|
|
except Exception:
|
|
|
|
|
raise BadRequest("Invalid partner_key")
|
|
|
|
|
|
|
|
|
|
if not click_id or not decoded_partner_key or not current_user.id:
|
|
|
|
|
raise BadRequest("Invalid partner information")
|
|
|
|
|
|
|
|
|
|
return BillingService.sync_partner_tenants_bindings(current_user.id, decoded_partner_key, click_id)
|