Exemplo n.º 1
0
def callback():
    data = XMLData.parse(request.data)

    if data.return_code == 'SUCCESS' and data.result_code == 'SUCCESS':
        payment_no = data.out_trade_no
        transaction_id = data.transaction_id
        paid_at = datetime.strptime(data.time_end, '%Y%m%d%H%M%S')

        payment = Payment.get_by_payment_no(payment_no)
        if payment is not None and payment.status != Payment.STATUS_PAID:
            wechat_transaction = query_order_from_wechat(transaction_id)
            if wechat_transaction is None:
                return fail(HTTP_BAD_REQUEST)

            if (wechat_transaction.return_code == 'SUCCESS' and
                        wechat_transaction.result_code == 'SUCCESS'):

                remark = payment.remark
                remark['wechat_transaction_id'] = transaction_id
                payment.remark = remark

                payment.paid_at = paid_at
                payment.status = Payment.STATUS_PAID
                db.session.add(payment)

                if payment.payment_type == Payment.TYPE_RECHARGE:
                    pocket = Pocket.get_or_create_by_user_id(payment.user_id)
                    pocket.add(payment.amount)

                elif payment.payment_type == Payment.TYPE_BUY:
                    order = Order.get_by_payment_id(payment.id)
                    if order is None:
                        return fail(HTTP_BAD_REQUEST)

                    items = order.get_items()
                    for each in items:
                        if each.item_id is None:
                            item_id = gen_item_id(payment.user_id)
                            Collecation.create(
                                user_id=payment.user_id,
                                mode_id=Mode.get_default_id(),
                                item_id=item_id)
                            each.item_id = item_id
                            db.session.add(each)
                    order.status = Order.STATUS_PAYED
                    order.payment_method = Order.PAYED_WECHAT
                    db.session.add(order)

                db.session.commit()
                return success()

    return fail(HTTP_BAD_REQUEST)
Exemplo n.º 2
0
def get_collection_list():
    # json = {
    #     'page': 1,
    #     'size': 10,
    #     'put_in_status': 100,
    #     'stage': 100,
    # }

    if request.json is None:
        return fail(HTTP_OK, EMSG_PARAMS_MISSING)

    page = request.json.get('page')
    size = request.json.get('size')
    stage = request.json.get('stage')
    put_in_status = request.json.get('put_in_status')

    if not isinstance(page, int) or \
            not isinstance(size, int) or \
            not isinstance(stage, int):
        logger.error(
            "请求参数错误: page = {} size = {} stage = {} put_in_status = {}".format(
                page, size, stage, put_in_status))
        return fail(HTTP_OK, EMSG_PARAMS_ERROR)

    # 请求参数必须为正数
    if page <= 0 or size <= 0:
        msg = "请求参数错误: page = {} size = {} stage = {} put_in_status = {}".format(
            page, size, stage, put_in_status)
        logger.error(msg)
        return fail(HTTP_OK, msg)

    result_list = Collecation.get_collection_list(page, stage, put_in_status,
                                                  size)
    return success(result_list)
Exemplo n.º 3
0
def collecation():
    current_user = get_current_user()
    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')
    collecations = Collecation.get_multi_by_user(current_user.id)
    collecations = [each.as_resp() for each in collecations]
    return success(collecations)
Exemplo n.º 4
0
def delete_order(c_id):
    msg = u"删除订单失败: {}".format(c_id)
    try:
        is_success, msg = Collecation.delete(c_id)
        if is_success:
            return success(msg)
    except:
        logger.error("删除订单异常: {}".format(traceback.format_exc()))

    return fail(HTTP_OK, msg)
Exemplo n.º 5
0
def update_store():
    # json = {
    #     'goods_address': '423-10',
    #     'id': 1,
    # }
    if request.json is None:
        return fail(HTTP_OK, EMSG_PARAMS_MISSING)

    goods_address = request.json.get('goods_address')
    c_id = request.json.get('id')

    is_success, msg = Collecation.put_in(c_id, goods_address)
    if is_success:
        return success(msg)
    return fail(HTTP_OK, msg)
Exemplo n.º 6
0
def update_stage():
    # json = {
    #     'stage': 100,
    #     'id': 1,
    # }
    if request.json is None:
        return fail(HTTP_OK, EMSG_PARAMS_MISSING)

    stage = request.json.get('stage')
    c_id = request.json.get('id')

    is_success, msg = Collecation.update_stage(c_id, stage)
    if is_success:
        return success(msg)
    return fail(HTTP_OK, msg)
Exemplo n.º 7
0
def get_collection_num():
    # json = {
    #     'stage': 100,
    #     'put_in_status': 100,
    # }
    if request.json is None:
        return fail(HTTP_OK, u"参数错误")

    stage = request.json.get('stage')
    put_in_status = request.json.get('put_in_status')
    if stage is None or put_in_status is None:
        return fail(HTTP_OK, u"参数错误")

    try:
        return success(Collecation.get_collection_num(stage, put_in_status))
    except:
        logger.error("查询数据数目接口失败: stage = {} put_in_status = {} {}".format(
            stage, put_in_status, traceback.format_exc()))
    return fail(HTTP_OK, u"获取存储订单数目查询失败")
Exemplo n.º 8
0
def use_collecation():
    current_user = get_current_user()
    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    if request.json is None:
        return fail(HTTP_BAD_REQUEST, u'缺少参数')

    # 存储计费方式 现在默认是2块钱一天
    mode_id = request.json.get('mode_id')
    mode = Mode.get(mode_id)
    if mode is None:
        return fail(HTTP_BAD_REQUEST, u'参数错误')

    collecations = request.json.get('collecations')

    for each in collecations:
        id_ = each.get('id', None)
        if id_ is None:
            return fail(HTTP_BAD_REQUEST, u'参数错误')

    # 获得钱包信息
    pocket = Pocket.get_or_create_by_user_id(current_user.id)
    if pocket is None:
        return fail(HTTP_SERVER_ERROR, u'获取钱包信息失败')

    # 获取总金钱数目
    balance = pocket.balance
    total_fee = 0

    collection_list = []
    for each in collecations:
        c = Collecation.get(each.get('id'))
        if c is None:
            continue
        if c.status == Collecation.STATUS_USING:
            continue
        c.mode_id = mode_id
        c.status = Collecation.STATUS_USING

        # 存储入库时间
        c.requested_at = datetime.utcnow()

        # 用户添加的备注信息
        c.remark = each.get('remark')

        # 算总共需要的额度
        total_fee += Mode.get_mode_fee(mode_id)

        collection_list.append(c)

    # 如果总的消费大于余额  则不进行存储
    if total_fee > balance:
        return fail(HTTP_BAD_REQUEST,
                    u'您当前余额为 %0.2f 元,不足以支付此订单' % (pocket.balance * 1.0 / 100))

    # todo 求所有箱子的剩余天数
    # # 算出剩下来的钱
    # remain_fee = balance - total_fee
    #
    # # 计算已经入库的所有箱子的费用
    # coll_list = Collecation.get_used_coll_by_user(current_user.id)
    # cost_
    # for item in coll_list:

    # 开始存储订单
    for item in collection_list:
        db.session.add(item)
    db.session.commit()

    return success()
Exemplo n.º 9
0
def create_order():
    current_user = get_current_user()

    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    user_id = current_user.id

    if request.json is None:
        return fail(HTTP_BAD_REQUEST, EMSG_PARAMS_MISSING)

    # 产品ID?
    product_id = request.json.get('product_id')

    # 得先判断产品是否存在,你请求个没有的产品肯定不行
    product = Product.get(product_id)

    if product is None:
        return fail(HTTP_BAD_REQUEST, EMSG_PRODUCT_NOT_FOUND)

    # 地址ID
    address_id = request.json.get('address_id')

    # 支付方式
    payment_method = request.json.get('payment_method') or 'wechat'

    address = Address.get(address_id)
    if address is None:
        return fail(HTTP_BAD_REQUEST, EMSG_PARAMS_ERROR)

    if address.user_id != user_id:
        return fail(HTTP_BAD_REQUEST, EMSG_PARAMS_ERROR)

    # 买了多个箱子
    box_num = int(request.json.get('count'))
    if box_num <= 0:
        logger.warn("箱子数目不正确: {}".format(box_num))
        return fail(HTTP_BAD_REQUEST, u"箱子数目不正确")

    # 计算箱子的费用
    product_fee = product.price * box_num

    # 邮寄费用默认为0
    express_fee = EXPRESS_FEE

    order = Order.create(
        user_id=user_id,
        address_id=address_id,
        product_fee=product_fee,
        express_fee=express_fee,
        box_num=box_num
    )

    items = []
    for each in range(box_num):
        item = OrderItem.create(
            user_id=user_id,
            product_id=product_id,
            order_id=order.id
        )
        items.append(item)

    # 总费用
    total_fee = product_fee + express_fee

    is_success, rv = False, None

    if payment_method == Order.PAYED_WECHAT:
        payment = Payment.create(
            user_id=user_id,
            amount=total_fee,
            payment_type=Payment.TYPE_BUY,
        )

        order.payment_id = payment.id
        db.session.add(order)
        db.session.commit()

        # 这里是微信支付的流程
        is_success, rv = create_jsapi_params(current_user.openid, payment)

    elif payment_method == Order.PAYED_CRASH:
        pocket = Pocket.get_or_create_by_user_id(user_id)
        if pocket.balance >= total_fee:
            pocket.cut_down(total_fee)
            db.session.commit()

            # 不明白为什么现金买的 就有详细订单,微信支付的就没有详细订单了
            for each in items:
                item_id = gen_item_id(user_id)
                Collecation.create(
                    user_id=user_id,
                    mode_id=Mode.get_default_id(),
                    item_id=item_id)
                each.item_id = item_id
                db.session.add(each)

            order.status = Order.STATUS_PAYED
            order.payment_method = payment_method
            db.session.add(order)
            is_success = True
        else:
            return fail(HTTP_BAD_REQUEST,
                        u'您当前余额为 %0.2f 元,不足以支付此订单' %
                        (pocket.balance * 1.0 / 100))

    if is_success:
        return success(rv)
    else:
        if rv:
            logger.warn(rv)
        return fail(HTTP_BAD_REQUEST, u'购买失败')