views.py
def initiat_payment(request):
if request.method == "POST":
payment_form = forms.PaymentForm(request.POST)
if payment_form.is_valid():
payment = payment_form.save()
render(request, 'checkout.html', {'payment_form':payment_form})
else:
payment_form = forms.PaymentForm()
total_amt=0
if 'cartdata' in request.session:
for p_id,item in request.session['cartdata'].items():
total_amt+=int(item['qty'])*float(item['price'])
render(request, 'checkout.html',{'cart_data':request.session['cartdata'],'totalitems':len(request.session['cartdata']),'total_amt':total_amt})
return render(request, 'checkout.html', {'payment_form':payment_form,'total_amt':total_amt})
models.py
PAYMENT_OPTIONS=(('Cash','Cash On Delivery'),('Transfer','Direct Bank Transfer'),('Paystack','Pay Online [Secured]'))
class Payment(models.Model):
ref = models.CharField(max_length=30)
customer = models.CharField(max_length=300, null=True)
Shipping_address = models.CharField(max_length=200)
Hostel_name = models.CharField(max_length=50)
amount=models.CharField(max_length=100)
Payment_option= models.TextField(choices=PAYMENT_OPTIONS, default='Cash')
date_created = models.DateTimeField(default=now)
verified = models.BooleanField(default=False, null=True)
forrms.py
class PaymentForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["Payment_option"].widget.attrs.update({
'required':'',
'class':"checkout__payment__detail",
})
class Meta:
model=Payment
fields=('amount','Payment_option','customer')
type or paste code here