示例#1
0
    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, national_number = get_country_code_and_national_number(phone_number)

        if backend_instance is None or _sms_backend_is_global(backend_instance):
            billable.gateway_fee = SmsGatewayFee.get_by_criteria(
                backend_api_id,
                direction,
                backend_instance=backend_instance,
                country_code=country_code,
                national_number=national_number,
            )
            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)
            else:
                smsbillables_logging.error(
                    "No matching gateway fee criteria for SMSLog %s" % message_log._id
                )

        # 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

        if backend_api_id == TestSMSBackend.get_api_id():
            billable.is_valid = False

        billable.save()

        return billable
示例#2
0
    def _get_gateway_fee(cls, backend_api_id, backend_id,
                         phone_number, direction, couch_id, backend_message_id, domain):
        country_code, national_number = get_country_code_and_national_number(phone_number)

        backend_instance = None
        if backend_id is not None:
            backend_instance = SQLMobileBackend.load(
                backend_id,
                api_id=backend_api_id,
                is_couch_id=True,
                include_deleted=True,
            )

        is_gateway_billable = (
            backend_id is None
            or backend_instance.is_global
            or toggles.ENABLE_INCLUDE_SMS_GATEWAY_CHARGING.enabled(domain)
        )

        direct_gateway_fee = gateway_fee = multipart_count = conversion_rate = None

        if is_gateway_billable:
            if backend_instance and backend_instance.using_api_to_get_fees:
                if backend_message_id:
                    direct_gateway_fee, multipart_count = \
                        cls.get_charge_details_through_api(backend_instance, backend_message_id)

                    gateway_fee = SmsGatewayFee.get_by_criteria(
                        backend_api_id,
                        direction,
                    )
                else:
                    log_smsbillables_error(
                        "Could not create gateway fee for message %s: no backend_message_id" % couch_id
                    )
            else:
                gateway_fee = SmsGatewayFee.get_by_criteria(
                    backend_api_id,
                    direction,
                    backend_instance=backend_id,
                    country_code=country_code,
                    national_number=national_number,
                )
            if gateway_fee:
                conversion_rate = cls.get_conversion_rate(gateway_fee)
            else:
                log_smsbillables_error(
                    "No matching gateway fee criteria for SMS %s" % couch_id
                )
        return _ProviderChargeInfo(
            direct_gateway_fee,
            gateway_fee,
            multipart_count,
            conversion_rate
        )
示例#3
0
    def _get_gateway_fee(cls, backend_api_id, backend_instance, phone_number,
                         direction, couch_id, backend_message_id, domain):
        country_code, national_number = get_country_code_and_national_number(
            phone_number)
        is_gateway_billable = backend_instance is None or _sms_backend_is_global(
            backend_instance
        ) or toggles.ENABLE_INCLUDE_SMS_GATEWAY_CHARGING.enabled(domain)

        if is_gateway_billable:
            is_twilio_message = backend_api_id == SQLTwilioBackend.get_api_id()
            if is_twilio_message:
                twilio_charges = cls._get_twilio_charges(
                    backend_message_id, backend_instance, direction, couch_id)
                gateway_fee = twilio_charges.gateway_fee
                direct_gateway_fee = twilio_charges.twilio_gateway_fee
            else:
                gateway_fee = SmsGatewayFee.get_by_criteria(
                    backend_api_id,
                    direction,
                    backend_instance=backend_instance,
                    country_code=country_code,
                    national_number=national_number,
                )
                direct_gateway_fee = None
            if gateway_fee:
                conversion_rate = gateway_fee.currency.rate_to_default
                if conversion_rate != 0:
                    return _GatewayChargeInfo(gateway_fee, conversion_rate,
                                              direct_gateway_fee)
                else:
                    log_smsbillables_error(
                        "Gateway fee conversion rate for currency %s is 0" %
                        gateway_fee.currency.code)
                    return _GatewayChargeInfo(gateway_fee, None,
                                              direct_gateway_fee)
            else:
                log_smsbillables_error(
                    "No matching gateway fee criteria for SMS %s" % couch_id)
        return _GatewayChargeInfo(None, None, None)
示例#4
0
    def _get_gateway_fee(cls, backend_api_id, backend_instance,
                         phone_number, direction, couch_id, backend_message_id, domain):
        country_code, national_number = get_country_code_and_national_number(phone_number)
        is_gateway_billable = backend_instance is None or _sms_backend_is_global(
            backend_instance) or toggles.ENABLE_INCLUDE_SMS_GATEWAY_CHARGING.enabled(domain)

        if is_gateway_billable:
            is_twilio_message = backend_api_id == SQLTwilioBackend.get_api_id()
            if is_twilio_message:
                twilio_charges = cls._get_twilio_charges(
                    backend_message_id, backend_instance, direction, couch_id
                )
                gateway_fee = twilio_charges.gateway_fee
                direct_gateway_fee = twilio_charges.twilio_gateway_fee
            else:
                gateway_fee = SmsGatewayFee.get_by_criteria(
                    backend_api_id,
                    direction,
                    backend_instance=backend_instance,
                    country_code=country_code,
                    national_number=national_number,
                )
                direct_gateway_fee = None
            if gateway_fee:
                conversion_rate = gateway_fee.currency.rate_to_default
                if conversion_rate != 0:
                    return _GatewayChargeInfo(gateway_fee, conversion_rate, direct_gateway_fee)
                else:
                    log_smsbillables_error(
                        "Gateway fee conversion rate for currency %s is 0"
                        % gateway_fee.currency.code
                    )
                    return _GatewayChargeInfo(gateway_fee, None, direct_gateway_fee)
            else:
                log_smsbillables_error(
                    "No matching gateway fee criteria for SMS %s" % couch_id
                )
        return _GatewayChargeInfo(None, None, None)