In this short tutorial am going to show you how to connect flutterwave api to your Django application to be able to collect mobile money payments, for Uganda. But for other countries the procedure varies.
First you will install the flutter wave python package , I will assume you have python and Django installed. If you are using python only, not with Django the logic in this tutorial should be easy to follow.
pip install rave_python
Add this view to your views.py, The explanation is below.
from rave_python import Rave, RaveExceptions, Misc
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
rave = Rave("ENTER_YOUR_PUBLIC_KEY", "ENTER_YOUR_SECRET_KEY", usingEnv = False)
@login_required
def billing(request):
context = {}
if request.method == "POST":
amount = int(request.POST['amount'])
number = request.POST['number']
email = request.POST['email']
mini = 20000
if amount >= mini:
# mobile payload
payload = {
"amount": amount,
"email": email,
"phonenumber": number,
"redirect_url": "http://127.0.0.1:8000/webhook",
"IP": ""
}
try:
res = rave.UGMobile.charge(payload)
if res['status'] == 'success':
return redirect(res['link'])
except RaveExceptions.TransactionChargeError as e:
print(e.err)
print(e.err["flwRef"])
messages.error(request, f'{e.err["flwRef"]}')
except RaveExceptions.TransactionVerificationError as e:
print(e.err["errMsg"])
print(e.err["txRef"])
messages.error(request, f'{e.err["errMsg"]} - {e.err["txRef"]}')
else:
messages.error(request, f"Amount is less than the minimum ({mini})")
return render(request, 'user-billing.html', context)
context = {}
return render(request, 'user-billing.html', context)
The code above is a view function that connects to a form in the frontend, where the user has to type their details for payment, that is the mobile number, email and the amount they want to pay.
When a post request is initiated some checks are done and the user is redirected to the link sent to by flutterwave for verification, it is found in res[‘link’]. Once you get this link you have to redirect the user to it, so that they can enter the OTP sent to their number. The rest you can see from the code. This “redirect_url”: “http://127.0.0.1:8000/webhook”, has to be designed as a webhook, for those who don’t know what a webhook is, It is in simple terms an endpoint where information is sent to. In this context it is the end point where the information about the success of the transaction is sent to, so that you do something with it, like allow the user to access the service you are charging them for.
Now you have to design the webhook function , and don’t forget to register the view in the urls.py file in Django directory
# WEBHOOK VIEW, DONT FORGET TO ADD IT IN THE URLS.PY
@login_required
def payment(request):
if request.method == 'GET':
resp = request.GET['resp']
resp = json.loads(resp)
state = resp['status']
if rave.UGMobile.verify(resp["txRef"]):
# Do something
return HttpResponse("404")
And that is it you can be able to access mobile money payments.
DONT FORGET
- To get you public and secret keys from the flutterwave dashboard
- To register you webhook view and add it in the apayload code
- To verify the transaction, this is for security purposes
- The OTP for test environment is 123456, but it may change