def validate_client(imei, app_version, function): if not check_imei(imei): print_error(action='Response', function=function, branch='IMEI_INVALID', api_version='v1.0', imei=imei) return {'head': ErrorCode.IMEI_INVALID} if app_version not in get_valid_channel_version_name() and app_version not in get_valid_avatar_version_name() \ and imei not in get_white_imei_list(): print_error(action='Response', function=function, branch='APP_INVALID', api_version='v1.0', imei=imei) return {'head': ErrorCode.APP_INVALID} if imei in get_back_imei_list(): print_info(action='Response', function=function, branch='BLACK_IMEI', api_version='v1.0', imei=imei) return {'head': ErrorCode.BLACK_IMEI} return "valid"
def validate_app_exception(imei, app_version, function): if app_version not in get_app_version_list() and app_version not in get_valid_avatar_version_name() \ and imei not in get_white_imei_list(): print_error(action='Response', function=function, branch='EXCEPTION_APP_INVALID', api_version='v1.0', imei=imei) return {'head': ErrorCode.EXCEPTION_APP_INVALID} return "valid"
def post(self): print("receive data:", request.data.decode('utf-8')) weixin_pay = WexinPay() # xml data ret = weixin_pay.reply(ret_msg=request.data.decode('utf-8')) # dict data ret_info = weixin_pay.xml_to_dict(ret) # print("response data: ", ret, ret_info) if ret_info['return_code'] == 'SUCCESS': res = weixin_pay.xml_to_dict(request.data.decode('utf-8')) wx_res = WeixinPayResults.query.filter_by( transaction_id=res['transaction_id']).first() if wx_res is None: wx_res = WeixinPayResults() wx_res.appid = res['appid'] wx_res.mch_id = res['mch_id'] if 'device_info' in res: wx_res.device_info = res['device_info'] wx_res.nonce_str = res['nonce_str'] wx_res.sign = res['sign'] wx_res.result_code = res['result_code'] if 'err_code' in res: wx_res.err_code = res['err_code'] if 'err_code_des' in res: wx_res.err_code_des = res['err_code_des'] wx_res.openid = res['openid'] if 'is_subscribe' in res: wx_res.is_subscribe = res['is_subscribe'] wx_res.trade_type = res['trade_type'] wx_res.bank_type = res['bank_type'] wx_res.total_fee = res['total_fee'] if 'fee_type' in res: wx_res.fee_type = res['fee_type'] wx_res.cash_fee = res['cash_fee'] if 'coupon_fee' in res: wx_res.coupon_fee = res['coupon_fee'] if 'coupon_count' in res: wx_res.coupon_count = res['coupon_count'] if 'err_code_des' in res: wx_res.err_code_des = res['err_code_des'] wx_res.transaction_id = res['transaction_id'] wx_res.out_trade_no = res['out_trade_no'] if 'attach' in res: wx_res.attach = res['attach'] wx_res.time_end = res['time_end'] db.session.add(wx_res) # key order if res['out_trade_no'].startswith('key'): key_order = KeyOrder.query.filter_by( id=res['out_trade_no']).with_lockmode('update').first() if key_order is not None and key_order.status == 0: key_order.pay_time = datetime.datetime.now() key_order.status = 1 db.session.add(key_order) key_info = Key.query.filter_by( id=key_order.key_id).with_lockmode('update').first() if key_info is None: key_record = KeyRecord.query.filter_by( id='00000000000000').with_lockmode( 'update').first() if key_record is not None: key_record.count += 1 db.session.add(key_record) key = Key() key.key_record_id = key_record.id key.id = key_order.key_id db.session.add(key) db.session.commit() else: ware_order = MemberWareOrder.query.filter_by( order_number=res['out_trade_no']).with_lockmode( 'update').first() if ware_order is not None and ware_order.status == 0: # 添加支付时间的记录 godin_account = GodinAccount.query.filter_by( godin_id=ware_order.buyer_godin_id).limit(1).first() user_pay_time = UserPayTime() user_pay_time.phone_num = godin_account.phone_num user_pay_time.status = 1 db.session.add(user_pay_time) db.session.commit() vip_user = VipMembers.query.filter_by( godin_id=ware_order.buyer_godin_id).first() # 用户不存在时 if vip_user is None: db.session.commit() print_error(action='Response', function=self.__class__.__name__, branch='vip_user is null', buyer_godin_id=ware_order.buyer_godin_id, transaction_id=res['transaction_id']) return Response(ret, status=200, mimetype='application/xml') ware_info = MemberWare.query.filter_by( id=ware_order.ware_id).first() if ware_info is not None: # 获取升级至黄金(grade = 1)还是铂金(grade = 2) # 验证用户vip是否过期 gradechange = False # 是否grade需要改变 if vip_user.grade == 2: if vip_user.valid_time and vip_user.valid_time > datetime.datetime.now( ): pass else: vip_user.grade = 1 gradechange = True if vip_user.grade == 1: if vip_user.gold_valid_time and vip_user.gold_valid_time > datetime.datetime.now( ): pass else: vip_user.grade = 0 gradechange = True # 修改grade字段 if gradechange: db.session.add(vip_user) db.session.commit() if vip_user.grade == 2 or vip_user.grade == 3: if ware_info.gold_or_platinum == 1: old_order = MemberWareOrder.query.filter_by(buyer_godin_id=ware_order.buyer_godin_id, status=1). \ order_by(MemberWareOrder.pay_time.desc()).first() else: db.session.commit() print_error( action='Response', function=self.__class__.__name__, branch='supervip can not pay godvip', buyer_godin_id=ware_order.buyer_godin_id, transaction_id=res['transaction_id']) return Response(ret, status=200, mimetype='application/xml') else: old_order = MemberWareOrder.query.filter_by(buyer_godin_id=ware_order.buyer_godin_id, status=1, buy_grade=ware_info.gold_or_platinum)\ .order_by(MemberWareOrder.pay_time.desc()).first() if old_order is not None: # 最后一个付款订单结束的时间的下一秒 if old_order.end_time < datetime.datetime.now(): ware_order.start_time = datetime.datetime.now() else: ware_order.start_time = old_order.end_time + datetime.timedelta( seconds=1) else: ware_order.start_time = datetime.datetime.now() vip_type = VipType.query.filter_by( number=ware_info.category).first() if vip_type is not None: ware_order.end_time = ware_order.start_time + datetime.timedelta( days=vip_type.days) ware_order.status = 1 ware_order.pay_time = datetime.datetime.now() if vip_user is not None: # 获取升级至黄金(grade = 1)还是铂金(grade = 2) if vip_user.grade == 2 or vip_user.grade == 3: vip_user.valid_time = ware_order.end_time vip_user.grade = 2 else: if ware_info.gold_or_platinum == 1: vip_user.valid_time = ware_order.end_time vip_user.grade = 2 else: vip_user.gold_valid_time = ware_order.end_time vip_user.grade = 1 if not vip_user.first_pay_time: vip_user.first_pay_time = datetime.datetime.now() if vip_user.status == 0: vip_user.status = 1 vip_user.cur_pay_cate = ware_info.category db.session.add(vip_user) db.session.add(ware_order) # 添加会员收益记录 member_earn_divide(db, res['out_trade_no']) db.session.commit() return Response(ret, status=200, mimetype='application/xml')