def create(cls, message_log, api_response=None): phone_number = clean_phone_number(message_log.phone_number) direction = message_log.direction billable = cls( log_id=message_log._id, phone_number=phone_number, direction=direction, date_sent=message_log.date, ) # Fetch gateway_fee backend_api_id = message_log.backend_api backend_instance = message_log.backend_id country_code = get_country_code(phone_number) billable.gateway_fee = SmsGatewayFee.get_by_criteria( backend_api_id, direction, backend_instance=backend_instance, country_code=country_code) if billable.gateway_fee is not None: conversion_rate = billable.gateway_fee.currency.rate_to_default if conversion_rate != 0: billable.gateway_fee_conversion_rate = conversion_rate else: smsbillables_logging.error( "Gateway fee conversion rate for currency %s is 0", billable.gateway_fee.currency.code) # Fetch usage_fee todo domain = message_log.domain billable.usage_fee = SmsUsageFee.get_by_criteria(direction, domain=domain) if billable.usage_fee is None: smsbillables_logging.error( "Did not find usage fee for direction %s and domain %s" % (direction, domain)) if api_response is not None: billable.api_response = api_response billable.save() return billable
def create(cls, message_log, api_response=None): phone_number = clean_phone_number(message_log.phone_number) direction = message_log.direction billable = cls( log_id=message_log._id, phone_number=phone_number, direction=direction, date_sent=message_log.date, domain=message_log.domain, ) # Fetch gateway_fee backend_api_id = message_log.backend_api backend_instance = message_log.backend_id country_code = get_country_code(phone_number) billable.gateway_fee = SmsGatewayFee.get_by_criteria( backend_api_id, direction, backend_instance=backend_instance, country_code=country_code ) if billable.gateway_fee is not None: conversion_rate = billable.gateway_fee.currency.rate_to_default if conversion_rate != 0: billable.gateway_fee_conversion_rate = conversion_rate else: smsbillables_logging.error("Gateway fee conversion rate for currency %s is 0", billable.gateway_fee.currency.code) # Fetch usage_fee todo domain = message_log.domain billable.usage_fee = SmsUsageFee.get_by_criteria( direction, domain=domain ) if billable.usage_fee is None: smsbillables_logging.error("Did not find usage fee for direction %s and domain %s" % (direction, domain)) if api_response is not None: billable.api_response = api_response billable.save() return billable
def handle_api_response(cls, message, **kwargs): response = kwargs.get("response") logging.info("[Billing] Tropo API Response %s" % response) country_code = get_country_code(message.phone_number, failhard=True) rate_item = TropoSMSRate.get_default(direction=message.direction, country_code="%d" % country_code) if message.direction == INCOMING: billable = cls.new_billable(rate_item, message) if billable: billable.tropo_id = INCOMING_MSG_ID billable.save() return else: find_success = re.compile('(<success>).*(</success>)') match = find_success.search(response) successful = bool(match and match.group() == '<success>true</success>') if successful: billable = cls.new_billable(rate_item, message) if billable: billable.tropo_id = cls.get_tropo_id(response) billable.save() return logging.error("[Billing] Did not successfully bill Tropo Message with ID # %s" % message.get_id)