支付状态
来源:11-7 drf的缓存设置
慕妹4516714
2021-08-11
老师,我这里支付可以成功,就是支付后支付状态未更新,数据库也没有改变。
class AliPayView(APIView):
def get(self, request):
"""
处理支付宝的return_url
"""
processed_dict = {}
for key, value in request.GET.items():
processed_dict[key] = value
sign = processed_dict.pop("sign", None)
alipay = AliPay(
appid=appid,
app_notify_url="http://127.0.0.1:8000/alipay/return/",
app_private_key_path=private_key_path,
alipay_public_key_path=ali_pub_key_path,
debug=True,
return_url="http://127.0.0.1:8000/alipay/return/"
)
verify_result = alipay.verify(processed_dict, sign)
if verify_result is True:
order_sn = processed_dict.get('out_trade_no', None)
trade_no = processed_dict.get('trade_no', None)
trade_status = processed_dict.get('trade_status', None)
existed_orders = OrderInfo.objects.filter(order_sn=order_sn)
for existed_order in existed_orders:
existed_order.pay_status = trade_status
existed_order.trade_no = trade_no
existed_order.pay_time = datetime.now()
existed_order.save()
response = redirect("index")
response.set_cookie("nextPath", "pay", max_age=3)
return response
else:
response = redirect("index")
return response
def post(self, request):
"""
处理支付宝的notify_url
"""
processed_dict = {}
for key, value in request.POST.items():
processed_dict[key] = value
sign = processed_dict.pop("sign", None)
print(processed_dict)
alipay = AliPay(
appid=appid,
app_notify_url="http://127.0.0.1:8000/alipay/return/",
app_private_key_path=private_key_path,
alipay_public_key_path=ali_pub_key_path,
debug=True,
return_url="http://127.0.0.1:8000/alipay/return/"
)
verify_result = alipay.verify(processed_dict, sign)
if verify_result is True:
order_sn = processed_dict.get('out_trade_no', None)
trade_no = processed_dict.get('trade_no', None)
trade_status = processed_dict.get('trade_status', None)
# 更新数据库订单状态
"""
OrderInfo.objects.filter(order_sn=order_sn).update(
trade_no=trade_no, # 更改交易号
pay_status=trade_status, # 更改支付状态
pay_time=timezone.now() # 更改支付时间
)
"""
existed_orders = OrderInfo.objects.filter(order_sn=order_sn)
for existed_order in existed_orders:
existed_order.pay_status = trade_status
existed_order.trade_no = trade_no
existed_order.pay_time = timezone.now()
order_goods = existed_order.goods.all()
for order_good in order_goods:
# 获取订单中商品和商品数量,然后将商品的销量进行增加
goods = order_good.goods
goods.sold_num += order_good.goods_num
goods.save()
existed_order.save()
return Response("success")
这是代码,报错pay_status’ cannot be null
如果把get方法里判断状态的注释,不报错,但状态依旧未修改
if verify_result is True:
# order_sn = processed_dict.get('out_trade_no', None)
# trade_no = processed_dict.get('trade_no', None)
# trade_status = processed_dict.get('trade_status', None)
#
# existed_orders = OrderInfo.objects.filter(order_sn=order_sn)
# for existed_order in existed_orders:
# existed_order.pay_status = trade_status
# existed_order.trade_no = trade_no
# existed_order.pay_time = datetime.now()
# existed_order.save()
response = redirect("index")
return response
else:
response = redirect("index")
return response
写回答
1回答
-
bobby
2021-08-12
pay_status报错是mysql报的错。说你保存到mysql中的时候并没有设置pay_status,你有没有debug看支付后能不能跳转到这个逻辑中执行?
022021-08-13
Python前后端分离开发Vue+Django REST framework实战
Django REST framework课程视频,RESTFul API前后端分离开发
2873 学习 · 2457 问题
相似问题