Beispiel #1
0
    def __init__(self, username, password, product_code, cache=None,
                 timeout=None, endpoint=None):
        self.product_code = product_code
        self.client = Client(
            username,
            password,
            product_code,
            cache,
            timeout,
            endpoint,
        )

        super(CheddarProduct, self).__init__()
Beispiel #2
0
    def get_client(self, **kwargs):
        client_kwargs = copy(self.client_defaults)
        client_kwargs.update(kwargs)

        c = Client(**client_kwargs)

        return c
Beispiel #3
0
    def get_client(self, **kwargs):
        ''' Helper mthod for instantiating client. '''
        client_kwargs = copy(self.client_defaults)
        client_kwargs.update(kwargs)

        c = Client(**client_kwargs)

        return c
Beispiel #4
0
 def __init__(self, username, password, product_code, cache=None, timeout=None, endpoint=None):
     self.product_code = product_code
     self.client = Client(
         username,
         password,
         product_code,
         cache,
         timeout,
         endpoint,
     )
     
     super(CheddarProduct, self).__init__()
Beispiel #5
0
class CheddarProduct(object):
    def __init__(self,
                 username,
                 password,
                 product_code,
                 cache=None,
                 timeout=None,
                 endpoint=None):
        self.product_code = product_code
        self.client = Client(
            username,
            password,
            product_code,
            cache,
            timeout,
            endpoint,
        )

        super(CheddarProduct, self).__init__()

    def __repr__(self):
        return u'CheddarProduct: %s' % self.product_code

    def get_all_plans(self):
        response = self.client.make_request(path='plans/get')
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]

        return plans

    def get_plan(self, code):
        response = self.client.make_request(
            path='plans/get',
            params={'code': code},
        )
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]

        return plans[0]

    def create_customer(self, code, first_name, last_name, email, plan_code, \
                        company=None, is_vat_exempt=None, vat_number=None, \
                        notes=None, first_contact_datetime=None, \
                        referer=None, campaign_term=None, \
                        campaign_name=None, campaign_source=None, \
                        campaign_medium=None, campaign_content=None, \
                        meta_data=None, initial_bill_date=None, method=None,\
                        cc_number=None, cc_expiration=None, \
                        cc_card_code=None, cc_first_name=None, \
                        cc_last_name=None, cc_email=None, cc_company=None, \
                        cc_country=None, cc_address=None, cc_city=None, \
                        cc_state=None, cc_zip=None, return_url=None, \
                        cancel_url=None, charges=None, items=None):

        data = self.build_customer_post_data(code, first_name, last_name, \
                    email, plan_code, company, is_vat_exempt, vat_number, \
                    notes, first_contact_datetime, referer, campaign_term, \
                    campaign_name, campaign_source, campaign_medium, \
                    campaign_content, meta_data, initial_bill_date, method, \
                    cc_number, cc_expiration, cc_card_code, cc_first_name, \
                    cc_last_name, cc_email, cc_company, cc_country, cc_address, \
                    cc_city, cc_state, cc_zip, return_url, cancel_url)

        if charges:
            for i, charge in enumerate(charges):
                data['charges[%d][chargeCode]' % i] = charge['code']
                data['charges[%d][quantity]' % i] = charge.get('quantity', 1)
                data['charges[%d][eachAmount]' %
                     i] = '%.2f' % charge['each_amount']
                data['charges[%d][description]' % i] = charge.get(
                    'description', '')

        if items:
            for i, item in enumerate(items):
                data['items[%d][itemCode]' % i] = item['code']
                data['items[%d][quantity]' % i] = item.get('quantity', 1)

        response = self.client.make_request(path='customers/new', data=data)
        cusotmer_parser = CustomersParser()
        customers_data = cusotmer_parser.parse_xml(response.content)
        customer = Customer(product=self, **customers_data[0])

        return customer

    def build_customer_post_data(self, code=None, first_name=None,\
                last_name=None, email=None, plan_code=None, \
                company=None, is_vat_exempt=None, vat_number=None, \
                notes=None, first_contact_datetime=None, \
                referer=None, campaign_term=None, \
                campaign_name=None, campaign_source=None, \
                campaign_medium=None, campaign_content=None, \
                meta_data=None, initial_bill_date=None, method=None,\
                cc_number=None, cc_expiration=None, \
                cc_card_code=None, cc_first_name=None, \
                cc_last_name=None, cc_email=None, cc_company=None, \
                cc_country=None, cc_address=None, cc_city=None, \
                cc_state=None, cc_zip=None, return_url=None, cancel_url=None, \
                bill_date=None):

        data = {}

        if code:
            data['code'] = code

        if first_name:
            data['firstName'] = first_name

        if last_name:
            data['lastName'] = last_name

        if email:
            data['email'] = email

        if plan_code:
            data['subscription[planCode]'] = plan_code

        if company:
            data['company'] = company

        if is_vat_exempt is not None:
            if is_vat_exempt:
                data['isVatExempt'] = 1
            else:
                data['isVatExempt'] = 0

        if vat_number:
            data['vatNumber'] = vat_number

        if notes:
            data['notes'] = notes

        if first_contact_datetime:
            data['firstContactDatetime'] = self.client.format_datetime(
                first_contact_datetime)

        if referer:
            data['referer'] = referer

        if campaign_term:
            data['campaignTerm'] = campaign_term

        if campaign_name:
            data['campaignName'] = campaign_name

        if campaign_source:
            data['campaignSource'] = campaign_source

        if campaign_content:
            data['campaignContent'] = campaign_content

        if meta_data:
            for key, value in meta_data.iteritems():
                full_key = 'metaData[%s]' % key
                data[full_key] = value

        if initial_bill_date:
            data['subscription[initialBillDate]'] = self.client.format_date(
                initial_bill_date)

        if method:
            data['subscription[method]'] = method

        if cc_number:
            data['subscription[ccNumber]'] = cc_number

        if cc_expiration:
            data['subscription[ccExpiration]'] = cc_expiration

        if cc_card_code:
            data['subscription[ccCardCode]'] = cc_card_code

        if cc_first_name:
            data['subscription[ccFirstName]'] = cc_first_name

        if cc_last_name:
            data['subscription[ccLastName]'] = cc_last_name

        if cc_email:
            data['subscription[ccEmail]'] = cc_email

        if cc_company:
            data['subscription[ccCompany]'] = cc_company

        if cc_country:
            data['subscription[ccCountry]'] = cc_country

        if cc_address:
            data['subscription[ccAddress]'] = cc_address

        if cc_city:
            data['subscription[ccCity]'] = cc_city

        if cc_state:
            data['subscription[ccState]'] = cc_state

        if cc_zip:
            data['subscription[ccZip]'] = cc_zip

        if return_url:
            data['subscription[returnUrl]'] = return_url

        if cancel_url:
            data['subscription[cancelUrl]'] = cancel_url

        if bill_date:
            data['subscription[changeBillDate]'] = self.client.format_datetime(
                bill_date)

        return data

    def get_customers(self):
        customers = []

        try:
            response = self.client.make_request(path='customers/get')
        except NotFound:
            response = None

        if response:
            cusotmer_parser = CustomersParser()
            customers_data = cusotmer_parser.parse_xml(response.content)
            for customer_data in customers_data:
                customers.append(Customer(product=self, **customer_data))

        return customers

    def get_customer(self, code):

        response = self.client.make_request(
            path='customers/get',
            params={'code': code},
        )
        cusotmer_parser = CustomersParser()
        customers_data = cusotmer_parser.parse_xml(response.content)

        return Customer(product=self, **customers_data[0])

    def delete_all_customers(self):
        '''
        This method does exactly what you think it does.  Calling this method
        deletes all customer data in your cheddar product and the configured
        gateway.  This action cannot be undone.
        
        DO NOT RUN THIS UNLESS YOU REALLY, REALLY, REALLY MEAN TO!
        '''
        response = self.client.make_request(
            path='customers/delete-all/confirm/%d' % int(time()),
            method='POST')
Beispiel #6
0
class CheddarProduct(object):
    
    def __init__(self, username, password, product_code, cache=None, timeout=None, endpoint=None):
        self.product_code = product_code
        self.client = Client(
            username,
            password,
            product_code,
            cache,
            timeout,
            endpoint,
        )
        
        super(CheddarProduct, self).__init__()
        
    def __repr__(self):
        return u'CheddarProduct: %s' % self.product_code
        
    def get_all_plans(self):
        response = self.client.make_request(path='plans/get')
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]
        
        return plans
        
    def get_plan(self, code):
        response = self.client.make_request(
            path='plans/get',
            params={'code': code},
        )
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]
        
        return plans[0]
        
    def create_customer(self, code, first_name, last_name, email, plan_code, \
                        coupon_code=None, company=None, is_vat_exempt=None, vat_number=None, \
                        notes=None, first_contact_datetime=None, \
                        referer=None, campaign_term=None, \
                        campaign_name=None, campaign_source=None, \
                        campaign_medium=None, campaign_content=None, \
                        meta_data=None, initial_bill_date=None, method=None,\
                        cc_number=None, cc_expiration=None, \
                        cc_card_code=None, cc_first_name=None, \
                        cc_last_name=None, cc_email=None, cc_company=None, \
                        cc_country=None, cc_address=None, cc_city=None, \
                        cc_state=None, cc_zip=None, return_url=None, \
                        cancel_url=None, charges=None, items=None):

        data = self.build_customer_post_data(code, first_name, last_name, \
                    email, plan_code, coupon_code, company, is_vat_exempt, vat_number, \
                    notes, first_contact_datetime, referer, campaign_term, \
                    campaign_name, campaign_source, campaign_medium, \
                    campaign_content, meta_data, initial_bill_date, method, \
                    cc_number, cc_expiration, cc_card_code, cc_first_name, \
                    cc_last_name, cc_email, cc_company, cc_country, cc_address, \
                    cc_city, cc_state, cc_zip, return_url, cancel_url)
        
        if charges:
            for i, charge in enumerate(charges):
                data['charges[%d][chargeCode]' % i] = charge['code']
                data['charges[%d][quantity]' % i] = charge.get('quantity', 1)
                data['charges[%d][eachAmount]' % i] = '%.2f' % charge['each_amount']
                data['charges[%d][description]' % i] = charge.get('description', '')

        if items:
            for i, item in enumerate(items):
                data['items[%d][itemCode]' % i] = item['code']
                data['items[%d][quantity]' % i] = item.get('quantity', 1)
        
        response = self.client.make_request(path='customers/new', data=data)
        customer_parser = CustomersParser()
        customers_data = customer_parser.parse_xml(response.content)
        customer = Customer(product=self, **customers_data[0])
        
        return customer
        
    def build_customer_post_data(self, code=None, first_name=None,\
                last_name=None, email=None, plan_code=None, \
                coupon_code=None, company=None, is_vat_exempt=None, vat_number=None, \
                notes=None, first_contact_datetime=None, \
                referer=None, campaign_term=None, \
                campaign_name=None, campaign_source=None, \
                campaign_medium=None, campaign_content=None, \
                meta_data=None, initial_bill_date=None, method=None,\
                cc_number=None, cc_expiration=None, \
                cc_card_code=None, cc_first_name=None, \
                cc_last_name=None, cc_email=None, cc_company=None, \
                cc_country=None, cc_address=None, cc_city=None, \
                cc_state=None, cc_zip=None, return_url=None, cancel_url=None, \
                bill_date=None):

        data = {}
        
        if code:
            data['code'] = code
        
        if first_name:
            data['firstName'] = first_name
        
        if last_name:
            data['lastName'] = last_name
            
        if email:
            data['email'] = email
        
        if plan_code:
            data['subscription[planCode]'] = plan_code
            
        if coupon_code:
            data['subscription[couponCode]'] = coupon_code
        
        if company:
            data['company'] = company

        if is_vat_exempt is not None:
            if is_vat_exempt:
                data['isVatExempt'] = 1
            else:
                data['isVatExempt'] = 0

        if vat_number:
            data['vatNumber'] = vat_number

        if notes:
            data['notes'] = notes

        if first_contact_datetime:
            data['firstContactDatetime'] = self.client.format_datetime(first_contact_datetime)

        if referer:
            data['referer'] = referer

        if campaign_term:
            data['campaignTerm'] = campaign_term

        if campaign_name:
            data['campaignName'] = campaign_name

        if campaign_source:
            data['campaignSource'] = campaign_source

        if campaign_content:
            data['campaignContent'] = campaign_content

        if meta_data:
            for key, value in meta_data.iteritems():
                full_key = 'metaData[%s]' % key
                data[full_key] = value

        if initial_bill_date:
            data['subscription[initialBillDate]'] = self.client.format_date(initial_bill_date)
        
        if method:
            data['subscription[method]'] = method

        if cc_number:
            data['subscription[ccNumber]'] = cc_number

        if cc_expiration:
            data['subscription[ccExpiration]'] = cc_expiration

        if cc_card_code:
            data['subscription[ccCardCode]'] = cc_card_code

        if cc_first_name:
            data['subscription[ccFirstName]'] = cc_first_name

        if cc_last_name:
            data['subscription[ccLastName]'] = cc_last_name

        if cc_email:
            data['subscription[ccEmail]'] = cc_email

        if cc_company:
            data['subscription[ccCompany]'] = cc_company

        if cc_country:
            data['subscription[ccCountry]'] = cc_country

        if cc_address:
            data['subscription[ccAddress]'] = cc_address

        if cc_city:
            data['subscription[ccCity]'] = cc_city

        if cc_state:
            data['subscription[ccState]'] = cc_state

        if cc_zip:
            data['subscription[ccZip]'] = cc_zip

        if return_url:
            data['subscription[returnUrl]'] = return_url
        
        if cancel_url:
            data['subscription[cancelUrl]'] = cancel_url
        
        if bill_date:
            data['subscription[changeBillDate]'] = self.client.format_datetime(bill_date)

        return data
        
    def get_customers(self, filter_data=None):
        '''
        Returns all customers. Sometimes they are too much and cause internal 
        server errors on CG. API call permits post parameters for filtering 
        which tends to fix this
        https://cheddargetter.com/developers#all-customers

        filter_data
            Will be processed by urlencode and can be used for filtering
            Example value: [
                ("subscriptionStatus": "activeOnly"),
                ("planCode[]": "100GB"), ("planCode[]": "200GB")
            ]
        '''
        customers = []
        
        try:
            response = self.client.make_request(path='customers/get', data=filter_data)
        except NotFound:
            response = None
        
        if response:
            customer_parser = CustomersParser()
            customers_data = customer_parser.parse_xml(response.content)
            for customer_data in customers_data:
                customers.append(Customer(product=self, **customer_data))
            
        return customers
        
    def get_customer(self, code=None, invoice_number=None):
       
        if code:
            parameters = {'code': code}
        elif invoice_number:
            parameters = {'invoiceNumber': invoice_number}
        else:
            raise ValueError('code or invoice_number must be provided')

        response = self.client.make_request(
            path='customers/get',
            params=parameters,
        )
        customer_parser = CustomersParser()
        customers_data = customer_parser.parse_xml(response.content)
        
        return Customer(product=self, **customers_data[0])

    def waive_invoice(self, number=None, id=None):
        '''
        Adds a charge to an invoice for balancing it to 0. Useful for 
        getting rid of an old invoice that is now irrelevant but gets 
        in the way of reactivating a customer account. 

        Must call run_outstanding_invoice on customer to get rid of that
        invoice completely.
        '''
        data = {}
  
        if id:
            data['id'] = id
        elif number:
            data['number'] = number
        else:
            raise Exception('Must provide id or number of invoice as argument')

        response = self.client.make_request(
            path='invoices/waive',
            data=data,
        )
    
    def delete_all_customers(self):
        '''
        This method does exactly what you think it does.  Calling this method
        deletes all customer data in your cheddar product and the configured
        gateway.  This action cannot be undone.
        
        DO NOT RUN THIS UNLESS YOU REALLY, REALLY, REALLY MEAN TO!
        '''
        response = self.client.make_request(
            path='customers/delete-all/confirm/%d' % int(time()),
            method='POST'
        )
Beispiel #7
0
class CheddarProduct(object):
    def __init__(self, username, password, product_code, cache=None, timeout=None, endpoint=None):
        self.product_code = product_code
        self.client = Client(username, password, product_code, cache, timeout, endpoint)

        super(CheddarProduct, self).__init__()

    def __repr__(self):
        return u"CheddarProduct: %s" % self.product_code

    def get_all_plans(self):
        response = self.client.make_request(path="plans/get")
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]

        return plans

    def get_plan(self, code):
        response = self.client.make_request(path="plans/get", params={"code": code})
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]

        return plans[0]

    def create_customer(
        self,
        code,
        first_name,
        last_name,
        email,
        plan_code,
        company=None,
        is_vat_excempt=None,
        vat_number=None,
        notes=None,
        first_contact_datetime=None,
        referer=None,
        campaign_term=None,
        campaign_name=None,
        campaign_source=None,
        campaign_medium=None,
        campaign_content=None,
        meta_data=None,
        initial_bill_date=None,
        cc_number=None,
        cc_expiration=None,
        cc_card_code=None,
        cc_first_name=None,
        cc_last_name=None,
        cc_company=None,
        cc_country=None,
        cc_address=None,
        cc_city=None,
        cc_state=None,
        cc_zip=None,
        charges=None,
        items=None,
    ):

        data = self.build_customer_post_data(
            code,
            first_name,
            last_name,
            email,
            plan_code,
            company,
            is_vat_excempt,
            vat_number,
            notes,
            first_contact_datetime,
            referer,
            campaign_term,
            campaign_name,
            campaign_source,
            campaign_medium,
            campaign_content,
            meta_data,
            initial_bill_date,
            cc_number,
            cc_expiration,
            cc_card_code,
            cc_first_name,
            cc_last_name,
            cc_company,
            cc_country,
            cc_address,
            cc_city,
            cc_state,
            cc_zip,
        )

        if charges:
            for i, charge in enumerate(charges):
                data["charges[%d][chargeCode]" % i] = charge["code"]
                data["charges[%d][quantity]" % i] = charge.get("quantity", 1)
                data["charges[%d][eachAmount]" % i] = charge["each_amount"]
                data["charges[%d][description]" % i] = charge.get("description", "")

        if items:
            for i, item in enumerate(items):
                data["items[%d][itemCode]" % i] = item["code"]
                data["items[%d][quantity]" % i] = item.get("quantity", 1)

        response = self.client.make_request(path="customers/new", data=data)
        cusotmer_parser = CustomersParser()
        customers_data = cusotmer_parser.parse_xml(response.content)
        customer = Customer(product=self, **customers_data[0])

        return customer

    def build_customer_post_data(
        self,
        code=None,
        first_name=None,
        last_name=None,
        email=None,
        plan_code=None,
        company=None,
        is_vat_excempt=None,
        vat_number=None,
        notes=None,
        first_contact_datetime=None,
        referer=None,
        campaign_term=None,
        campaign_name=None,
        campaign_source=None,
        campaign_medium=None,
        campaign_content=None,
        meta_data=None,
        initial_bill_date=None,
        cc_number=None,
        cc_expiration=None,
        cc_card_code=None,
        cc_first_name=None,
        cc_last_name=None,
        cc_company=None,
        cc_country=None,
        cc_address=None,
        cc_city=None,
        cc_state=None,
        cc_zip=None,
        bill_date=None,
    ):

        data = {}

        if code:
            data["code"] = code

        if first_name:
            data["firstName"] = first_name

        if last_name:
            data["lastName"] = last_name

        if email:
            data["email"] = email

        if plan_code:
            data["subscription[planCode]"] = plan_code

        if company:
            data["company"] = company

        if is_vat_excempt is not None:
            if is_vat_excempt:
                data["isVatExcempt"] = 1
            else:
                data["isVatExcempt"] = 0

        if vat_number:
            data["vatNumber"] = vat_number

        if notes:
            data["notes"] = notes

        if first_contact_datetime:
            data["firstContactDatetime"] = self.client.format_datetime(first_contact_datetime)

        if referer:
            data["referer"] = referer

        if campaign_term:
            data["campaignTerm"] = campaign_term

        if campaign_name:
            data["campaignName"] = campaign_name

        if campaign_source:
            data["campaignSource"] = campaign_source

        if campaign_content:
            data["campaignContent"] = campaign_content

        if meta_data:
            for key, value in meta_data.iteritems():
                full_key = "metaData[%s]" % key
                data[full_key] = value

        if initial_bill_date:
            data["subscription[initialBillDate]"] = self.client.format_date(initial_bill_date)

        if cc_number:
            data["subscription[ccNumber]"] = cc_number

        if cc_expiration:
            data["subscription[ccExpiration]"] = cc_expiration

        if cc_card_code:
            data["subscription[ccCardCode]"] = cc_card_code

        if cc_first_name:
            data["subscription[ccFirstName]"] = cc_first_name

        if cc_last_name:
            data["subscription[ccLastName]"] = cc_last_name

        if cc_company:
            data["subscription[ccCompany]"] = cc_company

        if cc_country:
            data["subscription[ccCountry]"] = cc_country

        if cc_address:
            data["subscription[ccAddress]"] = cc_address

        if cc_city:
            data["subscription[ccCity]"] = cc_city

        if cc_state:
            data["subscription[ccState]"] = cc_state

        if cc_zip:
            data["subscription[ccZip]"] = cc_zip

        if bill_date:
            data["subscription[changeBillDate]"] = self.client.format_datetime(bill_date)

        return data

    def get_customers(self):
        customers = []

        try:
            response = self.client.make_request(path="customers/get")
        except NotFound:
            response = None

        if response:
            cusotmer_parser = CustomersParser()
            customers_data = cusotmer_parser.parse_xml(response.content)
            for customer_data in customers_data:
                customers.append(Customer(product=self, **customer_data))

        return customers

    def get_customer(self, code):

        response = self.client.make_request(path="customers/get", params={"code": code})
        cusotmer_parser = CustomersParser()
        customers_data = cusotmer_parser.parse_xml(response.content)

        return Customer(product=self, **customers_data[0])

    def delete_all_customers(self):
        """
        This method does exactly what you think it does.  Calling this method
        deletes all customer data in your cheddar product and the configured
        gateway.  This action cannot be undone.
        
        DO NOT RUN THIS UNLESS YOU REALLY, REALLY, REALLY MEAN TO!
        """
        response = self.client.make_request(path="customers/delete-all/confirm/1", method="POST")
Beispiel #8
0
class CheddarProduct(object):
    
    def __init__(self, username, password, product_code, cache=None, timeout=None, endpoint=None):
        self.product_code = product_code
        self.client = Client(
            username,
            password,
            product_code,
            cache,
            timeout,
            endpoint,
        )
        
        super(CheddarProduct, self).__init__()
        
    def __repr__(self):
        return u'CheddarProduct: %s' % self.product_code
        
    def get_all_plans(self):
        response = self.client.make_request(path='plans/get')
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]
        
        return plans
        
    def get_plan(self, code):
        response = self.client.make_request(
            path='plans/get',
            params={'code': code},
        )
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]
        
        return plans[0]
        
    def create_customer(self, code, first_name, last_name, email, plan_code, \
                        company=None, is_vat_exempt=None, vat_number=None, \
                        notes=None, first_contact_datetime=None, \
                        referer=None, campaign_term=None, \
                        campaign_name=None, campaign_source=None, \
                        campaign_medium=None, campaign_content=None, \
                        meta_data=None, initial_bill_date=None, method=None,\
                        cc_number=None, cc_expiration=None, \
                        cc_card_code=None, cc_first_name=None, \
                        cc_last_name=None, cc_email=None, cc_company=None, \
                        cc_country=None, cc_address=None, cc_city=None, \
                        cc_state=None, cc_zip=None, return_url=None, \
                        cancel_url=None, charges=None, items=None):

        data = self.build_customer_post_data(code, first_name, last_name, \
                    email, plan_code, company, is_vat_exempt, vat_number, \
                    notes, first_contact_datetime, referer, campaign_term, \
                    campaign_name, campaign_source, campaign_medium, \
                    campaign_content, meta_data, initial_bill_date, method, \
                    cc_number, cc_expiration, cc_card_code, cc_first_name, \
                    cc_last_name, cc_email, cc_company, cc_country, cc_address, \
                    cc_city, cc_state, cc_zip, return_url, cancel_url)
        
        if charges:
            for i, charge in enumerate(charges):
                data['charges[%d][chargeCode]' % i] = charge['code']
                data['charges[%d][quantity]' % i] = charge.get('quantity', 1)
                data['charges[%d][eachAmount]' % i] = '%.2f' % charge['each_amount']
                data['charges[%d][description]' % i] = charge.get('description', '')

        if items:
            for i, item in enumerate(items):
                data['items[%d][itemCode]' % i] = item['code']
                data['items[%d][quantity]' % i] = item.get('quantity', 1)
        
        response = self.client.make_request(path='customers/new', data=data)
        cusotmer_parser = CustomersParser()
        customers_data = cusotmer_parser.parse_xml(response.content)
        customer = Customer(product=self, **customers_data[0])
        
        return customer
        
    def build_customer_post_data(self, code=None, first_name=None,\
                last_name=None, email=None, plan_code=None, \
                company=None, is_vat_exempt=None, vat_number=None, \
                notes=None, first_contact_datetime=None, \
                referer=None, campaign_term=None, \
                campaign_name=None, campaign_source=None, \
                campaign_medium=None, campaign_content=None, \
                meta_data=None, initial_bill_date=None, method=None,\
                cc_number=None, cc_expiration=None, \
                cc_card_code=None, cc_first_name=None, \
                cc_last_name=None, cc_email=None, cc_company=None, \
                cc_country=None, cc_address=None, cc_city=None, \
                cc_state=None, cc_zip=None, return_url=None, cancel_url=None, \
                bill_date=None):

        data = {}
        
        if code:
            data['code'] = code
        
        if first_name:
            data['firstName'] = first_name
        
        if last_name:
            data['lastName'] = last_name
            
        if email:
            data['email'] = email
        
        if plan_code:
            data['subscription[planCode]'] = plan_code
        
        if company:
            data['company'] = company

        if is_vat_exempt is not None:
            if is_vat_exempt:
                data['isVatExempt'] = 1
            else:
                data['isVatExempt'] = 0

        if vat_number:
            data['vatNumber'] = vat_number

        if notes:
            data['notes'] = notes

        if first_contact_datetime:
            data['firstContactDatetime'] = self.client.format_datetime(first_contact_datetime)

        if referer:
            data['referer'] = referer

        if campaign_term:
            data['campaignTerm'] = campaign_term

        if campaign_name:
            data['campaignName'] = campaign_name

        if campaign_source:
            data['campaignSource'] = campaign_source

        if campaign_content:
            data['campaignContent'] = campaign_content

        if meta_data:
            for key, value in meta_data.iteritems():
                full_key = 'metaData[%s]' % key
                data[full_key] = value

        if initial_bill_date:
            data['subscription[initialBillDate]'] = self.client.format_date(initial_bill_date)
        
        if method:
            data['subscription[method]'] = method

        if cc_number:
            data['subscription[ccNumber]'] = cc_number

        if cc_expiration:
            data['subscription[ccExpiration]'] = cc_expiration

        if cc_card_code:
            data['subscription[ccCardCode]'] = cc_card_code

        if cc_first_name:
            data['subscription[ccFirstName]'] = cc_first_name

        if cc_last_name:
            data['subscription[ccLastName]'] = cc_last_name

        if cc_email:
            data['subscription[ccEmail]'] = cc_email

        if cc_company:
            data['subscription[ccCompany]'] = cc_company

        if cc_country:
            data['subscription[ccCountry]'] = cc_country

        if cc_address:
            data['subscription[ccAddress]'] = cc_address

        if cc_city:
            data['subscription[ccCity]'] = cc_city

        if cc_state:
            data['subscription[ccState]'] = cc_state

        if cc_zip:
            data['subscription[ccZip]'] = cc_zip

        if return_url:
            data['subscription[returnUrl]'] = return_url
        
        if cancel_url:
            data['subscription[cancelUrl]'] = cancel_url
        
        if bill_date:
            data['subscription[changeBillDate]'] = self.client.format_datetime(bill_date)

        return data
        
    def get_customers(self):
        customers = []
        
        try:
            response = self.client.make_request(path='customers/get')
        except NotFound:
            response = None
        
        if response:
            cusotmer_parser = CustomersParser()
            customers_data = cusotmer_parser.parse_xml(response.content)
            for customer_data in customers_data:
                customers.append(Customer(product=self, **customer_data))
            
        return customers
        
    def get_customer(self, code):
        
        response = self.client.make_request(
            path='customers/get',
            params={'code': code},
        )
        cusotmer_parser = CustomersParser()
        customers_data = cusotmer_parser.parse_xml(response.content)
        
        return Customer(product=self, **customers_data[0])
    
    def delete_all_customers(self):
        '''
        This method does exactly what you think it does.  Calling this method
        deletes all customer data in your cheddar product and the configured
        gateway.  This action cannot be undone.
        
        DO NOT RUN THIS UNLESS YOU REALLY, REALLY, REALLY MEAN TO!
        '''
        response = self.client.make_request(
            path='customers/delete-all/confirm/%d' % int(time()),
            method='POST'
        )
Beispiel #9
0
class CheddarProduct(object):
    def __init__(self, username, password, product_code, cache=None, timeout=None, endpoint=None):
        self.product_code = product_code
        self.client = Client(username, password, product_code, cache, timeout, endpoint)

        super(CheddarProduct, self).__init__()

    def __repr__(self):
        return u"CheddarProduct: %s" % self.product_code

    def get_all_plans(self):
        response = self.client.make_request(path="plans/get")
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]

        return plans

    def get_plan(self, code):
        response = self.client.make_request(path="plans/get", params={"code": code})
        plans_parser = PlansParser()
        plans_data = plans_parser.parse_xml(response.content)
        plans = [PricingPlan(**plan_data) for plan_data in plans_data]

        return plans[0]

    def create_customer(
        self,
        code,
        first_name,
        last_name,
        email,
        plan_code,
        company=None,
        is_vat_exempt=None,
        vat_number=None,
        notes=None,
        first_contact_datetime=None,
        referer=None,
        campaign_term=None,
        campaign_name=None,
        campaign_source=None,
        campaign_medium=None,
        campaign_content=None,
        meta_data=None,
        initial_bill_date=None,
        method=None,
        cc_number=None,
        cc_expiration=None,
        cc_card_code=None,
        cc_first_name=None,
        cc_last_name=None,
        cc_email=None,
        cc_company=None,
        cc_country=None,
        cc_address=None,
        cc_city=None,
        cc_state=None,
        cc_zip=None,
        coupon_code=None,
        return_url=None,
        cancel_url=None,
        charges=None,
        items=None,
    ):

        data = self.build_customer_post_data(
            code,
            first_name,
            last_name,
            email,
            plan_code,
            company,
            is_vat_exempt,
            vat_number,
            notes,
            first_contact_datetime,
            referer,
            campaign_term,
            campaign_name,
            campaign_source,
            campaign_medium,
            campaign_content,
            meta_data,
            initial_bill_date,
            method,
            cc_number,
            cc_expiration,
            cc_card_code,
            cc_first_name,
            cc_last_name,
            cc_email,
            cc_company,
            cc_country,
            cc_address,
            cc_city,
            cc_state,
            cc_zip,
            coupon_code,
            return_url,
            cancel_url,
        )

        if charges:
            for i, charge in enumerate(charges):
                data["charges[%d][chargeCode]" % i] = charge["code"]
                data["charges[%d][quantity]" % i] = charge.get("quantity", 1)
                data["charges[%d][eachAmount]" % i] = "%.2f" % (charge["each_amount"])
                data["charges[%d][description]" % i] = charge.get("description", "")

        if items:
            for i, item in enumerate(items):
                data["items[%d][itemCode]" % i] = item["code"]
                data["items[%d][quantity]" % i] = item.get("quantity", 1)

        response = self.client.make_request(path="customers/new", data=data)
        customer_parser = CustomersParser()
        customers_data = customer_parser.parse_xml(response.content)
        customer = Customer(product=self, **customers_data[0])

        return customer

    def build_customer_post_data(
        self,
        code=None,
        first_name=None,
        last_name=None,
        email=None,
        plan_code=None,
        company=None,
        is_vat_exempt=None,
        vat_number=None,
        notes=None,
        first_contact_datetime=None,
        referer=None,
        campaign_term=None,
        campaign_name=None,
        campaign_source=None,
        campaign_medium=None,
        campaign_content=None,
        meta_data=None,
        initial_bill_date=None,
        method=None,
        cc_number=None,
        cc_expiration=None,
        cc_card_code=None,
        cc_first_name=None,
        cc_last_name=None,
        cc_email=None,
        cc_company=None,
        cc_country=None,
        cc_address=None,
        cc_city=None,
        cc_state=None,
        cc_zip=None,
        coupon_code=None,
        return_url=None,
        cancel_url=None,
        bill_date=None,
    ):

        data = {}

        if code:
            data["code"] = code

        if first_name:
            data["firstName"] = first_name

        if last_name:
            data["lastName"] = last_name

        if email:
            data["email"] = email

        if plan_code:
            data["subscription[planCode]"] = plan_code

        if company:
            data["company"] = company

        if is_vat_exempt is not None:
            if is_vat_exempt:
                data["isVatExempt"] = 1
            else:
                data["isVatExempt"] = 0

        if vat_number:
            data["vatNumber"] = vat_number

        if notes:
            data["notes"] = notes

        if first_contact_datetime:
            data["firstContactDatetime"] = self.client.format_datetime(first_contact_datetime)

        if referer:
            data["referer"] = referer

        if campaign_term:
            data["campaignTerm"] = campaign_term

        if campaign_name:
            data["campaignName"] = campaign_name

        if campaign_source:
            data["campaignSource"] = campaign_source

        if campaign_content:
            data["campaignContent"] = campaign_content

        if meta_data:
            for key, value in meta_data.iteritems():
                full_key = "metaData[%s]" % key
                data[full_key] = value

        if initial_bill_date:
            data["subscription[initialBillDate]"] = self.client.format_date(initial_bill_date)

        if method:
            data["subscription[method]"] = method

        if cc_number:
            data["subscription[ccNumber]"] = cc_number

        if cc_expiration:
            data["subscription[ccExpiration]"] = cc_expiration

        if cc_card_code:
            data["subscription[ccCardCode]"] = cc_card_code

        if cc_first_name:
            data["subscription[ccFirstName]"] = cc_first_name

        if cc_last_name:
            data["subscription[ccLastName]"] = cc_last_name

        if cc_email:
            data["subscription[ccEmail]"] = cc_email

        if cc_company:
            data["subscription[ccCompany]"] = cc_company

        if cc_country:
            data["subscription[ccCountry]"] = cc_country

        if cc_address:
            data["subscription[ccAddress]"] = cc_address

        if cc_city:
            data["subscription[ccCity]"] = cc_city

        if cc_state:
            data["subscription[ccState]"] = cc_state

        if cc_zip:
            data["subscription[ccZip]"] = cc_zip

        if coupon_code:
            data["subscription[couponCode]"] = coupon_code

        if return_url:
            data["subscription[returnUrl]"] = return_url

        if cancel_url:
            data["subscription[cancelUrl]"] = cancel_url

        if bill_date:
            data["subscription[changeBillDate]"] = self.client.format_datetime(bill_date)

        return data

    def get_customers(self, filter_data=None):
        """
        Returns all customers. Sometimes they are too much and cause internal
        server errors on CG. API call permits post parameters for filtering
        which tends to fix this
        https://cheddargetter.com/developers#all-customers

        filter_data
            Will be processed by urlencode and can be used for filtering
            Example value: [
                ("subscriptionStatus": "activeOnly"),
                ("planCode[]": "100GB"), ("planCode[]": "200GB")
            ]
        """
        customers = []

        try:
            response = self.client.make_request(path="customers/get", data=filter_data)
        except NotFound:
            response = None

        if response:
            customer_parser = CustomersParser()
            customers_data = customer_parser.parse_xml(response.content)
            for customer_data in customers_data:
                customers.append(Customer(product=self, **customer_data))

        return customers

    def get_customer(self, code):

        response = self.client.make_request(path="customers/get", params={"code": code})
        customer_parser = CustomersParser()
        customers_data = customer_parser.parse_xml(response.content)

        return Customer(product=self, **customers_data[0])

    def delete_all_customers(self):
        """
        This method does exactly what you think it does.  Calling this method
        deletes all customer data in your cheddar product and the configured
        gateway.  This action cannot be undone.

        DO NOT RUN THIS UNLESS YOU REALLY, REALLY, REALLY MEAN TO!
        """
        self.client.make_request(path="customers/delete-all/confirm/%d" % int(time()), method="POST")

    def get_all_promotions(self):
        """
        Returns all promotions.
        https://cheddargetter.com/developers#promotions
        """
        promotions = []

        try:
            response = self.client.make_request(path="promotions/get")
        except NotFound:
            response = None

        if response:
            promotions_parser = PromotionsParser()
            promotions_data = promotions_parser.parse_xml(response.content)
            promotions = [Promotion(**promotion_data) for promotion_data in promotions_data]

        return promotions

    def get_promotion(self, code):
        """
        Get the promotion with the specified coupon code.
        https://cheddargetter.com/developers#single-promotion
        """

        response = self.client.make_request(path="promotions/get", params={"code": code})
        promotion_parser = PromotionsParser()
        promotion_data = promotion_parser.parse_xml(response.content)

        return Promotion(**promotion_data[0])