import os from PyQt6.QtWidgets import QApplication, QButtonGroup, QMessageBox, QPushButton from PyQt6.QtGui import QIcon from PyQt6.QtCore import QSize from PyQt6 import QtCore from core.services.prepare_tickets.ticket_tracker import delete_ticket_data from core.controllers.tickets.TicketPayController import check_if_paid from core.models.Result import Result, ResultError from gui.v2.infrastructure.setup_observers import connection_observer, ticket_observer from gui.v2.ui.pages.Page import Page from gui.v2.ui.popups.message_box import style_message_box, mark_confirm_button from gui.v2.workers.ticketing_worker_thread import TicketingWorkerThread HOW_MANY_PROFILES_DEFAULT = 6 class TicketCryptoPickerPage(Page): def __init__(self, page_stack, main_window=None, parent=None): super().__init__("TicketCrypto", page_stack, main_window, parent) self.update_status = main_window self.selected_plan = None self.bypass_existing = False self.worker = None self.title.setText("Payment Method") self.title.setGeometry(QtCore.QRect(510, 30, 250, 40)) self.title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter) self.button_reverse.setVisible(True) self.button_reverse.clicked.connect(self.reverse) button_info = [ ("monero", "monero", (545, 75)), ("bitcoin", "bitcoin", (545, 290)), ("lightning", "lightnering", (545, 180)), ("litecoin", "litecoin", (545, 395)), ] self.buttonGroup = QButtonGroup(self) self.buttons = [] for j, (currency, icon_name, position) in enumerate(button_info): button = QPushButton(self) button.setGeometry(position[0], position[1], 185, 75) button.setIconSize(QSize(190, 120)) button.setCheckable(True) button.setIcon(QIcon(os.path.join(self.btn_path, f"{icon_name}.png"))) button.setProperty('currency', currency) self.buttons.append(button) self.buttonGroup.addButton(button, j) button.clicked.connect(self.on_currency_selected) def set_selected_plan(self, plan_key): self.selected_plan = plan_key self.bypass_existing = False self.update_status.update_status(f"Selected plan: {plan_key}") for btn in self.buttons: btn.setChecked(False) def on_currency_selected(self): selected_button = self.buttonGroup.checkedButton() if not selected_button or not self.selected_plan: return currency = selected_button.property('currency') self.start_initiate_payment(currency) def check_if_paid_for_existing(self, temp_billing_code): self.update_status.update_status("Checking if paid...") result = check_if_paid(temp_billing_code, ticket_observer, connection_observer) if isinstance(result, dict) and result.get('valid') and result.get('payment_status') == 'paid': self.update_status.update_status("Already Paid!") self.custom_window.navigator.navigate("ticket_prep") prep_page = self.custom_window.navigator.get_cached("ticket_prep") if prep_page: prep_page.start_prep() else: self.update_status.update_status("Not yet paid.") clipboard = QApplication.clipboard() clipboard.setText(temp_billing_code) not_paid_msg = f"The billing code is not yet showing paid for {temp_billing_code}. Right now, your clipboard has the billing code, to paste it in any text editor. If you did pay, either wait longer for blockchain confirmation, or contact customer support with the code in your clipboard now." info = QMessageBox(self) info.setWindowTitle("Not Paid") info.setText(not_paid_msg) info.setStandardButtons(QMessageBox.StandardButton.Ok) style_message_box(info) mark_confirm_button(info.button(QMessageBox.StandardButton.Ok)) info.exec() def start_initiate_payment(self, currency): self.update_status.update_status("Initiating payment...") self.worker = TicketingWorkerThread('INITIATE_PAYMENT', params={ 'how_many_profiles': HOW_MANY_PROFILES_DEFAULT, 'which_key': self.selected_plan, 'which_cryptocurrency': currency, 'bypass_existing': self.bypass_existing, }) self.worker.invoice_ready.connect(self.on_invoice_ready) self.worker.error.connect(self.on_error) self.worker.start() def on_invoice_ready(self, invoice): if invoice is None or invoice is False: self.update_status.update_status("Could not initiate payment.") return if not invoice.valid: return self._handle_api_errors(invoice) # error_code = getattr(invoice, 'error_code', None) self.custom_window.navigator.navigate("payment_details") payment_page = self.custom_window.navigator.get_cached("payment_details") if payment_page is not None: payment_page.set_ticket_invoice(invoice.data, self.selected_plan) def _handle_api_errors(self, invoice: Result): error_code = invoice.error_type if error_code == ResultError.ALREADY_EXISTS and not self.bypass_existing: self._prompt_wipe_existing(invoice) return elif error_code == ResultError.BILLING_CODE_EXISTS and not self.bypass_existing: temp_billing_code = getattr(invoice, 'temp_billing_code', None) print(f"temp_billing_code is {temp_billing_code}") if temp_billing_code: self._prompt_wipe_billingcode(temp_billing_code) return else: self._prompt_wipe_billingcode("NONE") return else: error_msg = invoice.message # msg = getattr(invoice, 'final_error_msg', None) or error_code self.update_status.update_status(error_msg) return def _prompt_wipe_existing(self, invoice): msg = QMessageBox(self) msg.setWindowTitle("Existing tickets found") msg.setText("You already have ticket data. Wipe it and start over?") msg.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) style_message_box(msg) mark_confirm_button(msg.button(QMessageBox.StandardButton.Yes)) result = msg.exec() if result == QMessageBox.StandardButton.Yes: self.bypass_existing = True delete_ticket_data() currency_btn = self.buttonGroup.checkedButton() if currency_btn: self.start_initiate_payment(currency_btn.property('currency')) else: self.update_status.update_status("Cancelled.") def _prompt_wipe_billingcode(self, temp_billing_code): msg = QMessageBox(self) msg.setWindowTitle("Existing billing code found") msg.setText("You already have a ticket billing code. Do you want to use it? Only hit YES if you ALREADY paid.") msg.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) style_message_box(msg) mark_confirm_button(msg.button(QMessageBox.StandardButton.Yes)) result = msg.exec() if result == QMessageBox.StandardButton.Yes: self.update_status.update_status("Reusing same code") self.check_if_paid_for_existing(temp_billing_code) else: self.bypass_existing = True delete_ticket_data() currency_btn = self.buttonGroup.checkedButton() if currency_btn: self.start_initiate_payment(currency_btn.property('currency')) def on_error(self, msg): self.update_status.update_status(f"Payment error: {msg}") def reverse(self): self.custom_window.navigator.navigate("plan_picker")