Esempio n. 1
0
def pay_query(request):
    '''
    当面付结果查询逻辑
    rtype:
        json
    '''
    context = {}
    user = request.user
    trade_num = request.session['out_trade_no']
    paid = False
    # 等待1秒后再查询支付结果
    time.sleep(1)
    res = alipay.api_alipay_trade_query(out_trade_no=trade_num)
    if res.get("trade_status", "") == "TRADE_SUCCESS":
        paid = True
        amount = Decimal(res.get("total_amount", 0))
        # 生成对于数量的充值码
        code = MoneyCode.objects.create(number=amount)
        # 充值操作
        user.balance += code.number
        user.save()
        code.user = user.username
        code.isused = True
        code.save()
        # 将充值记录和捐赠绑定
        donate = Donate.objects.create(user=user, money=amount)
        # 后台数据库增加记录
        record = AlipayRecord.objects.create(username=user,
                                             info_code=trade_num,
                                             amount=amount,
                                             money_code=code)
        del request.session['out_trade_no']
        # 返回充值信息
        info = {
            'title': '充值成功!',
            'subtitle': '请去商品界面购买商品!',
            'status': 'success',
        }
        context['info'] = info

    # 如果三次还没成功择关闭订单
    if paid is False:
        alipay.api_alipay_trade_cancel(out_trade_no=trade_num)
        info = {
            'title': '支付查询失败!',
            'subtitle': '亲,确认支付了么?',
            'status': 'error',
        }
        context['info'] = info
    result = json.dumps(context, ensure_ascii=False)
    return HttpResponse(result, content_type='application/json')
Esempio n. 2
0
def pay_query(request):
    '''
    当面付结果查询逻辑
    rtype:
        json
    '''
    context = {}
    user = request.user
    trade_num = request.session['out_trade_no']
    paid = False
    # 等待1秒后再查询支付结果
    time.sleep(1)
    res = alipay.api_alipay_trade_query(out_trade_no=trade_num)
    if res.get("trade_status", "") == "TRADE_SUCCESS":
        paid = True
        amount = Decimal(res.get("total_amount", 0))
        # 生成对于数量的充值码
        code = MoneyCode.objects.create(number=amount)
        # 充值操作
        user.balance += code.number
        user.save()
        code.user = user.username
        code.isused = True
        code.save()
        # 将充值记录和捐赠绑定
        donate = Donate.objects.create(user=user, money=amount)
        # 后台数据库增加记录
        record = PayRecord.objects.create(username=user,
                                          info_code=trade_num, amount=amount, money_code=code)
        del request.session['out_trade_no']
        # 返回充值信息
        info = {
            'title': '充值成功!',
            'subtitle': '请去商品界面购买商品!',
            'status': 'success', }
        context['info'] = info

    # 如果三次还没成功择关闭订单
    if paid is False:
        alipay.api_alipay_trade_cancel(out_trade_no=trade_num)
        info = {
            'title': '支付查询失败!',
            'subtitle': '亲,确认支付了么?',
            'status': 'error', }
        context['info'] = info
    result = json.dumps(context, ensure_ascii=False)
    return HttpResponse(result, content_type='application/json')