Example #1
0
class Invoice:
    def __init__(self, config=None):
        if config is None:
            self.client = Client(configuration.ZOHO_SUBSCRIPTION_CONFIG)
        else:
            self.client = Client(config)

    def list_invoices_by_customer(self, customer_id):
        cache_key = "zoho_invoices_%s" % customer_id
        response = self.client.get_from_cache(cache_key)
        if response is None:
            invoices_by_customer_uri = 'invoices?customer_id=%s' % customer_id
            result = self.client.send_request("GET", invoices_by_customer_uri)
            if type(result) is HTTPError:
                result_bytes = result.response._content
                result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
                return result_dict['message']
            else:
                response = result['invoices']
                self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)
        return response

    def get_invoice(self, invoice_id):
        cache_key = "zoho_invoices_%s" % invoice_id
        response = self.client.get_from_cache(cache_key)
        if response is None:
            invoice_by_invoice_id_uri = 'invoices/%s' % invoice_id
            result = self.client.send_request("GET", invoice_by_invoice_id_uri)
            if type(result) is HTTPError:
                result_bytes = result.response._content
                result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
                return result_dict['message']
            else:
                response = result['invoice']
                self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)

        return response

    def get_invoice_pdf(self, invoice_id):
        data = {'query': {'accept': 'pdf'}}
        invoice_pdf_by_invoice_id_uri = 'invoices/%s' % invoice_id
        headers = {"Accept": "application/pdf"}
        result = self.client.send_request("GET",
                                          invoice_pdf_by_invoice_id_uri,
                                          data=data,
                                          headers=headers)
        if type(result) is HTTPError:
            result_bytes = result.response._content
            result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
            return result_dict['message']
        return result
class Addon:
    def __init__(self, config=None):
        if config is None:
            self.client = Client(configuration.ZOHO_SUBSCRIPTION_CONFIG)
        else:
            self.client = Client(config)

    def list_addons(self, filters=None):
        cache_key = 'addons'
        response = self.client.get_from_cache(cache_key)
        if response is None:
            list_of_addons_uri = 'addons'
            result = self.client.send_request("GET", list_of_addons_uri)
            response = result['addons']
            self.client.add_to_cache(cache_key, response)

        if filters is not None:
            for addon in response:
                if (addon['name'] == filters['name']
                        or addon['addon_code'] == filters['addon_code']):
                    return addon
        else:
            print("Returning from cache : " + cache_key)

        return response

    def get_addon(self, addon_code=None):
        cache_key = 'addon_%s' % addon_code
        response = self.client.get_from_cache(cache_key)
        if response is None:
            addon_by_addon_code = 'addons/%s' % addon_code
            result = self.client.send_request("GET", addon_by_addon_code)
            if type(result) is HTTPError:
                result_bytes = result.response._content
                result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
                return result_dict['message']
            else:
                response = result['addon']
                self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)

        return response
class Subscription:
    def __init__(self, config=None):
        if config is None:
            self.client = Client(configuration.ZOHO_SUBSCRIPTION_CONFIG)
        else:
            self.client = Client(config)

    def plan(self):
        return Plan(self.client)

    def customer(self):
        return Customer(self.client)

    def add_on(self):
        return Addon(self.client)

    def invoice(self):
        return Invoice(self.client)

    def hosted_page(self):
        return HostedPage(self.client)

    def get(self, id):
        cache_key = "zoho_subscription_%s" % id
        response = self.client.get_from_cache(cache_key)
        if response is None:
            get_subscription_by_id_uri = "subscriptions/%s" % id
            response = self.client.send_request("GET",
                                                get_subscription_by_id_uri)
            self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)
        return response

    def create(self, data):
        return self.client.send_request("POST",
                                        'subscriptions',
                                        data=data,
                                        headers=None)

    def buy_add_on(self, subscription_id, data):
        buy_add_on_uri = 'subscriptions/%s/buyonetimeaddon' % subscription_id
        return self.client.send_request("POST",
                                        buy_add_on_uri,
                                        data=data,
                                        headers=None)

    def associate_coupon(self, subscription_id, coupon_code):
        coupon_uri = 'subscriptions/%s/coupons/%s' % (subscription_id,
                                                      coupon_code)
        return self.client.send_request("POST", coupon_uri)

    def reactivate(self, subscription_id):
        reactivate_uri = 'subscriptions/%s/reactivate' % subscription_id
        self.client.send_request("POST", reactivate_uri)

    def list_subscriptions_by_customer(self, customer_id):
        cache_key = "zoho_subscriptions_by_customer_%s" % customer_id
        response = self.client.get_from_cache(cache_key)
        if response is None:
            subscriptions_by_customer_uri = 'subscriptions?customer_id=%s' % customer_id
            result = self.client.send_request("GET",
                                              subscriptions_by_customer_uri)
            response = result['subscriptions']
            self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)
        return response

    def get_subscriptions(self, subscription_id):
        cache_key = "zoho_subscriptions_by_customer_%s" % subscription_id
        response = self.client.get_from_cache(cache_key)
        if response is None:
            subscriptions_by_subscription_id_uri = 'subscriptions/%s' % subscription_id
            result = self.client.send_request(
                "GET", subscriptions_by_subscription_id_uri)
            if type(result) is HTTPError:
                result_bytes = result.response._content
                result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
                return result_dict['message']
            else:
                response = result['subscription']
                self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)
        return response
Example #4
0
class Plan:
    add_on_types = [
        'recurring',
        'one_time',
    ]

    def __init__(self, config=None):
        if config is None:
            self.client = Client(configuration.ZOHO_SUBSCRIPTION_CONFIG)
        else:
            self.client = Client(config)

    def list_plans(self, filters=None, with_add_ons=True, add_on_type=None):
        cache_key = 'plans'
        response = self.client.get_from_cache(cache_key)
        if response is None:
            list_of_plan_uri = 'plans'
            result = self.client.send_request("GET", list_of_plan_uri)
            response = result['plans']
            self.client.add_to_cache(cache_key, response)

        if filters is not None:
            for plan in response:
                if (plan['name'] == filters['name']
                        or plan['plan_code'] == filters['plan_code']):
                    return plan

        # if with_add_ons is not None:
        #     if with_add_ons in add_on_type:
        #         return None
        else:
            print("Returning from cache : " + cache_key)
        return response

    def get_plan(self, plan_code):
        cache_key = 'plan_%s' % plan_code
        response = self.client.get_from_cache(cache_key)
        if response is None:
            plan_by_plan_code = 'plans/%s' % plan_code
            result = self.client.send_request("GET", plan_by_plan_code)
            if type(result) is HTTPError:
                result_bytes = result.response._content
                result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
                return result_dict['message']
            else:
                response = result['plan']
                self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)

        return response

    def get_addons_for_plan(self, plan_code):
        cache_key = 'plans'
        addon_code_list = []
        addon = Addon()
        response = self.client.get_from_cache(cache_key)
        if response is None:
            list_of_plan_uri = 'plans'
            result = self.client.send_request("GET", list_of_plan_uri)
            response = result['plans']
            self.client.add_to_cache(cache_key, response)
            if plan_code is not None:
                for each_plan in response:
                    if each_plan.get("addons"):
                        if each_plan.get('plan_code') == plan_code:
                            for each_addon_code in each_plan["addons"]:
                                addon_code_list.append(
                                    addon.get_addon(
                                        each_addon_code['addon_code']))
                return addon_code_list
            else:
                print("Returning from cache : " + cache_key)

    # Filter Plan

    def get_price_by_plan_code(self, plan_code):
        cache_key = 'plan_%s' % plan_code
        response = self.client.get_from_cache(cache_key)
        if response is None:
            plan_by_plan_code = 'plans/%s' % plan_code
            result = self.client.send_request("GET", plan_by_plan_code)
            if type(result) is HTTPError:
                result_bytes = result.response._content
                result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
                return result_dict['message']
            else:
                response = result['plan']
                self.client.add_to_cache(cache_key, response)
                recurring_price = response['recurring_price']
                return recurring_price
        else:
            print("Returning from cache : " + cache_key)
        return response
Example #5
0
class Customer:
    def __init__(self, config=None):
        if config is None:
            self.client = Client(configuration.ZOHO_SUBSCRIPTION_CONFIG)
        else:
            self.client = Client(config)

    def get_list_customers_by_Email(self, customer_email):
        cache_key = 'zoho_customer_%s' % hashlib.md5(
            customer_email.encode('utf-8')).hexdigest()
        response = self.client.get_from_cache(cache_key)
        if response is None:
            list_of_customers_by_Email_uri = 'customers?email=%s' % customer_email
            result = self.client.send_request("GET",
                                              list_of_customers_by_Email_uri)
            response = result['customers']
            self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)

        return response

    def get_customer_by_Email(self, customer_email):
        customers = self.get_list_customers_by_Email(customer_email)
        if len(customers) == 0:
            return {
                "message": f"customer with email {customer_email} not found"
            }
        return self.get_customer_by_id(customers[0]['customer_id'])

    def get_customer_by_id(self, customer_id):
        cache_key = 'zoho_customer_%s' % customer_id
        response = self.client.get_from_cache(cache_key)
        if response is None:
            customer_by_customer_id_uri = 'customers/%s' % customer_id
            result = self.client.send_request("GET",
                                              customer_by_customer_id_uri)
            if type(result) is HTTPError:
                result_bytes = result.response._content
                result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
                return result_dict['message']
            else:
                response = result['customer']
                self.client.add_to_cache(cache_key, response)
        else:
            print("Returning from cache : " + cache_key)

        return response

    def update_customer(self, customer_id, data):
        cache_key = 'zoho_customer_%s' % customer_id
        headers = {'Content-type': 'application/json'}
        update_customer_by_customer_id = 'customers/%s' % customer_id
        result = self.client.send_request("PUT",
                                          update_customer_by_customer_id,
                                          data=data,
                                          headers=headers)
        if type(result) is HTTPError:
            result_bytes = result.response._content
            result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
            return result_dict['message']
        if result['code'] == 0:
            customer_val = result['customer']
            self.delete_customer_cache(cache_key)
            return customer_val
        else:
            return None

    def delete_customer_cache(self, cache_key_by_id):
        result = self.client.delete_from_cache(cache_key_by_id)
        return result

    def create_customer(self, data):
        headers = {'Content-type': 'application/json'}
        result = self.client.send_request("POST",
                                          'customers',
                                          data=data,
                                          headers=headers)
        if type(result) is HTTPError:
            result_bytes = result.response._content
            result_dict = ast.literal_eval(result_bytes.decode('utf-8'))
            return result_dict['message']
        else:
            if result.get('customer'):
                customer = result['customer']
                return customer