Esempio n. 1
0
def pay_order(order_id=0):
    order = Order.find(order_id)

    user = request.user
    if order.state != Order.State.NORMAL or user.balance + order.balance < 0 or user.coupon + order.coupon < 0:
        return str(Response(code=ResponseCode.DATA_NOT_EXIST, msg='订单已失效'))

    if order.money < 0:
        if user.openid:
            wxorder = WXOrder(user, order)
            tokens = wxorder.get_token()
            if not tokens:
                return str(
                    Response(code=ResponseCode.OPERATE_ERROR, msg='订单生成失败'))

            return str(Response(data=tokens))
        elif not user.openid:
            return str(
                Response(code=ResponseCode.AUTH_REQUIRED, msg='请微信关注服务号'))
        else:
            return str(Response(code=ResponseCode.PARAMETER_ERROR, msg='参数错误'))
    else:
        user.balance += order.balance
        user.coupon += order.coupon
        user.save()
        order.close()
        return Response().out()
Esempio n. 2
0
def payment_order():
    user = request.user
    item_id = request.args.get('item_id')
    item = PaymentItem.find(item_id)

    if user.openid or not item_id:
        order = Order(uid=user.id,
                      name=item.name,
                      money=-item.money,
                      balance=item.money + item.charge,
                      type=Order.Type.CHARGE)
        order.set_order_id()
        resp = order.save(return_keys=[Order.PKEY])
        order = Order.find(resp[Order.PKEY])

        wxorder = WXOrder(user, order)
        tokens = wxorder.get_token()
        if not tokens:
            return str(Response(code=ResponseCode.OPERATE_ERROR, msg='订单生成失败'))

        return str(Response(data={'order_id': order.id, 'order': tokens}))
    elif not user.openid:
        return str(Response(code=ResponseCode.AUTH_REQUIRED, msg='请微信关注服务号'))
    else:
        return str(Response(code=ResponseCode.PARAMETER_ERROR, msg='参数错误'))
Esempio n. 3
0
def _pay_cart_with_balance(carts):
    user = request.user
    money = 0
    name = ''

    for each in carts:
        pd = Product.find(each['product_id'])
        money += pd.price * each['num']
        if name:
            name = '%s,%sx%s' % (name, pd.name, each['num'])
        else:
            name = '%sx%s' % (pd.name, each['num'])

    if user.balance >= money:
        for each in carts:
            ct = Cart(**each)
            ct.state = Cart.State.FINISHED
            ct.save()

        order = Order(uid=user.id,
                      name=name,
                      balance=-money,
                      type=Order.Type.PAY)
        order.set_order_id()
        order.state = Order.State.FINISHED
        resp = order.save(return_keys=[Order.PKEY])
        order = Order.find(resp[Order.PKEY])

        user.balance -= money
        user.save()
        return Response(data=order.to_dict()).out()
    elif user.openid:
        order = Order(uid=user.id,
                      name=name,
                      money=user.balance - money,
                      balance=-user.balance,
                      type=Order.Type.PAY)
        order.set_order_id()
        resp = order.save(return_keys=[Order.PKEY])
        order = Order.find(resp[Order.PKEY])

        wxorder = WXOrder(user, order)
        tokens = wxorder.get_token()
        if not tokens:
            return Response(code=ResponseCode.OPERATE_ERROR,
                            msg='订单生成失败').out()

        return Response(code=ResponseCode.LOW_BALANCE,
                        msg='余额不足',
                        data={
                            'need_money': money - user.balance,
                            'order_id': order.id,
                            'order': tokens
                        }).out()
    else:
        return str(Response(code=ResponseCode.AUTH_REQUIRED, msg='请微信关注服务号'))
Esempio n. 4
0
def _pay_cart_with_coupon(carts):
    user = request.user

    if user.is_founder():
        discount = 0.4
    elif user.is_cofounder():
        discount = 0.3
    else:
        discount = 0.2

    money = 0
    name = ''
    for each in carts:
        pd = Product.find(each['product_id'])
        money += pd.price * each['num']
        if name:
            name = '%s,%sx%s' % (name, pd.name, each['num'])
        else:
            name = '%sx%s' % (pd.name, each['num'])

    discount_money = min(user.coupon, int(money * discount))
    need_money = money - discount_money

    if user.openid:
        order = Order(uid=user.id,
                      name=name,
                      money=-need_money,
                      coupon=-discount_money,
                      type=Order.Type.PAY)
        order.set_order_id()
        resp = order.save(return_keys=[Order.PKEY])
        order = Order.find(resp[Order.PKEY])

        wxorder = WXOrder(user, order)
        tokens = wxorder.get_token()
        if not tokens:
            return str(Response(code=ResponseCode.OPERATE_ERROR, msg='订单生成失败'))

        return str(
            Response(code=ResponseCode.LOW_BALANCE,
                     msg='余额不足',
                     data={
                         'need_money': need_money,
                         'order_id': order.id,
                         'order': tokens
                     }))
    else:
        return str(Response(code=ResponseCode.AUTH_REQUIRED, msg='请微信关注服务号'))
Esempio n. 5
0
def join_event(event_id=0):
    ev = Event.find(event_id)
    user = request.user
    user_ev = UserEvent.query(uid=user.id, event_id=ev.id)
    if user_ev and user_ev['state'] == UserEvent.State.INIT:
        return Response(code=ResponseCode.DUPLICATE_DATA, msg='已经报名成功').out()

    if ev.fee <= 0:
        if user_ev:
            user_ev = UserEvent(**user_ev)
            user_ev.state = UserEvent.State.INIT
            user_ev.save()
        else:
            UserEvent(uid=user.id, event_id=ev.id).save()

        return Response().out()
    elif user.openid:
        order = Order(uid=user.id,
                      name=ev.title,
                      money=-ev.fee,
                      item_id=ev.id,
                      type=Order.Type.JOIN_EVENT)
        order.set_order_id()
        resp = order.save(return_keys=[Order.PKEY])
        order = Order.find(resp[Order.PKEY])

        wxorder = WXOrder(user, order)
        tokens = wxorder.get_token()
        if not tokens:
            return Response(code=ResponseCode.OPERATE_ERROR,
                            msg='订单生成失败').out()

        return Response(code=ResponseCode.LOW_BALANCE,
                        msg='余额不足',
                        data={
                            'need_money': ev.fee,
                            'order_id': order.id,
                            'order': tokens
                        }).out()
    else:
        return str(Response(code=ResponseCode.AUTH_REQUIRED, msg='请微信关注服务号'))
Esempio n. 6
0
def _buy_product_with_coupon(product_id):
    pd = Product.find(product_id)
    user = request.user
    if user.is_founder():
        discount = 0.4
    elif user.is_cofounder():
        discount = 0.3
    else:
        discount = 0.2

    discount_money = min(user.coupon, int(pd.price * discount))
    need_money = pd.price - discount_money

    if user.openid:
        order = Order(uid=user.id,
                      name=pd.name,
                      money=-need_money,
                      coupon=-discount_money,
                      type=Order.Type.PAY)
        order.set_order_id()
        order.save()

        wxorder = WXOrder(user, order)
        tokens = wxorder.get_token()
        if not tokens:
            return str(Response(code=ResponseCode.OPERATE_ERROR, msg='订单生成失败'))

        return str(
            Response(code=ResponseCode.LOW_BALANCE,
                     msg='余额不足',
                     data={
                         'need_money': need_money,
                         'order': tokens
                     }))
    else:
        return str(Response(code=ResponseCode.AUTH_REQUIRED, msg='请微信关注服务号'))
Esempio n. 7
0
def _buy_product_with_balance(product_id):
    pd = Product.find(product_id)
    user = request.user

    if user.balance >= pd.price:
        order = Order(uid=user.id,
                      name=pd.name,
                      money=user.balance - pd.price,
                      balance=-user.balance,
                      type=Order.Type.PAY)
        order.set_order_id()
        order.save()

        user.balance -= pd.price
        user.save()

        return Response(data=pd.to_dict()).out()
    elif user.openid:
        order = Order(uid=user.id,
                      name=pd.name,
                      money=user.balance - pd.price,
                      balance=-user.balance,
                      type=Order.Type.PAY)
        order.set_order_id()
        order.save()

        order = WXOrder(user, order)

        tokens = order.get_token()
        if not tokens:
            return Response(code=ResponseCode.OPERATE_ERROR,
                            msg='订单生成失败').out()

        return Response(code=ResponseCode.LOW_BALANCE,
                        msg='余额不足',
                        data={
                            'need_money': pd.price - user.balance,
                            'order': tokens
                        }).out()
    else:
        return str(Response(code=ResponseCode.AUTH_REQUIRED, msg='请微信关注服务号'))
Esempio n. 8
0
def notify():
    result = WXOrder.notify(request.stream.read())
    if result:
        return '''<xml>