예제 #1
0
    def remove_credits(self, quantity, sid=None):
        """
        Suspends an existent subscriptions

        :param sid: ID of an existent subscriptions in API
        :param plan_identifier: the identifier of a plan (it's not ID)
        """
        data = []
        if not sid:
            if self.id:
                if not self.credits_based:
                    raise errors.\
                        IuguSubscriptionsException(value="Instance must be " \
                                        "object of SubscriptionCreditsBased")
                sid = self.id
            else:
                raise errors.IuguSubscriptionsException(
                    value="ID (sid) can't be empty")

        urn = "/v1/subscriptions/{sid}/remove_credits".format(sid=sid)
        data.append(("quantity", quantity))
        response = self._conn.put(urn, data)

        if not self.is_credit_based(response):
            raise errors.IuguSubscriptionsException(value="Instance must be " \
                                        "object of SubscriptionCreditsBased")

        response["_type"] = "credit_based"
        return SubscriptionCreditsBased(**response)
예제 #2
0
    def create(self,
               customer_id,
               credits_cycle,
               price_cents=None,
               credits_min=None,
               expires_at=None,
               only_on_charge_success=None,
               subitems=None,
               custom_variables=None):
        """
        Create a subscription based in credits and return the instance
        this class.

        :param: custom_variables: a dict {'key': 'value'}
        """

        if price_cents is None or price_cents <= 0:
            raise errors.IuguSubscriptionsException(value="price_cents must be " \
                                         "greater than 0")

        credits_based = self.credits_based

        if custom_variables:
            assert isinstance(custom_variables, dict), "Required a dict"
            custom_data = self.custom_variables_list(custom_variables)

        kwargs_local = locals().copy()
        kwargs_local.pop('self')
        urn = "/v1/subscriptions"
        self.data = kwargs_local
        response = self._conn.post(urn, self.data)
        response["_type"] = "credit_based"
        return SubscriptionCreditsBased(**response)
예제 #3
0
    def save(self):
        """ Saves an instance of this class that was persisted or raise error
         if no instance.

        NOTE: to use create() or set() for add/change custom_variables
        """
        if self.id:
            sid = self.id
        else:
            raise errors.IuguSubscriptionsException(value="Save is support "\
                        "only to returned API object.")

        kwargs = {}
        # TODO: to improve this ineffective approach.
        # Currently this check if the set's parameters was passed. If changes
        # occurs in set() to revise this k in if used to mount kwargs
        for k, v in self.__dict__.items():
            if v is not None:
                if  k == "expires_at" or \
                    k == "subitems" or k == "suspended" or \
                    k == "skip_charge" or k == "price_cents" or \
                    k == "credits_cycle" or k == "credits_min" or \
                    k == "custom_variables" :
                    kwargs[k] = v
                    last_valid_k = k

                    if isinstance(v, list) and len(v) == 0 and last_valid_k:
                        # solves problem with arguments of empty lists
                        del kwargs[last_valid_k]
                        del last_valid_k

        return self.set(sid, **kwargs)
예제 #4
0
    def change_plan(self, plan_identifier, sid=None):
        """
        Changes the plan for existent subscriptions

        :param sid: ID of an existent subscriptions in API
        :param plan_identifier: the identifier of a plan (it's not ID)
        """
        if not sid:
            if self.id:
                # short-circuit
                if "credits_based" in self.__dict__ and self.credits_based:
                    raise errors.\
                        IuguSubscriptionsException(value="Instance must be " \
                                        "object of IuguSubscriptionsException")
                sid = self.id
            else:
                raise errors.IuguSubscriptionsException(
                    value="ID (sid) can't be empty")

        urn = "/v1/subscriptions/{sid}/change_plan/{plan_identifier}"\
                .format(sid=sid, plan_identifier=plan_identifier)
        response = self._conn.post(urn, [])

        if self.is_credit_based(response):
            response["_type"] = "credit_based"
            return SubscriptionCreditsBased(**response)

        response["_type"] = "general"
        return IuguSubscription(**response)
예제 #5
0
    def save(self):
        """Saves an instance of subscription and return own class instance
        modified"""

        if self.id:
            sid = self.id
        else:
            raise errors.IuguSubscriptionsException(value="Save is support "\
                        "only to returned API object.")

        kwargs = {}
        # TODO: to improve this ineffective approach
        # Currently this check if the required set's parameters was passed
        # If changes occurs in set() to revise this k in if used to mount kwargs
        for k, v in self.__dict__.items():
            if v is not None:
                if  k == "plan_identifier" or \
                    k == "expires_at" or k == "subitems" or \
                    k == "suspended" or k == "skip_charge" or \
                                k == "custom_variables":
                    kwargs[k] = v
                    last_valid_k = k

                if isinstance(v, list) and len(v) == 0 and last_valid_k:
                    # solves problem with arguments of empty lists
                    del kwargs[last_valid_k]

        return self.set(sid, **kwargs)
예제 #6
0
    def remove(self, sid=None):
        """
        Removes a subscription given id or instance

        :param sid: ID of an existent subscriptions in API
        """
        if not sid:
            if self.id:
                sid = self.id
            else:
                raise errors.IuguSubscriptionsException(
                    value="ID (sid) can't be empty")

        urn = "/v1/subscriptions/{sid}".format(sid=sid)
        self._conn.delete(urn, [])
예제 #7
0
    def remove(self, invoice_id=None):
        """
        Removes an invoice by id or instance and returns None
        """
        invoice_id = invoice_id if invoice_id else self.id
        if invoice_id is None:
            raise errors.IuguSubscriptionsException(
                value="ID (invoice_id) can't be empty")

        urn = "/v1/invoices/{invoice_id}".format(invoice_id=invoice_id)
        response = self.__conn.delete(urn, [])
        obj = IuguInvoice(**response)
        # TODO: list comprehensions ?
        if obj:
            for k, v in self.__dict__.items():
                self.__dict__[k] = None
예제 #8
0
    def suspend(self, sid=None):
        """
        Suspends an existent subscriptions

        :param sid: ID of an existent subscriptions in API
        """
        if not sid:
            if self.id:
                sid = self.id
            else:
                raise errors.IuguSubscriptionsException(
                    value="ID (sid) can't be empty")

        urn = "/v1/subscriptions/{sid}/suspend".format(sid=sid)
        response = self._conn.post(urn, [])

        if self.is_credit_based(response):
            response["_type"] = "credit_based"
            return SubscriptionCreditsBased(**response)

        response["_type"] = "general"
        return IuguSubscription(**response)
예제 #9
0
    def data(self, kwargs):
        """
        Body data for request send
        """
        data = []
        self.id = kwargs.get("sid")
        self.customer_id = kwargs.get("customer_id")
        self.plan_identifier = kwargs.get("plan_identifier")
        self.expires_at = kwargs.get("expires_at")
        self.only_on_charge_success = kwargs.get("only_on_charge_success")
        self.subitems = kwargs.get("subitems")
        self.custom_variables = kwargs.get("custom_data")
        self.credits_based = kwargs.get("credits_based")
        self.credits_min = kwargs.get("credits_min")
        self.credits_cycle = kwargs.get("credits_cycle")
        self.price_cents = kwargs.get("price_cents")
        self.suspended = kwargs.get("suspended")
        self.skip_charge = kwargs.get("skip_charge")

        if self.id:
            data.append(("id", self.id))

        if self.customer_id:
            data.append(("customer_id", self.customer_id))

        if self.plan_identifier:
            data.append(("plan_identifier", self.plan_identifier))

        if self.expires_at:
            data.append(("expires_at", self.expires_at))

        if self.only_on_charge_success:
            value_charge_success = str(self.only_on_charge_success)
            value_charge_success = value_charge_success.lower()
            data.append(("only_on_charge_success", value_charge_success))

        if self.subitems:
            if isinstance(self.subitems, list):
                for item in self.subitems:
                    data.extend((item.to_data(is_subscription=True)))
            else:
                raise errors.IuguSubscriptionsException("The subitems must be " \
                    "a list of obj Item")

        if self.custom_variables:  # TODO: to create test
            data.extend(self.custom_variables)

        # credit based subscriptions
        if self.credits_based is not None:
            value_credits_based = str(self.credits_based)
            value_credits_based = value_credits_based.lower()
            data.append(("credits_based", value_credits_based))

        if self.credits_min:
            data.append(("credits_min", self.credits_min))

        if self.credits_cycle:
            data.append(("credits_cycle", self.credits_cycle))

        if self.price_cents:
            data.append(("price_cents", self.price_cents))

        if self.suspended is not None:
            value_suspended = str(self.suspended)
            value_suspended = value_suspended.lower()
            data.append(("suspended", value_suspended))

        if self.skip_charge is not None:
            value_skip_charge = str(self.skip_charge)
            value_skip_charge = value_skip_charge.lower()
            data.append(("skip_charge", value_skip_charge))

        self._data = data