hydraveil-gui/gui/v2/actions/payment_actions.py

95 lines
3.3 KiB
Python

from core.services.payment_phase.payment_state import PaymentData
from core.services.subscriptions.check_payment_loop import CheckPaidTimer
from core.services.subscriptions.subscriptions_by_protocol import create_invoice, save_billing_code, return_pre_existing_payment_data, get_profile_subscription
from core.models.enum_models import PaymentType
from core.models.pydantic_models.ServerReply import SubscriptionInvoice
from core.models.Result import Result
from core.models.Subscription import Subscription
from core.errors.logger import logger
# gui
from gui.v2.infrastructure.ThreadSafetyTool import ThreadSafetyTool
from gui.v2.infrastructure.take_plumbing_action import take_action
from gui.v2.infrastructure.setup_observers import connection_observer
from typing import Callable, Optional
def gui_starts_invoice(profile_id: int, ui_caller: Callable) -> Result:
"""Create a new invoice if we don't have one yet, or re-use the saved data."""
subscription = get_profile_subscription(profile_id)
print(f"in gui_starts_invoice, subscription is {subscription}")
if not subscription:
logger.info(f"Starting a New Invoice for Profile {profile_id}..")
take_action(
action=create_invoice,
ui_caller=ui_caller,
profile_id=profile_id,
connection_observer=connection_observer
)
return Result(valid=True)
else:
return return_pre_existing_payment_data(profile_id=profile_id, subscription=subscription)
def save_and_begin_timer(
ticketing: bool,
gui_callback: Callable,
billing_code: str,
profile_id: Optional[int] = None,
expires_at: Optional[str] = None,
) -> bool:
"""
First saves the billing code & profile expiration, if it's an individual (non-ticket) crypto-based one.
Then it Starts a loop of checking if the invoice is paid and saves it.
"""
if ticketing:
payment_type = PaymentType.TICKETS
logger.error("Not yet setup for ticketing")
return False
# NON-ticketing:
elif profile_id and expires_at:
payment_type = PaymentType.CRYPTO
saved = save_billing_code(profile_id=profile_id, billing_code=billing_code, expires_at=expires_at)
if not saved.valid:
return False
else:
logger.error("You need a profile id and expires_at for individual profiles")
return False
# Setup PyQt6 GUI as mainthread to bring back to:
pyqt_threader = ThreadSafetyTool()
# setup the timer with the callback:
# it doesn't return result here. It calls the gui_callback function.
check_paid_timer = CheckPaidTimer(
view_callback=gui_callback,
billing_code=billing_code,
ui_dispatcher=pyqt_threader,
payment_type=payment_type,
connection_observer=connection_observer
)
return True # this is just that it started
# def create_an_invoice(ui_caller: Callable, profile_id: int, connection_observer: ConnectionObserver) -> SubscriptionInvoice|str:
# """Create an Invoice for regular single profile billing"""
# take_action(
# action=create_invoice,
# ui_caller=ui_caller,
# profile_id=profile_id,
# connection_observer=connection_observer
# )
# subscription_plan_id=subscription_plan_id,
# location_id=location_id,
# protocol_choice=protocol_choice,
# currency=currency_choice,