def get(self, request): """ 处理支付宝的return_url返回 """ processed_dict = {} # 1. 获取GET中参数 for key, value in request.GET.items(): processed_dict[key] = value # 2. 取出sign sign = processed_dict.pop("sign", None) # 3. 生成ALipay对象 alipay = AliPay.getAlipay() verify_re = alipay.verify(processed_dict, sign) # 这里可以不做操作。因为不管发不发return url。notify url都会修改订单状态。 if verify_re is True: order_sn = processed_dict.get('out_trade_no', None) trade_no = processed_dict.get('trade_no', None) existed_orders = OrderInfo.objects.filter(order_sn=order_sn) for existed_order in existed_orders: existed_order.pay_status = '已支付' existed_order.trade_no = trade_no existed_order.pay_time = datetime.now() existed_order.save() return HttpResponse({'订单已支付完成'})
def get_alipay_url(self, obj): alipay = AliPay.getAlipay() url = alipay.direct_pay(subject=obj.order_sn, out_trade_no=obj.order_sn, total_amount=obj.order_mount, return_url=RETURN_URL) re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format( data=url) return re_url
def post(self, request): """ 处理支付宝的notify_url """ #存放post里面所有的数据 processed_dict = {} #取出post里面的数据 for key, value in request.POST.items(): processed_dict[key] = value #把signpop掉,文档有说明 sign = processed_dict.pop("sign", None) #生成一个Alipay对象 alipay = AliPay.getAlipay() #进行验证 verify_re = alipay.verify(processed_dict, sign) # 如果验签成功 if verify_re is True: #商户网站唯一订单号 order_sn = processed_dict.get('out_trade_no', None) #支付宝系统交易流水号 trade_no = processed_dict.get('trade_no', None) # 查询数据库中订单记录 existed_orders = OrderInfo.objects.filter(order_sn=order_sn) for existed_order in existed_orders: # 订单商品项 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.pay_status = '已支付' existed_order.trade_no = trade_no existed_order.pay_time = datetime.now() existed_order.save() #需要返回一个'success'给支付宝,如果不返回,支付宝会一直发送订单支付成功的消息 return Response("success")