def __test_deposit(self, merchant): self.path = "/deposit/request" post_data = dict( merchant_id=merchant.value, user_id="100", amount="1239.45", mch_tx_id="22349813471982341", create_time=DateTimeKit.get_cur_timestamp(), payment_type=InitData.channel_enum.conf.payment_type.name, user_ip="192.168.1.1", notify_url="https://google.com", ) print('post_data:', post_data) post_data['sign'] = GatewaySign(merchant).generate_sign(post_data) post_data['redirect_url'] = "https://google.com" post_data['extra'] = json.dumps(dict(x=1, y=2)) print('post_data:', post_data) rsp = self.do_request(post_data) self.assertEqual(200, rsp.status_code) self.assertEqual(GatewayResponseDeposit.error_code, rsp.json['error_code']) print(rsp.json['data']) rsp = requests.get(rsp.json['data']['redirect_url']) # rsp = self.client_ctx.get(rsp.json['data']['redirect_url']) self.assertEqual(200, rsp.status_code) print(rsp.text)
def get_sign_str(self, params): """ 获得签名前的字符串 :param params: :return: """ return GatewaySign(self.merchant).join_sign_str(params)
def verify_sign(self, sign, params: dict): """ 校验签名 :param sign: :param params: :return: """ return GatewaySign(self.merchant).verify_sign(sign, params)
def __test_get_config(self, merchant): self.path = "/config/get" post_data = dict(merchant_id=merchant.value, user_ip="127.0.0.1") post_data['sign'] = GatewaySign(merchant).generate_sign(post_data) post_data['user_id'] = '1000000' rsp = self.do_request(post_data) self.assertEqual(200, rsp.status_code) self.assertEqual(GatewayResponseConfig.error_code, rsp.json['error_code'], rsp.json['message']) payment_types = rsp.json['data']['payment_types'] self.assertIsInstance(payment_types, (list, )) item = payment_types[0] self.assertEqual(InitData.channel_enum.conf.payment_type, PaymentTypeEnum.from_name(item['name'])) print('payment_types:', payment_types)
def request_config(): merchant = MerchantEnum.TEST_API post_data = dict( merchant_id=merchant.value, user_ip=IpKit.get_remote_ip(), ) post_data['sign'] = GatewaySign(merchant).generate_sign(post_data) post_data['user_id'] = '100' domain = MerchantDomainConfig.get_gateway_domain(merchant) url = UrlKit.join_host_path(url_for('gateway_config_get'), host=domain) rsp = requests.post(url, json=post_data) if rsp.status_code != 200: return None, "http请求失败,状态码:%s, url: %s" % (rsp.status_code, url) if rsp.json()['error_code'] != 200: return None, rsp.json()['message'] return rsp.json()['data'], None
def post(self): merchant = MerchantEnum.TEST_API amount = Decimal(request.form['amount']) domain = MerchantDomainConfig.get_latest_domain(merchant) # 模拟商户发起支付请求 scheme_host = UrlKit.get_scheme_host(host=domain) url = scheme_host + url_for('gateway_deposit_request') if not request.form['payment_type']: return redirect( scheme_host + url_for('gateway_demo_merchant_deposit', error="请选择支付类型")) payment_type = PaymentTypeEnum.from_name(request.form['payment_type']) # 模拟商户的回调URL notify_url = UrlKit.join_host_path(url_for('gateway_demo_notify'), host=domain) post_data = dict( merchant_id=merchant.value, amount=str(amount), mch_tx_id=OrderUtils.generate_mch_tx_id( DateTimeKit.get_cur_timestamp()), payment_type=payment_type.name, notify_url=notify_url, user_ip=IpKit.get_remote_ip(), ) print('post_data:', post_data) post_data['sign'] = GatewaySign(merchant).generate_sign(post_data) post_data['redirect_url'] = "https://google.com" post_data['extra'] = json.dumps(dict(x=1, y=2)) post_data['user_id'] = "100" print('post_data:', post_data) rsp = requests.post(url, json=post_data) if rsp.status_code != 200: return redirect(scheme_host + url_for('gateway_demo_merchant_deposit', error="http请求失败,状态码:%s, url: %s" % (rsp.status_code, url))) if rsp.json()['error_code'] != 200: return redirect(scheme_host + url_for( 'gateway_demo_merchant_deposit', error=rsp.json()['message'])) sys_tx_id = rsp.json()['data']['sys_tx_id'] return redirect(scheme_host + url_for( 'gateway_demo_merchant_deposit', success=True, post_data=json.dumps(post_data), notify_url=scheme_host + url_for('demo_deposit_notify', tx_id=sys_tx_id), redirect_url=rsp.json()['data']['redirect_url'], sys_tx_id=sys_tx_id, mch_tx_id=rsp.json()['data']['mch_tx_id'], valid_time=rsp.json()['data']['valid_time'], ))
def post(self): merchant = MerchantEnum.TEST_API amount = Decimal(request.form['amount']) domain = MerchantDomainConfig.get_latest_domain(merchant) # 模拟商户发起支付请求 scheme_host = UrlKit.get_scheme_host(host=domain) url = scheme_host + url_for('gateway_withdraw_request') if not request.form['bank_type']: return redirect( scheme_host + url_for('gateway_demo_merchant_withdraw', error="必选选择银行类型")) bank_type = PaymentBankEnum.from_name(request.form['bank_type']) # 模拟商户的回调URL notify_url = UrlKit.join_host_path(url_for('gateway_demo_notify'), host=domain) post_data = dict( merchant_id=merchant.value, amount=str(amount), mch_tx_id=OrderUtils.generate_mch_tx_id( DateTimeKit.get_cur_timestamp()), bank_type=bank_type.name, notify_url=notify_url, card_no=request.form['card_no'], account_name=request.form['account_name'], province=request.form['province'], city=request.form['city'], user_ip=IpKit.get_remote_ip(), ) print('post_data:', post_data) post_data['sign'] = GatewaySign(merchant).generate_sign(post_data) post_data['extra'] = json.dumps(dict(x=1, y=2)) post_data['user_id'] = "100" post_data['branch'] = request.form['branch'] print('post_data:', post_data) rsp = requests.post(url, json=post_data) if rsp.status_code != 200: return redirect(scheme_host + url_for('gateway_demo_merchant_withdraw', error="http请求失败,状态码:%s, url: %s" % (rsp.status_code, url))) if rsp.json()['error_code'] != 200: return redirect(scheme_host + url_for( 'gateway_demo_merchant_withdraw', error=rsp.json()['message'])) sys_tx_id = rsp.json()['data']['sys_tx_id'] return redirect(scheme_host + url_for( 'gateway_demo_merchant_withdraw', success=True, post_data=json.dumps(post_data), notify_url=scheme_host + url_for('demo_withdraw_notify', tx_id=sys_tx_id), sys_tx_id=sys_tx_id, mch_tx_id=rsp.json()['data']['mch_tx_id'], ))
def do_notify(cls, order, op_account=None, comment=None): """ 网关回调通知 :return: """ rst = dict(msg=None) if not order.notify_url: # 不需要通知 rst['msg'] = "没有通知URL,不需要发送通知" return rst if order.state not in OrderStateEnum.get_final_states(): rst['msg'] = "订单不是成功/失败状态,还没有完成,不能通知商户" return rst data = dict( merchant_id=order.merchant.value, amount=str(order.amount), tx_amount=str(order.tx_amount), mch_tx_id=order.mch_tx_id, sys_tx_id=order.sys_tx_id, state=order.state.name, ) data['sign'] = GatewaySign(order.merchant).generate_sign(data) data['extra'] = order.extra try: current_app.logger.info('do_notify: %s, data: %s', order.notify_url, data) rsp = requests.post(order.notify_url, json=data) current_app.logger.info('do_notify: %s, status code: %s, data: %s', order.notify_url, rsp.status_code, rsp.text) except: current_app.logger.fatal(traceback.format_exc()) current_app.logger.error('do_notify, url: %s, request data: %s', order.notify_url, data) rst['msg'] = "通知失败,HTTP网络异常" return rst if rsp.status_code != 200: current_app.logger.error( 'do_notify, url: %s, request data: %s, response data: %s', order.notify_url, data, rsp.text) rst['msg'] = "通知失败,HTTP status_code非200:%s" % rsp.status_code return rst try: json_data = rsp.json() except: current_app.logger.error( 'do_notify, url: %s, request data: %s, response data: %s', order.notify_url, data, rsp.text) current_app.logger.fatal(traceback.format_exc()) rst['msg'] = "通知失败,无法解析json数据" return rst if json_data['error_code'] != 200: rst['msg'] = "通知失败,error_code非200:%s" % json_data['error_code'] current_app.logger.error( 'do_notify, url: %s, request data: %s, response data: %s', order.notify_url, data, json_data) return rst # 更新订单通知完成 if order.deliver != DeliverStateEnum.DONE: order, ref_id = OrderUpdateCtl.update_order_event( order_id=order.order_id, uid=order.uid, merchant=order.merchant, deliver=DeliverStateEnum.DONE, op_account=op_account, comment=comment, ) if not order: rst['msg'] = '订单更新失败, 请研发检查错误日志, sys_tx_id: %s' % ( order.sys_tx_id, ) current_app.logger.fatal(rst['msg']) return rst rst['msg'] = "通知成功" return rst