Skip to content

Commit

Permalink
Final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamOtieno committed Dec 29, 2021
1 parent 8b997a7 commit 3cc143f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ __pycache__/
# C extensions
*.so

venv

# Distribution / packaging
.Python
build/
Expand Down
40 changes: 39 additions & 1 deletion README.md
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)
```
6 changes: 6 additions & 0 deletions pyproject.toml
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 added python_flutterwave/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions python_flutterwave/payment.py
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()
18 changes: 18 additions & 0 deletions setup.cfg
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

0 comments on commit 3cc143f

Please sign in to comment.