| 
									
										
										
										
											2023-12-03 20:59:29 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2023-12-18 16:59:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 20:59:29 +08:00
										 |  |  | import requests | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-18 16:59:31 +08:00
										 |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-04-25 18:20:08 +08:00
										 |  |  | from models.account import TenantAccountJoin, TenantAccountRole | 
					
						
							| 
									
										
										
										
											2023-12-18 16:59:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 20:59:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BillingService: | 
					
						
							|  |  |  |     base_url = os.environ.get('BILLING_API_URL', 'BILLING_API_URL') | 
					
						
							|  |  |  |     secret_key = os.environ.get('BILLING_API_SECRET_KEY', 'BILLING_API_SECRET_KEY') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def get_info(cls, tenant_id: str): | 
					
						
							|  |  |  |         params = {'tenant_id': tenant_id} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-12 17:59:07 +08:00
										 |  |  |         billing_info = cls._send_request('GET', '/subscription/info', params=params) | 
					
						
							| 
									
										
										
										
											2023-12-03 20:59:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return billing_info | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2023-12-05 16:53:55 +08:00
										 |  |  |     def get_subscription(cls, plan: str, | 
					
						
							|  |  |  |                          interval: str, | 
					
						
							|  |  |  |                          prefilled_email: str = '', | 
					
						
							|  |  |  |                          tenant_id: str = ''): | 
					
						
							| 
									
										
										
										
											2023-12-03 20:59:29 +08:00
										 |  |  |         params = { | 
					
						
							|  |  |  |             'plan': plan, | 
					
						
							|  |  |  |             'interval': interval, | 
					
						
							|  |  |  |             'prefilled_email': prefilled_email, | 
					
						
							|  |  |  |             'tenant_id': tenant_id | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-12 17:59:07 +08:00
										 |  |  |         return cls._send_request('GET', '/subscription/payment-link', params=params) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def get_model_provider_payment_link(cls, | 
					
						
							|  |  |  |                                         provider_name: str, | 
					
						
							|  |  |  |                                         tenant_id: str, | 
					
						
							| 
									
										
										
										
											2024-01-26 18:26:15 +08:00
										 |  |  |                                         account_id: str, | 
					
						
							|  |  |  |                                         prefilled_email: str): | 
					
						
							| 
									
										
										
										
											2023-12-12 17:59:07 +08:00
										 |  |  |         params = { | 
					
						
							|  |  |  |             'provider_name': provider_name, | 
					
						
							|  |  |  |             'tenant_id': tenant_id, | 
					
						
							| 
									
										
										
										
											2024-01-26 18:26:15 +08:00
										 |  |  |             'account_id': account_id, | 
					
						
							|  |  |  |             'prefilled_email': prefilled_email | 
					
						
							| 
									
										
										
										
											2023-12-12 17:59:07 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         return cls._send_request('GET', '/model-provider/payment-link', params=params) | 
					
						
							| 
									
										
										
										
											2023-12-03 20:59:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-01-26 18:26:15 +08:00
										 |  |  |     def get_invoices(cls, prefilled_email: str = '', tenant_id: str = ''): | 
					
						
							|  |  |  |         params = { | 
					
						
							|  |  |  |             'prefilled_email': prefilled_email, | 
					
						
							|  |  |  |             'tenant_id': tenant_id | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-03 20:59:29 +08:00
										 |  |  |         return cls._send_request('GET', '/invoices', params=params) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def _send_request(cls, method, endpoint, json=None, params=None): | 
					
						
							|  |  |  |         headers = { | 
					
						
							|  |  |  |             "Content-Type": "application/json", | 
					
						
							|  |  |  |             "Billing-Api-Secret-Key": cls.secret_key | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         url = f"{cls.base_url}{endpoint}" | 
					
						
							|  |  |  |         response = requests.request(method, url, json=json, params=params, headers=headers) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response.json() | 
					
						
							| 
									
										
										
										
											2023-12-18 16:59:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-01-26 18:26:15 +08:00
										 |  |  |     def is_tenant_owner_or_admin(current_user): | 
					
						
							| 
									
										
										
										
											2023-12-18 16:59:31 +08:00
										 |  |  |         tenant_id = current_user.current_tenant_id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         join = db.session.query(TenantAccountJoin).filter( | 
					
						
							|  |  |  |             TenantAccountJoin.tenant_id == tenant_id, | 
					
						
							|  |  |  |             TenantAccountJoin.account_id == current_user.id | 
					
						
							|  |  |  |         ).first() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-25 21:55:08 +08:00
										 |  |  |         if not TenantAccountRole.is_privileged_role(join.role): | 
					
						
							| 
									
										
										
										
											2024-01-26 18:26:15 +08:00
										 |  |  |             raise ValueError('Only team owner or team admin can perform this action') |