def insert(self,m_id,uid,username,ip,m_json,type): members = Member.query.filter(Member.username == username).first() try: deposit = Deposit() deposit.memberId = uid deposit.username = username deposit.systemBankAccountId = m_id deposit.number = int(m_json['orderid']) deposit.status = 1 deposit.type = type deposit.applicationHost = ip deposit.applicationAmount = m_json['amount'] deposit.applicationTime = m_json['currentTime'] if type == 100004: deposit.auditTime = m_json['currentTime'] if 'bankId' in m_json: deposit.bankAccountId = m_json['bankId'] if 'remitter' in m_json: deposit.remitter = m_json['remitter'] if 'msg' in m_json: deposit.msg = m_json['msg'] if 'income_type' in m_json: deposit.income_type = m_json['income_type'] deposit.pay_type = m_json['pay_type'] deposit.isAcdemen = 1 db.session.add(deposit) db.session.commit() OperationHistory().PublicMeDatasApply(type, deposit) return True; except Exception as e: db.session.rollback() db.session.remove() paylogger.error("%s用户公司入款错误 : "%(username,e)) return False;
def post(self, id): print("触发异步通知") resStr = "" if id == 'otc': id = '6' Getway = GatewaytFactory.getPaymentGateway(int(id)) try: Getway.notify() except BaseException as e: paylogger.error("异步通知解析错误-----------") paylogger.exception(e) resStr = Getway.makeResponse() m_response = make_response(resStr) 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 creat_order(self, qr_code, random_decimal): onlinetrade = OnlinetradesDao() onlinetradesExpansion = OnlinetradesExpansion() onlinetrade.qr_code = qr_code onlinetrade.order_no = creat_order_no(self.__context['pay_type'], self.__merchantDao.id) onlinetrade.mer_code = self.__context['mer_code'] onlinetrade.amount = self.__context['amount'] onlinetrade.discount_amount = random_decimal onlinetrade.real_amount = self.__context['amount'] - random_decimal onlinetrade.org_order_no = self.__context['org_order_no'] onlinetrade.pay_type = self.__context['pay_type'] onlinetrade.cost_agent = self.__cost_agent() onlinetrade.cost_service = self.__cost_service() onlinetrade.action_time = int(time.time()) onlinetrade.state = 1 if self.__context['user_name']: onlinetrade.drawee = self.__context['user_name'] onlinetrade.user_name = self.__merchantDao.username if self.__context['remark']: onlinetrade.remark = self.__context['remark'] org = str(onlinetrade.order_no) + str(self.__context['org_order_no']) org_no = encrypt_md5(org) onlinetradesExpansion.org_order_no = self.__context['org_order_no'] onlinetradesExpansion.order_no = onlinetrade.order_no onlinetradesExpansion.temp_code = org_no onlinetradesExpansion.mer_notify_url = self.__context['notify_url'] order = self.__context order['amount'] = float(order['amount']) onlinetradesExpansion.expansion_data = json.dumps(order) try: db.session.add(onlinetrade) db.session.add(onlinetradesExpansion) db.session.commit() self.__onlinetradeDao = onlinetrade self.__onlinetradeExpansionDao = onlinetradesExpansion except Exception as e: code_manager.cancel(qr_code, Decimal.from_float(self.__context['amount']), random_decimal) db.session.rollback() db.session.remove() paylogger.exception(e) paylogger.error("生成订单异常") raise Exception(e)
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() type = self.findPayType(self.context) self.context['type'] = type payUrl = self.context['pay_url'] mdata = self.createData() response = requests.post(payUrl, data=mdata, headers=None) if response.status_code >= 400: paylogger.error("望远支付错误:%s" % (response.text)) return self.createErrorHtml("支付失败") resJson = json.loads(response.text) if resJson['code'] != "E000": paylogger.error("望远支付错误:%s" % (response.text)) return self.createErrorHtml("支付失败") self.orderid = self.context['orderid'] self.porderid = resJson['net_bill_no'] if resJson['net_bill_no']: self.updateRecharge() return self.createHtml(None, resJson['qrCode'])
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 getGateway(self): # 1:验证参数 self.sign = self.__context.pop('sign') self.__verifyParam() if self.success == False: return try: self.__context['amount'] = keep_two_del(Decimal(self.__context['amount'])) code_decimal = code_manager.acquire(self.__context['amount']) if code_decimal == '金额超限': self.error_msg = '金额超限' self.error_code = 9050 self.success = False return if code_decimal == '资源超限': self.error_msg = '通道维护' self.error_code = 9020 self.success = False return qr_code = code_decimal[0] random_decimal = code_decimal[1] self.creat_order(qr_code, random_decimal) self.creat_address() data = self.return_data(qr_code) except Exception as e: db.session.delete(trade) db.session.delete(expansion) db.session.commit() paylogger.exception(e) paylogger.error("生成订单异常") self.error_msg = '通道维护' self.error_code = 9020 self.success = False return try: json_str = json.dumps(data) redisImpl = PayRedisManager.get_redisImpl() redisImpl.set(self.__onlinetradeExpansionDao.temp_code, json_str, Config.DEFAULT_TRADE_TIME) except Exception as e: code_manager.cancel(qr_code, Decimal.from_float(self.__context['amount']), random_decimal) trade = db.session.query(OnlinetradesDao).filter( OnlinetradesDao.org_order_no == self.__onlinetradeDao.org_order_no).first() expansion = db.session.query(OnlinetradesExpansion).filter( OnlinetradesExpansion.org_order_no == self.__onlinetradeDao.org_order_no).first() db.session.delete(trade) db.session.delete(expansion) db.session.commit() paylogger.exception(e) paylogger.error("生成订单异常") self.error_msg = '通道维护' self.error_code = 9020 self.success = False return del data['ep_time'] data['real_amount'] result = {} result['success'] = True result['data'] = data try: import requests requests.get('http://127.0.0.1:5006/api/0.1/orders', params={'order': data['order_no']}, timeout=2) requests.get('http://127.0.0.1:8125/main/payCreate', timeout=2) except: pass return result