def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', order_id='', star='', sign='') if '' in (param.app_id, param.session, param.order_id, param.star, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign( [param.app_id, param.session, param.order_id, param.star]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_user = db.app_user.find_one({'uname': uname}, {'coupon': 1}) if db_user == None: # 不应该发生 return json.dumps({'ret': -5, 'msg': '未找到用户信息'}) # 订单打分 db.order_app.update_one({ 'order_id': param.order_id, 'user': uname }, { '$set': { 'star': int(param.star) }, '$push': { 'history': (app_helper.time_str(), uname, '订单打分') }, }) return json.dumps({'ret': 0, 'msg': '订单已打分!'}) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', secret='', sign='') if '' in (param.app_id, param.session, param.secret, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign( [param.app_id, param.session, param.secret]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_user = db.app_user.find_one({'uname': uname}, { 'coupon': 1, 'credit': 1 }) if db_user == None: # 不应该发生 return json.dumps({'ret': -5, 'msg': '未找到用户信息'}) # 返回 return json.dumps({ 'ret': 0, 'data': { 'credit': '%.2f' % db_user.get('credit', 0.0) } }) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', name='', tel='', addr='', sign='') if '' in (param.app_id, param.session, param.name, param.tel, param.addr, param.sign): return json.dumps({'ret' : -2, 'msg' : '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign([param.app_id, param.session, param.name, param.tel, param.addr]) if md5_str!=param.sign: return json.dumps({'ret' : -1, 'msg' : '签名验证错误'}) # 需要判断地址是否有对应门店,否则不在送货范围内 # app_helper.check_address() # 更新个人资料 new_addr = ( app_helper.my_rand(), param.name.strip(), param.tel.strip(), param.addr.strip(), int(time.time()) ) r = db.app_user.update_one({'uname':uname}, {'$push' : {'address' : new_addr}}) # 返回 return json.dumps({'ret' : 0, 'data' : { 'addr_id' : new_addr[0], }}) else: return json.dumps({'ret' : -4, 'msg' : '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', order_id='', sign='') if '' in (param.app_id, param.session, param.order_id, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign( [param.app_id, param.session, param.order_id]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_user = db.app_user.find_one({'uname': uname}, {'coupon': 1}) if db_user == None: # 不应该发生 return json.dumps({'ret': -5, 'msg': '未找到用户信息'}) # 获得订单 db_order = db.order_app.find_one( { 'order_id': param.order_id, 'user': uname }, { 'status': 1, 'cart': 1, 'due': 1, 'shop': 1 }) if db_order == None: return json.dumps({'ret': -3, 'msg': '未找到订单!'}) elif db_order['status'] != 'DUE': return json.dumps({'ret': -3, 'msg': '不是待付款订单!'}) # 取消订单 db.order_app.update_one({ 'order_id': param.order_id, }, { '$set': { 'status': 'CANCEL' }, '$push': { 'history': (app_helper.time_str(), uname, '取消账单') }, }) return json.dumps({'ret': 0, 'msg': '订单已取消!'}) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', secret='', sign='') if '' in (param.app_id, param.session, param.secret, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign( [param.app_id, param.session, param.secret]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_user = db.app_user.find_one({'uname': uname}, {'coupon': 1}) if db_user == None: # 不应该发生 return json.dumps({'ret': -5, 'msg': '未找到用户信息'}) # 检查是否有新红包 app_helper.check_hb(uname) # 这里应该增加对有效期的检查!!! coupon = [] unused = 0 for i in db_user['coupon']: if app_helper.time_str(format=1) > i[1]: # 过期抵用券不返回 2015-08-22 continue coupon.append({ 'id': i[0], 'valid': i[1], 'cash': i[2], 'status': 'unused' if i[3] == 1 else 'used', }) unused += (1 if i[3] == 1 else 0) # 返回 return json.dumps({ 'ret': 0, 'data': { 'coupon': coupon, 'total': len(coupon), 'unused': unused, } }) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', secret='', sign='') if '' in (param.app_id, param.session, param.secret, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign( [param.app_id, param.session, param.secret]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_user = db.app_user.find_one({'uname': uname}, {'address': 1}) if db_user == None: # 不应该发生 return json.dumps({'ret': -5, 'msg': '未找到用户信息'}) addr = [] for i in db_user['address']: addr.append({ 'id': i[0], 'name': i[1], 'tel': i[2], 'addr': i[3], 'tick': i[4] if len(i) > 4 else 0, }) addr2 = quick(addr) # 返回 return json.dumps({ 'ret': 0, 'data': { 'addr': addr2, 'total': len(addr2), } }) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', shop_id='', order_id='', addr_id='', coupon_id='', cart='', sign='') print param if '' in (param.app_id, param.session, param.shop_id, param.addr_id, param.cart, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign([ param.app_id, param.session, param.order_id, param.shop_id, param.addr_id, param.coupon_id, param.cart ]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) # 记录版本信息 db.app_user.update_one({'uname': uname}, {'$set': { 'version': 'v1' }}) # mice 为黄牛手机号标志,mice==1不可以下单 2015-08-22 db_user = db.app_user.find_one( {'uname': uname}, #, 'mice':{'$ne':1}}, { 'coupon': 1, 'address': 1, 'credit': 1, 'app_id': 1, 'mice': 1 }) if db_user == None: # 不应该发生 return json.dumps({'ret': -9, 'msg': '未找到用户信息'}) # 检查mice, 排除白名单 if db_user.get('mice') == 1 and uname not in app_helper.WHITE_LIST: print 'mice !!!' return json.dumps({'ret': -9, 'msg': '未找到用户信息'}) app_id = db_user['app_id'] # 修改未付款的过期订单 db.order_app.update_many( { #'uname' : uname, 'status': 'DUE', 'deadline': { '$lt': int(time.time()) } }, {'$set': { 'status': 'TIMEOUT' }}) # 检查是否有新红包 #app_helper.check_hb(uname) # 先要核对送货地址是否在门店送货范围内!!!!!!! -- 需补充 # 查找shop db_shop = db.base_shop.find_one({'_id': ObjectId(param.shop_id)}) if db_shop == None: return json.dumps({'ret': -6, 'msg': 'shop_id错误'}) # 查询收货地址 address = None for i in db_user['address']: if i[0] == param.addr_id: address = list(i) break if address == None: return json.dumps({'ret': -7, 'msg': 'addr_id错误'}) ########################################################################### # 用收货电话检查黄牛 2015-08-22 db_recv = db.recv_tel.find_one({'tel': address[2]}) if db_recv: one_more = 0 if uname not in db_recv['unames']: # 补充疑似账号 db.recv_tel.update_one({'tel': address[2]}, {'$push': { 'unames': uname }}) one_more = 1 if len(db_recv['unames']) + one_more > 10: # 改为10,2015-10-12 # 发现 mice mice = 1 for b in db_recv['unames']: if b in app_helper.WHITE_LIST: # 过滤白名单相关号码 mice = 0 break db.app_user.update_many( {'uname': { '$in': db_recv['unames'] }}, {'$set': { 'mice': mice }}) db.app_user.update_many( {'openid': { '$in': db_recv['unames'] }}, {'$set': { 'mice': mice }}) if one_more == 1: db.app_user.update_one({'uname': uname}, {'$set': { 'mice': mice }}) if mice == 1: print '!!! mice:', address[ 2] #, uname, db_recv['unames'] return json.dumps({'ret': -9, 'msg': '黄牛下单1'}) else: db.recv_tel.insert_one({'tel': address[2], 'unames': [uname]}) print 'insert', address[2] # 用收货地址检查黄牛, 不准确,不能标注 2015-08-23 db_recv = db.recv_addr.find_one({'addr': address[3]}) if db_recv: one_more = 0 if uname not in db_recv['unames']: db.recv_addr.update_one({'addr': address[3]}, {'$push': { 'unames': uname }}) one_more = 1 if len(db_recv['unames']) + one_more > 10: # 改为10,2015-10-12 # 发现疑似mice,不标注,因为不确定 print '!!! maybe a mice:', address[3].encode( 'utf-8') #, uname, db_recv['unames'] else: db.recv_addr.insert_one({ 'addr': address[3], 'unames': [uname] }) #print 'insert', address[2] # 用app_id检查黄牛 appid_count = db.app_user.find({ 'app_id': app_id }, { '_id': 1 }).count() if appid_count > 10 and app_id.strip( ) != '': # app_id 可能为空,新绑定的用户 # 改为10,2015-10-12 # 发现 mice db_mice = db.app_user.find({'app_id': app_id}, {'uname': 1}) mice = [] for m in db_mice: if m['uname'] in app_helper.WHITE_LIST: # 过滤白名单 mice = [] break else: mice.append(m['uname']) db.app_user.update_many({'uname': { '$in': mice }}, {'$set': { 'mice': 1 }}) if mice != []: print '!!! mice by app_id: ', mice return json.dumps({'ret': -9, 'msg': '黄牛下单1'}) # 查黄牛-结束 ########################################################################### # 查找优惠券 # 未查到,则不使用优惠券 coupon = None for i in db_user['coupon']: if i[0] == param.coupon_id: coupon = list(i) break # 转换cart数据为json,应该有异常捕获 !!! cart = json.loads(param.cart) #print cart if len(cart) == 0: return json.dumps({'ret': -5, 'msg': '购物车无数据'}) if param.order_id == '': #cc = 1 #while cc!=None: # # 取得sku计数, 不与线下order共用 # db_sa = db.user.find_one_and_update( # {'uname' : 'settings'}, # {'$inc' : {'app_count' : 1}}, # {'app_count' : 1} # ) # order_id = 'n%06d' % db_sa['app_count'] # # 防止订单号重复 # cc = db.order_app.find_one({'order_id' : order_id},{'_id':1}) order_id = app_helper.get_new_order_id('v1') print 'new order_id', order_id else: order_id = param.order_id cc = db.order_app.find_one( { #'uname' : uname, # 防止app的bug,重复order_id 'order_id': order_id, }, { 'status': 1, }) if cc != None and cc[ 'status'] != 'DUE': # 检查订单状态,只有due才可以checkout print "BUG! order_id status" return json.dumps({'ret': -99, 'msg': '参数错误'}) # 订单状态:DUE, PAID, ONROAD, COMPLETED, CANCELED, FINISH # 默认运费 5元,免邮门槛 29元 order = { 'status': 'DUE', 'uname': uname, 'shop': db_shop['_id'], 'user': uname, 'order_id': order_id, 'order_source': app_helper.get_devive_type(param.app_id), 'address': address, # 收货地址 'coupon': coupon, # 使用的优惠券 'cart': [], 'cost': '0.00', # 成本合计,参考 'total': '0.00', # 价格小计,加项 'coupon_disc': '0.00', # 优惠券抵扣,减项 'first_disc': '0.00', # 首单立减, 减项 'delivery_fee': '0.00', # 运费,加项 'due': '0.00', # 应付价格 'uname_id': db_user['_id'], # for processor 'next_status': '', 'lock': 0, 'man': 0, 'retry': 0, 'comment': '', 'b_time': int(time.time()), 'e_time': int(time.time()), 'deadline': int(time.time() + 60 * 15), } # 统计旧订单数,为0则是首单 2015-09-29 old_order_num = db.order_app.find( { 'user': uname, 'status': { '$nin': ['DUE', 'TIMEOUT', 'CANCEL'] } }, { '_id': 1 }).count() # item = { # “product_id” : “k000011”, # “num” : “5”, # } # 应该只有 k-prod cart_to_return = [] cate_001 = 0 b3_sku = 0 for item in cart: # sku db_sku = db.sku_store.find_one( {'product_id': item['product_id']}, { 'app_title': 1, 'is_onsale': 1, 'special_price': 1, 'ref_price': 1, 'maximun': 1, 'list_in_app': 1, }) if db_sku == None: # 应该不会发生 print 'Error: db_sku==None' continue if db_sku['list_in_app'] == -3: # B3 整箱预售 # -3 不启动B3销售 r = db.inventory.find_one( # 线上销售要检查库存 { 'product_id' : item['product_id'], 'list_in_app' : {'$ne' : 0}, 'shop' : ObjectId(setting.B3_shop), }, { 'cost_price' : 1, 'ref_prod_id' : 1, 'price' : 1, 'sku' : 1, 'num' : 1, 'category' : 1, 'first_order' : 1 } ) b3_sku += 1 else: r = db.inventory.find_one( # 线上销售要检查库存 { 'product_id' : item['product_id'], 'list_in_app' : {'$ne' : 0}, 'shop' : db_shop['_id'], }, { 'cost_price' : 1, 'ref_prod_id' : 1, 'price' : 1, 'sku' : 1, 'num' : 1, 'category' : 1, 'first_order' : 1 } ) if r: # 如果库存数据中没此sku,会忽略掉,此情况应该不会发生 new_num = int(item['num']) new_num = new_num if new_num <= r['num'] else r['num'] new_num = max(0, new_num) # 发现过小于零的情况,微信 # 检查是不是 001 (水果) 分类 if r['category'] == '001': cate_001 += 1 # 检查是否限购 if db_sku['maximun'] > 0: ''' # 每日限购,生成当天的时间tick tday = app_helper.time_str(format=1) begin_d = '%s 00:00:00' % tday end_d = '%s 23:59:59' % tday begin_t = int(time.mktime(time.strptime(begin_d,"%Y-%m-%d %H:%M:%S"))) end_t = int(time.mktime(time.strptime(end_d,"%Y-%m-%d %H:%M:%S"))) print begin_d, end_d, begin_t, end_t # 检查时间段内购买记录 c = db.order_app.find({ 'uname' : uname, 'order_id' : {'$ne':order_id}, 'status' : {'$ne':'TIMEOUT'}, 'cart.product_id' : item['product_id'], '$and' : [{'b_time' : {'$gt' : begin_t}}, {'b_time' : {'$lt' : end_t}}], }, {'_id':1}).count() print 'findings: ',c if c>0: # 限购商品只允许购买1次 new_num=0 else: new_num=min(new_num, db_sku['maximun']) print 'limit : ',new_num ''' # 每单限购 if new_num > db_sku['maximun']: new_num = db_sku['maximun'] item['num'] = '%d' % new_num # 防止iOS闪退!!! print 'limit : ', new_num ''' # 买一送一 每单限购1件 if item['product_id'] in app_helper.buy_1_give_1: #new_num=min(new_num, 1) #print 'buy 1 give 1 limit : ',new_num new_item = { 'product_id' : item['product_id'], 'num' : '%d' % new_num, 'num2' : new_num, 'price' : r['price'], 'title' : db_sku['app_title'], } else: ''' # 首单可见商品,非首单用户 2015-09-29 if r.has_key('first_order') and r[ 'first_order'] == 1 and old_order_num > 0: # 非首单用户,不让购买,比较生硬 new_num = 0 item['num'] = '%d' % new_num # 防止iOS闪退!!! new_item = { 'product_id': item['product_id'], 'num': item['num'], 'num2': new_num, 'price': r['price'], 'title': db_sku['app_title'], } # 是否有优惠价格 if db_sku['is_onsale']==1 and \ float(db_sku['special_price'])<float(r['price']): # 优惠价格比门店价格低 new_item['price'] = db_sku['special_price'] # 计算总价 item_price = round(new_num * float(new_item['price']), 2) new_item['price'] = '%.2f' % item_price cart_to_return.append(new_item) # 返回到app的cart不包含cost cost_price = r['cost_price'] #if item[0][0]=='w': # w-prod 信息都用 u-prod的替换 # new_item['product_id'] = r['ref_prod_id'] # new_item['w_id'] = item[0] # # 查询成本, 从对应u-prod当前成本 # r2 = db.inventory.find_one({ # u-prod # 'shop' : db_shop['shop'], # 'product_id' : r['ref_prod_id'], # }, {'cost_price':1}) # cost_price = r2['cost_price'] # 计算成本 item_cost = round(new_num * float(cost_price), 2) new_item['cost'] = '%.2f' % item_cost # 加入cart order['cart'].append(new_item) # 累计售价和成本 order['total'] = '%.2f' % (float(order['total']) + item_price) order['cost'] = '%.2f' % (float(order['cost']) + item_cost) else: # 店内未找到库存, !!!应该不会发生 new_item = { 'product_id': item['product_id'], 'num': item['num'], 'num2': 0, 'price': '0.00', 'cost': '0.00', 'title': db_sku['app_title'], } cart_to_return.append(new_item) # 返回到app的cart不包含cost order['cart'].append(new_item) tt = float(order['total']) if tt > 0: # 免邮门槛 #if tt<29: # 免邮门槛 29 if tt < app_helper.free_delivery: # 免邮门槛 order[ 'delivery_fee'] = '%.2f' % app_helper.delivery_fee # 运费5元 ''' # 首单立减 first_promote元, 商品总额大于 first_promote_threshold元 cut_now = app_helper.first_promote # if cate_001>0 and (tt+float(order['delivery_fee']))>=app_helper.first_promote_threshold and \ db.order_app.find({'user':uname, 'status':{'$nin':['DUE','TIMEOUT','CANCEL']}},{'_id':1}).count()==0: order['first_disc'] = '%.2f' % cut_now ''' # 首单立减 first_promote元, 商品总额大于 first_promote_threshold元 if cate_001 > 0 and old_order_num == 0: # 符合首单条件,且有一个水果商品 print '首单' if str(db_shop['_id']) in app_helper.first_promote2_shop and \ (tt+float(order['delivery_fee']))>=app_helper.first_promote2_threshold: # 站点落在 指定站点范围内,使用首单立减2 print '首单立减 - 指定站点' order[ 'first_disc'] = '%.2f' % app_helper.first_promote2 elif (tt + float(order['delivery_fee']) ) >= app_helper.first_promote_threshold: # 其他站点使用首单立减1 print '首单立减' order['first_disc'] = '%.2f' % app_helper.first_promote # 优惠券, 检查有效期, 优惠券门槛为10元 if float(order['first_disc'])==0.0 and coupon!=None and \ coupon[3]==1 and app_helper.time_str(format=1)<=coupon[1]: if len(coupon ) > 5 and coupon[5] == 'apple' and cate_001 < 1: # 水果券,但没有水果 2015-09-29 print '水果券没水果' order['coupon'] = None elif len(coupon) > 5 and coupon[5] == 'b3' and b3_sku < 1: # 整箱券,但没有整箱 2015-10-18 print '整箱券没整箱' order['coupon'] = None else: if len(coupon) > 4: # (id, 有效期, 金额, 是否已用, 门槛) 2015-09-27 # 有门槛信息,使用优惠券门槛信息 if (tt + float(order['delivery_fee'])) < coupon[4]: order['coupon'] = None else: order['coupon_disc'] = coupon[2] else: # 使用默认条件 if float(coupon[2]) == 6.0 and ( tt + float(order['delivery_fee'])) < 29.9: order['coupon'] = None elif float(coupon[2]) == 9.0 and ( tt + float(order['delivery_fee'])) < 39.9: order['coupon'] = None elif (tt + float(order['delivery_fee'])) < 14.9: order['coupon'] = None else: order['coupon_disc'] = coupon[2] else: order['coupon'] = None # 计算应付:价格合计 - 优惠券 - 首单立减 + 运费 print(tt + float(order['delivery_fee']) - float(order['coupon_disc']) - float(order['first_disc'])) print tt, float(order['delivery_fee']), float( order['coupon_disc']), float(order['first_disc']) order['due'] = '%.2f' % (tt + float(order['delivery_fee']) - float(order['coupon_disc']) - float(order['first_disc'])) if float(order['due']) <= 0: order['due'] = '0.10' # 如果没有,则insert #db.order_app.replace_one({'order_id':order_id}, order, upsert=True) db.order_app.update_one({'order_id': order_id}, { '$set': order, '$push': { 'history': (app_helper.time_str(), uname, '提交结算') } }, upsert=True) ret_json = { # 返回结果,实际有库存的结果, 'ret': 0, 'data': { 'order_id': order['order_id'], 'shop_id': str(order['shop']), 'shop': db_shop['name'], # 可能会变,如果地址与门店不匹配的时候 'addr_id': address[0], 'cart_num': len(order['cart']), 'cart': cart_to_return, 'total': order['total'], 'coupon': coupon[0] if order['coupon'] else '', 'coupon_disc': order['coupon_disc'], 'first_disc': order['first_disc'], 'delivery_fee': order['delivery_fee'], 'due': order['due'], 'credit': '%.2f' % db_user.get('credit', 0.0) } } print ret_json return json.dumps(ret_json) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', order_id='', total='', sign='') if '' in (param.app_id, param.order_id, param.session, param.total, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) if web.ctx.has_key('environ'): client_ip = web.ctx.environ['REMOTE_ADDR'] else: return json.dumps({'ret': -5, 'msg': '无法取得客户端ip地址'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign( [param.app_id, param.session, param.order_id, param.total]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_shop = db.base_shop.find_one( {'_id': ObjectId(setting.default_shop)}, {'name': 1}) # 统一下单接口获取 prepay_id nonce_str = app_helper.my_rand(30) body = 'U掌柜app' trade_type = 'APP' order_id = param.order_id.encode('utf-8') total_fee = param.total.encode('utf-8') para = [('appid', wx_appid), ('body', body), ('mch_id', mch_id), ('nonce_str', nonce_str), ('notify_url', notify_url), ('out_trade_no', order_id), ('spbill_create_ip', client_ip), ('total_fee', total_fee), ('trade_type', trade_type)] #print para stringA = '&'.join('%s=%s' % i for i in para) stringSignTemp = '%s&key=%s' % (stringA, api_key) sign = hashlib.md5(stringSignTemp).hexdigest().upper() para_xml = '<xml>' \ '<appid>'+wx_appid+'</appid>' \ '<mch_id>'+mch_id+'</mch_id>' \ '<nonce_str>'+nonce_str+'</nonce_str>' \ '<sign>'+sign+'</sign>' \ '<body>'+body+'</body>' \ '<out_trade_no>'+order_id+'</out_trade_no>' \ '<total_fee>'+total_fee+'</total_fee>' \ '<spbill_create_ip>'+client_ip+'</spbill_create_ip>' \ '<notify_url>'+notify_url+'</notify_url>' \ '<trade_type>'+trade_type+'</trade_type>' \ '</xml>' print para_xml #return json.dumps({'ret' : 0, 'data' : 'here'}) urllib3.disable_warnings() pool = urllib3.PoolManager(num_pools=2, timeout=180, retries=False) url = 'https://api.mch.weixin.qq.com/pay/unifiedorder' r = pool.urlopen('POST', url, body=para_xml) if r.status == 200: data = r.data print data return json.dumps({'ret': 0, 'data': data}) else: return json.dumps({'ret': -1, 'data': r.status}) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', query='', sign='') if '' in (param.app_id, param.session, param.query, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign( [param.app_id, param.session, param.query]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_user = db.app_user.find_one({'uname': uname}, { 'coupon': 1, 'credit': 1 }) if db_user == None: # 不应该发生 return json.dumps({'ret': -5, 'msg': '未找到用户信息'}) # 修改为付款的过期订单 r = db.order_app.update_many( { 'uname': uname, 'status': 'DUE', 'deadline': { '$lt': int(time.time()) } }, {'$set': { 'status': 'TIMEOUT' }}) #print r # 获得订单 if param.query == 'ALL': condition = {'user': uname, 'status': {'$ne': 'TIMEOUT'}} else: condition = {'user': uname, 'status': QUERY.get(param.query)} db_order = db.order_app.find( condition, { 'status': 1, 'cart': 1, 'due': 1, 'shop': 1, 'history': 1, 'order_id': 1, 'deadline': 1 }).sort([('order_id', -1)]) order_list = [] for i in db_order: # 取购物车中第一个商品的图片 db_sku = db.sku_store.find_one( {'product_id': i['cart'][0]['product_id']}, {'base_sku': 1}) base_sku = db.dereference(db_sku['base_sku']) image = base_sku['image'][0] if base_sku.has_key( 'image') and len(base_sku['image']) > 0 else '' order_list.append({ 'order_id': i['order_id'], 'order_time': i['history'][0][0], 'image': '/%s/%s' % (image[:2], image), 'status': helper.ORDER_STATUS['APP'].get(i['status'], '未知状态'), 'due': i['due'], 'count': len(i['cart']), }) return json.dumps({ 'ret': 0, 'data': { 'order_list': order_list, 'credit': '%.2f' % db_user.get('credit', 0.0) } }) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', order_id='', sign='') if '' in (param.app_id, param.session, param.order_id, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign( [param.app_id, param.session, param.order_id]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_user = db.app_user.find_one({'uname': uname}, { 'coupon': 1, 'credit': 1 }) if db_user == None: # 不应该发生 return json.dumps({'ret': -5, 'msg': '未找到用户信息'}) # 获得订单 #print param.order_id, uname db_order = db.order_app.find_one({ 'order_id': param.order_id, 'user': uname }) if db_order == None: return json.dumps({'ret': -3, 'msg': '未找到订单!'}) cart = [] for i in db_order['cart']: r = db.sku_store.find_one({'product_id': i['product_id']}, {'base_sku': 1}) base_sku = db.dereference(r['base_sku']) image = base_sku['image'][0] if base_sku.has_key( 'image') and len(base_sku['image']) > 0 else '' cart.append({ 'product_id': i['product_id'], 'title': i['title'], 'price': i['price'], 'num2': i['num2'], 'image': '/%s/%s' % (image[:2], image), }) data = { 'order_id': db_order['order_id'], 'shop': str(db_order['shop']), # 需要中文名 'status': helper.ORDER_STATUS['APP'].get(db_order['status'], '未知状态'), # 需要中文名 'deadline': db_order['deadline'] - int(time.time()), # 离支付截至的时间,秒数 'delivery': { 'address': db_order['address'][3], 'contact': db_order['address'][1], 'contact_tel': db_order['address'][2], 'runner': db_order['runner']['name'] if db_order.has_key('runner') else '', # 送货员姓名 'runner_tel': db_order['runner']['tel'] if db_order.has_key('runner') else '', # 送货员电话 }, 'cart': cart, 'total': db_order['total'], 'coupon': db_order['coupon'][0] if db_order['coupon'] else '', 'coupon_disc': db_order['coupon_disc'], 'first_disc': db_order['first_disc'], 'delivery_fee': db_order['delivery_fee'], 'due': db_order['due'], 'star': db_order.get('star', 1), 'credit': '%.2f' % db_user.get('credit', 0.0), } return json.dumps({'ret': 0, 'data': data}) else: return json.dumps({'ret': -4, 'msg': '无效的session'})
def POST(self): web.header('Content-Type', 'application/json') param = web.input(app_id='', session='', order_id='', pay_type='', data='', sign='') print param if '' in (param.app_id, param.session, param.order_id, param.pay_type, param.sign): return json.dumps({'ret': -2, 'msg': '参数错误'}) uname = app_helper.logged(param.session) # 检查session登录 if uname: #验证签名 md5_str = app_helper.generate_sign([ param.app_id, param.session, param.order_id, param.pay_type, param.data ]) if md5_str != param.sign: return json.dumps({'ret': -1, 'msg': '签名验证错误'}) db_user = db.app_user.find_one({'uname': uname}, { 'coupon': 1, 'credit': 1 }) if db_user == None: # 不应该发生 return json.dumps({'ret': -5, 'msg': '未找到用户信息'}) # 支付操作:1,记录订单支付,2.改变订单状态,3.修改库存显示 !!!!!! # 获得订单 db_order = db.order_app.find_one( {'order_id': param.order_id}, #{'status':1, 'cart':1, 'due':1, 'shop':1} {'_id': 0}) if db_order == None: return json.dumps({'ret': -3, 'msg': '未找到订单!'}) # 支付宝和微信支付订单,已PAID说明提前收到异步通知 if db_order['status'] == 'PAID' and param.pay_type in ('ALIPAY', 'WXPAY'): # 记录此次调用 db.order_app.update_one({ 'order_id': param.order_id, }, { '$set': { 'pay_type': param.pay_type, 'pay': db_order['due'], 'paid2_time': app_helper.time_str(), 'paid2_tick': int(time.time()), }, '$push': { 'history': (app_helper.time_str(), uname, '提交付款') }, }) return json.dumps({ 'ret': 0, 'data': { 'order_id': param.order_id, 'due': db_order['due'], 'paid': db_order['due'], 'status': '已支付' } }) # 只能处理未支付订单 if db_order['status'] != 'DUE': return json.dumps({'ret': -3, 'msg': '不是待付款订单!'}) # 余额支付和支付宝/微信支付未到账处理 # # 余额支付中对B3的处理与notify中处理一样 2015-09-14 # if param.pay_type == 'CREDIT': # 余额支付0元提交的问题,原因未知 2015.08.20 if round(float(db_order['due']), 2) <= 0.0: return json.dumps({'ret': -2, 'msg': '参数错误'}) # 检查余额是否够支付 if float(db_order['due']) > db_user.get('credit', 0.0): return json.dumps({'ret': -6, 'msg': '余额不足!'}) # 使用的优惠券失效 #db_user = db.app_user.find_one({'uname':r['uname']}) coupon = [] if db_order['coupon'] != None: for i in db_user['coupon']: if i[0] == db_order['coupon'][0]: # 这次使用 #coupon.append((i[0],i[1],i[2],0)) i2 = list(i) i2[3] = 0 coupon.append(i2) else: coupon.append(i) else: coupon = db_user['coupon'] # 更新优惠券 db.app_user.update_one({'uname': db_order['uname']}, {'$set': { 'coupon': coupon }}) # 正常减库存! # item = [ product_id, num, num2, price] # k - num 库存数量 print "修改库存." b2 = [] # C端商品 b3 = [] # B3整箱预售商品 b3_total = 0.0 for item in db_order['cart']: # 暂停整箱预售 2015-10-27 #r3 = db.sku_store.find_one({'product_id' : item['product_id']}, # {'list_in_app':1}) #if r3['list_in_app']==3: # B3商品不需要改库存 # b3_total += float(item['price']) # b3.append(item) # item['title'] = item['title']+u'(整箱预售,次日送达)' # b2.append(item) # continue # 买一送一 if item['product_id'] in app_helper.buy_1_give_1: lc_num2 = float(item['num2']) item['num2'] = int(lc_num2 + lc_num2) item['title'] = item['title'].replace(u'买一送一', u'特惠活动') # 过滤数量价格为零的 if item['num2'] == 0 and float(item['price']) == 0.0: continue r = db.inventory.find_one_and_update( # 不检查库存,有可能负库存 { 'product_id' : item['product_id'], 'shop' : db_order['shop'], }, { '$inc' : { 'num' : 0-float(item['num2']), # num2 实际购买数量 'pre_pay_num' : float(item['num2']), # 记录预付数量 } #'$push' : { 'history' : (helper.time_str(), # helper.get_session_uname(), '售出 %s' % str(item['num']))}, }, {'_id':1} ) #print r if r == None: # 不应该发生 return json.dumps({'ret': -9, 'msg': '修改库存失败,请联系管理员!'}) else: b2.append(item) # 更新第3方库存 2015-10-10 app_helper.elm_modify_num(db_order['shop'], item['product_id']) # 检查是否有b3商品, 3种情况 # 1. b2, b3 都有,拆单 # 2. 只有b3,站点改为B3站点,保留收货站点 # 3. 只有b2,保持订单不变 #print b2 #print b3 if len(b3) > 0 and (len(b2) - len(b3)) > 0: # 情况1 print "拆单" r4 = db_order.copy() r4['order_id'] = r4['order_id'] + u'-b3' r4['shop_0'] = db_order['shop'] r4['shop'] = ObjectId(setting.B3_shop) r4['cart'] = b3 r4['status'] = 'PAID' r4['ali_trade_no'] = param.get('trade_no') r4['paid_time'] = param.get('gmt_payment') r4['paid_tick'] = int(time.time()) r4['history'] = [(app_helper.time_str(), 'credit', '余额付款-拆单')] r4['total'] = '%.2f' % b3_total r4['cost'] = '0.00' r4['coupon_disc'] = '0.00' r4['first_disc'] = '0.00' r4['delivery_fee'] = '0.00' r4['due'] = '0.00' db.order_app.insert_one(r4) # 增加子订单 elif len(b3) > 0: # 情况 2 print "订单改到B3站点" db.order_app.update_one({'order_id': param.order_id}, { '$set': { 'shop_0': db_order['shop'], 'shop': ObjectId(setting.B3_shop), } }) else: # 情况3,什么都不做 print "订单保持不变" # 更新销货单信息 db.order_app.update_one( { 'order_id': param.order_id, }, { '$set': { 'status': 'PAID', 'cart': b2, # 更新购物车 2015-09-11 'pay_type': param.pay_type, 'pay': db_order['due'], 'paid_time': app_helper.time_str(), 'paid_tick': int(time.time()), }, '$push': { 'history': (app_helper.time_str(), uname, '余额付款') }, }) # 消费余额 db.app_user.update_one({ 'uname': uname, }, { '$inc': { 'credit': 0 - float(db_order['due']), }, '$push': { 'history': (app_helper.time_str(), uname, '消费余额 %s' % db_order['due'].encode('utf-8')) }, }) elif param.pay_type in ('ALIPAY', 'WXPAY'): # 更新销货单信息, r = db.order_app.find_one_and_update( { 'order_id': param.order_id, 'status': 'DUE' }, { '$set': { 'status': 'PREPAID', 'pay_type': param.pay_type, 'pay': db_order['due'], 'paid2_time': app_helper.time_str(), 'paid2_tick': int(time.time()), 'pay_data': param.data, }, '$push': { 'history': (app_helper.time_str(), uname, '提交付款') }, }, {'status': 1}) # 如果不是DUE,说明已收到异步通知 if r == None: db.order_app.update_one({ 'order_id': param.order_id, }, { '$set': { 'pay_type': param.pay_type, 'pay': db_order['due'], 'paid2_time': app_helper.time_str(), 'paid2_tick': int(time.time()), }, '$push': { 'history': (app_helper.time_str(), uname, '提交付款') }, }) # 返回 return json.dumps({ 'ret': 0, 'data': { 'order_id': param.order_id, 'due': db_order['due'], 'paid': db_order['due'], 'status': '已支付' } }) else: return json.dumps({'ret': -4, 'msg': '无效的session'})