Skip to content

Commit

Permalink
Merge pull request #4 from adilmohak/main
Browse files Browse the repository at this point in the history
  • Loading branch information
isrugeek committed Jun 2, 2023
2 parents 04980d0 + 2e42962 commit 87e0c9b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# vscode editor
.vscode/
81 changes: 53 additions & 28 deletions chapa/api.py
Expand Up @@ -21,19 +21,22 @@ class Chapa:
Simple SDK for Chapa Payment gateway
"""

def __init__(self, secret, base_ur='https://api.chapa.co', api_version='v1',
response_format='json'):
def __init__(
self,
secret,
base_ur="https://api.chapa.co",
api_version="v1",
response_format="json",
):
self._key = secret
self.base_url = base_ur
self.api_version = api_version
if response_format and response_format in ['json', 'obj']:
if response_format and response_format in ["json", "obj"]:
self.response_format = response_format
else:
raise ValueError('response_format must be \'json\' or \'obj\'')
raise ValueError("response_format must be 'json' or 'obj'")

self.headers = {
'Authorization': f'Bearer {self._key}'
}
self.headers = {"Authorization": f"Bearer {self._key}"}

def send_request(self, url, method, data=None, params=None, headers=None):
"""
Expand Down Expand Up @@ -88,8 +91,20 @@ def _construct_request(self, *args, **kwargs):

return res

def initialize(self, email: str, amount: int, first_name: str, last_name: str, tx_ref: str,
currency='ETB', callback_url=None, customization=None, headers=None):
def initialize(
self,
email: str,
amount: int,
first_name: str,
last_name: str,
tx_ref: str,
currency="ETB",
phone_number=None,
callback_url=None,
return_url=None,
customization=None,
headers=None,
):
"""
Initialize the Transaction
Expand All @@ -99,9 +114,13 @@ def initialize(self, email: str, amount: int, first_name: str, last_name: str, t
first_name (str): first name of the customer
last_name (str): last name of the customer
tx_ref (str): your transaction id
phone_number (str, optional): phone number of the customer.
Defaults to None.
currency (str, optional): currency the transaction. Defaults to 'ETB'.
callback_url (str, optional): url for the customer to redirect after payment.
callback_url (str, optional): function that runs when payment is successful.
Defaults to None.
return_url (str, optional): web address to redirect the user after payment is
successful. Defaults to None.
customization (dict, optional): customization, currently 'title' and 'description'
are available. Defaults to None.
headers(dict, optional): header to attach on the request. Default to None
Expand All @@ -112,44 +131,50 @@ def initialize(self, email: str, amount: int, first_name: str, last_name: str, t
"""

data = {
'first_name': first_name,
'last_name': last_name,
'tx_ref': tx_ref,
'currency': currency,
"first_name": first_name,
"last_name": last_name,
"tx_ref": tx_ref,
"currency": currency,
}

if not isinstance(amount, int):
if str(amount).replace('.', '', 1).isdigit() and float(amount) > 0:
if str(amount).replace(".", "", 1).isdigit() and float(amount) > 0:
pass
else:
raise ValueError("invalid amount")
elif isinstance(amount, int):
if amount < 0:
raise ValueError("invalid amount")

data['amount'] = amount
data["amount"] = amount

if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
raise ValueError("invalid email")

data['email'] = email
data["email"] = email

if phone_number:
data["phone_number"] = phone_number

if callback_url:
data['callback_url'] = callback_url
data["callback_url"] = callback_url

if return_url:
data["return_url"] = return_url

if customization:
if 'title' in customization:
data['customization[title]'] = customization['title']
if 'description' in customization:
data['customization[description]'] = customization['description']
if 'logo' in customization:
data['customization[logo]'] = customization['logo']
if "title" in customization:
data["customization[title]"] = customization["title"]
if "description" in customization:
data["customization[description]"] = customization["description"]
if "logo" in customization:
data["customization[logo]"] = customization["logo"]

response = self._construct_request(
url=f'{self.base_url}/{self.api_version}/transaction/initialize',
url=f"{self.base_url}/{self.api_version}/transaction/initialize",
method="post",
data=data,
headers=headers
headers=headers,
)
return response

Expand All @@ -164,9 +189,9 @@ def verify(self, transaction, headers=None):
response(Response): response object of the response data return from the Chapa server.
"""
response = self._construct_request(
url=f'{self.base_url}/{self.api_version}/transaction/verify/{transaction}',
url=f"{self.base_url}/{self.api_version}/transaction/verify/{transaction}",
method="get",
headers=headers
headers=headers,
)
return response

Expand Down

0 comments on commit 87e0c9b

Please sign in to comment.