Ejemplo n.º 1
0
 def enable(self, subscription_code, email_token):
     
     """
     Enables a particular subscription
     
     ARGS:
     subscription_code = subscription_code of the user
     email_token = Email_token
     
     """
     
     end_point = '/enable'
     method = 'POST'
     
     payload = {
         'code': subscription_code,
         'token': email_token
     }
     
     url = self.api_host + self.resource_path + end_point
     response, status, headers = self.client.request(method, url,
                                                         self.request_headers,
                                                         post_data=payload)
     self._response_headers = headers
     self._status_code = status
     self._result = response
     if not response.get('status', False):
         raise error.APIError(response.get('message')) 
     return response                    
Ejemplo n.º 2
0
    def create(self, name, amount, interval='monthly'):
        """
        Creates a new subscription plan with the following arguments.
        ARGS:
        
        name = Name of the plan (required)
        amount = cost for the plan in kobo. For example NGN 2,100 will be 210000 without any symbol
        interval = plan interval with the default set to monthly
        """
        
        end_point = ''
        method = 'POST'
        
        payload = {
            "name": name,
            "interval": interval,
            "amount": amount
        }        

        url = self.api_host + self.resource_path + end_point
        response, status, headers = self.client.request(method, url,
                                                        self.request_headers,
                                                        post_data=payload)
        self._response_headers = headers
        self._status_code = status
        self._result = response
        if not response.get('status', False):
            raise error.APIError(response.get('message')) 
        return response        
Ejemplo n.º 3
0
    def create(self, email, first_name=None, last_name=None, phone=None):
        """
        Creates a new paystack customer account with the following args
        ARGS:
        email = Customer's email address which is required
        first_name, last_name, and phone are all optional with a default value of None
        """
        end_point = ''
        method = 'POST'
        
        payload = {
            "email": email,
            "first_name": first_name,
            "last_name": last_name,
            "phone": phone
        }        

        url = self.api_host + self.resource_path + end_point
        response, status, headers = self.client.request(method, url,
                                                        self.request_headers,
                                                        post_data=payload)
        self._response_headers = headers
        self._status_code = status
        self._result = response
        if not response.get('status', False):
            raise error.APIError(response.get('message')) 
        
        return response
Ejemplo n.º 4
0
 def all(self):
     end_point = ''
     method = 'GET'
     
     url = self.api_host + self.resource_path + end_point
     response, status, headers = self.client.request(method, url, 
                                                     self.request_headers)
     
     self._response_headers = headers
     self._status_code = status
     self._result = response
     if not response.get('status', False):
         raise error.APIError(response.get('message')) 
     return response             
Ejemplo n.º 5
0
    def initialize(self, amount, email,
                   plan=None, reference=None, callback_url=None):  # pragma no cover
        """
        Transaction resource initialisation method.

        Args:
            amount (int): Amount to pay in Kobo.
            email (string): Client's email address.
            plan (string, optional): You customer billing plan.
            ref (string, optional): Unique transaction reference.

        Raises:
            error.APIError: Something generally bad happened... :()
            error.ValidationError: Bad input.

        Returns:
            response (dict): Response data from Paystack
        """
        endpoint = '/initialize'
        method = 'POST'

        self.reference = (lambda ref: ref if ref else self.reference)(reference)
        self.email = email
        self.amount = amount
        payload = {
            "amount": amount,
            "email": email,
            "plan": plan,
            "reference": reference,
            "callback_url": callback_url
        }
        if self.reference:
            payload.update(reference=self.reference)

        url = self.api_host + self.resource_path + endpoint
        response, status, headers = self.client.request(method, url,
                                                        self.request_headers,
                                                        post_data=payload)
        self._response_headers = headers
        self._status_code = status
        self._result = response
        if not response.get('status', False):
            raise error.APIError(response.get('message'))

        self.authorization_url = self._result['data'].get('authorization_url')
        self.access_code = self._result['data'].get('access_code')

        return response
Ejemplo n.º 6
0
 def get_one(self, plan_id_or_plan_code):
     """
     Get a particular plan
     """
     
     end_point = '/%s' % (plan_id_or_plan_code)
     method = 'GET'
 
     url = self.api_host + self.resource_path + end_point
     response, status, headers = self.client.request(method, url,
                                                         self.request_headers
                                                             )
     self._response_headers = headers
     self._status_code = status
     self._result = response
     if not response.get('status', False):
         raise error.APIError(response.get('message')) 
     return response          
Ejemplo n.º 7
0
 def get(self, subscription_id):
     """
     Gets a particular subscription using the subscription_id
     
     """
     
     end_point = '/%s' % (subscription_id)
     method = 'GET'
     
     url = self.api_host + self.resource_path + end_point
     response, status, headers = self.client.request(method, url,
                                                         self.request_headers
                                                         )
     self._response_headers = headers
     self._status_code = status
     self._result = response
     if not response.get('status', False):
         raise error.APIError(response.get('message')) 
     return response                
Ejemplo n.º 8
0
 def update(self, plan_id, data):
     """
     Update a particular plan with the plan_id
     
     """
     
     end_point = '/{}'.format(plan_id)
     method = 'PUT'
     
     url = self.api_host + self.resource_path + end_point
     response, status, headers = self.client.request(method, url, self.request_headers, post_data=data)
     
     self._response_headers = headers
     self._status_code = status
     self._result = response
     
     if not response.get('status', False):
         raise error.APIError(response.get('message'))
     return response
Ejemplo n.º 9
0
    def _class_name(cls, resource_path):
        """
        Summary.

        Args:
            resource_path (TYPE): Description

        Raises:
            error.APIError: Description

        Returns:
            name (TYPE): Description
        """
        if cls == BaseAPIResource:
            raise error.APIError(
                'You cannot instantiate the API Base class directory.'
                'This is an abstract class.  You should perform '
                'actions on its subclasses (e.g. Charge, Customer).')
        return util.utf8(resource_path)
Ejemplo n.º 10
0
    def verify(self, ref=None):  # pragma no cover
        """
        Verify transaction instance.

        Args:
            ref (string, optional): Unique transaction reference

        Raises:
            error.APIError: Something generally bad happened... :()
            error.ValidationError: Bad input.

        Returns:
            response (dict): Dictionary containing payment verification details
        """
        method = 'GET'

        if not ref and not self.reference:
            raise error.ValidationError('A unique object reference was not '
                                        'provided during instantiation. You'
                                        ' must provide a reference for this'
                                        ' transaction.')

        self.reference = (lambda ref: ref if ref else self.reference)(ref)

        endpoint = '/verify/' + self.reference
        url = self.api_host + self.resource_path + endpoint

        response, status, headers = self.client.request(method, url,
                                                        self.request_headers
                                                        )
        self._response_headers = headers
        self._status_code = status
        self._result = response['data']
        if not response.get('status', False):
            raise error.APIError(response.get('message'))

        self.authorization_code = self\
            ._result['authorization']['authorization_code']

        return response
Ejemplo n.º 11
0
 def create(self, customer, plan, authorization=None):
     """
     Create new subscription for a customer
     
     ARGS:
     customer = The customer's email address or customer code
     plan = The plan code to subscribe for.
     authorization = If customer has multiple authorizations, you can set the desired authorization 
     you wish to use for this subscription here. 
     If this is not supplied, the customer's most recent authorization would be used
     
     The returned subscription object has an email_token
      
     One is created for each subscription so customers can cancel their subscriptions from within the
     invoices sent to their mailboxes. Since they are not authorized, 
     the email tokens are what we use to authenticate the requests over the API.
     """
     
     end_point = ''
     method = 'POST'
     
     payload = {
         'customer': customer,
         'plan': plan
     }
     
     url = self.api_host + self.resource_path + end_point
     response, status, headers = self.client.request(method, url,
                                                         self.request_headers,
                                                         post_data=payload)
     self._response_headers = headers
     self._status_code = status
     self._result = response
     if not response.get('status', False):
         raise error.APIError(response.get('message')) 
     return response
Ejemplo n.º 12
0
    def charge(self, auth_code=None, amount=None,
               email=None, reference=None):  # pragma no cover
        """
        Bill a transaction to a customer's account.

        Args:
            auth_code (string, optional): Paystack verified authorization_code
            amount (int, optional): Amount to pay in Kobo.
            email (string, optional): Client's email address.
            reference (string, optional): Unique transaction reference.

        Raises:
            error.APIError: Something generally bad happened... :()
            error.ValidationError: Bad input.

        Returns:
            response (dict): Response data from Paystack
        """
        endpoint = '/charge_authorization'
        method = 'POST'

        if not auth_code and not self.authorization_code:
            raise error.ValidationError('This transaction object does not'
                                        ' have an authorization code.You must'
                                        ' provide an authorization code for'
                                        ' this transaction.')
        if not amount and not self.amount:
            raise error.ValidationError('There is no amount specified for this'
                                        ' transaction. You must provide the '
                                        ' transaction amount in Kobo.')
        if not email and not self.email:
            raise error.ValidationError('The customer\'s email address wss not'
                                        ' specified.')

        authorization_code = (
            lambda auth_code: auth_code if auth_code else self
            .authorization_code)(auth_code)

        email = (
            lambda email: email if email else self.email)(email)

        amount = (
            lambda amount: amount if amount else self.amount)(amount)

        payload = {
            "reference": reference,
            "amount": amount,
            "email": email,
            "authorization_code": authorization_code
        }

        url = self.api_host + self.resource_path + endpoint
        response, status, headers = self.client.request(method, url,
                                                        self.request_headers,
                                                        post_data=payload
                                                        )
        self._response_headers = headers
        self._status_code = status
        self._result = response
        if not response.get('status', False):
            raise error.APIError(response.get('message'))

        return response