def run(): while True: try: Order.check_order_process() except Exception as e: logging.error(traceback.format_exc()) db.session.rollback() logging.error('Engine.run error exception: {}'.format(e)) db.session.close() time.sleep(10)
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()
def all_orders(): if not request.user.is_employee(): return Response(code=ResponseCode.OPERATE_ERROR, msg='没有权限').out() max_id = request.args.get('max_id', 0) orders = Order.query(fetchone=False, state=Order.State.FINISHED, orderby='id asc', extra={'id>': max_id}) resp = [] for each in orders: data = Order(**each).to_dict() user = User.find(each['uid']) data['user_name'] = user.name resp.append(data) return Response(data=resp).out()
def order_creator(channel, method, properties, body): try: order = Order.from_json(body) order.save() print("[x] Received message to create order to user: {}".format( order.customer.email)) except Exception as ex: print(f"An error occurred while trying to save your order in db {ex}")
def order(order_id=0): order = Order.find(order_id) if order.uid != request.user.id: return Response(code=ResponseCode.OPERATE_ERROR, msg='没有权限查看他人订单').out() resp = order.to_dict() resp['user_name'] = request.user.name time.sleep(0.5) return Response(data=resp).out()
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='参数错误'))
def post(self, order_number): parser = reqparse.RequestParser() parser.add_argument('security_password', type=str, required=True, nullable=False, location='json') parsed_args = parser.parse_args() if not g.current_user.security_password: abort(400, code=1012, message={'security_password': '******'}) if not g.current_user.verify_security_password( parsed_args['security_password']): abort(400, code=1002, message={ 'security_password': '******' }) order = Order.query.filter(Order.side == g_order_side.SELL, Order.number == order_number, Order.user_id == g.current_user.id).first() if order is None: abort(400, code=1001, message={'order_number': 'order does not exist'}) match_order = order.match_order if match_order is None: abort(400, code=1001, message={'match_order': 'order does not match'}) match_order.updated_at = datetime.datetime.now() ret, reason = Order.confirm_order(match_order) if not ret: abort(400, code=reason['code'], message=reason['message']) confirm_order = reason db.session.add(confirm_order) db.session.commit() return order
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='请微信关注服务号'))
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='请微信关注服务号'))
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='请微信关注服务号'))
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='请微信关注服务号'))
from typing import List from app.model.order import Order order1 = Order("1", 100.0) order2 = Order("2", 150.0) orders = [order1, order2] class OrderRepository: def get_orders(self) -> List[Order]: return orders def save_order(self, order: Order): orders.append(order) order_repository = OrderRepository()
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='请微信关注服务号'))
def orders(): orders = Order.query(fetchone=False, uid=request.user.id, orderby='id desc') return Response(data=[Order(**each).to_dict() for each in orders]).out()
def __init__(self): self.products = Products() self.categories = Categories() self.order = Order()
def put(self, order_number): parser = reqparse.RequestParser() parser.add_argument('status', type=int, required=True, choices=(g_order_status.CONFIRMED, g_order_status.CANCELLED), location='json') parser.add_argument('side', type=int, default=g_order_side.SELL, choices=(g_order_side.BUY, g_order_side.SELL), location='json') parsed_args = parser.parse_args() order = Order.query.filter(Order.number == order_number, Order.side == parsed_args['side']).first() if order is None: abort(400, code=1001, message={'order_id': 'order does not exist'}) if (order.status == g_order_status.PENDING or order.status == g_order_status.MATCH or g_order_status.PAID) and \ parsed_args['status'] == g_order_status.CANCELLED: if parsed_args['side'] == g_order_side.SELL: match_order = MatchOrder.query.filter( MatchOrder.sell_number == order.number).first() else: match_order = MatchOrder.query.filter( MatchOrder.buy_number == order.number).first() if match_order is None: rows_changed = Order.query.filter( db.or_(Order.status == g_order_status.PENDING, Order.status == g_order_status.MATCH, Order.status == g_order_status.PAID), Order.number == order.number, Order.side == parsed_args['side']).update( dict(status=g_order_status.CANCELLED)) if not rows_changed: abort(400, code=1002, message={'status': 'status not allow'}) else: rows_changed = Order.query.filter( db.or_(Order.status == g_order_status.PENDING, Order.status == g_order_status.MATCH, Order.status == g_order_status.PAID), Order.side == g_order_side.SELL, Order.number == match_order.sell_number).update( dict(status=g_order_status.CANCELLED)) if not rows_changed: abort(400, code=1002, message={'status': 'status not allow'}) rows_changed = Order.query.filter( db.or_(Order.status == g_order_status.PENDING, Order.status == g_order_status.MATCH, Order.status == g_order_status.PAID), Order.side == g_order_side.BUY, Order.number == match_order.buy_number).update( dict(status=g_order_status.CANCELLED)) if not rows_changed: abort(400, code=1002, message={'status': 'status not allow'}) if parsed_args['side'] == g_order_side.SELL: detail = {'message': '后台取消挂单释放'} assets = Assets.get_assets(order.user_id) assets.update_community_balance(order.amount, g_assets_record_type.SELL, detail) elif order.status == g_order_status.PAID and parsed_args[ 'status'] == g_order_status.CONFIRMED: if parsed_args['side'] == g_order_side.SELL: match_order = MatchOrder.query.filter( MatchOrder.sell_number == order.number).first() else: match_order = MatchOrder.query.filter( MatchOrder.buy_number == order.number).first() if match_order is None: abort(400, code=1001, message={'order_id': 'mach order does not exist'}) ret, reason = Order.confirm_order(match_order) if not ret: abort(400, code=reason['code'], message=reason['message']) confirm_order = reason db.session.add(confirm_order) else: message = { g_order_status.CANCELLED: g_order_status.PENDING, g_order_status.CONFIRMED: g_order_status.PAID } abort(400, code=1002, message={ 'status': 'status != %d' % message[parsed_args['status']] }) db.session.commit() return order
def put(self): args = order_parser.parse_args() new_order = Order(**args) order_repository.save_order(new_order) return new_order