예제 #1
0
파일: models.py 프로젝트: qoda/rapidpro
    def trigger_airtime_event(cls, org, ruleset, contact, event):
        from temba.api.models import get_api_user

        api_user = get_api_user()

        channel = None
        contact_urn = None
        # if we have an SMS event use its channel and contact urn
        if event:
            channel = event.channel
            contact_urn = event.contact_urn

        if not contact_urn:
            contact_urn = contact.get_urn(TEL_SCHEME)

        airtime = AirtimeTransfer.objects.create(
            org=org,
            channel=channel,
            contact=contact,
            recipient=contact_urn.path,
            amount=0,
            created_by=api_user,
            modified_by=api_user,
        )

        message = "None"
        try:

            if not org.is_connected_to_transferto():
                message = "No transferTo Account connected to this organization"
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            config = org.config
            account_currency = config.get(TRANSFERTO_ACCOUNT_CURRENCY, "")
            if not account_currency:
                org.refresh_transferto_account_currency()
                config = org.config
                account_currency = config.get(TRANSFERTO_ACCOUNT_CURRENCY, "")

            action = "msisdn_info"
            request_kwargs = dict(
                action=action,
                destination_msisdn=airtime.recipient,
                currency=account_currency,
                delivered_amount_info="1",
            )
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(
                response.text)

            error_code = int(content_json.get("error_code", None))
            error_txt = content_json.get("error_txt", None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (
                    error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            country_name = content_json.get("country", "")
            country_code = get_country_code_by_name(country_name)
            country_config = ruleset.config.get(country_code, dict())
            amount = country_config.get("amount", 0)

            airtime.amount = amount

            product_list = content_json.get("product_list", [])
            if not isinstance(product_list, list):
                product_list = [product_list]

            skuid_list = content_json.get("skuid_list", [])
            if not isinstance(skuid_list, list):
                skuid_list = [skuid_list]

            local_info_value_list = content_json.get("local_info_value_list",
                                                     [])
            if not isinstance(local_info_value_list, list):
                local_info_value_list = [local_info_value_list]

            product_local_value_map = dict(
                zip([float(elt) for elt in local_info_value_list],
                    product_list))

            product_skuid_value_map = dict(zip(product_list, skuid_list))

            targeted_prices = [
                float(i) for i in local_info_value_list
                if float(i) <= float(amount)
            ]

            denomination = None
            skuid = content_json.get("skuid", None)
            if targeted_prices:
                denomination_key = max(targeted_prices)
                denomination = product_local_value_map.get(
                    denomination_key, None)
                skuid = product_skuid_value_map.get(denomination)

            elif skuid:
                minimum_local = content_json.get(
                    "open_range_minimum_amount_local_currency", 0)
                maximum_local = content_json.get(
                    "open_range_maximum_amount_local_currency", 0)
                minimum_acc_currency = content_json.get(
                    "open_range_minimum_amount_requested_currency", 0)
                maximum_acc_currency = content_json.get(
                    "open_range_maximum_amount_requested_currency", 0)

                local_interval = float(maximum_local) - float(minimum_local)
                acc_currency_interval = float(maximum_acc_currency) - float(
                    minimum_acc_currency)
                amount_interval = float(amount) - float(minimum_local)
                amount_interval_acc_currency = (
                    amount_interval * acc_currency_interval) / local_interval
                denomination = str(
                    float(minimum_acc_currency) + amount_interval_acc_currency)

            if denomination is not None:
                airtime.denomination = denomination

            if float(amount) <= 0:
                message = (
                    "Failed by invalid amount configuration or missing amount configuration for %s"
                    % country_name)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            if denomination is None:  # pragma: needs cover
                message = "No TransferTo denomination matched"
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            action = "reserve_id"
            request_kwargs = dict(action=action)
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(
                response.text)

            error_code = int(content_json.get("error_code", None))
            error_txt = content_json.get("error_txt", None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (
                    error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            transaction_id = content_json.get("reserved_id")

            action = "topup"
            request_kwargs = dict(
                action=action,
                reserved_id=transaction_id,
                msisdn=channel.address if channel else "",
                destination_msisdn=airtime.recipient,
                currency=account_currency,
                product=airtime.denomination,
            )

            if skuid:
                request_kwargs["skuid"] = skuid

            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(
                response.text)

            error_code = int(content_json.get("error_code", None))
            error_txt = content_json.get("error_txt", None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (
                    error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            message = "Airtime Transferred Successfully"
            airtime.status = AirtimeTransfer.SUCCESS

        except Exception as e:
            import traceback

            traceback.print_exc()

            airtime.status = AirtimeTransfer.FAILED
            message = "Error transferring airtime: %s" % str(e)

        finally:
            airtime.message = message
            airtime.save()

        return airtime
예제 #2
0
    def trigger_airtime_event(cls, org, ruleset, contact, event):
        from temba.api.models import get_api_user

        api_user = get_api_user()

        channel = None
        contact_urn = None
        # if we have an SMS event use its channel and contact urn
        if event:
            channel = event.channel
            contact_urn = event.contact_urn

        if not contact_urn:
            contact_urn = contact.get_urn(TEL_SCHEME)

        airtime = AirtimeTransfer.objects.create(
            org=org,
            channel=channel,
            contact=contact,
            recipient=contact_urn.path,
            amount=0,
            created_by=api_user,
            modified_by=api_user,
        )

        message = "None"
        try:

            if not org.is_connected_to_transferto():
                message = "No transferTo Account connected to this organization"
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            config = org.config
            account_currency = config.get(TRANSFERTO_ACCOUNT_CURRENCY, "")
            if not account_currency:
                org.refresh_transferto_account_currency()
                config = org.config
                account_currency = config.get(TRANSFERTO_ACCOUNT_CURRENCY, "")

            action = "msisdn_info"
            request_kwargs = dict(
                action=action,
                destination_msisdn=airtime.recipient,
                currency=account_currency,
                delivered_amount_info="1",
            )
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(response.text)

            error_code = int(content_json.get("error_code", None))
            error_txt = content_json.get("error_txt", None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            country_name = content_json.get("country", "")
            country_code = get_country_code_by_name(country_name)
            country_config = ruleset.config.get(country_code, dict())
            amount = country_config.get("amount", 0)

            airtime.amount = amount

            product_list = content_json.get("product_list", [])
            if not isinstance(product_list, list):
                product_list = [product_list]

            skuid_list = content_json.get("skuid_list", [])
            if not isinstance(skuid_list, list):
                skuid_list = [skuid_list]

            local_info_value_list = content_json.get("local_info_value_list", [])
            if not isinstance(local_info_value_list, list):
                local_info_value_list = [local_info_value_list]

            product_local_value_map = dict(zip([float(elt) for elt in local_info_value_list], product_list))

            product_skuid_value_map = dict(zip(product_list, skuid_list))

            targeted_prices = [float(i) for i in local_info_value_list if float(i) <= float(amount)]

            denomination = None
            skuid = content_json.get("skuid", None)
            if targeted_prices:
                denomination_key = max(targeted_prices)
                denomination = product_local_value_map.get(denomination_key, None)
                skuid = product_skuid_value_map.get(denomination)

            elif skuid:
                minimum_local = content_json.get("open_range_minimum_amount_local_currency", 0)
                maximum_local = content_json.get("open_range_maximum_amount_local_currency", 0)
                minimum_acc_currency = content_json.get("open_range_minimum_amount_requested_currency", 0)
                maximum_acc_currency = content_json.get("open_range_maximum_amount_requested_currency", 0)

                local_interval = float(maximum_local) - float(minimum_local)
                acc_currency_interval = float(maximum_acc_currency) - float(minimum_acc_currency)
                amount_interval = float(amount) - float(minimum_local)
                amount_interval_acc_currency = (amount_interval * acc_currency_interval) / local_interval
                denomination = str(float(minimum_acc_currency) + amount_interval_acc_currency)

            if denomination is not None:
                airtime.denomination = denomination

            if float(amount) <= 0:
                message = (
                    "Failed by invalid amount configuration or missing amount configuration for %s" % country_name
                )
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            if denomination is None:  # pragma: needs cover
                message = "No TransferTo denomination matched"
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            action = "reserve_id"
            request_kwargs = dict(action=action)
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(response.text)

            error_code = int(content_json.get("error_code", None))
            error_txt = content_json.get("error_txt", None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            transaction_id = content_json.get("reserved_id")

            action = "topup"
            request_kwargs = dict(
                action=action,
                reserved_id=transaction_id,
                msisdn=channel.address if channel else "",
                destination_msisdn=airtime.recipient,
                currency=account_currency,
                product=airtime.denomination,
            )

            if skuid:
                request_kwargs["skuid"] = skuid

            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(response.text)

            error_code = int(content_json.get("error_code", None))
            error_txt = content_json.get("error_txt", None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            message = "Airtime Transferred Successfully"
            airtime.status = AirtimeTransfer.SUCCESS

        except Exception as e:
            import traceback

            traceback.print_exc()

            airtime.status = AirtimeTransfer.FAILED
            message = "Error transferring airtime: %s" % str(e)

        finally:
            airtime.message = message
            airtime.save()

        return airtime
예제 #3
0
    def trigger_airtime_event(cls, org, ruleset, contact, event):

        api_user = get_api_user()

        channel = None
        contact_urn = None
        # if we have an SMS event use its channel and contact urn
        if event:
            channel = event.channel
            contact_urn = event.contact_urn

        if not contact_urn:
            contact_urn = contact.get_urn(TEL_SCHEME)

        airtime = AirtimeTransfer.objects.create(org=org,
                                                 channel=channel,
                                                 contact=contact,
                                                 recipient=contact_urn.path,
                                                 amount=0,
                                                 created_by=api_user,
                                                 modified_by=api_user)

        message = "None"
        try:

            if not org.is_connected_to_transferto():
                message = "No transferTo Account connected to this organization"
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            action = 'msisdn_info'
            request_kwargs = dict(action=action,
                                  destination_msisdn=airtime.recipient)
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(
                response.content)

            error_code = int(content_json.get('error_code', None))
            error_txt = content_json.get('error_txt', None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (
                    error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            country_name = content_json.get('country', '')
            country_code = get_country_code_by_name(country_name)
            amount_config = ruleset.config_json()
            country_config = amount_config.get(country_code, dict())
            amount = country_config.get('amount', 0)

            airtime.amount = amount

            product_list = content_json.get('product_list', [])

            if not isinstance(product_list, list):  # pragma: needs cover
                product_list = [product_list]

            targeted_prices = [
                float(i) for i in product_list if float(i) <= float(amount)
            ]

            denomination = None
            if targeted_prices:
                denomination = max(targeted_prices)
                airtime.denomination = denomination

            if float(amount) <= 0:
                message = "Failed by invalid amount configuration or missing amount configuration for %s" % country_name
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            if denomination is None:  # pragma: needs cover
                message = "No TransferTo denomination matched"
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            action = 'reserve_id'
            request_kwargs = dict(action=action)
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(
                response.content)

            error_code = int(content_json.get('error_code', None))
            error_txt = content_json.get('error_txt', None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (
                    error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            transaction_id = content_json.get('reserved_id')

            action = 'topup'
            request_kwargs = dict(action=action,
                                  reserved_id=transaction_id,
                                  msisdn=channel.address if channel else '',
                                  destination_msisdn=airtime.recipient,
                                  product=airtime.denomination)
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(
                response.content)

            error_code = int(content_json.get('error_code', None))
            error_txt = content_json.get('error_txt', None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (
                    error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            message = "Airtime Transferred Successfully"
            airtime.status = AirtimeTransfer.SUCCESS

        except Exception as e:
            import traceback
            traceback.print_exc()

            airtime.status = AirtimeTransfer.FAILED
            message = "Error transferring airtime: %s" % unicode(e)

        finally:
            airtime.message = message
            airtime.save()

        return airtime
예제 #4
0
파일: models.py 프로젝트: ewheeler/rapidpro
    def trigger_airtime_event(cls, org, ruleset, contact, event):

        api_user = get_api_user()

        channel = None
        contact_urn = None
        # if we have an SMS event use its channel and contact urn
        if event:
            channel = event.channel
            contact_urn = event.contact_urn

        if not contact_urn:
            contact_urn = contact.get_urn(TEL_SCHEME)

        airtime = AirtimeTransfer.objects.create(org=org, channel=channel, contact=contact, recipient=contact_urn.path,
                                                 amount=0, created_by=api_user, modified_by=api_user)

        message = "None"
        try:

            if not org.is_connected_to_transferto():
                message = "No transferTo Account connected to this organization"
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            action = 'msisdn_info'
            request_kwargs = dict(action=action, destination_msisdn=airtime.recipient)
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(response.content)

            error_code = int(content_json.get('error_code', None))
            error_txt = content_json.get('error_txt', None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            country_name = content_json.get('country', '')
            country_code = get_country_code_by_name(country_name)
            amount_config = ruleset.config_json()
            country_config = amount_config.get(country_code, dict())
            amount = country_config.get('amount', 0)

            airtime.amount = amount

            product_list = content_json.get('product_list', [])

            if not isinstance(product_list, list):
                product_list = [product_list]

            targeted_prices = [float(i) for i in product_list if float(i) <= float(amount)]

            denomination = None
            if targeted_prices:
                denomination = max(targeted_prices)
                airtime.denomination = denomination

            if float(amount) <= 0:
                message = "Failed by invalid amount configuration or missing amount configuration for %s" % country_name
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            if denomination is None:
                message = "No TransferTo denomination matched"
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            action = 'reserve_id'
            request_kwargs = dict(action=action)
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(response.content)

            error_code = int(content_json.get('error_code', None))
            error_txt = content_json.get('error_txt', None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            transaction_id = content_json.get('reserved_id')

            action = 'topup'
            request_kwargs = dict(action=action,
                                  reserved_id=transaction_id,
                                  msisdn=channel.address if channel else '',
                                  destination_msisdn=airtime.recipient,
                                  product=airtime.denomination)
            response = airtime.get_transferto_response(**request_kwargs)
            content_json = AirtimeTransfer.parse_transferto_response(response.content)

            error_code = int(content_json.get('error_code', None))
            error_txt = content_json.get('error_txt', None)

            if error_code != 0:
                message = "Got non-zero error code (%d) from TransferTo with message (%s)" % (error_code, error_txt)
                airtime.status = AirtimeTransfer.FAILED
                raise Exception(message)

            message = "Airtime Transferred Successfully"
            airtime.status = AirtimeTransfer.SUCCESS

        except Exception as e:
            import traceback
            traceback.print_exc()

            airtime.status = AirtimeTransfer.FAILED
            message = "Error transferring airtime: %s" % unicode(e)

        finally:
            airtime.message = message
            airtime.save()

        return airtime