支付完成后无法显示成功页面

unable to show success page after completion of payment

我正在为我的学习 eccomerce 项目使用 razorpay 支付网关 web 集成一切工作罚款支付可以完成我想要的只是在使用测试模式在我的订单模型中完成支付后存储一些信息

我的views.py结帐

 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,})

付款完成后出现什么错误我想显示html页面并更改数据库中的sttus但是付款完成后handlerequest throwinh me http 505错误

@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") 

知道我在处理请求视图中做错了什么吗

订购型号

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)
            

在您的 post 方法中,在您分配

order.razorpay_order_id = razorpay_order['id']

你需要保存它。好像没了。

在您的结帐视图中

def get(self, request, *args, **kwargs):
        order = Order.objects.get(pk = self.kwargs['order'])
        settings = SiteSettings.objects.all().first()


        # Razorpay client auth
        client = razorpay.Client(auth = (settings.razorpay_client_id, settings.razorpay_secret))
        
        if order.razorpay_order_id is None:    
            order_amount = order.total*100
            order_currency = settings.currency
            order_receipt = 'Reciept'
            notes = {'id': order.id, 'email': order.email_id, 'date':str(datetime.now()), 'amount': order.total, 'is_ios': order.is_ios, 'is_android': order.is_android}   # OPTIONAL
            data = {"amount":order_amount, "currency":order_currency, "receipt":order_receipt, "notes":notes, "payment_capture":'0'}
            razorpay_order = client.order.create(data = data)

            # Razorpay order inserted into database order
            order.razorpay_order_id = razorpay_order["id"]
            order.save()

        else:
            razorpay_order = client.order.fetch(order.razorpay_order_id)


        return render(request, self.template_name, {'settings':settings, 'order':order, 'razorpay_order': razorpay_order})

    def post(self, request, *args, **kwargs):
        o = self.kwargs['order']+ ' POST'
        
        settings = SiteSettings.objects.all().first()
        return render(request, self.template_name, {'settings':settings, 'order':o})

在你的处理程序中

    settings    = SiteSettings.objects.all().first()
    order       = Order.objects.get(id=request.POST.get('order_id'))

    #Razorpay authentication
    client = razorpay.Client(auth = (settings.razorpay_client_id, settings.razorpay_secret))
    
    #Razorpay transaction details
    razorpay_transaction_details = { 
        'razorpay_order_id':    request.POST.get('razorpay_order_id'),
        'razorpay_payment_id':  request.POST.get('razorpay_payment_id'),
        'razorpay_signature':   request.POST.get('razorpay_signature')
    }

    #Razorpay payment verification
    client.utility.verify_payment_signature(razorpay_transaction_details)
        
    #Razorpay capture payment Make sure you change this accordingly.
    payment_transaction_details = client.payment.capture(razorpay_transaction_details['razorpay_payment_id'], order.total*100, {"currency":"payment_currency"})
    order.order_description     = payment_transaction_details
    order.status                = 'paid'
    order.save()


    context = {'request': request.POST, 'order': order, 'settings':settings, 'UserData':UserData, 'Password': Password}

    return render(request, 'web/rpay-success.html', context)

你可以在其中包裹一个 try except 块来渲染失败。

问题不在于您的视图,而在于您模型的保存方法。 在其他部分你什么都不做。一般应该叫super吧

请修改保存功能如下,然后尝试:

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)