Skip to content

Commit

Permalink
Version 0.1.4
Browse files Browse the repository at this point in the history
- Added MPESA Payments
- Refactored previous method calls (No major change)
- Added simple build script
  • Loading branch information
WilliamOtieno committed Dec 30, 2021
1 parent 063c90b commit 1276356
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ from python_flutterwave import payment
payment.token = 'YOUR_SECRET_KEY'
```

- To trigger a payment that returns a redirect uri
- To trigger a standard payment that returns a redirect uri

```
uri = payment.initiate_payment(tx_ref="qwerty", amount=100, redirect_url='your_callback_url',
Expand All @@ -37,4 +37,15 @@ then it may be `http://example.com/callback/?status=successful&tx_ref=qwerty&tra
- 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)
print(details)
```

- To trigger an automatic mpesa charge on your customer, first configure your Webhook url in the dashboard, it may be a
simple server; Flutterwave will post some data regarding your transaction status in that url. This method call will
return a Python dict object. You can decide what to do thereon.
```
mpesa_trans_details = payment.trigger_mpesa_payment(tx_ref="qwertyuio", amount=100, currency='KES',
email='[email protected]', phone_number='1234567890',
full_name='John Doe')
print(mpesa_trans_details)
```
6 changes: 6 additions & 0 deletions build_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

rm -rf dist/
rm -rf *.egg-info

python -m build
33 changes: 29 additions & 4 deletions python_flutterwave/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ def initiate_payment(tx_ref: str, amount: float, currency: str, redirect_url: st
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
"""This is used to initiate standard 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}",
"currency": f"{currency}".upper(),
"redirect_url": f"{redirect_url}",
"payment_options": f"{payment_options}",
"customer": {
Expand All @@ -38,7 +38,7 @@ def initiate_payment(tx_ref: str, amount: float, currency: str, redirect_url: st
return link


def get_payment_details(trans_id: str):
def get_payment_details(trans_id: str) -> dict:
"""
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
Expand All @@ -52,4 +52,29 @@ def get_payment_details(trans_id: str):
}

response = requests.request("GET", url, headers=headers, data=payload)
return response.json()
return dict(response.json())


def trigger_mpesa_payment(tx_ref: str, amount: float, currency: str, email: Optional[str], phone_number: str,
full_name: str) -> dict:
"""
This will automatically trigger an MPESA payment from your customer. It will return a dictionary with details
regarding the transaction. Flutterwave will also send the status to your webhook configured in the dashboard.
"""
url = "https://api.flutterwave.com/v3/charges?type=mpesa"

payload = json.dumps({
"tx_ref": f"{tx_ref}",
"amount": f"{amount}",
"currency": f"{currency}".upper(),
"email": f"{email}",
"phone_number": f"{phone_number}",
"fullname": f"{full_name}"
})
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)
return dict(response.json())
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = python-flutterwave
version = 0.0.4
version = 0.1.4
author = William Otieno
author_email = [email protected]
description = Python Wrapper for interacting with the Flutterwave Payments API
Expand Down

0 comments on commit 1276356

Please sign in to comment.