feat: stirpe subscription re checkout

fix: other small changes
This commit is contained in:
deepvasoya 2025-05-29 11:00:49 +05:30
parent e6fbae493b
commit 4df268c8ac
4 changed files with 31 additions and 15 deletions

View File

@ -217,7 +217,7 @@ class AuthService:
if not user: if not user:
raise ResourceNotFoundException("User not found") raise ResourceNotFoundException("User not found")
user.username = data.username.lower() user.username = data.username
self.db.add(user) self.db.add(user)
self.db.commit() self.db.commit()

View File

@ -79,7 +79,7 @@ class MasterAppointmentServices:
if existing_appointment_type and existing_appointment_type.id != appointment_type_id: if existing_appointment_type and existing_appointment_type.id != appointment_type_id:
raise ResourceNotFoundException("Appointment type already exists") raise ResourceNotFoundException("Appointment type already exists")
appointment_type_db.type = appointment_type.type appointment_type_db.type = appointment_type.type.lower()
self.db.add(appointment_type_db) self.db.add(appointment_type_db)
self.db.commit() self.db.commit()

View File

@ -30,6 +30,7 @@ class StripeServices:
self.db: Session = next(get_db()) self.db: Session = next(get_db())
self.logger = logger self.logger = logger
self.webhook_secret = os.getenv("STRIPE_WEBHOOK_SECRET") self.webhook_secret = os.getenv("STRIPE_WEBHOOK_SECRET")
self.redirect_url = os.getenv("FRONTEND_URL")
self.dashboard_service = DashboardService() self.dashboard_service = DashboardService()
async def create_customer(self, user_id: int, email: str, name: str): async def create_customer(self, user_id: int, email: str, name: str):
@ -214,8 +215,8 @@ class StripeServices:
'payment_method_types': ['card'], 'payment_method_types': ['card'],
'mode': 'subscription', 'mode': 'subscription',
'line_items': line_items, 'line_items': line_items,
'success_url': 'http://54.79.156.66/', 'success_url': self.redirect_url,
'cancel_url': 'http://54.79.156.66/', 'cancel_url': self.redirect_url,
'metadata': metadata, 'metadata': metadata,
'subscription_data': { 'subscription_data': {
'metadata': metadata 'metadata': metadata
@ -268,7 +269,13 @@ class StripeServices:
self.logger.info("Async payment succeeded") self.logger.info("Async payment succeeded")
elif event["type"] == "checkout.session.completed": elif event["type"] == "checkout.session.completed":
self.update_payment_log(event["data"]["object"]["metadata"]["unique_clinic_id"], event["data"]["object"]["metadata"]["clinic_id"]) unique_clinic_id = event["data"]["object"]["metadata"]["unique_clinic_id"]
clinic_id = event["data"]["object"]["metadata"]["clinic_id"]
customer_id = event["data"]["object"]["metadata"]["customer_id"]
account_id = event["data"]["object"]["metadata"]["account_id"]
total = event["data"]["object"]["amount_total"]
metadata = event["data"]["object"]["metadata"]
self.update_payment_log(unique_clinic_id, clinic_id, customer_id, account_id, total, metadata)
# TODO: handle subscription period end # TODO: handle subscription period end
@ -280,18 +287,27 @@ class StripeServices:
finally: finally:
self.db.close() self.db.close()
def update_payment_log(self, unique_clinic_id:str, clinic_id:int): def update_payment_log(self, unique_clinic_id:str, clinic_id:int, customer_id:str, account_id:str, total:float, metadata:any):
try: try:
payment_log = self.db.query(PaymentLogs).filter(PaymentLogs.unique_clinic_id == unique_clinic_id).first()
self.db.query(PaymentSessions).filter(PaymentSessions.clinic_id == clinic_id).delete() self.db.query(PaymentSessions).filter(PaymentSessions.clinic_id == clinic_id).delete()
if payment_log:
payment_log.payment_status = "success"
self.db.commit()
clinic = self.db.query(Clinics).filter(Clinics.id == clinic_id).first() payment_log = PaymentLogs(
if clinic: customer_id=customer_id,
clinic.status = ClinicStatus.UNDER_REVIEW account_id=account_id,
self.db.commit() amount=total,
clinic_id=clinic_id,
unique_clinic_id=unique_clinic_id,
payment_status="paid",
metadata_logs=json.dumps(metadata.to_dict())
)
self.db.add(payment_log)
self.db.commit()
clinic = self.db.query(Clinics).filter(Clinics.id == clinic_id).first()
if clinic:
clinic.status = ClinicStatus.UNDER_REVIEW
self.db.add(clinic)
self.db.commit()
except Exception as e: except Exception as e:
self.logger.error(f"Error updating payment log: {e}") self.logger.error(f"Error updating payment log: {e}")

View File

@ -154,7 +154,7 @@ class UserServices:
fees_to_be["per_call_charges"] = offer.per_call_charges fees_to_be["per_call_charges"] = offer.per_call_charges
fees_to_be["total"] = offer.setup_fees + fees_to_be["subscription_fees"] + offer.per_call_charges fees_to_be["total"] = offer.setup_fees + fees_to_be["subscription_fees"] + offer.per_call_charges
payment_link = await self.stripe_service.create_subscription_checkout(fees_to_be, 1, stripe_account.id,stripe_customer.id) payment_link = await self.stripe_service.create_subscription_checkout(fees_to_be, new_clinic.id, stripe_account.id,stripe_customer.id)
return payment_link.url return payment_link.url
except Exception as e: except Exception as e: