diff --git a/README.md b/README.md index 84bc721..4015573 100644 --- a/README.md +++ b/README.md @@ -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. ``` @@ -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='johndoe@gmail.com', + phone_number='789456123', full_name='John Doe', account_bank='057') +print(details) +``` diff --git a/python_flutterwave/payment.py b/python_flutterwave/payment.py index 8ee9f67..a3ee1d7 100644 --- a/python_flutterwave/payment.py +++ b/python_flutterwave/payment.py @@ -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" @@ -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" @@ -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()) diff --git a/setup.cfg b/setup.cfg index b4dca13..d163e6b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = python-flutterwave -version = 0.1.4 +version = 0.2.5 author = William Otieno author_email = jimmywilliamotieno@gmail.com description = Python Wrapper for interacting with the Flutterwave Payments API