Skip to content

Commit 3f1b2eb

Browse files
committed
refactor project
1 parent e3eacd3 commit 3f1b2eb

File tree

10 files changed

+144
-270
lines changed

10 files changed

+144
-270
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ dist
66
build
77
venv
88
.idea
9-
__pycache__
9+
__pycache__
10+
.env

kkiapay/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .core import Kkiapay
1+
from .api import Kkiapay

kkiapay/api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from .base import KkiapayBase
2+
3+
4+
class Kkiapay(KkiapayBase):
5+
def __init__(self, public_key: str, private_key: str, secret: str, sandbox=False) -> None:
6+
r"""Initialize Kkiapay client.
7+
:param public_key: Public key from https://app.kkiapay.me/dashboard/developers/keys.
8+
:param private_key: Provate key from https://app.kkiapay.me/dashboard/developers/keys.
9+
:param secret: Kiapay secret from https://app.kkiapay.me/dashboard/developers/keys.
10+
:param sandbox: A boolean value to specify the environment (Sandbox or Live).
11+
"""
12+
self._initialize_credentials(public_key, private_key, secret, sandbox)
13+
14+
15+
def verify_transaction(self, transaction_id: str) -> dict:
16+
r"""Verify a transaction.
17+
:param transaction_id: transaction ID
18+
"""
19+
return self._verify_transaction(transaction_id)
20+
21+
22+
def refund_transaction(self, transaction_id: str) -> dict:
23+
r"""Refund a specific transaction.
24+
:param transaction_id: transaction ID
25+
"""
26+
return self._refund_transaction(transaction_id)

kkiapay/base.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from .utils import make_request
2+
3+
SANDBOX_BASE_URL = "https://api-sandbox.kkiapay.me"
4+
PRODUCTION_BASE_URL = "https://api.kkiapay.me"
5+
6+
7+
class KkiapayBase:
8+
def _initialize_credentials(self, public_key: str, private_key: str, secret: str, sandbox: bool) -> None:
9+
self.public_key = public_key
10+
self.private_key = private_key
11+
self.secret = secret
12+
self.is_sandbox_environment = sandbox
13+
14+
def _build_request_headers(self) -> dict:
15+
return {
16+
"Accept": "application/json",
17+
"X-SECRET-KEY": self.secret,
18+
"X-API-KEY": self.public_key,
19+
"X-PRIVATE-KEY": self.private_key,
20+
}
21+
22+
def _build_request_url(self, path: str) -> str:
23+
BASE_URL = SANDBOX_BASE_URL if self.is_sandbox_environment else PRODUCTION_BASE_URL
24+
return f"{BASE_URL}/{path}"
25+
26+
def _verify_transaction(self, transaction_id: str):
27+
return make_request(
28+
"post",
29+
self._build_request_url("api/v1/transactions/status"),
30+
json={"transactionId": transaction_id},
31+
headers=self._build_request_headers()
32+
)
33+
34+
def _refund_transaction(self, transaction_id: str):
35+
return make_request(
36+
"post",
37+
self._build_request_url("api/v1/transactions/revert"),
38+
json={"transactionId": transaction_id},
39+
headers=self._build_request_headers()
40+
)

kkiapay/core.py

Lines changed: 0 additions & 92 deletions
This file was deleted.

kkiapay/exceptions.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

kkiapay/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import requests
2+
from requests.auth import HTTPBasicAuth
3+
4+
5+
def make_request(method: str, url: str, data=None, json=None, auth=None, **kwargs):
6+
"""Perform a request.
7+
Usage::
8+
>>> from shared.helpers import make_request
9+
>>> req = make_request('get', 'https://api-sandbox.kkiapay.me')
10+
"""
11+
12+
try:
13+
r = getattr(requests, method)(
14+
url,
15+
data=data,
16+
json=json,
17+
auth=auth if not auth else HTTPBasicAuth(**auth),
18+
**kwargs,
19+
)
20+
21+
return r.json()
22+
except requests.exceptions.RequestException as e:
23+
raise e

readme.md

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
- [Introduction](#introduction)
44
- [Installation](#installation)
55
- [Usage](#usage)
6-
- [Road map](#road-map)
76
- [Contributing](#contributing)
87
- [License](#license)
98

@@ -23,6 +22,8 @@ pip install kkiapay
2322

2423
Behold, the power of `kkiapay-python`:
2524

25+
### Verify a transaction
26+
2627
```python
2728
from kkiapay import Kkiapay
2829

@@ -31,84 +32,42 @@ k = Kkiapay('public_key', 'private_key', 'secret', sandbox=True)
3132
transaction = k.verify_transaction('LVFNrK1nx')
3233

3334
print(transaction)
34-
# => KkiapayTransaction(
35-
# performed_at='2019-07-11T11:24:42.687Z',
36-
# type='DEBIT',
37-
# status='FAILED',
38-
# source='MOBILE_MONEY',
39-
# amount=1,
40-
# fees=0,
41-
# country='BJ',
42-
# reason='invalid_number',
43-
# transactionId='LVFNrK1nx',
44-
# performedAt='07/11/2019'
45-
# )
46-
47-
print(transaction.status)
48-
# => FAILED
49-
print(transaction.amount)
50-
# => 1
35+
# => {
36+
# "performed_at":"2023-02-20T17:44:47.842Z",
37+
# "received_at":1676915100302,
38+
# "type":"DEBIT",
39+
# "status":"SUCCESS",
40+
# "source":"MOBILE_MONEY",
41+
# "source_common_name":"mtn-benin",
42+
# "amount":100,
43+
# "fees":2,
44+
# "net":0,
45+
# "externalTransactionId":"test",
46+
# "transactionId":"123",
47+
# ...
48+
# }
5149
```
5250

53-
## Road map
54-
55-
`kkiapay-python` is still under heavy development, we decided to ship it in this early stage so you can help us make it better.
51+
### Refund a transaction
5652

57-
Here's the plan for what's coming:
58-
59-
- [x] Sandbox and Live environments
60-
- [x] Verify Transaction
61-
- [x] Refund Transaction - `Only available on Live`
62-
- [x] Schedule Payout
63-
- [ ] Add better errors and exceptions handling
64-
- [ ] Add tests.
53+
```python
54+
from kkiapay import Kkiapay
6555

56+
k = Kkiapay('public_key', 'private_key', 'secret', sandbox=True)
6657

67-
## Schedule Payout
58+
transaction = k.refund_transaction('LVFNrK1nx')
6859

69-
### Example
70-
Below is an example of the function usage:
71-
```python
72-
# Setup Scheduled Payout using 'ROOF' algorithm
73-
schedule_payout = k.setup_payout(algorithm = '2',
74-
roof_amount = '10000',
75-
destination = '61000000',
76-
destination_type = '1',
77-
send_notification = True,
78-
country_code = '229'
79-
)
80-
81-
print(schedule_payout)
82-
#{
83-
# 'response':
84-
# {
85-
# 'merchant': '5d34445784deb700073a0281',
86-
# 'meta_data': '',
87-
# 'algorithm': 'roof',
88-
# 'rate_frequency': '',
89-
# 'roof_amount': '10000',
90-
# 'active': True,
91-
# 'send_notification': True,
92-
# 'destination_type': 'MOBILE_MONEY',
93-
# 'destination': '22961000000',
94-
# 'job_name': '',
95-
# 'account': '5d34445784deb700073a0282'
96-
# },
97-
# 'status_code': 200
98-
#}
60+
print(transaction)
61+
# => {
62+
# "code":"SUCCESS",
63+
# "description":"REVERTED",
64+
# "transactionId":"123"
65+
# }
9966
```
10067

101-
### Attribute Matrix
102-
| Name | Required | Possible Values | Description |
103-
|:-----------------:|:--------:|:------------------------------------------:|:---------------------------------------------------------------------------------:|
104-
| **algorithm** | **M** | {"1": 'rate', "2": 'roof'} | Specify the algorithm to be used. |
105-
| **destination_type** | **M** | {"1": 'MOBILE_MONEY', "2": 'BANK_ACCOUNT'} | Specify the Destination type |
106-
| **destination** | **M** | '61000000' | Specify the Destination number/account Number |
107-
| **rate_frequency** | M/O | {"1": '3d', "2": '1w', "3": '1m'} | Specify the Rate Frequency. Required in case 'rate' algorithm is used |
108-
| **roof_amount** | M/O | '10000' | Specify the Roof amount. |
109-
| **send_notification** | O | Boolean (True, False) | Specify is a Notification should be sent |
110-
| **country_code** | O | '229' | Specify the Country Code of the destination number is case 'MOBILE MONEY' is used |
68+
### Schedule Payout
11169

70+
Schedule payout API is deprecated and no longer supported as of Feb 20th, 2023 from the API and can be done only on the [dashboard](https://app.kkiapay.me/dashboard) until further notice.
11271

11372
## Contributing
11473

requirements.txt

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,3 @@
1-
appdirs==1.4.3
2-
atomicwrites==1.3.0
3-
attrs==19.1.0
4-
black==19.3b0
5-
bleach==3.1.4
6-
certifi==2019.6.16
7-
chardet==3.0.4
8-
Click==7.0
9-
docutils==0.14
10-
idna==2.8
11-
importlib-metadata==0.18
12-
mock==3.0.5
13-
more-itertools==7.1.0
14-
packaging==19.0
15-
pkginfo==1.5.0.1
16-
pluggy==0.12.0
17-
py==1.8.0
18-
Pygments==2.4.2
19-
pyparsing==2.4.0
20-
pytest==5.0.1
21-
readme-renderer==24.0
221
requests==2.22.0
23-
requests-mock==1.6.0
24-
requests-toolbelt==0.9.1
25-
six==1.12.0
26-
toml==0.10.0
27-
tqdm==4.32.2
28-
twine==1.13.0
29-
urllib3==1.25.3
30-
wcwidth==0.1.7
31-
webencodings==0.5.1
32-
zipp==0.5.2
2+
responses==0.22.0
3+
pytest==7.2.1

0 commit comments

Comments
 (0)