from payee.constants.constants import BANK_DETAILS_PAYEE_TYPE class BankDetailsDataHelper: @classmethod def build_bank_details(cls, params: dict): return { 'type': params['type'], **cls.get_strategy(params['type'])(params), 'payout_method': cls.build_payout_method(params), } @classmethod def get_strategy(cls, payee_type: str): return { BANK_DETAILS_PAYEE_TYPE.INDIVIDUAL: cls.build_individual_payee, BANK_DETAILS_PAYEE_TYPE.COMPANY: cls.build_company_payee, }[payee_type] @staticmethod def build_payout_method(params: dict): return { 'bank_account_type': params['bank_account_type'], 'country': params['country'], 'currency': params['currency'], 'bank_field_details': params['bank_field_details'], } @staticmethod def build_individual_payee_contact(params: dict): return { 'last_name': params['last_name'], 'first_name': params['first_name'], 'date_of_birth': params['date_of_birth'], 'email': params.get('email'), 'phone': params.get('phone'), 'phone_country': params.get('phone_country'), } @staticmethod def build_company_payee_contact(params: dict): return { 'email': params.get('email'), 'phone': params.get('phone'), 'phone_country': params.get('phone_country'), } @classmethod def build_individual_payee(cls, params: dict): return { 'address': cls.build_payee_address(params), 'contact': cls.build_individual_payee_contact(params), } @classmethod def build_company_payee(cls, params: dict): company_info = {'name': params['name']} return { 'address': cls.build_payee_address(params), 'company': company_info, 'contact': cls.build_company_payee_contact(params), } @staticmethod def format_payee_name(params: dict, flattened=False): """Render payee name.""" match params['type']: case BANK_DETAILS_PAYEE_TYPE.INDIVIDUAL: params = params if flattened else params['contact'] return f'{params["first_name"].strip()} {params["last_name"].strip()}' case BANK_DETAILS_PAYEE_TYPE.COMPANY: params = params if flattened else params['company'] return params['name'].strip() @classmethod def build_payee_identity(cls, params: dict, flattened=False): """ Returns main fields to distinct payees. Intended for check if the payee is the same legal entity from Payoneer perspective when updating the data, and should we update or recreate the payee. E.g. name, date of birth etc. """ data = { 'type': params['type'], 'name': cls.format_payee_name(params, flattened).upper(), } match params['type']: case BANK_DETAILS_PAYEE_TYPE.INDIVIDUAL: params = params if flattened else params['contact'] data['date_of_birth'] = params['date_of_birth'] case BANK_DETAILS_PAYEE_TYPE.COMPANY: params = params if flattened else params['company'] # TODO: doublecheck if we need extra fields for company pass return data @classmethod def get_email(cls, params: dict, flattened=False): contact_params = params if flattened else params.get('contact', {}) return contact_params.get('email') @classmethod def get_phone(cls, params: dict, flattened=False): contact_params = params if flattened else params.get('contact', {}) return contact_params.get('phone') @classmethod def get_phone_country(cls, params: dict, flattened=False): contact_params = params if flattened else params.get('contact', {}) return contact_params.get('phone_country') @classmethod def get_account_holder_name(cls, params: dict, flattened=False): bank_field_details = ( params.get('bank_field_details', []) if flattened else params.get('payout_method', {}).get('bank_field_details', []) ) return next( (d['value'] for d in bank_field_details if d['name'] == 'AccountName'), None ) @classmethod def get_country_code(cls, params: dict, flattened=False) -> str | None: """Get country code from banking details.""" return ( params.get('country_code') if flattened else (params.get('address') or {}).get('country_code') ) @classmethod def build_payee_address( cls, params: dict, exclude_country_code: bool = False ) -> dict[str, str]: """Helper to format payee address.""" result = { 'address_1': params.get('address_1'), 'address_2': params.get('address_2') or '', 'city': params.get('city'), 'province': params.get('province'), 'zip': params.get('zip'), } if not exclude_country_code: result['country_code'] = params.get('country_code') return result