Example #1
0
 def _get_provider_charges(cls, backend_message_id, backend_instance, direction, couch_id, backend_api_id):
     if backend_message_id:
         if backend_api_id == SQLTwilioBackend.get_api_id():
             message = get_twilio_message(backend_instance, backend_message_id)
             status = message.status
             price = message.price
         elif backend_api_id == InfobipBackend.get_api_id():
             message = get_infobip_message(backend_instance, backend_message_id)
             status = message['status']['name']
             price = message['price']['pricePerMessage']
         else:
             raise ProviderFeeNotSupportedException("backend_message_id=%s" % backend_message_id)
         if status is None or status.lower() in [
             'accepted',
             'queued',
             'sending',
             'receiving',
         ] or price is None:
             raise RetryBillableTaskException("backend_message_id=%s" % backend_message_id)
         return _ProviderChargeInfo(
             abs(Decimal(price)),
             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
         )
         return _ProviderChargeInfo(None, None)
Example #2
0
def get_infobip_message(backend_instance, backend_message_id):
    from corehq.messaging.smsbackends.infobip.models import InfobipBackend
    try:
        infobip_backend = SQLMobileBackend.load(
            backend_instance,
            api_id=InfobipBackend.get_api_id(),
            is_couch_id=True,
            include_deleted=True,
        )
        config = infobip_backend.config
        api_channel = '/sms/1'
        api_suffix = '/reports'
        if config.scenario_key:
            api_channel = '/omni/1'

        headers = {
            'Authorization': f'App {config.auth_token}',
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
        parameters = {'messageId': backend_message_id}
        messages = _get_infobip_message_details(api_channel, api_suffix,
                                                config, headers, parameters)
        if not messages:
            api_suffix = '/logs'
            messages = _get_infobip_message_details(api_channel, api_suffix,
                                                    config, headers,
                                                    parameters)
        return messages[0]
    except Exception as e:
        raise RetryBillableTaskException(str(e))
Example #3
0
 def _get_multipart_count(cls, backend_api_id, backend_instance, backend_message_id, multipart_count):
     if backend_api_id == SQLTwilioBackend.get_api_id():
         twilio_message = get_twilio_message(backend_instance, backend_message_id)
         if twilio_message.num_segments is not None:
             return int(twilio_message.num_segments)
         else:
             raise RetryBillableTaskException("backend_message_id=%s" % backend_message_id)
     elif backend_api_id == InfobipBackend.get_api_id():
         infobip_message = get_infobip_message(backend_instance, backend_message_id)
         segments = infobip_message['messageCount'] \
             if 'messageCount' in infobip_message else infobip_message['smsCount']
         if segments is not None:
             return int(segments)
         else:
             raise RetryBillableTaskException("backend_message_id=%s" % backend_message_id)
     else:
         return multipart_count
Example #4
0
 def _get_multipart_count(cls, backend_api_id, backend_instance, backend_message_id, multipart_count):
     if backend_api_id == SQLTwilioBackend.get_api_id():
         twilio_message = get_twilio_message(backend_instance, backend_message_id)
         if twilio_message.num_segments is not None:
             return int(twilio_message.num_segments)
         else:
             raise RetryBillableTaskException("backend_message_id=%s" % backend_message_id)
     else:
         return multipart_count
Example #5
0
    def get_charge_details_through_api(cls, backend_instance, backend_message_id):
        status, price, multipart_count = backend_instance.get_provider_charges(backend_message_id)
        if status is None or status.lower() in [
            'accepted',
            'queued',
            'sending',
            'receiving',
        ] or price is None:
            raise RetryBillableTaskException("backend_message_id=%s" % backend_message_id)

        return abs(Decimal(price)), multipart_count
Example #6
0
 def _get_twilio_charges(cls, backend_message_id, backend_instance, direction, couch_id):
     if backend_message_id:
         twilio_message = get_twilio_message(backend_instance, backend_message_id)
         if twilio_message.status in [
             'accepted',
             'queued',
             'sending',
             'receiving',
         ] or twilio_message.price is None:
             raise RetryBillableTaskException("backend_message_id=%s" % backend_message_id)
         return _TwilioChargeInfo(
             Decimal(twilio_message.price) * -1,
             SmsGatewayFee.get_by_criteria(
                 SQLTwilioBackend.get_api_id(),
                 direction,
             )
         )
     else:
         log_smsbillables_error(
             "Could not create gateway fee for Twilio message %s: no backend_message_id" % couch_id
         )
         return _TwilioChargeInfo(None, None)
Example #7
0
    def get_message(self, backend_message_id):
        try:
            config = self.config
            api_channel = '/sms/1'
            api_suffix = '/reports'
            if config.scenario_key:
                api_channel = '/omni/1'

            headers = {
                'Authorization': f'App {config.auth_token}',
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
            parameters = {'messageId': backend_message_id}
            messages = self._get_message_details(api_channel, api_suffix,
                                                 config, headers, parameters)
            if not messages:
                api_suffix = '/logs'
                messages = self._get_message_details(api_channel, api_suffix,
                                                     config, headers,
                                                     parameters)
            return messages[0]
        except Exception as e:
            raise RetryBillableTaskException(str(e))
Example #8
0
def get_twilio_message(backend_instance, backend_message_id):
    try:
        return _get_twilio_client(backend_instance).messages.get(
            backend_message_id).fetch()
    except TwilioRestException as e:
        raise RetryBillableTaskException(str(e))