From 1276356faac102b89177ea2ae7fa8c1a202e6b60 Mon Sep 17 00:00:00 2001 From: WilliamOtieno Date: Thu, 30 Dec 2021 21:19:06 +0300 Subject: [PATCH] Version 0.1.4 - Added MPESA Payments - Refactored previous method calls (No major change) - Added simple build script --- README.md | 13 ++++++++++++- build_script.sh | 6 ++++++ python_flutterwave/payment.py | 33 +++++++++++++++++++++++++++++---- setup.cfg | 2 +- 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 build_script.sh diff --git a/README.md b/README.md index e3d25a8..84bc721 100644 --- a/README.md +++ b/README.md @@ -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', @@ -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='johndoe@gmail.com', phone_number='1234567890', + full_name='John Doe') +print(mpesa_trans_details) ``` diff --git a/build_script.sh b/build_script.sh new file mode 100644 index 0000000..11e9a22 --- /dev/null +++ b/build_script.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +rm -rf dist/ +rm -rf *.egg-info + +python -m build diff --git a/python_flutterwave/payment.py b/python_flutterwave/payment.py index 243072d..8ee9f67 100644 --- a/python_flutterwave/payment.py +++ b/python_flutterwave/payment.py @@ -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": { @@ -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 @@ -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()) diff --git a/setup.cfg b/setup.cfg index f957c0b..b4dca13 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = python-flutterwave -version = 0.0.4 +version = 0.1.4 author = William Otieno author_email = jimmywilliamotieno@gmail.com description = Python Wrapper for interacting with the Flutterwave Payments API