diff --git a/readme.md b/readme.md index f5aef83..df47b76 100644 --- a/readme.md +++ b/readme.md @@ -24,19 +24,39 @@ In order to use our API, you will need to : Visite [our website ](https://slick-pay.com/) and create an account. If you are merchant you have to add your satim information like username and password #### 2. Get api's key After logging in, from your dashboard, you will able to get your PUBLIC_KEY. -#### 3. Configure through environment variables +#### 3. Configure API Client +```python +from slickpay import slickPayApi + +env = "dev" +api_client = slickPayApi() +api_client.config(environment=env, public_key="PUBLIC_KEY") +``` +#### 4. Configure through environment variables Create a .env file and inside your .env file create a variable like this: ```python public_key= your_public_key - sandbox= True or False ``` +Import environment variables +```python +import os +from dotenv import load_dotenv +load_dotenv() + +from slickpay import slickPayApi + +env = "prod" +api_client = slickPayApi() +api_client.config(environment=env, public_key=os.environ["public_key"]) +``` + # How to use? ## Concerning merchant account ## InvoiceTransferMerchant ### Create Invoice ```python from slickpay import InvoiceTransferMerchant - invoiceMerchant = InvoiceTransferMerchant() + invoiceMerchant = InvoiceTransferMerchant(client=api_client) data = { 'amount': 10000, @@ -80,7 +100,7 @@ from slickpay import InvoiceTransferMerchant ### Create ```python from slickpay import Account - userAccount = Account() + userAccount = Account(client=api_client) data = { "title" : "Lorem Ipsum", "lastname" : "Lorem", @@ -115,7 +135,7 @@ from slickpay import Account ### Create Contact ```python from slickpay import Contact - userContact = Contact() + userContact = Contact(client=api_client) data = { "title" : "Lorem Ipsum", "lastname" : "Lorem", @@ -151,7 +171,7 @@ from slickpay import Contact ### Calculate Commission ```python from slickpay import Transfer - userTransfer = Transfer() + userTransfer = Transfer(client=api_client) res=userTransfer.calculateCommission(amount) return res ``` @@ -189,7 +209,7 @@ from slickpay import Transfer ### Calculate Commission ```python from slickpay import PaymentAggregation - userPaymentAggregation = PaymentAggregation() + userPaymentAggregation = PaymentAggregation(client=api_client) data = { "type": "percentage", "total": 10000, @@ -266,7 +286,7 @@ from slickpay import PaymentAggregation ### Calculate Commission ```python from slickpay import InvoiceTransfer - userInvoiceTransfer = InvoiceTransfer() + userInvoiceTransfer = InvoiceTransfer(client=api_client) res=userInvoiceTransfer.calculateCommissionInvoice(amount) return res ``` @@ -314,4 +334,4 @@ from slickpay import InvoiceTransfer ``` # More help - [Slick-Pay website](https://slick-pay.com/) -- [devapi-slick-pay](https://www.devapi.slick-pay.com/) \ No newline at end of file +- [devapi-slick-pay](https://www.devapi.slick-pay.com/) diff --git a/setup.py b/setup.py index 3a78285..00e2c75 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='slick-pay', packages=['slick-pay'], # Chose the same as "name" - version='0.3', # Start with a small number and increase it with every change you make + version='0.4', # Start with a small number and increase it with every change you make license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository description='slick-pay Gateway (Python Library)', # Give a short description about your library author='Rahim', # Type in your name diff --git a/slickpay/__init__.py b/slickpay/__init__.py index 295a337..adcbb93 100644 --- a/slickpay/__init__.py +++ b/slickpay/__init__.py @@ -4,4 +4,4 @@ from slickpay.user import Transfer from slickpay.user import PaymentAggregation from slickpay.user import InvoiceTransfer - +from slickpay.client import SlickPayApi diff --git a/slickpay/client.py b/slickpay/client.py new file mode 100644 index 0000000..2a9dd8b --- /dev/null +++ b/slickpay/client.py @@ -0,0 +1,58 @@ +import requests + +API_VERSION = "v2" +DEV_API_URL = "https://www.devapi.slick-pay.com/api/" +PROD_API_URL = "https://www.prodapi.slick-pay.com/api/" + + +class SlickPayApi: + def __init__(self) -> None: + self.api_version = API_VERSION + self._config = {} + + def config(self, environment, public_key): + if environment in ["dev", "prod"]: + self._config["public_key"] = public_key + + if environment == "dev": + self._config["base_url"] = DEV_API_URL + + if environment == "prod": + self._config["base_url"] = PROD_API_URL + + return self._config + + def build_request_url(self, url_path): + return f'{self._config["base_url"]}{self.api_version}/{url_path}' + + @property + def headers(self): + if self._config: + return { + "Authorization": f'Bearer {self._config["public_key"]}', + "Accept": "application/json", + } + + def post(self, url_path, data): + request_url = self.build_request_url(url_path=url_path) + if self.headers: + response = requests.post(url=request_url, headers=self.headers, json=data) + return response.json() + + def get(self, url_path): + request_url = self.build_request_url(url_path=url_path) + if self.headers: + response = requests.get(url=request_url, headers=self.headers) + return response.json() + + def put(self, url_path, data): + request_url = self.build_request_url(url_path=url_path) + if self.headers: + response = requests.put(url=request_url, headers=self.headers, json=data) + return response.json() + + def delete(self, url_path): + request_url = self.build_request_url(url_path=url_path) + if self.headers: + response = requests.delete(url=request_url, headers=self.headers) + return response.json() diff --git a/slickpay/merchant.py b/slickpay/merchant.py index 17b6363..11bfa5b 100644 --- a/slickpay/merchant.py +++ b/slickpay/merchant.py @@ -1,64 +1,28 @@ -import os - -import requests -from dotenv import load_dotenv - -load_dotenv() - - class InvoiceTransferMerchant: + def __init__(self, client) -> None: + self.client = client def createInvoice(self, data): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/merchants/invoices' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/merchants/invoices' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.post(url, headers=headers, json=data) - body = res.json() - return body + url = "merchants/invoices" + response = self.client.post(url_path=url, data=data) + return response def invoiceDetail(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/merchants/invoices/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/merchants/invoices/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"invoices/{int(uid)}" + response = self.client.get(url_path=url) + return response def listInvoice(self, offset, page): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/merchants/invoices?offset={int(offset)}&page={int(page)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/merchants/invoices?offset={int(offset)}&page={int(page)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"merchants/invoices?offset={int(offset)}&page={int(page)}" + response = self.client.get(url_path=url) + return response def updateInvoice(self, data, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/merchants/invoices/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/merchants/invoices/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.put(url, headers=headers, json=data) - - return res.json() + url = f"merchants/invoices/{int(uid)}" + response = self.client.put(url_path=url, data=data) + return response def deleteInvoice(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/merchants/invoices/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/merchants/invoices/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.delete(url, headers=headers) - - return res.json() + url = f"merchants/invoices/{int(uid)}" + response = self.client.delete(url_path=url) + return response diff --git a/slickpay/user.py b/slickpay/user.py index 7c3cb6a..f6b7bce 100644 --- a/slickpay/user.py +++ b/slickpay/user.py @@ -1,340 +1,164 @@ -import os - -import requests - -from dotenv import load_dotenv - -load_dotenv() - - class Account: + def __init__(self, client) -> None: + self.client = client def create(self, data): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/accounts' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/accounts' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.post(url, headers=headers, data=data) - body = res.json() - return body + url = "users/accounts" + response = self.client.post(url_path=url, data=data) + return response def update(self, uid, data): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/accounts/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/accounts/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - - res = requests.put(url, headers=headers, json=data) - - return res.json() + url = f"users/accounts/{int(uid)}" + response = self.client.put(url_path=url, data=data) + return response def list(self, offset, page): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/accounts?offset={int(offset)}&page={int(page)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/accounts?offset={int(offset)}&page={int(page)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/accounts?offset={int(offset)}&page={int(page)}" + response = self.client.get(url_path=url) + return response def accountDetails(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/accounts/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/accounts/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/accounts/{int(uid)}" + response = self.client.get(url_path=url) + return response def delete(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/accounts/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/accounts/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.delete(url, headers=headers) - - return res.json() + url = f"users/accounts/{int(uid)}" + response = self.client.delete(url_path=url) + return response class Contact: + def __init__(self, client) -> None: + self.client = client def createContact(self, data): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/contacts' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/contacts' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.post(url, headers=headers, json=data) - body = res.json() - return body + url = "users/contacts" + response = self.client.post(url_path=url, data=data) + return response def contactDetail(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/contacts/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/contacts/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/contacts/{int(uid)}" + response = self.client.get(url_path=url) + return response def listContact(self, offset, page): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/contacts?offset={int(offset)}&page={int(page)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/contacts?offset={int(offset)}&page={int(page)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/contacts?offset={int(offset)}&page={int(page)}" + response = self.client.get(url_path=url) + return response def updateContact(self, data, uid): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/contacts/'+str(uid) - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/contacts/'+str(uid) - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.put(url, headers=headers, data=data) - - return res.json() + url = f"users/contacts/{str(uid)}" + response = self.client.put(url_path=url, data=data) + return response def deleteContact(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/contacts/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/contacts/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.delete(url, headers=headers) - - return res.json() + url = f"users/contacts/{int(uid)}" + response = self.client.delete(url_path=url) + return response class Transfer: + def __init__(self, client) -> None: + self.client = client def calculateCommission(self, amount): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/transfers/commission' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/transfers/commission' - - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - data = { - 'amount': amount - } - res = requests.post(url, headers=headers, data=data) - return res.json() + url = "users/transfers/commission" + response = self.client.post(url_path=url, data=amount) + return response def createPayment(self, data): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/transfers' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/transfers' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.post(url, headers=headers, json=data) - # print(res) - body = res.json() - return body - # return res + url = "users/transfers" + response = self.client.post(url_path=url, data=data) + return response - def paymentDetail(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/transfers/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/transfers/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - return res.json() + def paymentDetail(self, uid): + url = f"users/transfers/{int(uid)}" + response = self.client.get(url_path=url) + return response def listTransfer(self, offset, page): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/transfers?offset={int(offset)}&page={int(page)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/transfers?offset={int(offset)}&page={int(page)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/transfers?offset={int(offset)}&page={int(page)}" + response = self.client.get(url_path=url) + return response def updateTransfer(self, data, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/transfers/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/transfers/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.put(url, headers=headers, json=data) - - return res.json() + url = f"users/transfers/{int(uid)}" + response = self.client.put(url_path=url, data=data) + return response def deleteTransfer(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/transfers/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/transfers/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.delete(url, headers=headers) - - return res.json() + url = f"users/transfers/{int(uid)}" + response = self.client.delete(url_path=url) + return response class PaymentAggregation: + def __init__(self, client) -> None: + self.client = client def commission(self, data): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/aggregations/commission' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/aggregations/commission' - - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - - res = requests.post(url, headers=headers, json=data) - return res.json() + url = "users/aggregations/commission" + response = self.client.post(url_path=url, data=data) + return response def create(self, data): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/aggregations' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/aggregations' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.post(url, headers=headers, json=data) - body = res.json() - return body + url = "users/aggregations" + response = self.client.post(url_path=url, data=data) + return response def details(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/aggregations/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/aggregations/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/aggregations/{int(uid)}" + response = self.client.get(url_path=url) + return response def list(self, offset, page): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/aggregations?offset={int(offset)}&page={int(page)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/aggregations?offset={int(offset)}&page={int(page)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/aggregations?offset={int(offset)}&page={int(page)}" + response = self.client.get(url_path=url) + return response def update(self, data, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/aggregations/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/aggregations/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.put(url, headers=headers, json=data) - body = res.json() - return body + url = f"users/aggregations/{int(uid)}" + response = self.client.put(url_path=url, data=data) + return response def delete(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/aggregations/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/aggregations/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.delete(url, headers=headers) - body = res.json() - return body + url = f"users/aggregations/{int(uid)}" + response = self.client.delete(url_path=url) + return response class InvoiceTransfer: + def __init__(self, client) -> None: + self.client = client def calculateCommissionInvoice(self, amount): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/invoices/commission' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/invoices/commission' - - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - data = { - 'amount': amount - } - res = requests.post(url, headers=headers, data=data) - return res.json() + url = "users/invoices/commission" + response = self.client.post(url_path=url, data=data) + return response def createInvoice(self, data): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/invoices' - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/invoices' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.post(url, headers=headers, json=data) - body = res.json() - return body + url = "users/invoices" + response = self.client.post(url_path=url, data=data) + return response def InvoiceDetail(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/invoices/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/invoices/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/invoices/{int(uid)}" + response = self.client.get(url_path=url) + return response def listInvoice(self, offset, page): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/invoices?offset={int(offset)}&page={int(page)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/invoices?offset={int(offset)}&page={int(page)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.get(url, headers=headers) - - return res.json() + url = f"users/invoices?offset={int(offset)}&page={int(page)}" + response = self.client.get(url_path=url) + return response def updateInvoice(self, data, uid): - print(self) - if os.environ["sandbox"]: - url = 'https://www.devapi.slick-pay.com/api/v2/users/invoices/'+str(uid) - else: - url = 'https://www.prodapi.slick-pay.com/api/v2/users/invoices/'+str(uid) - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.put(url, headers=headers, json=data) - - return res.json() + url = f"users/invoices/{str(uid)}" + response = self.client.put(url_path=url, data=data) + return response def deleteInvoice(self, uid): - print(self) - if os.environ["sandbox"]: - url = f'https://www.devapi.slick-pay.com/api/v2/users/invoices/{int(uid)}' - else: - url = f'https://www.prodapi.slick-pay.com/api/v2/users/invoices/{int(uid)}' - headers = {"Authorization": f'{str(os.environ["public_key"])}', "Accept": "application/json"} - res = requests.delete(url, headers=headers) - - return res.json() + url = f"users/invoices/{int(uid)}" + response = self.client.delete(url_path=url) + return response