Skip to content

Commit

Permalink
Version 0.1.4
Browse files Browse the repository at this point in the history
- Added USSD Payments
- Added USSD example to docs
- Refactored previous docs (No major change)
  • Loading branch information
WilliamOtieno committed Jan 1, 2022
1 parent e7531bd commit e2821e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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
- To trigger an automatic MPESA charge on your customer through STK push, 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.
```
Expand All @@ -49,3 +49,15 @@ mpesa_trans_details = payment.trigger_mpesa_payment(tx_ref="qwertyuio", amount=1
full_name='John Doe')
print(mpesa_trans_details)
```

- To initiate a USSD payment to your customer, configure your webhook url in the dashboard where Flutterwave will post data
regarding the transaction. This method call will return a Python Dict object from which you can extract the USSD code to
show your customer for payment completion. For other banks, you may also need to extract the `payment_code` from the result.

- By default, `NGN` is the only supported currency for USSD payments so this method automatically uses `NGN`
- Only a number of banks support `USSD` so you have to refer to the docs to check your bank and its corresponding `account_bank` code.
```
details = payment.initiate_ussd_payment(tx_ref="123erd", amount=100, email='[email protected]',
phone_number='789456123', full_name='John Doe', account_bank='057')
print(details)
```
37 changes: 34 additions & 3 deletions python_flutterwave/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ 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
"""
url = f"https://api.flutterwave.com/v3/transactions/{trans_id}/verify"

Expand All @@ -58,8 +57,9 @@ def get_payment_details(trans_id: str) -> dict:
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.
This will automatically trigger an MPESA payment in form of an STK Push 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"

Expand All @@ -78,3 +78,34 @@ def trigger_mpesa_payment(tx_ref: str, amount: float, currency: str, email: Opti

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


def initiate_ussd_payment(tx_ref: str, account_bank: str, amount: float, email: str, phone_number: str,
full_name: str) -> dict:
"""
:param tx_ref: str
:param account_bank: str
:param amount: float
:param email: str
:param phone_number: str
:param full_name: str
:return: dict
"""
url = "https://api.flutterwave.com/v3/charges?type=ussd"
payload = json.dumps({
"tx_ref": f"{tx_ref}",
"account_bank": f"{account_bank}",
"amount": f"{amount}",
"currency": "NGN",
"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.1.4
version = 0.2.5
author = William Otieno
author_email = [email protected]
description = Python Wrapper for interacting with the Flutterwave Payments API
Expand Down

0 comments on commit e2821e5

Please sign in to comment.