def notify(self): #接受参数 parser = self.getNotifyRequestParameter() paylogger.info("异步通知传入参数%s" % (parser)) #验证参数 success = self.validate(parser) if success: # self.updateRecharge() self.accountChange(100004, 0, None) else: self.accountChange(100004, 99, "支付失败")
def toPay(self): extraStr = self.context['extra'] if extraStr: try: self.context.update(ast.literal_eval(extraStr)) except Exception as e: paylogger.exception(e) paylogger.error("汇丰支付获取extra数据错误:%s" % (extraStr)) return self.createErrorHtml() param = {} param['orgId'] = self.context['orgId'] #位机构号 param['account'] = self.context['code'] #商户号 param['tranTp'] = "0" #交易类型 T0填0,T1填1 private_key = self.context['secret_key'] #异步地址 param['notifyUrl'] = self.context['nodify_url'] currentTime = self.context['currentTime'] localTime = time.localtime(currentTime) param['orgOrderNo'] = self.context['orderid'] type = str(self.findPayType(self.context)) param['source'] = str(type) #付款方式 payUrl = self.context['pay_url'] m_amount = self.context['amount'] * 100 param['amount'] = str('%.0f' % m_amount) keyList = sorted(param.keys(), reverse=False) paramStr = '' for key in keyList: if param[key]: paramStr += '%s=%s&' % (key, param[key]) paramStr = paramStr[:-1] private_keyBytes = base64.b64decode(private_key) priKey = RSA.importKey(private_keyBytes) signer = PKCS1_v1_5.new(priKey) hash_obj = MD5.new(paramStr.encode('utf-8')) signature = base64.b64encode(signer.sign(hash_obj)) signature = bytes.decode(signature) param['signature'] = signature m_headers = {'Content-Type': 'application/json'} m_data = json.dumps(param) response = requests.post(payUrl, data=m_data, headers=m_headers) paylogger.info('汇丰支付%s' % (m_data)) if response.status_code >= 400: paylogger.error("汇丰支付错误:%s" % (response.text)) return self.createErrorHtml("支付失败") resJson = json.loads(response.text) if resJson['respCode'] != "200": paylogger.error("汇丰支付错误:%s" % (response.text)) return self.createErrorHtml("支付失败") return self.createHtml(None, resJson['qrcode'])
def synchor(self): parser = self.getRequestParameter() paylogger.info("同步通知传入参数%s" % (parser)) success = self.validate(parser) self.updateRecharge() m_result = {} m_result['orderid'] = parser['out_trade_no'] m_result['amount'] = parser['money'] if success: m_result['msg'] = '支付成功' m_result['code'] = 1 else: m_result['msg'] = '支付失败' m_result['code'] = 0 return m_result
def post(self, id): parser = RequestParser(trim=True) parser.add_argument('amount', type=float) parser.add_argument('remitter', type=str) parser.add_argument('msg', type=str) parser.add_argument('income_type', type=int) parser.add_argument('pay_type', type=int) parser.add_argument('bankId', type=int) parser.add_argument('accountName', type=int) m_args = parser.parse_args(strict=True) if not hasattr(g, 'current_member'): return {'errorCode': "9999", 'errorMsg': "用戶未登录", 'success': False} paylogger.info("%s用户公司入款 args:%s" % (g.current_member.username, m_args)) uid = g.current_member.id member = Member.query.filter(Member.id == uid).first() status = member.status if status == 2: return { 'errorCode': "9999", 'errorMsg': "该用户已被冻结", 'success': False } if status == 0: return { 'errorCode': "9999", 'errorMsg': "该用户已被禁用", 'success': False } if m_args['amount'] == None or m_args['pay_type'] == None: return {"success": False, 'errorCode': "1010", 'errorMsg': "充值失败"} m_amount = m_args['amount'] if m_amount <= 0: return { "success": False, 'errorCode': "1010", 'errorMsg': "充值失败,充值金额不正确" } ip = utils.host_to_value(request.remote_addr) m_res = Payment().recharge(id, m_args, g.current_member.id, g.current_member.username, ip) if m_res: return {"success": True} else: return {"success": False, 'errorCode': "1010", 'errorMsg': "充值失败"}
def post(self, id): parser = RequestParser(trim=True) parser.add_argument('amount', type=float) parser.add_argument('pay_type', type=int) parser.add_argument('bank_type', type=str) m_args = parser.parse_args(strict=True) if not hasattr(g, 'current_member'): return {'errorCode': "9999", 'errorMsg': "用戶未登录", 'success': False} uid = g.current_member.id member = Member.query.filter(Member.id == uid).first() status = member.status if status == 2: m_html = ''' <body class="notfound"> <div class="wrapper"> <div class="big">页面不见了!</div> <div>该用户钱包已经被冻结</div> <div> </body> ''' if status == 0: m_html = ''' <body class="notfound"> <div class="wrapper"> <div class="big">页面不见了!</div> <div>该用户已被禁用</div> <div> </body> ''' m_response = make_response(m_html) m_response.headers["Content-type"] = "text/html;charset=UTF-8" return m_response paylogger.info("%s用户使用线上支付%s" % (g.current_member.id, m_args['amount'])) realIp = request.remote_addr ip = utils.host_to_value(realIp) m_res = Payment().pay(id, m_args, g.current_member.id, g.current_member.username, ip, realIp) paylogger.info("请求html------------------------") paylogger.info(m_res) paylogger.info("请求html------------------------") m_response = make_response(m_res) m_response.headers["Content-type"] = "text/html;charset=UTF-8" return m_response
def pay(self,m_id,m_args,uid,username,ip,realIp): m_amount = m_args['amount'] m_sql = '''select ol.id, ol.pay_url,ol.accounts_url,ol.nodify_url,ol.return_url, ol.pay_type_relation,so.secret_key,so.tb,so.min_amount,so.max_amount,so.code, so.pay_type,so.id bankid ,ol.name gwname, so.name,so.extra from blast_sysadmin_online so,tb_bank_online_list ol where so.bank_online_id = ol.id and so.id = %s and so.enable = 1 and ol.enable = 1 '''%(m_id) m_result = db.session.execute(m_sql).first() if m_result is None: paylogger.info("%s用户支付错误,没有找到对应的网关信息"%(username)) abort(500) m_json = dict(m_result) m_json['amount'] = m_amount; m_bool = (m_json['min_amount'] is not None and m_amount < m_json['min_amount']) or (m_json['max_amount'] is not None and m_amount > m_json['max_amount']) if m_bool: paylogger.info("%s用户支付错误,支付金额错误"%(username)) return ''' <body> <a>支付错误:%s</a> </body> '''%("支付金额错误") m_gid = m_json['id'] currentTime= int(time.time()); m_json['currentTime'] = currentTime m_json['orderid'] = createOrderId(0,uid,1,None) m_json['ip'] = ip m_json['realIp'] = realIp m_json["username"]=username m_json["bank_type"] = m_args["bank_type"] m_context =m_json #从静态工厂方法实例化具体的支付网关 paylogger.info("%s用户支付%s使用%s支付方式"%(username,m_amount,m_json['name'])) Gateway = GatewaytFactory.getPaymentGateway(m_gid) Gateway.setContext(m_context) try: m_res = Gateway.toPay() self.insert(m_id, uid, username, ip, m_json,100004) try: requests.request('GET', 'http://127.0.0.1:8125/main/memberOnlinePayment', timeout=1) except: pass except Exception as e: paylogger.exception(e) paylogger.error("%s用户支付%s错误"%(username,m_amount)) abort(http_status_code=500,**{"success":False, 'errorCode': "1010", 'errorMsg': "充值错误"}) return m_res
def accountChange(self, type, errorType, errorMsg): #获取入款信息 deposit = self.getRechargeByOrderId(self.orderid) if self.amount is None: self.amount = deposit.applicationAmount errorType = errorType errorMsg = errorMsg if deposit.status != 1: return False if errorType == 0: if deposit.applicationAmount != self.amount: errorType = 99 paylogger.info('%s 充值金额错误' % (self.orderid)) # return {'messages': '%s 充值金额错误'%(self.orderid), 'success': False} if deposit.status != 1: errorType = 98 paylogger.info('%s 状态错误' % (self.orderid)) return False if errorType == 99: #更新入款表 msql = '''update blast_member_recharge set state = 99,rechargeTime = %s,msg='%s' where rechargeId = %s and type =%s ''' % (int(time.time()), errorMsg, self.orderid, type) db.session.execute(msql) try: db.session.commit() except Exception: db.session.rollback() db.session.remove() # return {'messages': '%s 状态错误' % (self.orderid), 'success': False} return False paylogger.info("会员%s入款%s 入款类型%s" % (deposit.username, self.amount, type)) #获取用户账户信息 member = Member.query.filter( Member.username == deposit.username).first() #根据用户获取优惠策略 m_sql = '''select %s yh,ckjhb from blast_member_level where id = (select grade from blast_members where username = "******") ''' if type == 100004: m_sql = m_sql % ("xszfyh", deposit.username) elif type == 100003: m_sql = m_sql % ("gsrkyh", deposit.username) m_res = db.session.execute(m_sql).first() # 根据存款金额在优惠方案集合中找到最优的一个 # if 'yh' in m_res: if m_res.yh and m_res.yh is not None: yh = self.findckyh(self.amount, json.loads(m_res.yh)) else: yh = None paylogger.info("会员%s入款%s 优惠策略%s" % (deposit.username, self.amount, yh)) #初始化各种优惠项 yh_jhbs = 1.0 #稽核倍数 yh_yhbl = 0 #优惠比例 yh_yhsx = 0 #优惠上线 yh_ckje = 0 #存款金额 if yh: if 'yhsx' in yh and yh['yhsx'] is not '' or "" or None: yh_yhsx = yh['yhsx'] if 'ckje' in yh and yh['ckje'] is not '' or "" or None: yh_ckje = yh['ckje'] if 'jhbs' in yh and yh['jhbs'] is not '' or "" or None: yh_jhbs = float(yh['jhbs']) if 'yhbl' in yh and yh['yhbl'] is not '' or "" or None: yh_yhbl = float(yh['yhbl']) jh = None if m_res.ckjhb: jh = json.loads(m_res.ckjhb) ckjh = 1.0 if type == 100004: if 'xszf' in jh and jh['xszf']: ckjh = float(jh['xszf']) elif type == 100003: if 'gsrk' in jh and jh['gsrk']: ckjh = float(jh['gsrk']) else: if 'dkzf' in jh and jh['dkzf']: ckjh = float(jh['dkzf']) #入款稽核 jh = self.amount * ckjh #优惠计算 yh_amount = self.amount * yh_yhbl #判断优惠上线 if yh: if 'yhsx' in yh and yh['yhsx']: yh_yhsx = float(yh['yhsx']) if yh_amount > yh_yhsx: yh_amount = yh_yhsx #优惠稽核 yh_jh = yh_amount * yh_jhbs # m_args['uid'] = g.current_user.id #更新入款表 userid = None porderid = self.porderid if hasattr(g, 'current_user'): userid = g.current_user.id msql = '''update blast_member_recharge set state = 2,rechargeTime = %s,rechargeAmount = %s,coin = %s,auditTime=%s, auditUser = %s ,p_orderid=%s where rechargeId = %s and type =%s ''' % (int(time.time()), self.amount, member.balance, int(time.time()), userid, porderid, self.orderid, type) else: msql = '''update blast_member_recharge set state = 2,rechargeTime = %s,rechargeAmount = %s,coin = %s,auditTime=%s,p_orderid=%s where rechargeId = %s and type =%s ''' % (int(time.time()), self.amount, member.balance, int(time.time()), porderid, self.orderid, type) db.session.execute(msql) paylogger.info("会员%s入款%s,入款稽核%s" % (deposit.username, self.amount, jh)) paylogger.info("会员%s入款%s,优惠金额%s" % (deposit.username, self.amount, yh_amount)) paylogger.info("会员%s入款%s,优惠稽核%s" % (deposit.username, self.amount, yh_jh)) #更新用户余额 msql = '''update blast_members set coin = coin + %s ,yhje = yhje + %s where username = "******" ''' % (self.amount + yh_amount, yh_amount, member.username) db.session.execute(msql) #添加账变 coinLog = MemberAccountChangeRecord() coinLog.memberId = member.id coinLog.memberFrozenBalance = 0 coinLog.amount = self.amount coinLog.memberBalance = member.balance + self.amount if type == 100003: coinLog.accountChangeType = 100001 coinLog.info = '公司入款' elif type == 100004: coinLog.accountChangeType = 100002 coinLog.info = '线上支付' coinLog.time = int(time.time()) coinLog.actionUID = userid coinLog.host = deposit.applicationHost coinLog.orderId = self.orderid coinLog.rechargeid = self.orderid coinLog.auditCharge = jh coinLog.auditType = 2 coinLog.isAcdemen = 1 db.session.add(coinLog) if yh_amount > 0: coinLog = MemberAccountChangeRecord() coinLog.memberId = member.id coinLog.memberFrozenBalance = 0 coinLog.amount = yh_amount coinLog.memberBalance = member.balance + self.amount + yh_amount if type == 100003: coinLog.accountChangeType = 100010 coinLog.info = '公司入款优惠' elif type == 100004: coinLog.accountChangeType = 100011 coinLog.info = '线上支付优惠' coinLog.time = int(time.time()) coinLog.actionUID = userid coinLog.host = deposit.applicationHost coinLog.orderId = self.orderid coinLog.rechargeid = self.orderid coinLog.auditCharge = yh_jh coinLog.auditType = 3 db.session.add(coinLog) #累计账户金额 if type == 100003: msql = '''update blast_sysadmin_bank set amount = amount + %s where id = %s ''' % (self.amount, deposit.systemBankAccountId) elif type == 100004: msql = '''update blast_sysadmin_online set amount = amount + %s where id = %s ''' % (self.amount, deposit.systemBankAccountId) db.session.execute(msql) try: db.session.commit() except Exception: db.session.rollback() db.session.remove() return False paylogger.info("会员%s入款%s 金额增加完成" % (deposit.username, self.amount)) return True
def doGet(self, url, data, headers): paylogger.info("请求地址:%s,请求参数:%s" % (url, data)) response = requests.get(url, params=data, headers=headers) return response
def doPost(self, url, data, headers): paylogger.info("请求地址:%s,请求参数:%s" % (url, data)) response = requests.post(url, data=data, headers=headers) return response