Friday, 20 August 2021

unable to show success page after completion of payment

i am using razorpay payment gateway web integration for my learning eccomerce project everyhing work fine payment can be done all i want is to store some information after payment done in my order model using test mode

my views.py for checkout

 class Checkout_Pre(View):
        def post (self, request,):
            user = request.user
            address = Address.objects.filter(default=True, user=request.user)
            cart = request.session.get('cart')
            items = Item.get_items_by_id(list(cart.keys()))
            prefer = request.POST.get('payment')
            total_price = request.POST.get('paying_price')
            total_item_price = json.loads(total_price)
            with transaction.atomic():
                order = Order.objects.create(
                        user=user,
                        total_price=total_item_price,
                        address=address.first(),
                        method = prefer,
                        )
                for item in items:
                    item_order = OrderItem.objects.create(
                        order=order,
                        item=item,
                        size=cart.get(str(item.id)),
                        price=item.price,
                        )
                request.session['cart'] = {}   
            order_currency = 'INR'
    
            callback_url = 'http://'+ str(get_current_site(request))+"/handlerequest/"
            print(callback_url)
            notes = {'order-type': "Basic order from the coolbuy website", 'key':'value'}
            razorpay_order = razorpay_client.order.create(dict(amount=total_item_price*100, currency=order_currency, notes = notes, receipt=order.order_id, payment_capture='0'))
            print(razorpay_order['id'])
            order.razorpay_order_id = razorpay_order['id']
            order.save()
               
            return render(request, 'payment/razorpaypaymentsummary.html', {'order':order, 'order_id': razorpay_order['id'], 'orderId':order.order_id, 'final_price':total_item_price, 'razorpay_merchant_id':settings.razorpay_id, 'callback_url':callback_url,})

so what the error is after payment completion i want to show html page and change sttus in db but after payment completion the handlerequest throwinh me http 505 error

@csrf_exempt
def handlerequest(request):
    if request.method == "POST":
        try:
            payment_id = request.POST.get('razorpay_payment_id', '')
            order_id = request.POST.get('razorpay_order_id','')
            signature = request.POST.get('razorpay_signature','')
            params_dict = { 
            'razorpay_order_id': order_id, 
            'razorpay_payment_id': payment_id,
            'razorpay_signature': signature
            }
            try:
                order_db = Order.objects.get(razorpay_order_id=order_id)
            except:
                return HttpResponse("505 Not Found")
            order_db.razorpay_payment_id = payment_id
            order_db.razorpay_signature = signature
            order_db.save()
            result = razorpay_client.utility.verify_payment_signature(params_dict)
            if result==None:
                amount = order_db.total_amount * 100   #we have to pass in paisa
                try:
                    razorpay_client.payment.capture(payment_id, amount)
                    order_db.payment_status = 1
                    order_db.save()
                    return render(request, 'firstapp/payment/paymentsuccess.html',{'id':order_db.id})
                except:
                    order_db.payment_status = 2
                    order_db.save()
                    return render(request, 'firstapp/payment/paymentfailed.html')
            else:
                order_db.payment_status = 2
                order_db.save()
                return render(request, 'firstapp/payment/paymentfailed.html')
        except:
            return HttpResponse("505 not found") 

any idea what i am doing wrong here in my handlre request view

order model

class Order(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE,)
    address = models.ForeignKey(Address, default= True, on_delete=models.CASCADE)
    status = models.IntegerField(choices = status_choices, default=1)
    method = models.CharField(max_length=50, blank=False,)
    total_price = models.FloatField(blank=False, default=0)
    payment_status = models.IntegerField(choices = payment_status_choices, default=3)
    order_id = models.CharField(unique=True, max_length=200, null=True, default=None) 
    datetime_of_payment = models.DateTimeField(default=timezone.now)
    created_at = models.TimeField(auto_now=True, editable=False)
    razorpay_order_id = models.CharField(max_length=1000, null=True, blank=True)
    razorpay_payment_id = models.CharField(max_length=1000, null=True, blank=True)
    razorpay_signature = models.CharField(max_length=1000, null=True, blank=True)

    def __str__(self):
        return self.user.username + " " + str(self.order_id)

    def save(self, *args, **kwargs):
            if self.order_id is None and self.datetime_of_payment:
                self.order_id = self.datetime_of_payment.strftime('ORDER%Y%m%dODR') 
                return super(Order,self).save(*args, **kwargs)
            


from unable to show success page after completion of payment

No comments:

Post a Comment