37
loading...
This website collects cookies to deliver better user experience
views.py
and add the following code to it below what we already have:def product_detail(request, pk):
data = Product.objects.get(id=pk)
ctx={
'product':data
}
return render(request,
'product.html',
ctx)
urls.py
and add a path like so:path('product/<int:pk>/details', product_detail, name='details'),
forms.py
:touch electronics/forms.py
from django import forms
class PaymentForm(forms.Form):
name = forms.CharField(label='Your name', max_length=100)
email = forms.EmailField()
phone=forms.CharField(max_length=15)
amount = forms.FloatField()
def product_detail(request, pk):
data = Product.objects.get(id=pk)
if request.method=='POST':
form = PaymentForm(request.POST)
if form.is_valid():
name= form.cleaned_data['name']
email = form.cleaned_data['email']
amount = form.cleaned_data['amount']
phone = form.cleaned_data['phone']
return redirect(str(process_payment(name,email,amount,phone)))
else:
form = PaymentForm()
ctx={
'product':data,
'form':form
}
return render(request,
'product.html',
ctx)
views.py
add the code below to it.def process_payment(name,email,amount,phone):
auth_token= env('SECRET_KEY')
hed = {'Authorization': 'Bearer ' + auth_token}
data = {
"tx_ref":''+str(math.floor(1000000 + random.random()*9000000)),
"amount":amount,
"currency":"KES",
"redirect_url":"http://localhost:8000/callback",
"payment_options":"card",
"meta":{
"consumer_id":23,
"consumer_mac":"92a3-912ba-1192a"
},
"customer":{
"email":email,
"phonenumber":phone,
"name":name
},
"customizations":{
"title":"Supa Electronics Store",
"description":"Best store in town",
"logo":"https://getbootstrap.com/docs/4.0/assets/brand/bootstrap-solid.svg"
}
}
url = ' https://api.flutterwave.com/v3/payments'
response = requests.post(url, json=data, headers=hed)
response=response.json()
link=response['data']['link']
return link
.env
file. Thus create a file in 'electronics' called .env
and add the code below:SECRET_KEY='YOUR FLUTTERWAVE SECRET KEY'
.env
file you earlier created.views.py
to initialize our environment variables:import environ
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()
/tx_ref=ref&transaction_id=30490&status=successful
. Once we have this response data we can verify transaction status and provide value to our customers like saving it in the database and providing feedback to users. In this case we are going to simply print the response on the console. Feel free to play around with the response.views.py
:from django.views.decorators.http import require_http_methods
from django.http import HttpResponse
@require_http_methods(['GET', 'POST'])
def payment_response(request):
status=request.GET.get('status', None)
tx_ref=request.GET.get('tx_ref', None)
print(status)
print(tx_ref)
return HttpResponse('Finished')
urls.py
like so:from os import name
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from electronics.views import list_products, payment_response, product_detail
urlpatterns = [
path('admin/', admin.site.urls),
path('', list_products, name='list'),
path('product/<int:pk>/details', product_detail, name='details'),
path('callback', payment_response, name='payment_response')
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)