Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ __pycache__/
# C extensions
*.so

# IDE
*.idea/

# Distribution / packaging
.Python
env/
Expand Down Expand Up @@ -62,4 +65,4 @@ target/
.ipynb_checkpoints

# Vscode config
.vscode
.vscode
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ response = transaction.verify(refcode) #Verify a transaction given a reference

customer = Customer(authorization_key="sk_myauthorizationkeyfromthepaystackguys")
response = customer.create("customer2@gmail.com", "John", "Doe", phone="080123456789") #Add new customer
response = customer.getone(1234) #Get customer with id of 1234
response = customer.getone("CUS_xxxxyy") #Get customer with customer code of CUS_xxxxyy
response = customer.getall() #Get all customers


Expand All @@ -53,7 +53,4 @@ response = plan.getone(240) #Get plan with id of 240
response = plan.getall() #Get all plans

```
#Todo
Write more tests


32 changes: 11 additions & 21 deletions pypaystack/baseapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class BaseAPI(object):

"""
Base class for the pypaystack python API wrapper for paystack
Not to be instantiated directly.
Expand All @@ -15,7 +14,6 @@ class BaseAPI(object):
_CONTENT_TYPE = "application/json"
_BASE_END_POINT = "https://api.paystack.co"


def __init__(self, authorization_key=None):
if authorization_key:
self._PAYSTACK_AUTHORIZATION_KEY = authorization_key
Expand All @@ -24,18 +22,15 @@ def __init__(self, authorization_key=None):
if not self._PAYSTACK_AUTHORIZATION_KEY:
raise MissingAuthKeyError("Missing Authorization key argument or env variable")


def _url(self, path):
return self._BASE_END_POINT + path


def _headers(self):
return {
"Content-Type": self._CONTENT_TYPE,
"Authorization": "Bearer " + self._PAYSTACK_AUTHORIZATION_KEY,
"user-agent": "pyPaystack-{}".format(version.__version__)
}

return {
"Content-Type": self._CONTENT_TYPE,
"Authorization": "Bearer " + self._PAYSTACK_AUTHORIZATION_KEY,
"user-agent": "pyPaystack-{}".format(version.__version__)
}

def _parse_json(self, response_obj):
"""
Expand All @@ -49,23 +44,20 @@ def _parse_json(self, response_obj):
status = parsed_response.get('status', None)
message = parsed_response.get('message', None)
data = parsed_response.get('data', None)
#if data:
# message = data.get('gateway_response', None)
return response_obj.status_code, status, message, data


def _handle_request(self, method, url, data=None):

"""
Generic function to handle all API url calls
Returns a python tuple of status code, status(bool), message, data
"""
method_map = {
'GET':requests.get,
'POST':requests.post,
'PUT':requests.put,
'DELETE':requests.delete
}
'GET': requests.get,
'POST': requests.post,
'PUT': requests.put,
'DELETE': requests.delete
}

payload = json.dumps(data) if data else data
request = method_map.get(method)
Expand All @@ -81,6 +73,4 @@ def _handle_request(self, method, url, data=None):
return self._parse_json(response)
else:
body = response.json()
return response.status_code, body.get('status'), body.get('message'), body.get('errors')


return response.status_code, body.get('status'), body.get('message'), body.get('errors')
52 changes: 24 additions & 28 deletions pypaystack/customers.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
from .baseapi import BaseAPI


class Customer(BaseAPI):

def create(self, email, first_name=None, last_name=None, phone=None):
"""
Creates a new paystack customer account

args:
email -- Customer's email address
first_name-- Customer's first name (Optional)
last_name-- Customer's last name (Optional)
phone -- optional
phone -- optional
"""
url = self._url("/customer/")
payload = {
"first_name": first_name,
"last_name": last_name,
"email": email,
"phone": phone
}
return self._handle_request('POST', url, payload)

"first_name": first_name,
"last_name": last_name,
"email": email,
"phone": phone,
}
return self._handle_request("POST", url, payload)

def update(self, user_id, email, first_name=None, last_name=None, phone=None):
"""
Expand All @@ -31,37 +30,34 @@ def update(self, user_id, email, first_name=None, last_name=None, phone=None):
email -- Customer's email address
first_name-- Customer's first name (Optional)
last_name-- Customer's last name (Optional)
phone -- optional
phone -- optional
"""
url = self._url("/customer/{}/".format(user_id))
payload = {
"first_name": first_name,
"last_name": last_name,
"email": email,
"phone": phone
}
return self._handle_request('PUT', url, payload)

"first_name": first_name,
"last_name": last_name,
"email": email,
"phone": phone,
}
return self._handle_request("PUT", url, payload)

def getall(self, pagination=10):
"""
Gets all the customers we have at paystack in steps of (default) 50 records per page.
We can provide an optional pagination to indicate how many customer records we want to fetch per time

args:
pagination -- Count of data to return per call
"""
url = self._url("/customer/?perPage="+str(pagination))
return self._handle_request('GET', url)
url = self._url("/customer/?perPage=" + str(pagination))
return self._handle_request("GET", url)


def getone(self, user_id):
def getone(self, customer_code):
"""
Gets the customer with the given user id

args:
user_id -- The customer's user id
customer_code -- The customer's code
"""
url = self._url("/customer/{}/".format(user_id))
return self._handle_request('GET', url)

url = self._url("/customer/{}/".format(customer_code))
return self._handle_request("GET", url)
8 changes: 0 additions & 8 deletions pypaystack/tests/__init__.py

This file was deleted.

37 changes: 0 additions & 37 deletions pypaystack/tests/test_00_transaction.py

This file was deleted.

43 changes: 0 additions & 43 deletions pypaystack/tests/test_01_transactions_records.py

This file was deleted.

48 changes: 0 additions & 48 deletions pypaystack/tests/test_02_customer.py

This file was deleted.

34 changes: 0 additions & 34 deletions pypaystack/tests/test_03_customer_records.py

This file was deleted.

Loading