-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b997a7
commit 3cc143f
Showing
6 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ __pycache__/ | |
# C extensions | ||
*.so | ||
|
||
venv | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,40 @@ | ||
# python-fullterwave | ||
# Python Flutterwave | ||
|
||
### Description | ||
Python Wrapper for interacting with the Flutterwave API | ||
|
||
|
||
## Installation | ||
|
||
- ``pip install python-flutterwave`` | ||
|
||
## Usage | ||
|
||
- Create an account in Flutterwave and obtain your `Secret Key` only. | ||
|
||
``` | ||
from python_flutterwave import payment, initiate_payment | ||
payment.token = 'YOUR_SECRET_KEY' | ||
``` | ||
|
||
- To trigger a payment that returns a redirect uri | ||
|
||
``` | ||
uri = payment.initiate_payment(tx_ref="qwerty", amount=100, redirect_url='your_callback_url', | ||
payment_options='mpesa', customer_email='[email protected]', | ||
customer_phone_number='0123456789', currency='KES', customer_name='John Doe', | ||
title='Demo Payment', description='Just pay me...') | ||
print(uri) | ||
``` | ||
- Redirect the user to that uri where he/she will make the payment. | ||
- After payment is made, the user will be redirected to the `redirect_url` you declared but Flutterwave will append some | ||
info regarding the payment i.e. `transaction_id` and `tx_ref`. If your url is `https://example.com/callback` | ||
then it may be `http://example.com/callback/?status=successful&tx_ref=qwerty&transaction_id=2784792` | ||
- You should save the transaction_id to your DB as it will be used to query the transaction details. | ||
|
||
|
||
- To check the transaction details e.g. successful or not, grab the transaction_id from the previous step. | ||
``` | ||
details = payment.get_payment_details(transaction_id) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[build-system] | ||
requires = [ | ||
"setuptools>=54", | ||
"wheel" | ||
] | ||
build-backend = "setuptools.build_meta" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import requests | ||
import json | ||
from typing import Optional | ||
|
||
token = "" | ||
|
||
|
||
def initiate_payment(tx_ref: str, amount: float, currency: str, redirect_url: str, payment_options: str, | ||
customer_email: str, customer_phone_number: Optional[str], customer_name: str, | ||
title: Optional[str], | ||
description: Optional[str]) -> str: | ||
"""This is used to initiate payments. It takes in the arguments and returns the url to redirect users for | ||
payments """ | ||
payment_url = "https://api.flutterwave.com/v3/payments" | ||
payload = json.dumps({ | ||
"tx_ref": f"{tx_ref}", | ||
"amount": f"{amount}", | ||
"currency": f"{currency}", | ||
"redirect_url": f"{redirect_url}", | ||
"payment_options": f"{payment_options}", | ||
"customer": { | ||
"email": f"{customer_email}", | ||
"phonenumber": f"{customer_phone_number}", | ||
"name": f"{customer_name}" | ||
}, | ||
"customizations": { | ||
"title": f"{title}", | ||
"description": f"{description}", | ||
} | ||
}) | ||
headers = { | ||
'Authorization': f'Bearer {token}', | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
response = requests.request(method="POST", url=payment_url, headers=headers, data=payload) | ||
link = response.json()["data"]["link"] | ||
return link | ||
|
||
|
||
def get_payment_details(trans_id: str): | ||
""" | ||
Takes the transaction_id from the request and returns the status info in json. | ||
It transaction_id is different from the transaction_ref so it should be grabbed from the request in the redirect url | ||
""" | ||
url = f"https://api.flutterwave.com/v3/transactions/{trans_id}/verify" | ||
|
||
payload = {} | ||
headers = { | ||
'Authorization': f'Bearer {token}' | ||
} | ||
|
||
response = requests.request("GET", url, headers=headers, data=payload) | ||
return response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[metadata] | ||
name = python-flutterwave | ||
version = 0.0.1 | ||
author = William Otieno | ||
author_email = [email protected] | ||
description = Python Wrapper for interacting with the Flutterwave Payments API | ||
long_description = file: README.md | ||
long_description_content_type = text/markdown | ||
url = https://github.com/WilliamOtieno/python-flutterwave | ||
classifiers = | ||
Programming Language :: Python :: 3 | ||
License :: OSI Approved :: MIT License | ||
Operating System :: OS Independent | ||
|
||
[options] | ||
packages = find: | ||
python_requires = >=3.7 | ||
include_package_data = True |