gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:05:30 24 4
gpt4 key购买 nike

我正在为我的学习电子商务项目使用 razorpay 支付网关网络集成 一切工作都可以完成罚款支付 我想要的是在使用测试模式在我的订单模型中完成付款后存储一些信息
我的 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页面并更改db中的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")
知道我在处理请求 View 中做错了什么
订单模型
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)

最佳答案

问题不在于您的 View ,而在于您的模型的保存方法。
在其他部分你什么都不做。你应该一般地调用 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)

关于python - 支付完成后无法显示成功页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68676653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com