def init_withdraw_order_deal(cls, amount): """ 初始化一个提款订单 :param amount: :return: """ client_ip = '127.0.0.1' user = cls.get_user() bank_card = cls.get_bank_card() admin_user = cls.get_admin_user() order, error = WithdrawTransactionCtl.order_create( user=user, amount=amount, client_ip=client_ip, user_bank_id=bank_card.card_id, ) assert error is None rst = WithdrawTransactionCtl.order_alloc(admin_user.account, order.order_id, cls.merchant) assert isinstance(rst, (ResponseSuccess, )) channel = cls.get_withdraw_channel() rst = WithdrawTransactionCtl.order_deal(admin_user.account, order.order_id, order.merchant, channel.channel_id, test=True) assert isinstance(rst, (ResponseSuccess, )) return WithdrawTransactionCtl.get_order(order.sys_tx_id)
def get(self): """ 立马付充值回调 :return: """ if not EnvironEnum.is_local_evn(current_app.config['FLASK_ENV']): # 无论如何都记录一条log current_app.logger.info( 'ponypay withdraw callback, ip: %s, data: %s', IpKit.get_remote_ip(), request.args) form, error = PonyPayWithdrawForm().request_validate() if error: current_app.logger.fatal('msg: %s, data: %s', error.message, request.args) return BaseResponse('FAIlURE') # 交易订单id tx_id = form.corderid.data # 实际支付金额 tx_amount = Decimal(form.money.data) # 订单状态: 成功/失败 status = form.status.data # 签名 sign = form.sign.data # 客户端IP client_ip = form.client_ip.data order = WithdrawTransactionCtl.get_order(tx_id) if not order: return BaseResponse('FAIlURE') channel_config = ChannelConfig.query_by_channel_id(order.channel_id) controller = WithdrawCallbackPonypay(channel_config.channel_enum) # IP白名单校验 if not controller.check_ip(client_ip): current_app.logger.fatal('ip not allow, client_ip: %s, data: %s', client_ip, request.args) return BaseResponse('FAIlURE') # 签名验证 if not controller.check_sign(tx_id, tx_amount, sign): current_app.logger.fatal('invalid sign, client_ip: %s, data: %s', client_ip, request.args) return BaseResponse('FAIlURE') if status == '1': # 支付成功 if not WithdrawTransactionCtl.order_success(order, tx_amount): return BaseResponse('FAIlURE') else: # 支付失败 if not WithdrawTransactionCtl.order_fail(order): return BaseResponse('FAIlURE') return BaseResponse('SUCCESS')
def post(self): if not EnvironEnum.is_local_evn(current_app.config['FLASK_ENV']): # 无论如何都记录一条log current_app.logger.info('yinsao withdraw callback, ip: %s, data: %s, headers: %s', IpKit.get_remote_ip(), request.form, request.headers) # form, error = YinSaoForm.request_validate() # if error: # current_app.logger.fatal('msg: %s, data: %s', error.message, request.json) # return BaseResponse('FAIlURE') client_ip = IpKit.get_remote_ip() if not CallbackYinSao.check_ip(client_ip): current_app.logger.fatal('ip not allow, client_ip: %s, data: %s', client_ip, request.args) return BaseResponse('FAIlURE') data = {k: v for k, v in request.form.items()} tx_amount = Decimal(data['transAmt']) / Decimal('100') sys_tx_id = data['orderNo'] signature = data.pop('signature') key_list = sorted(list(data.keys())) request_str = "&".join(["{}={}".format(field, data[field]) for field in key_list if field != "risk"]) sign = CallbackYinSao.generate_sign(request_str) flag = CallbackYinSao.check_sign(signature, sign) if not flag: current_app.logger.fatal('invalid sign, data: %s, sign: %s, signature: %s', client_ip, data, sign, signature) return BaseResponse('FAIlURE') order = WithdrawTransactionCtl.get_order(sys_tx_id) if not order: return BaseResponse('FAIlURE') if str(data['respCode']) == '0000': # 支付成功 if not WithdrawTransactionCtl.order_success(order, tx_amount): return BaseResponse('FAILURE') else: # 支付失败 if not WithdrawTransactionCtl.order_fail(order): return BaseResponse('FAIlURE') return BaseResponse('SUCCESS')
def init_withdraw_order_alloc(cls, amount): client_ip = '127.0.0.1' user = cls.get_user() bank_card = cls.get_bank_card() admin_user = cls.get_admin_user() order, error = WithdrawTransactionCtl.order_create( user=user, amount=amount, client_ip=client_ip, user_bank_id=bank_card.card_id, ) assert error is None rst = WithdrawTransactionCtl.order_alloc(admin_user.account, order.order_id, cls.merchant) assert isinstance(rst, (ResponseSuccess, )) return WithdrawTransactionCtl.get_order(order.sys_tx_id)
def get(self): """ 手动通知商户 :return: """ if not request.args: return ResponseSuccess(message="参数规则:?tx_id=xx").as_response() try: tx_id = request.args['tx_id'] except: return ResponseSuccess(message="请输入 tx_id=,系统交易ID").as_response() order = WithdrawTransactionCtl.get_order(tx_id) rst = WithdrawTransactionCtl.do_notify( order=order, op_account='somebody', comment="后台人工状态通知", ) return ResponseSuccess(message=rst['msg']).as_response()
def post(self): """ 给商户发通知 :return: """ form, error = OrderStateNotifyFrom().request_validate() if error: return error.as_response() order_id = str(form.order_id.data) if form.type.data == PayTypeEnum.WITHDRAW: order = WithdrawTransactionCtl.get_order(order_id) rst = WithdrawTransactionCtl.do_notify(order=order, op_account=g.user.account, comment="商户后台手动通知") elif form.type.data == PayTypeEnum.DEPOSIT: order = DepositTransactionCtl.get_order(order_id) rst = DepositTransactionCtl.do_notify( order=order, op_account=g.user.account, comment="商户后台手动通知", ) return ResponseSuccess(message=rst['msg'])
def post(self): if not EnvironEnum.is_local_evn(current_app.config['FLASK_ENV']): # 无论如何都记录一条log current_app.logger.info( 'onepay withdraw callback, ip: %s, headers: %s, json data: %s', IpKit.get_remote_ip(), request.headers, request.json) # form, error = YinSaoForm.request_validate() # if error: # current_app.logger.fatal('msg: %s, data: %s', error.message, request.json) # return BaseResponse('FAIlURE') current_app.logger.fatal( 'onepay withdraw callback, ip: %s, form data: %s, headers: %s, json data: %s', IpKit.get_remote_ip(), request.form, request.headers, request.json) client_ip = IpKit.get_remote_ip() if not CallbackOnePay.check_ip(client_ip): current_app.logger.fatal('ip not allow, client_ip: %s, data: %s', client_ip, request.args) return BaseResponse('FAIlURE') flag = request.json['flag'] if flag == 'FAILED': return BaseResponse('FAIlURE') data = {k: v for k, v in request.json['data'].items()} no_need_sign = ['totalFactorage', 'sign', 'signType', 'detailList'] signature = data.pop('sign') key_list = sorted(list(data.keys())) request_str = "&".join([ "{}={}".format(field, data[field]) for field in key_list if data.get(field, False) and field not in no_need_sign ]) flag = CallbackOnePay.check_sign(signature, request_str) if not flag: current_app.logger.fatal( 'invalid sign, data: %s, sign: %s, data: %s', client_ip, data, signature, request_str) return BaseResponse('FAIlURE') withdraw_order = request.json['data']['detailList'][0] tx_id = withdraw_order['serialNo'] tx_amount = withdraw_order['amount'] order = WithdrawTransactionCtl.get_order(tx_id) if not order: return BaseResponse('FAIlURE') if str(withdraw_order['tradeStatus']) == '1': # 支付成功 if not WithdrawTransactionCtl.order_success( order, decimal.Decimal(tx_amount)): return BaseResponse('FAILURE') elif str(withdraw_order['tradeStatus']) == '2': # 支付失败 if not WithdrawTransactionCtl.order_fail(order): return BaseResponse('FAIlURE') return BaseResponse('SUCCESS')
def post(self): """ 专一付代付回调 :return: """ if not EnvironEnum.is_local_evn(current_app.config['FLASK_ENV']): # 无论如何都记录一条log current_app.logger.info( 'zhuanyifu withdraw callback, ip: %s, data: %s, headers: %s', IpKit.get_remote_ip(), request.json, request.headers) event = request.headers.get('ChinaRailway-Event') signature = request.headers.get('ChinaRailway-Signature') form, error = ZhuanYeFuWithdrawForm().request_validate() if error: current_app.logger.fatal('msg: %s, data: %s', error.message, request.args) return BaseResponse('FAIlURE') pp = signature.split('.') if event != "Pay.Succeeded": return ResponseSuccess(code=500).as_response() # 交易ID tx_id = form.order.data fee = Decimal(form.fee.data) order = WithdrawTransactionCtl.get_order(tx_id) if not order: return ResponseSuccess( code=500, message='curr order no found').as_response() curr_status = order.state if curr_status != OrderStateEnum.DEALING: return ResponseSuccess( code=500, message='curr order status must be DEALING').as_response() print(order.channel_id, form.order.data, order.merchant, order.create_time, order.order_id, order.uid) channel_config = ProxyChannelConfig.query_by_channel_id( order.channel_id) channel_cost = FeeCalculator.calc_cost(order.amount, channel_config.fee_type, channel_config.fee) if fee != channel_cost: current_app.logger.error( "ZYF withdraw fee info order_id:{}, channel_fee: {}, channel_cost:{}" .format(order.order_id, fee, channel_cost)) try: flag = CryptoKit.rsa_verify( pp[1], pp[0], channel_config.channel_enum.conf['plat_public_key']) if flag != True: return ResponseSuccess(code=500, message='签名错误').as_response() except Exception as e: return ResponseSuccess(code=500).as_response() # 代付金额 tx_amount = Decimal(form.amount.data) # 代付费率 fee = Decimal(form.fee.data) # 通道订单号 transaction = form.transaction.data client_ip = form.client_ip.data status = form.status.data if str(status) == "1": """ 修改订单状态, 记录代付费率 """ if not WithdrawTransactionCtl.order_success(order, tx_amount): return ResponseSuccess(code=500).as_response() elif str(status) == "2": """ 代付订单失败, 1.给用户退款,给商户退款+手续费 2. 修改订单状态 """ # order = WithdrawTransactionCtl.get_order(merchant, order_id) if not WithdrawTransactionCtl.order_fail(order): return ResponseSuccess(code=500).as_response() return ResponseSuccess(code=204).as_response()