def put(args): """ openapi 取消订单操作(退款) @param id 商品ID 更改订单状态 """ order_id = args['order_id'] msg = '' try: order = Order.from_id({ 'webapp_user': args['webapp_user'], 'webapp_owner': args['webapp_owner'], 'order_id': order_id }) msg, success = order.refund() info = u"openapi apiserver中修改订单状态失败, order_id:{}, cause:\n{}".format( args['order_id'], msg) watchdog.info(info) if msg: return 500, {'msg': msg, 'success': False} else: return 200, {'msg': msg, 'success': True} except: notify_message = u"openapi apiserver中修改订单状态失败, order_id:{}, cause:\n{}".format( args['order_id'], unicode_full_stack()) watchdog.alert(notify_message) return 500, {'msg': msg, 'success': False}
def get_order_review_list(args, need_product_detail=False): """ 得到会员已完成订单中所有未评价的商品列表的订单,或者已评价未晒图的订单 PreCondition: webapp_user_id, member_id, PostCondition: orders @see 原始代码位置 `webapp/modules/mall/request_util.py` """ #webapp_user_id = args.webapp_user.id # 游客身份 #member_id = args.member.id # 会员身份 #member_id = args.webapp_user.member.id need_product_detail = args.get('need_product_detail') # 得到会员的所有已完成的订单 #orders = mall_models.Order.by_webapp_user_id(webapp_user_id).filter(status=5) orders = Order.get_finished_orders_for_webapp_user({ 'webapp_user': args['webapp_user'], 'webapp_owner': args['webapp_owner'], }) # 获取全部订单id # orderIds = [order.id for order in orders] #orderHasProducts = OrderHasProduct.objects.filter(order_id__in=orderIds) #totalProductIds = [orderHasProduct.product_id for orderHasProduct in orderHasProducts] totalProductIds = [] for order in orders: for product in order.products: totalProductIds.append(product.id) #ProductReviews = mall_models.ProductReview.objects.filter(order_has_product_id__in=orderHasProductIds) product_reviews = OrderReview.get_order_product_reviews( {'orders': orders}) all_product_review_pictures = [] for product_review in product_reviews: all_product_review_pictures.extend(product_review.pictures) #ProductReviewPictureIds = [product_review_picture.product_review_id for product_review_picture in all_product_review_pictures] if need_product_detail: # TODO: to be optimized """ OrderReview.__process_with_product_detail({ 'orders': orders, }) """ pass else: # TODO: to be optimized """ OrderReview.__process_without_product_detail({ 'orders': orders, }) """ return orders
def put(args): """ @warning: 对外网开放的支付接口,只接受从h5支付方式列表发起的货到付款,高风险,改动需慎重 """ webapp_user = args['webapp_user'] webapp_owner = args['webapp_owner'] order_id = args['order_id'].split('-')[0] pay_interface_type = int(args['pay_interface_type']) # 此接口只可使用货到付款 if pay_interface_type != mall_models.PAY_INTERFACE_COD: watchdog.alert('货到付款接口被异常调用,woid:%s,webapp_user_id:%s', (webapp_owner.id, webapp_user.id)) return 500, {} order = Order.from_id({ 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, 'order_id': order_id }) try: is_success, msg = order.pay( pay_interface_type=mall_models.PAY_INTERFACE_COD) except: is_success = False msg = unicode_full_stack() watchdog.alert(msg) return { 'order_id': args['order_id'], 'is_success': is_success, }
def get(args): """ 获取订单微信支付的参数信息 @param order_id """ order = None order_id = args['order_id'] if order_id.startswith('vip_'): #如果是会员卡的订单,则特殊处理 order = MemberCardPayOrder.from_order_id({ 'webapp_owner': args['webapp_owner'], 'webapp_user': args['webapp_user'], 'order_id': order_id }) else: #普通商城订单 order = Order.from_id({ 'webapp_owner': args['webapp_owner'], 'webapp_user': args['webapp_user'], 'order_id': order_id }) pay_info = order.pay_info_for_pay_module( pay_interface_type=mall_models.PAY_INTERFACE_WEIXIN_PAY) return pay_info
def get(args): """ 获取订单微信支付的package参数信息 @param order_id """ order = None order_id = args['order_id'] config = int(args['config']) if order_id.startswith('vip_'): #如果是会员卡的订单,则特殊处理 order = MemberCardPayOrder.from_order_id({ 'webapp_owner': args['webapp_owner'], 'webapp_user': args['webapp_user'], 'order_id': order_id }) else: #普通商城订单 order = Order.from_id({ 'webapp_owner': args['webapp_owner'], 'webapp_user': args['webapp_user'], 'order_id': args['order_id'].split('-')[0] }) package_info = order.wx_package_for_pay_module(config=config) return package_info
def get(args): """ 获取物流详情 @param order_id @return 结果示例: ``` { "code": 200, "data": { "express_details": [ { "status": "2", "express_id": 13241234, "created_at": "2015-12-22 17:04:54", "display_index": 2, "ftime": "2015-12-22", "context": "哈哈1", "time": "2015-12-22 17:04:39", "id": 2 }, { "status": "1", "express_id": 147258368, "created_at": "2015-12-22 16:22:39", "display_index": 1, "ftime": "2015-12-22", "context": "嘻嘻2", "time": "2015-12-22 16:22:15", "id": 1 } ] }, "errMsg": "", "innerErrMsg": "" } ``` """ webapp_owner = args['webapp_owner'] webapp_user = args['webapp_user'] order = Order.from_id({ 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, 'order_id': args['order_id'] }) express_details = order.express_details for detail in express_details: print(detail.id, detail.content, detail.status) data = [AExpressDetails.to_dict(detail) for detail in express_details] return { "order_id": args['order_id'], "status": order.status_text, "express_company_name": order.readable_express_company_name, "express_number": order.express_number, "express_details": data }
def get(args): """ 获取购物车项目 @param id 商品ID """ webapp_user = args['webapp_user'] webapp_owner = args['webapp_owner'] order_id = args['order_id'] interface_id = args['pay_id'] is_oauthed = ('code' in args) pay_interface = PayInterface.from_id({ 'webapp_owner': webapp_owner, 'interface_id': interface_id }) weixin_pay_config = pay_interface.pay_config order = Order.from_id({ 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, 'order_id': order_id }) data = {} data['app_id'] = weixin_pay_config['app_id'] data['partner_id'] = weixin_pay_config['partner_id'] data['partner_key'] = weixin_pay_config['partner_key'] data['paysign_key'] = weixin_pay_config['paysign_key'] data['app_secret'] = weixin_pay_config['app_secret'] data['pay_version'] = weixin_pay_config['pay_version'] data['total_fee_display'] = order.final_price data['total_fee'] = int(order.final_price * 100) if data['pay_version'] == 0 or (bool(is_oauthed) and data['pay_version'] != 0): product_outlines = order.product_outlines product_names = ','.join( [product_outline.name for product_outline in product_outlines]) if len(product_names) > 127: product_names = product_names[:127] if order.edit_money: data['order_id'] = '{}-{}'.format( order_id, str(order.edit_money).replace('.', '').replace('-', '')) else: data['order_id'] = order_id data['domain'] = settings.PAY_HOST data['webapp_owner_id'] = webapp_owner.id data['pay_interface_type'] = pay_interface.type data[ 'pay_interface_related_config_id'] = pay_interface.related_config_id data['product_names'] = product_names data['user_ip'] = '181.181.181.181' return data
def get(args): """ 获取支付结果页面 @param id 订单order_id """ webapp_user = args['webapp_user'] webapp_owner = args['webapp_owner'] order_id = args['order_id'].split('-')[0] order = Order.from_id({ 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, 'order_id': order_id }) if not order.is_valid(): msg = u'订单({})不存在'.format(order_id) error_msg = u'weixin pay, stage:[get_pay_result], result:{}, exception:\n{}'.format( msg, msg) watchdog.alert(error_msg) return 500, {'msg': error_msg} is_show_red_envelope = False red_envelope_rule_id = 0 red_envelope = webapp_owner.red_envelope if RedEnvelope.can_show_red_envelope(order, red_envelope): # 是可以显示分享红包按钮 is_show_red_envelope = True red_envelope_rule_id = red_envelope['id'] order_config = OrderConfig.get_order_config( {'webapp_owner': webapp_owner}) qrcode_img = webapp_owner.qrcode_img activity_url = '' if order.is_group_buy: activity_url = order.order_group_info[ 'activity_url'] + '&from=pay_result' return { 'is_trade_success': True, 'order': order.to_dict('is_group_buy'), 'is_show_red_envelope': is_show_red_envelope, 'red_envelope_rule_id': red_envelope_rule_id, 'qrcode_img': qrcode_img, 'activity_url': activity_url, 'order_config': order_config }
def get(args): # 兼容修改价格后订单从支付模块返回的跳转(支付模块会添加edit_money) order_id = args['order_id'].split('-')[0].split('^')[0] order = Order.from_id({ 'webapp_owner': args['webapp_owner'], 'webapp_user': args['webapp_user'], 'order_id': order_id }) order_data = AOrder.to_dict(order) if order_data['order'].has_key('sub_orders'): order_data['order']['sub_orders'] = sorted(order_data['order']['sub_orders'], key=lambda order:"%d-%d" % (order['supplier'], order['supplier_user_id'])) order_data.update({'mall_type': args['webapp_owner'].user_profile.webapp_type}) return order_data
def get(args): """ 获取订单微信支付的参数信息 @param order_id """ order = Order.from_id({ 'webapp_owner': args['webapp_owner'], 'webapp_user': args['webapp_user'], 'order_id': args['order_id'] }) pay_info = order.pay_info_for_pay_module( pay_interface_type=mall_models.PAY_INTERFACE_ALIPAY) return pay_info
def put(args): """ 微信支付、支付宝回调接口 @return: """ webapp_user = args['webapp_user'] webapp_owner = args['webapp_owner'] order_id = args['order_id'].split('-')[0] if order_id.startswith('vip_'): #如果是会员卡的订单,则特殊处理 order = MemberCardPayOrder.from_order_id({ 'webapp_owner': args['webapp_owner'], 'webapp_user': args['webapp_user'], 'order_id': order_id }) order.pay() is_success = True msg = '' else: #普通商城订单 pay_interface_type = int(args['pay_interface_type']) order = Order.from_id({ 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, 'order_id': order_id }) try: is_success, msg = order.pay( pay_interface_type=pay_interface_type) except: is_success = False msg = unicode_full_stack() watchdog.alert(msg) return { 'order_id': args['order_id'], 'is_success': is_success, 'msg': msg }
def post(args): """ 更改订单状态 @todo 目前取消订单和确认收货都是通过此接口,需要分离 """ # if not get_wapi_lock(lockname='order_post_' + str(args['webapp_user'].id), lock_timeout=2): # watchdog.alert('wapi接口被刷,wapi:%s,webapp_user_id:%s' % ('mall.order_post', str(args['webapp_user'].id))) # reason_dict = { # "is_success": False, # "msg": u'请勿短时间连续下单', # "type": "coupon" # 兼容性type # } # return 500, {'detail': [reason_dict]} # 兼容修改价格后订单从支付模块返回的跳转(支付模块会添加edit_money) order_id = args['order_id'].split('-')[0] try: order = Order.from_id({ 'webapp_user': args['webapp_user'], 'webapp_owner': args['webapp_owner'], 'order_id': order_id }) action = args['action'] validate_result, reason = order.validate_order_action(action, args['webapp_user'].id) msg = u"apiserver中修改订单状态失败, order_id:{}, action:{}, cause:\n{}".format(args['order_id'], args['action'], reason) watchdog.info(msg) if not validate_result: return 500, {'msg': reason} if action == 'cancel': order.cancel() elif action == 'finish': order.finish() except: notify_message = u"apiserver中修改订单状态失败, order_id:{}, action:{}, cause:\n{}".format(args['order_id'], args['action'], unicode_full_stack()) watchdog.alert(notify_message) return 500, ''
def __get_waiting_review_orders(self): webapp_owner = self.context['webapp_owner'] webapp_user = self.context['webapp_user'] orders = Order.get_finished_orders_for_webapp_user({ 'webapp_owner': webapp_owner, 'webapp_user': webapp_user }) waiting_review_orders = [] for order in orders: waiting_review_order = WaitingReviewOrder.get_for_order({ 'webapp_owner': webapp_owner, 'order': order, 'webapp_user': webapp_user }) if waiting_review_order: waiting_review_orders.append(waiting_review_order) self.orders = waiting_review_orders
def get(args): """ 会员订单列表 @param type """ webapp_user = args['webapp_user'] webapp_owner = args['webapp_owner'] count_per_page = int(args.get('count_per_page', DEFAULT_COUNT_PER_PAGE)) cur_page = int(args['cur_page']) order_type = int(args['order_type']) orders = Order.get_for_list_page({ 'order_type': order_type, 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, # 'cur_page': cur_page, # 'count_per_page': count_per_page }) # finished 1.团购 # finished 2.订单循环 # todo 3.评论 # todo 4.商品 order_id2group_info = Order.get_group_infos_for_orders({ 'orders': orders, 'woid': webapp_owner.id }) # 过滤已取消的团购订单,但优惠抵扣的显示 # orders = filter(lambda order: not(order.is_group_buy and order.status == mall_models.ORDER_STATUS_CANCEL) or order.pay_interface_type == mall_models.PAY_INTERFACE_PREFERENCE ,orders) orders = filter( lambda order: not (order_id2group_info[order.order_id] and order. status == mall_models.ORDER_STATUS_CANCEL) or order.pay_interface_type == mall_models.PAY_INTERFACE_PREFERENCE, orders) # 注意:所有排序、过滤操作需要在分页之前 pageinfo, orders = paginator.paginate(orders, cur_page, count_per_page) param_data = { 'woid': args['webapp_owner'].id, 'member_id': args['webapp_user'].member.id } get_order_review_json = [] resp = Resource.use('marketapp_apiserver').get({ 'resource': 'evaluate.get_order_evaluates', 'data': param_data }) if resp: code = resp["code"] if code == 200: get_order_review_json = resp["data"]['orders'] order_id2review = {} if get_order_review_json: for order_review in get_order_review_json: order_id2review[int(order_review["order_id"] )] = order_review["order_is_reviewed"] # order_id指的是order.id order_datas = [] for order in orders: if order_id2review.has_key(order.id): review_is_finished = order_id2review[order.id] else: review_is_finished = False data = { 'id': order.id, 'order_id': order.order_id, 'status': order.status, 'pay_interface_type': order.pay_interface_type, 'created_at': order.created_at.strftime('%Y.%m.%d %H:%M'), 'final_price': order.final_price, 'has_sub_order': order.has_sub_order, 'has_multi_sub_order': order.has_multi_sub_order, 'express_number': order.express_number, 'review_is_finished': review_is_finished, 'red_envelope': order.red_envelope, 'red_envelope_created': order.red_envelope_created, 'products': [], 'is_group_buy': bool(order_id2group_info[order.order_id]), 'order_group_info': order_id2group_info[order.order_id] } order_products = OrderProducts.get_for_order({ 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, 'order': order }) total_product_count = 0 for order_product in order_products.products: product_data = { 'id': order_product.id, 'name': order_product.name, 'purchase_count': order_product.purchase_count, 'thumbnails_url': order_product.thumbnails_url, 'model': order_product.model.to_dict() if order_product.model else None } total_product_count += order_product.purchase_count data['products'].append(product_data) data['product_count'] = total_product_count data['pay_info'] = order.pay_info order_datas.append(data) order_config = OrderConfig.get_order_config( {'webapp_owner': webapp_owner}) return { 'orders': order_datas, 'order_config': order_config, 'page_info': pageinfo.to_dict() }
def __get_order_message_dict(self, order_id): template_data = dict() order = Order.from_order_id({"order_id": order_id}) webapp_user = WebAppUser.from_id({"id": order.webapp_user_id}) if webapp_user: template_data['touser'] = webapp_user.openid template_data['template_id'] = self.template_id template_data[ 'url'] = '%small/order_detail/?woid=%s&order_id=%s' % ( settings.H5_HOST, self.owner_id, order.order_id) template_data['topcolor'] = "#FF0000" detail_data = {} #template_message_detail = template_message.template_message detail_data["first"] = { "value": self.first_text, "color": "#000000" } detail_data["remark"] = { "value": self.remark_text, "color": "#000000" } order.express_company_name = order.formated_express_company_name #u'%s快递' % express_util.get_name_by_value(order.express_company_name) if self.attribute: attribute_data_list = self.attribute.split(',') for attribute_datas in attribute_data_list: attribute_data = attribute_datas.split(':') key = attribute_data[0].strip() attr = attribute_data[1].strip() if attr == 'final_price' and getattr(order, attr): value = u'¥%s[实际付款]' % getattr(order, attr) detail_data[key] = {"value": value, "color": "#173177"} elif hasattr(order, attr): if attr == 'final_price': value = u'¥%s[实际付款]' % getattr(order, attr) detail_data[key] = { "value": value, "color": "#173177" } elif attr == 'payment_time': dt = datetime.now() payment_time = dt.strftime('%Y-%m-%d %H:%M:%S') detail_data[key] = { "value": payment_time, "color": "#173177" } else: detail_data[key] = { "value": getattr(order, attr), "color": "#173177" } else: if 'number' == attr: number = len(order.products) detail_data[key] = { "value": number, "color": "#173177" } if 'product_name' == attr: products = order.products product_names = ','.join( [p['name'] for p in products]) detail_data[key] = { "value": product_names, "color": "#173177" } template_data['data'] = detail_data print ">>>>>@>@@>@@>@>>$>$>$>>>>>", template_data return template_data
def create_order(self, purchase_info): """ 由PurchaseInfo创建订单 """ # 预定义变量 order = None price_free_resources = None price_related_resources = None try: create_order_lock_is_success = self.__acquire_create_order_lock_by_purchase_info( purchase_info) if not create_order_lock_is_success: watchdog.alert(u'下单异常并发') raise OrderResourcesLockException([{ "is_success": False, "msg": u'请勿短时间连续下单,error when get lock', "type": "coupon" # 兼容性type }]) webapp_owner = self.context['webapp_owner'] webapp_user = self.context['webapp_user'] # 创建空订单 order = Order.empty_order(webapp_owner, webapp_user) # 初始化,不需要资源信息 order = self.__init_order(order, purchase_info) # 初始化商品信息 order = self.__process_products(order, purchase_info) # 申请订单价无关资源 price_free_resources = self.__allocate_price_free_resources( order, purchase_info) logging.info( u"price_free_resources={}".format(price_free_resources)) # 填充order package_order_service = PackageOrderService( webapp_owner, webapp_user) # 如果is_success=False, 表示分配资源失败 order, is_success, reasons, price_related_resources = package_order_service.package_order( order, price_free_resources, purchase_info) if is_success: # 组装订单成功 #如果前端提交了积分使用信息,识别哪些商品使用了积分 # Todo 确认是否不需要 if purchase_info.group2integralinfo: group2integralinfo = purchase_info.group2integralinfo for product_group in order.product_groups: if not product_group.uid in group2integralinfo: product_group.disable_integral_sale() else: for product_group in order.product_groups: product_group.disable_integral_sale() # 保存订单 order = self.__save_order(order, purchase_info) # 如果需要(比如订单保存失败),释放资源 if order and order.is_saved: #删除购物车 # TODO: 删除购物车不应该放在这里 logging.warning('to clean the CART') if purchase_info.is_purchase_from_shopping_cart: for product in order.products: webapp_user.shopping_cart.remove_product(product) return order else: # 创建订单失败 logging.error( "Failed to create Order object or save the order! Release all resources. order={}" .format(order)) self.__release_price_related_resources(price_free_resources) # PackageOrderService分配资源失败,price_related_resources应为[],不需要release raise OrderResourcesException(reasons) except OrderResourcesException as e: raise e except ResponseCodeException: pass except: msg = unicode_full_stack() print ">>>>>>>>>>!!!!!!>>>>>>", msg watchdog.alert(msg) self.__release_order(order, price_free_resources, price_related_resources) raise OrderFailureException finally: self.__release_create_order_lock()