"""Payee KYC logic.""" from abacus_common_logic.utils.features import is_feature_enabled from src.connectors.ows_abacus_account import ( get_account_payee, get_account_payment_term, update_account_payee, ) from src.connectors.ows_abacus_contract import get_reference_payment_entity from src.connectors.ows_payee import create_kyc_notification, delete_kyc_notifications from src.constants import ( AWAL_PAYMENT_ENTITY_PREFIX, ReferencePaymentType, TAP_AWAL_PAYONEER_MIGRATION, ) from src.constants.abacus_state import ACTION_STATUSES from src.constants.kyc_notification import KYC_MESSAGE from src.logic.abacus_state import set_states from src.models.ows_abacus_account import AccountPayeeUpdateModel from src.models.payee import Payee, PayeeEntityType def _is_awal_account(account_id: int) -> bool: """Check if account belongs to an AWAL payment entity.""" account_payment_term = get_account_payment_term(account_id) if not account_payment_term.payment_entity_id: return False payment_entity = get_reference_payment_entity( account_payment_term.payment_entity_id ) return payment_entity.payment_entity_name.startswith(AWAL_PAYMENT_ENTITY_PREFIX) def handle_kyc_approved(payee: Payee) -> None: """Handle payee KYC approved.""" params = {} if payee.payee_type == PayeeEntityType.account_payee: account_payee_id = int(payee.payee_id) account_payee = get_account_payee(account_payee_id) params['banking_details_review_status'] = ACTION_STATUSES.APPROVED params['payment_eligibility_status'] = ACTION_STATUSES.RUNNING # AWAL payees skip the internal Sony Finance manual review step # (payment_eligibility goes straight to APPROVED instead of # RUNNING) to ensure immediate payment eligibility post-KYC. # Non-AWAL (Orchard) payees still require Finance manual review. if is_feature_enabled(TAP_AWAL_PAYONEER_MIGRATION) and _is_awal_account( account_payee.account_id ): params['payment_eligibility_status'] = ACTION_STATUSES.APPROVED elif payee.payee_type == PayeeEntityType.payee: params['banking_eligibility_status'] = ACTION_STATUSES.COMPLETE if set_states(payee, KYC_MESSAGE.APPROVED, **params): delete_kyc_notifications(payee) if payee.payee_type == PayeeEntityType.account_payee: if account_payee.reference_payment_type_id == ReferencePaymentType.PAYCHEX: update_account_payee( account_payee_id, AccountPayeeUpdateModel( reference_payment_type_id=ReferencePaymentType.PAYONEER_WHITELABEL ), ) def handle_kyc_declined(payee: Payee, reason: str | None) -> None: """Handle payee KYC declined.""" params = {} if payee.payee_type == PayeeEntityType.account_payee: params['banking_details_review_status'] = ACTION_STATUSES.REJECTED elif payee.payee_type == PayeeEntityType.payee: params['banking_eligibility_status'] = ACTION_STATUSES.REJECTED if set_states(payee, reason or KYC_MESSAGE.DECLINED, **params): delete_kyc_notifications(payee) def handle_kyc_notification( payee: Payee, file_upload_link: str, requirement_id: str | None, sub_requirement_id: str | None, ) -> None: """Handle payee KYC notification.""" params = {} if payee.payee_type == PayeeEntityType.account_payee: params['banking_details_review_status'] = ACTION_STATUSES.RUNNING elif payee.payee_type == PayeeEntityType.payee: params['banking_eligibility_status'] = ACTION_STATUSES.RUNNING if set_states(payee, KYC_MESSAGE.NOTIFICATION, **params): delete_kyc_notifications(payee) create_kyc_notification( payee=payee, file_upload_link=file_upload_link, requirement_id=requirement_id, sub_requirement_id=sub_requirement_id, ) def handle_kyc_requirement_reopen( payee: Payee, file_upload_link: str, requirement_type_id: int | None, sub_requirement_status_id: int | None, possible_sub_requirement_types: str | None, entity_reference_id: str | None, entity_reference_type_id: int | None, ) -> None: """Handle payee KYC notification requirement re-open.""" params = {} if payee.payee_type == PayeeEntityType.account_payee: params['banking_details_review_status'] = ACTION_STATUSES.RUNNING elif payee.payee_type == PayeeEntityType.payee: params['banking_eligibility_status'] = ACTION_STATUSES.RUNNING if set_states(payee, KYC_MESSAGE.REOPENED, **params): delete_kyc_notifications(payee) create_kyc_notification( payee=payee, file_upload_link=file_upload_link, requirement_type_id=requirement_type_id, sub_requirement_status_id=sub_requirement_status_id, possible_sub_requirement_types=possible_sub_requirement_types, entity_reference_id=entity_reference_id, entity_reference_type_id=entity_reference_type_id, )