def bootstrap_unicel_gateway(orm):
    currency = (orm['accounting.Currency'] if orm else Currency).objects.get(code="INR")
    sms_gateway_fee_class = orm['smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee
    sms_gateway_fee_criteria_class = orm['smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria

    SmsGatewayFee.create_new(UnicelBackend.get_api_id(), INCOMING, 0.50,
                             currency=currency,
                             fee_class=sms_gateway_fee_class,
                             criteria_class=sms_gateway_fee_criteria_class)
    SmsGatewayFee.create_new(UnicelBackend.get_api_id(), OUTGOING, 0.50,
                             currency=currency,
                             fee_class=sms_gateway_fee_class,
                             criteria_class=sms_gateway_fee_criteria_class)
    logger.info("Updated Unicel gateway fees.")
    def setUp(self):
        self.domain = "biyeun"

        self.domain_obj = Domain()
        self.domain_obj.name = self.domain
        self.domain_obj.commtrack_enabled = True
        self.domain_obj.save()

        # delete any existing test SMS logs
        all_logs = SMSLog.by_domain_asc(self.domain).all()
        for log in all_logs:
            log.delete()

        # delete any existing billable items
        all_billables = SMSBillable.by_domain(self.domain)
        for billable in all_billables:
            billable.delete()

        self.usd_rate = BillableCurrency.get_existing_or_new_by_code("USD")
        self.usd_rate.conversion = 1.000
        self.usd_rate.source = "static"
        self.usd_rate.last_updated = datetime.datetime.utcnow()
        self.usd_rate.save()

        self.eur_rate = BillableCurrency.get_existing_or_new_by_code("EUR")
        self.eur_rate.conversion = 1.800
        self.eur_rate.source = "static"
        self.eur_rate.last_updated = datetime.datetime.utcnow()
        self.eur_rate.save()

        self.dimagi_surcharge = DimagiDomainSMSRate()
        self.dimagi_surcharge.domain = self.domain
        self.dimagi_surcharge.direction = OUTGOING
        self.dimagi_surcharge.base_fee = 0.002
        self.dimagi_surcharge.currency_code = self.dimagi_surcharge._admin_crud_class.currency_code
        self.dimagi_surcharge.last_modified = datetime.datetime.utcnow()
        self.dimagi_surcharge.save()

        self.dimagi_surcharge_I = DimagiDomainSMSRate()
        self.dimagi_surcharge_I.domain = self.domain
        self.dimagi_surcharge_I.direction = INCOMING
        self.dimagi_surcharge_I.base_fee = 0.001
        self.dimagi_surcharge_I.currency_code = self.dimagi_surcharge._admin_crud_class.currency_code
        self.dimagi_surcharge_I.last_modified = datetime.datetime.utcnow()
        self.dimagi_surcharge_I.save()

        self.unicel_rate = UnicelSMSRate()
        self.unicel_rate.direction = OUTGOING
        self.unicel_rate.base_fee = 0.01
        self.unicel_rate.currency_code = self.unicel_rate._admin_crud_class.currency_code
        self.unicel_rate.last_modified = datetime.datetime.utcnow()
        self.unicel_rate.save()

        self.unicel_incoming_rate = UnicelSMSRate()
        self.unicel_incoming_rate.direction = INCOMING
        self.unicel_incoming_rate.base_fee = 0.05
        self.unicel_incoming_rate.currency_code = self.unicel_incoming_rate._admin_crud_class.currency_code
        self.unicel_incoming_rate.last_modified = datetime.datetime.utcnow()
        self.unicel_incoming_rate.save()

        self.tropo_rate_any = TropoSMSRate()
        self.tropo_rate_any.direction = OUTGOING
        self.tropo_rate_any.base_fee = 0.02
        self.tropo_rate_any.country_code = ""
        self.tropo_rate_any.currency_code = self.tropo_rate_any._admin_crud_class.currency_code
        self.tropo_rate_any.last_modified = datetime.datetime.utcnow()
        self.tropo_rate_any.save()

        self.tropo_rate_us = TropoSMSRate()
        self.tropo_rate_us.direction = OUTGOING
        self.tropo_rate_us.base_fee = 0.01
        self.tropo_rate_us.country_code = "1"
        self.tropo_rate_us.currency_code = self.tropo_rate_us._admin_crud_class.currency_code
        self.tropo_rate_us.last_modified = datetime.datetime.utcnow()
        self.tropo_rate_us.save()

        self.tropo_rate_incoming = TropoSMSRate()
        self.tropo_rate_incoming.direction = INCOMING
        self.tropo_rate_incoming.base_fee = 0.0123
        self.tropo_rate_incoming.country_code = ""
        self.tropo_rate_incoming.currency_code = self.tropo_rate_incoming._admin_crud_class.currency_code
        self.tropo_rate_incoming.last_modified = datetime.datetime.utcnow()
        self.tropo_rate_incoming.save()


        # self.mach_rate = MachSMSRate()
        # self.mach_rate.direction = OUTGOING
        # self.mach_rate.base_fee = 0.005
        # self.mach_rate.network_surcharge = 0.0075
        # self.mach_rate.currency_code = self.mach_rate._admin_crud_class.currency_code
        # self.mach_rate.last_modified = datetime.datetime.utcnow()
        # self.mach_rate.country_code = "49"
        # self.mach_rate.mcc = "262"
        # self.mach_rate.mnc = "07"
        # self.mach_rate.network = "O2"
        # self.mach_rate.iso = "de"
        # self.mach_rate.country = "Germany"
        # self.mach_rate.save()
        #
        #
        # self.mach_number = MachPhoneNumber()
        # self.mach_number.phone_number = "+4917685675599"
        # self.mach_number.country = self.mach_rate.country
        # self.mach_number.network = self.mach_rate.network
        # self.mach_number.save()


        # FOR INCOMING UNICEL
        # Couch User with Verified Phone Number
        self.unicel_couch_user = WebUser.create(self.domain, "unicel_biyeun", "test123")
        self.unicel_couch_user.add_phone_number("+919971855708")
        self.unicel_couch_user.save()
        # Verified Number for Incoming Unicel Message
        self.vn_unicel = VerifiedNumber()
        self.vn_unicel.owner_id = self.unicel_couch_user.get_id
        self.vn_unicel.owner_doc_type = self.unicel_couch_user.__class__.__name__
        self.vn_unicel.domain = self.domain
        self.vn_unicel.backend_id = None # This is not needed unless you use corehq.apps.sms.api.send_sms_to_verified_number
        self.vn_unicel.phone_number = strip_plus(clean_phone_number(self.unicel_couch_user.phone_number))
        self.vn_unicel.verified = True
        self.vn_unicel.save()


        # FOR INCOMING TROPO
        # Couch User with Verified Phone Number
        self.tropo_couch_user = WebUser.create(self.domain, "tropo_biyeun", "test123")
        self.tropo_couch_user.add_phone_number("+4917685675599")
        self.tropo_couch_user.save()
        # Verified Phone Number for Incoming Tropo Message
        self.vn_tropo = VerifiedNumber()
        self.vn_tropo.owner_id = self.tropo_couch_user.get_id
        self.vn_tropo.owner_doc_type = self.tropo_couch_user.__class__.__name__
        self.vn_tropo.domain = self.domain
        self.vn_tropo.backend_id = None # This is not needed unless you use corehq.apps.sms.api.send_sms_to_verified_number
        self.vn_tropo.phone_number = strip_plus(clean_phone_number(self.tropo_couch_user.phone_number))
        self.vn_tropo.verified = True
        self.vn_tropo.save()


        self.test_message = "Test of CommCare HQ's SMS Tracking System."
        try:
            self.sms_config = getattr(settings, "SMS_TESTING")
        except AttributeError:
            self.sms_config = None

        try:
            self.tropo_token = getattr(settings, "TROPO_MESSAGING_TOKEN")
            self.tropo_backend = TropoBackend(messaging_token=self.tropo_token)
        except AttributeError:
            self.tropo_token = None
            self.tropo_backend = None

        try:
            unicel_config = getattr(settings, "UNICEL_CONFIG")
            self.unicel_backend = UnicelBackend(
                username=unicel_config["username"],
                password=unicel_config["password"],
                sender=unicel_config["sender"],
            )
        except (AttributeError, KeyError):
            self.unicel_backend = None
class BillingItemTests(TestCase):

    def setUp(self):
        self.domain = "biyeun"

        self.domain_obj = Domain()
        self.domain_obj.name = self.domain
        self.domain_obj.commtrack_enabled = True
        self.domain_obj.save()

        # delete any existing test SMS logs
        all_logs = SMSLog.by_domain_asc(self.domain).all()
        for log in all_logs:
            log.delete()

        # delete any existing billable items
        all_billables = SMSBillable.by_domain(self.domain)
        for billable in all_billables:
            billable.delete()

        self.usd_rate = BillableCurrency.get_existing_or_new_by_code("USD")
        self.usd_rate.conversion = 1.000
        self.usd_rate.source = "static"
        self.usd_rate.last_updated = datetime.datetime.utcnow()
        self.usd_rate.save()

        self.eur_rate = BillableCurrency.get_existing_or_new_by_code("EUR")
        self.eur_rate.conversion = 1.800
        self.eur_rate.source = "static"
        self.eur_rate.last_updated = datetime.datetime.utcnow()
        self.eur_rate.save()

        self.dimagi_surcharge = DimagiDomainSMSRate()
        self.dimagi_surcharge.domain = self.domain
        self.dimagi_surcharge.direction = OUTGOING
        self.dimagi_surcharge.base_fee = 0.002
        self.dimagi_surcharge.currency_code = self.dimagi_surcharge._admin_crud_class.currency_code
        self.dimagi_surcharge.last_modified = datetime.datetime.utcnow()
        self.dimagi_surcharge.save()

        self.dimagi_surcharge_I = DimagiDomainSMSRate()
        self.dimagi_surcharge_I.domain = self.domain
        self.dimagi_surcharge_I.direction = INCOMING
        self.dimagi_surcharge_I.base_fee = 0.001
        self.dimagi_surcharge_I.currency_code = self.dimagi_surcharge._admin_crud_class.currency_code
        self.dimagi_surcharge_I.last_modified = datetime.datetime.utcnow()
        self.dimagi_surcharge_I.save()

        self.unicel_rate = UnicelSMSRate()
        self.unicel_rate.direction = OUTGOING
        self.unicel_rate.base_fee = 0.01
        self.unicel_rate.currency_code = self.unicel_rate._admin_crud_class.currency_code
        self.unicel_rate.last_modified = datetime.datetime.utcnow()
        self.unicel_rate.save()

        self.unicel_incoming_rate = UnicelSMSRate()
        self.unicel_incoming_rate.direction = INCOMING
        self.unicel_incoming_rate.base_fee = 0.05
        self.unicel_incoming_rate.currency_code = self.unicel_incoming_rate._admin_crud_class.currency_code
        self.unicel_incoming_rate.last_modified = datetime.datetime.utcnow()
        self.unicel_incoming_rate.save()

        self.tropo_rate_any = TropoSMSRate()
        self.tropo_rate_any.direction = OUTGOING
        self.tropo_rate_any.base_fee = 0.02
        self.tropo_rate_any.country_code = ""
        self.tropo_rate_any.currency_code = self.tropo_rate_any._admin_crud_class.currency_code
        self.tropo_rate_any.last_modified = datetime.datetime.utcnow()
        self.tropo_rate_any.save()

        self.tropo_rate_us = TropoSMSRate()
        self.tropo_rate_us.direction = OUTGOING
        self.tropo_rate_us.base_fee = 0.01
        self.tropo_rate_us.country_code = "1"
        self.tropo_rate_us.currency_code = self.tropo_rate_us._admin_crud_class.currency_code
        self.tropo_rate_us.last_modified = datetime.datetime.utcnow()
        self.tropo_rate_us.save()

        self.tropo_rate_incoming = TropoSMSRate()
        self.tropo_rate_incoming.direction = INCOMING
        self.tropo_rate_incoming.base_fee = 0.0123
        self.tropo_rate_incoming.country_code = ""
        self.tropo_rate_incoming.currency_code = self.tropo_rate_incoming._admin_crud_class.currency_code
        self.tropo_rate_incoming.last_modified = datetime.datetime.utcnow()
        self.tropo_rate_incoming.save()


        # self.mach_rate = MachSMSRate()
        # self.mach_rate.direction = OUTGOING
        # self.mach_rate.base_fee = 0.005
        # self.mach_rate.network_surcharge = 0.0075
        # self.mach_rate.currency_code = self.mach_rate._admin_crud_class.currency_code
        # self.mach_rate.last_modified = datetime.datetime.utcnow()
        # self.mach_rate.country_code = "49"
        # self.mach_rate.mcc = "262"
        # self.mach_rate.mnc = "07"
        # self.mach_rate.network = "O2"
        # self.mach_rate.iso = "de"
        # self.mach_rate.country = "Germany"
        # self.mach_rate.save()
        #
        #
        # self.mach_number = MachPhoneNumber()
        # self.mach_number.phone_number = "+4917685675599"
        # self.mach_number.country = self.mach_rate.country
        # self.mach_number.network = self.mach_rate.network
        # self.mach_number.save()


        # FOR INCOMING UNICEL
        # Couch User with Verified Phone Number
        self.unicel_couch_user = WebUser.create(self.domain, "unicel_biyeun", "test123")
        self.unicel_couch_user.add_phone_number("+919971855708")
        self.unicel_couch_user.save()
        # Verified Number for Incoming Unicel Message
        self.vn_unicel = VerifiedNumber()
        self.vn_unicel.owner_id = self.unicel_couch_user.get_id
        self.vn_unicel.owner_doc_type = self.unicel_couch_user.__class__.__name__
        self.vn_unicel.domain = self.domain
        self.vn_unicel.backend_id = None # This is not needed unless you use corehq.apps.sms.api.send_sms_to_verified_number
        self.vn_unicel.phone_number = strip_plus(clean_phone_number(self.unicel_couch_user.phone_number))
        self.vn_unicel.verified = True
        self.vn_unicel.save()


        # FOR INCOMING TROPO
        # Couch User with Verified Phone Number
        self.tropo_couch_user = WebUser.create(self.domain, "tropo_biyeun", "test123")
        self.tropo_couch_user.add_phone_number("+4917685675599")
        self.tropo_couch_user.save()
        # Verified Phone Number for Incoming Tropo Message
        self.vn_tropo = VerifiedNumber()
        self.vn_tropo.owner_id = self.tropo_couch_user.get_id
        self.vn_tropo.owner_doc_type = self.tropo_couch_user.__class__.__name__
        self.vn_tropo.domain = self.domain
        self.vn_tropo.backend_id = None # This is not needed unless you use corehq.apps.sms.api.send_sms_to_verified_number
        self.vn_tropo.phone_number = strip_plus(clean_phone_number(self.tropo_couch_user.phone_number))
        self.vn_tropo.verified = True
        self.vn_tropo.save()


        self.test_message = "Test of CommCare HQ's SMS Tracking System."
        try:
            self.sms_config = getattr(settings, "SMS_TESTING")
        except AttributeError:
            self.sms_config = None

        try:
            self.tropo_token = getattr(settings, "TROPO_MESSAGING_TOKEN")
            self.tropo_backend = TropoBackend(messaging_token=self.tropo_token)
        except AttributeError:
            self.tropo_token = None
            self.tropo_backend = None

        try:
            unicel_config = getattr(settings, "UNICEL_CONFIG")
            self.unicel_backend = UnicelBackend(
                username=unicel_config["username"],
                password=unicel_config["password"],
                sender=unicel_config["sender"],
            )
        except (AttributeError, KeyError):
            self.unicel_backend = None

        # try:
        #     mach_config = getattr(settings, "MACH_CONFIG")
        #     self.mach_backend = MachBackend(
        #         account_id=mach_config["username"],
        #         password=mach_config["password"],
        #         sender_id="TEST",
        #     )
        # except (AttributeError, KeyError):
        #     self.mach_backend = None

    def tearDown(self):
        self.usd_rate.delete()
        self.eur_rate.delete()

        self.dimagi_surcharge.delete()
        self.dimagi_surcharge_I.delete()

        self.unicel_rate.delete()
        self.unicel_incoming_rate.delete()

        self.tropo_rate_any.delete()
        self.tropo_rate_us.delete()
        self.tropo_rate_incoming.delete()

        # self.mach_rate.delete()
        #
        # self.mach_number.delete()

        self.unicel_couch_user.delete()
        self.vn_unicel.delete()

        self.tropo_couch_user.delete()
        self.vn_tropo.delete()

        self.domain_obj.delete()

    def testOutgoingUnicelApi(self):
        self.assertEqual(self.unicel_rate.conversion_rate, self.usd_rate.conversion)
        msg = SMSLog(domain = self.domain,
            direction = OUTGOING,
            date = datetime.datetime.utcnow(),
            text = self.test_message)
        msg.save()

        if self.sms_config and self.sms_config.get("unicel") and self.unicel_backend:
            logging.info("\n\n[Billing - LIVE] UNICEL: Outgoing SMS Test.")
            msg.phone_number = self.sms_config.get("unicel")
            msg.save()
            data = self.unicel_backend.send(msg, delay=False)
        else:
            logging.info("\n\n[Billing] UNICEL: Outgoing SMS Test")
            data = "successful23541253235"
            msg.phone_number = "+555555555"
            msg.save()
            bill_client_for_sms(UnicelSMSBillable, msg.get_id, **dict(response=data))

        logging.info("Response from UNICEL: %s" % data)

        billable_items = UnicelSMSBillable.by_domain_and_direction(self.domain, OUTGOING)
        if billable_items:
            billable_item = billable_items[0]
            self.assertEqual(self.unicel_rate.base_fee * self.unicel_rate.conversion_rate,
                       billable_item.billable_amount)
            self.assertEqual(self.unicel_rate._id, billable_item.rate_id)
            self.assertEqual(msg._id, billable_item.log_id)
            self.assertEqual(self.unicel_rate.conversion_rate, billable_item.conversion_rate)
            self.assertEqual(self.dimagi_surcharge.base_fee, billable_item.dimagi_surcharge)
            self.assertEqual(data, billable_item.unicel_id)
            if billable_item.has_error:
                raise Exception("There were recorded errors creating an outgoing UNICEL billing rate: %s" %
                                billable_item.error_message)
        else:
            raise Exception("There were unknown errors creating an outgoing UNICEL billing rate!")


    def testIncomingUnicelApi(self):
        logging.info("\n\n[Billing] UNICEL: Incoming SMS Test")
        self.assertEqual(self.unicel_rate.conversion_rate, self.usd_rate.conversion)
        fake_post = {InboundParams.SENDER: str(self.unicel_couch_user.phone_number),
                     InboundParams.MESSAGE: self.test_message,
                     InboundParams.TIMESTAMP: datetime.datetime.now().strftime(DATE_FORMAT),
                     InboundParams.DCS: '8',
                     InboundParams.UDHI: '0'}

        class FakeRequest(object):
            # HACK
            params = {}
            @property
            def REQUEST(self):
                return self.params

        req = FakeRequest()
        req.params = fake_post

        log = unicel_incoming(req, delay=False)

        billable_items = UnicelSMSBillable.by_domain_and_direction(self.domain, INCOMING)
        if billable_items:
            billable_item = billable_items[0]
            self.assertEqual(self.unicel_incoming_rate.base_fee,
                                   billable_item.billable_amount)
            self.assertEqual(self.unicel_incoming_rate._id, billable_item.rate_id)
            self.assertEqual(log._id, billable_item.log_id)
            self.assertEqual(self.unicel_incoming_rate.conversion_rate, billable_item.conversion_rate)
            self.assertEqual(self.dimagi_surcharge_I.base_fee, billable_item.dimagi_surcharge)
            self.assertEqual(INCOMING_MSG_ID, billable_item.unicel_id)
            if billable_item.has_error:
                raise Exception("There were recorded errors creating an incoming UNICEL billing rate: %s" %
                                billable_item.error_message)
        else:
            raise Exception("There were unknown errors creating an incoming UNICEL billing rate!")

    def testOutgoingUSTropoApi(self):
        self.assertEqual(self.tropo_rate_us.conversion_rate, self.usd_rate.conversion)
        msg = SMSLog(domain = self.domain,
            direction = OUTGOING,
            date = datetime.datetime.utcnow(),
            text = self.test_message)
        msg.save()

        if self.sms_config and self.sms_config.get("tropo_us") and self.tropo_backend:
            logging.info("\n\n[Billing - LIVE] Tropo: Outgoing US SMS Test")
            msg.phone_number = self.sms_config.get("tropo_us")
            msg.save()
            data = self.tropo_backend.send(msg, delay=False)
        else:
            logging.info("\n\n[Billing] Tropo: Outgoing US SMS Test")
            data = "<session><success>true</success><token>faketoken</token><id>aadfg3Aa321gdc8e628df2\n</id></session>"
            msg.phone_number = "+16175005454"
            msg.save()
            bill_client_for_sms(TropoSMSBillable, msg.get_id, **dict(response=data))

        logging.info("Response from TROPO: %s" % data)
        tropo_id = TropoSMSBillable.get_tropo_id(data)
        logging.info("TROPO ID: %s" % tropo_id)

        billable_items = TropoSMSBillable.by_domain(self.domain)
        if billable_items:
            billable_item = billable_items[0]
            self.assertEqual(self.tropo_rate_us.base_fee,
                billable_item.billable_amount)
            self.assertEqual(self.tropo_rate_us._id, billable_item.rate_id)
            self.assertEqual(msg._id, billable_item.log_id)
            self.assertEqual(self.tropo_rate_us.conversion_rate, billable_item.conversion_rate)
            self.assertEqual(self.dimagi_surcharge.base_fee, billable_item.dimagi_surcharge)
            self.assertEqual(tropo_id, billable_item.tropo_id)
            billable_item.delete()
            if billable_item.has_error:
                raise Exception("There were recorded errors creating an outgoing US Tropo billing rate: %s" %
                                billable_item.error_message)
        else:
            raise Exception("There were unknown errors creating an outgoing US Tropo billing rate!")

    def testOutgoingInternationalTropoApi(self):
        self.assertEqual(self.tropo_rate_any.conversion_rate, self.usd_rate.conversion)
        msg = SMSLog(domain = self.domain,
            direction = OUTGOING,
            date = datetime.datetime.utcnow(),
            text = self.test_message)
        msg.save()

        if self.sms_config and self.sms_config.get("tropo_int") and self.tropo_backend:
            logging.info("\n\n[Billing - LIVE] Tropo:  Outgoing International SMS Test")
            msg.phone_number = self.sms_config.get("tropo_int")
            msg.save()
            data = self.tropo_backend.send(msg, delay=False)
        else:
            logging.info("\n\n[Billing] Tropo: Outgoing International SMS Test")
            data = "<session><success>true</success><token>faketoken</token><id>aadfg3Aa321gdc8e628df2\n</id></session>"
            msg.phone_number = "+4915253271951"
            msg.save()
            bill_client_for_sms(TropoSMSBillable, msg.get_id, **dict(response=data))

        logging.info("[Billing] Response from TROPO: %s" % data)
        tropo_id = TropoSMSBillable.get_tropo_id(data)
        logging.info("[Billing] TROPO ID: %s" % tropo_id)

        billable_items = TropoSMSBillable.by_domain(self.domain)
        if billable_items:
            billable_item = billable_items[0]
            self.assertEqual(self.tropo_rate_any.base_fee,
                billable_item.billable_amount)
            self.assertEqual(self.tropo_rate_any._id, billable_item.rate_id)
            self.assertEqual(msg._id, billable_item.log_id)
            self.assertEqual(self.tropo_rate_any.conversion_rate, billable_item.conversion_rate)
            self.assertEqual(self.dimagi_surcharge.base_fee, billable_item.dimagi_surcharge)
            self.assertEqual(tropo_id, billable_item.tropo_id)
            billable_item.delete()
            if billable_item.has_error:
                raise Exception("There were recorded errors creating an outgoing International Tropo billing rate: %s" %
                                billable_item.error_message)
        else:
            raise Exception("There were unknown errors creating an outgoing International Tropo billing rate!")

    def testIncomingTropoApi(self):
        logging.info("\n\n[Billing] Tropo: Incoming SMS Test")
        self.assertEqual(self.tropo_rate_incoming.conversion_rate, self.usd_rate.conversion)
        # fake_post = {InboundParams.SENDER: str(self.tropo_couch_user.phone_number),
        #              InboundParams.MESSAGE: self.test_message,
        #              InboundParams.TIMESTAMP: datetime.datetime.now().strftime(DATE_FORMAT),
        #              InboundParams.DCS: '8',
        #              InboundParams.UDHI: '0'}
        #
        # class FakeRequest(object):
        #     # HACK
        #     params = {}
        #     @property
        #     def REQUEST(self):
        #         return self.params
        #
        # req = FakeRequest()
        # req.params = fake_post

        log = create_incoming_msg(str(self.tropo_couch_user.phone_number), self.test_message, TropoBackend.get_api_id(), delay=False)

        billable_items = TropoSMSBillable.by_domain_and_direction(self.domain, INCOMING)

        if billable_items:
            billable_item = billable_items[0]
            self.assertEqual(self.tropo_rate_incoming.base_fee, billable_item.billable_amount)
            self.assertEqual(self.tropo_rate_incoming._id, billable_item.rate_id)
            self.assertEqual(log._id, billable_item.log_id)
            self.assertEqual(self.tropo_rate_incoming.conversion_rate, billable_item.conversion_rate)
            self.assertEqual(self.dimagi_surcharge_I.base_fee, billable_item.dimagi_surcharge)
            self.assertEqual(INCOMING_MSG_ID, billable_item.tropo_id)
            if billable_item.has_error:
                raise Exception("There were recorded errors creating an incoming Tropo billing rate: %s" %
                                billable_item.error_message)
        else:
            raise Exception("There were unknown errors creating an incoming Tropo billing rate!")
 def handle(self, *args, **options):
     backends = MobileBackend.view("sms/old_mobile_backend", include_docs=True).all()
     for backend in backends:
         print "Processing backend %s" % backend._id
         if backend._id == "MOBILE_BACKEND_UNICEL":
             backend = UnicelBackend.wrap(backend.to_json())
             backend.doc_type = "UnicelBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = True
             backend.username = settings.UNICEL_CONFIG.get("username")
             backend.password = settings.UNICEL_CONFIG.get("password")
             backend.sender = settings.UNICEL_CONFIG.get("sender")
             backend.reply_to_phone_number = settings.UNICEL_CONFIG.get("receive_phone", None)
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
         elif backend._id == "MOBILE_BACKEND_MACH":
             backend = MachBackend.wrap(backend.to_json())
             backend.doc_type = "MachBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = True
             backend.account_id = re.match(r".*id=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1)
             backend.password = re.match(r".*pw=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1)
             backend.sender_id = "DIMAGI"
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
         elif backend.outbound_module == "corehq.apps.tropo.api":
             backend = TropoBackend.wrap(backend.to_json())
             backend.doc_type = "TropoBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = True
             backend.messaging_token = backend.outbound_params.get("messaging_token")
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
         elif backend.outbound_module == "corehq.apps.sms.backend.http_api":
             print "REMINDER: Need to set global / domain for backend %s" % backend._id
             backend = HttpBackend.wrap(backend.to_json())
             backend.doc_type = "HttpBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = False
             backend.url = backend.outbound_params.get("url")
             backend.message_param = backend.outbound_params.get("message_param")
             backend.number_param = backend.outbound_params.get("number_param")
             backend.include_plus = backend.outbound_params.get("include_plus", False)
             backend.method = backend.outbound_params.get("method", "GET")
             backend.additional_params = backend.outbound_params.get("additional_params", {})
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
         elif backend.outbound_module == "corehq.apps.sms.test_backend":
             backend = TestSMSBackend.wrap(backend.to_json())
             backend.doc_type = "TestSMSBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = True
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
class Command(BaseCommand):
    args = ""
    help = "Migrate backends to support backend refactor."

    # Migrate all backends
    def handle(self, *args, **options):
        backends = MobileBackend.view("sms/old_mobile_backend", include_docs=True).all()
        for backend in backends:
            print "Processing backend %s" % backend._id
            if backend._id == "MOBILE_BACKEND_UNICEL":
                backend = UnicelBackend.wrap(backend.to_json())
                backend.doc_type = "UnicelBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = True
                backend.username = settings.UNICEL_CONFIG.get("username")
                backend.password = settings.UNICEL_CONFIG.get("password")
                backend.sender = settings.UNICEL_CONFIG.get("sender")
                backend.reply_to_phone_number = settings.UNICEL_CONFIG.get("receive_phone", None)
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()
            elif backend._id == "MOBILE_BACKEND_MACH":
                backend = MachBackend.wrap(backend.to_json())
                backend.doc_type = "MachBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = True
                backend.account_id = re.match(r".*id=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1)
                backend.password = re.match(r".*pw=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1)
                backend.sender_id = "DIMAGI"
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()
            elif backend.outbound_module == "corehq.apps.tropo.api":
                backend = TropoBackend.wrap(backend.to_json())
                backend.doc_type = "TropoBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = True
                backend.messaging_token = backend.outbound_params.get("messaging_token")
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()
            elif backend.outbound_module == "corehq.apps.sms.backend.http_api":
                print "REMINDER: Need to set global / domain for backend %s" % backend._id
                backend = HttpBackend.wrap(backend.to_json())
                backend.doc_type = "HttpBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = False
                backend.url = backend.outbound_params.get("url")
                backend.message_param = backend.outbound_params.get("message_param")
                backend.number_param = backend.outbound_params.get("number_param")
                backend.include_plus = backend.outbound_params.get("include_plus", False)
                backend.method = backend.outbound_params.get("method", "GET")
                backend.additional_params = backend.outbound_params.get("additional_params", {})
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()
            elif backend.outbound_module == "corehq.apps.sms.test_backend":
                backend = TestSMSBackend.wrap(backend.to_json())
                backend.doc_type = "TestSMSBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = True
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()

    # If no unicel backend was present, create one
    try:
        MobileBackend.get("MOBILE_BACKEND_UNICEL")
    except ResourceNotFound:
        backend = UnicelBackend(
            domain = None,
            name = "MOBILE_BACKEND_UNICEL",
            authorized_domains = [],
            is_global = True,
            username = settings.UNICEL_CONFIG.get("username"),
            password = settings.UNICEL_CONFIG.get("password"),
            sender = settings.UNICEL_CONFIG.get("sender"),
            reply_to_phone_number = settings.UNICEL_CONFIG.get("receive_phone", None),
        )
        backend._id = backend.name
        backend.save()

    # If no mach backend was present, create one
    try:
        MobileBackend.get("MOBILE_BACKEND_MACH")
    except ResourceNotFound:
        backend = MachBackend(
            domain = None,
            name = "MOBILE_BACKEND_MACH",
            authorized_domains = [],
            is_global = True,
            account_id = re.match(r".*id=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1),
            password = re.match(r".*pw=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1),
            sender_id = "DIMAGI",
        )
        backend._id = backend.name
        backend.save()

    # If no test backend was present, create one
    try:
        MobileBackend.get("MOBILE_BACKEND_TEST")
    except ResourceNotFound:
        backend = TestSMSBackend(
            domain = None,
            name = "MOBILE_BACKEND_TEST",
            authorized_domains = [],
            is_global = True,
        )
        backend._id = backend.name
        backend.save()
Example #6
0
 def handle(self, *labels, **options):
     SmsGatewayFee.create_new(UnicelBackend.get_api_id(), INCOMING, 0.50,
                              currency=Currency.objects.get(code="INR"))
     SmsGatewayFee.create_new(UnicelBackend.get_api_id(), OUTGOING, 0.50,
                              currency=Currency.objects.get(code="INR"))
     logger.info("Updated Unicel gateway fees.")
Example #7
0
                        continue

                    mach_number = MachPhoneNumber.get_by_number(message.phone_number, mach_row)
                    rate_item = MachSMSRate.get_by_number(message.direction, mach_number) if mach_number else None

                    billable = cls.new_billable(rate_item, message)
                    if billable:
                        billable.contacted_mach_api = datetime.datetime.now(tz=pytz.utc)
                        billable.update_mach_delivery_status(mach_row)
                        billable.save()
                        return
                    logging.error("[Billing] MACH API Response was successful, but creating the MACH "
                                  "billable was not. SMSLog # %s" % message.get_id)
                else:
                    logging.error("[Billing] There was an error retrieving message delivery information from MACH.")
            else:
                logging.error("[Billing] There was an error accessing the MACHI API.")
        else:
            logging.error("[Billing] There was an error while trying to send an SMS via MACH.")

    @staticmethod
    def api_name():
        return "Mach"


API_TO_BILLABLE = {
    UnicelBackend.get_api_id(): UnicelSMSBillable,
    TropoBackend.get_api_id(): TropoSMSBillable,
    MachBackend.get_api_id(): MachSMSBillable,
}