代码改变世界

部分还款-还款试算接口与还款接口-python

2019-03-04 14:40  改改~_~  阅读(898)  评论(0编辑  收藏  举报

一、还款试算、还款接口,

1、只传入参数loan_Code

2、还款接口参数化以下:

"loanCode": loanCode1,
"orderId": orderId1,
"payMoney": payMoneytotal2,

import requests
import json
import pymysql

loanCode1="JKSQ20220610049084DK"
'''
# 还款试算接口
'''
ur1='http://10.253.43.83:8399/repayment/order/repaymentTrial'
headers = {'Content-Type':'application/json','Accept': '*/*'}
Value = {
  "accountNo": "ZH20220202071059",
  "channelNo": "0014",
  "endPeriod": 1,
  "isSettle": "1",
  "loanCode": loanCode1,
  "payCode": "5815042292859904",
  "period": 1,
  "repayWay": "2"
}
r = requests.post(url=ur1,json=Value,headers=headers)
repones2 =r.json()
repones3=json.dumps(repones2,indent=4,ensure_ascii=False)#ensure_ascii=False将返回的json汉字化
print('1',repones3)
print('2',r.status_code)
print('3',r.headers)

repones = r.text
print('11',repones)
print(type(repones))#<class 'str'>

s=repones
s_dic=eval(s)
print('s_dic的类型为:',type(s_dic))#自发一串转换为字典
print('s_dic:',s_dic)
payBen=s_dic['data']['payBen']#打印字典中的某个值
payIni=s_dic['data']['payIni']#打印字典中的某个值
payFee=s_dic['data']['payFee']#打印字典中的某个值
payMoney=s_dic['data']['payMoney']#打印字典中的某个值
orderId1=s_dic['data']['orderId']#打印字典中的某个值
print('payBen:',payBen,'类型为:',type(payBen))
print('payIni:',payIni,'类型为:',type(payIni))
print('payFee:',payFee,'类型为:',type(payFee))
print('payMoney:',payMoney,'类型为:',type(payMoney))
print('orderId:',orderId1,'类型为:',type(orderId1))

#判断payMoney的值是否为payBen、payIni、payFee的和
payMoneytotal=payBen+payIni+payFee
print(payMoneytotal)
payMoneytotal2=round(payMoneytotal,2)#取小数点后两位数
print(payMoneytotal2)
if payMoneytotal2==payMoney:
  print('payMoney的值,PASS:','payBen+payIni+payFee=payMoney')
else:
  print('呵呵哒...')

# 连接数据判断接口返回的值是否和数据表中的一致
mydb = pymysql.connect(host="172.31.20.2", port=3306, user="root", passwd="sjroot", db="user", charset='utf8')
mycursor = mydb.cursor()
sql = "select must_amt from repay.t_external_borrow_repay where loan_code = 'JKSQ20220208048780DK' and period=1"
mycursor.execute(sql)
myresult =mycursor.fetchall()
for x in myresult:
    # print(x,type(x))
    print(x[0])

sql2 = "select must_amt from repay.t_external_borrow_fee_asset where loan_code = 'JKSQ20220208048780DK' and period=1"
mycursor.execute(sql2)
myresult =mycursor.fetchall()
for x in myresult:
    # print(x,type(x))
    print(x[0])



'''
还款接口
'''
ur12='http://10.253.43.83:8399/repayment/order/repaymentRequest'
headers2 = {'Content-Type':'application/json','Accept': '*/*'}
Value2 = {
  "accountNo": "ZH20220202071059",
  "accountType": "capital",
  "channelNo": "0014",
  "endPeriod": 1,
  "loanCode": loanCode1,
  "orderId": orderId1,
  "payCode": "5815042292859904",
  "payMoney": payMoneytotal2,
  "period": 1,
  "repayWay": "2",
  "settleStatus": "2"
}
r = requests.post(url=ur12,json=Value2,headers=headers2)
repones2 =r.json()
repones3=json.dumps(repones2,indent=4,ensure_ascii=False)#ensure_ascii=False将返回的json汉字化
print('11',repones3)
print('22',r.status_code)
print('33',r.headers)

repones = r.text
print('11',repones)
print(type(repones))#<class 'str'>

 

 

运行结果:

D:\Python3\python.exe E:/python/20190221接口/还款试算与还款接口.py
1 {
    "msg": "成功",
    "data": {
        "loanCode": "JKSQ20220610049084DK",
        "payBen": 1146.34,
        "payIni": 49.59,
        "payFee": 245.03,
        "payMoney": 1440.96,
        "payTime": "2022-07-10 09:01:55",
        "period": 1,
        "endPeriod": 1,
        "repayWay": "2",
        "settleStatus": "4",
        "payCode": "5815042292859904",
        "accountNo": "ZH20220202071059",
        "channelNo": "0014",
        "orderId": "101002_2022071009015429687561453",
        "feeList": [
            {
                "feeName": "jlgA首金服务费(期收)",
                "feeCode": "FC20181214163415006",
                "feeMoney": 245.03
            }
        ]
    },
    "code": 20000
}
2 200
3 {'X-Application-Context': 'repayment-asset-order-web:pre:8399', 'X-Request-Id': 'x-9d45dfbc-90e1-4950-9dc4-e121a68aa30f', 'Content-Type': 'application/json;charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Sun, 10 Jul 2022 01:01:55 GMT'}
11 {"msg":"成功","data":{"loanCode":"JKSQ20220610049084DK","payBen":1146.34,"payIni":49.59,"payFee":245.03,"payMoney":1440.96,"payTime":"2022-07-10 09:01:55","period":1,"endPeriod":1,"repayWay":"2","settleStatus":"4","payCode":"5815042292859904","accountNo":"ZH20220202071059","channelNo":"0014","orderId":"101002_2022071009015429687561453","feeList":[{"feeName":"jlgA首金服务费(期收)","feeCode":"FC20181214163415006","feeMoney":245.03}]},"code":20000}
<class 'str'>
s_dic的类型为: <class 'dict'>
s_dic: {'msg': '成功', 'data': {'loanCode': 'JKSQ20220610049084DK', 'payBen': 1146.34, 'payIni': 49.59, 'payFee': 245.03, 'payMoney': 1440.96, 'payTime': '2022-07-10 09:01:55', 'period': 1, 'endPeriod': 1, 'repayWay': '2', 'settleStatus': '4', 'payCode': '5815042292859904', 'accountNo': 'ZH20220202071059', 'channelNo': '0014', 'orderId': '101002_2022071009015429687561453', 'feeList': [{'feeName': 'jlgA首金服务费(期收)', 'feeCode': 'FC20181214163415006', 'feeMoney': 245.03}]}, 'code': 20000}
payBen: 1146.34 类型为: <class 'float'>
payIni: 49.59 类型为: <class 'float'>
payFee: 245.03 类型为: <class 'float'>
payMoney: 1440.96 类型为: <class 'float'>
orderId: 101002_2022071009015429687561453 类型为: <class 'str'>
1440.9599999999998
1440.96
payMoney的值,PASS: payBen+payIni+payFee=payMoney
1146.34
49.59
245.0300
11 {
    "msg": "成功",
    "data": {
        "msg": "还款申请已提交",
        "code": 20000
    },
    "code": 20000
}
22 200
33 {'X-Application-Context': 'repayment-asset-order-web:pre:8399', 'X-Request-Id': 'x-09755d11-ebd3-4c4a-ae83-7c68d06e70ce', 'Content-Type': 'application/json;charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Sun, 10 Jul 2022 01:01:57 GMT'}
11 {"msg":"成功","data":{"msg":"还款申请已提交","code":20000},"code":20000}
<class 'str'>

Process finished with exit code 0

二、优化

解决只传loan_code调用试算接口、还款接口

需要参数化:

"accountNo": "ZH20220202071059",
  "payCode": "5815042292859904",
这两个值从"select user_id,account_no from repay.t_asset_core where loan_code =‘JKSQ20220710049184DK’取出

 以下调试取出accountNopayCode

import requests
import json
import pymysql
loanCode1='JKSQ20220710049184DK'
'''
# 还款试算接口
'''

mydb = pymysql.connect(host="172.31.20.2", port=3306, user="root", passwd="sjroot", db="user", charset='utf8')
mycursor = mydb.cursor()
sql = "select user_id,account_no from repay.t_asset_core where loan_code = '{0}';".format(loanCode1)
# sql = "select user_id,account_no from repay.t_asset_core where loan_code = 'JKSQ20220711049208DK'"
mycursor.execute(sql)
myresult =mycursor.fetchall()
for x in myresult:
    print('user_id,account_no:',x,type(x))

user_id2=x[0]
account_no2=x[1]
print('user_id2:',user_id2)
print('account_no2:',account_no2)
# user_id3='"'+user_id2+'"'
# account_no3='"'+account_no2+'"'
# print('user_id3:',user_id3)
# print('account_no3:',account_no3)

ur1='http://10.253.43.83:8399/repayment/order/repaymentTrial'
headers = {'Content-Type':'application/json','Accept': '*/*'}
Value = {
  "accountNo": account_no2,
  "channelNo": "0014",
  "endPeriod": 1,
  "isSettle": "1",
  "loanCode": loanCode1,
  "payCode": user_id2,
  "period": 1,
  "repayWay": "2"
}
r = requests.post(url=ur1,json=Value,headers=headers)
repones2 =r.json()
repones3=json.dumps(repones2,indent=4,ensure_ascii=False)#ensure_ascii=False将返回的json汉字化
print('1',repones3)
print('2',r.status_code)
print('3',r.headers)

执行结果:

D:\Python3\python.exe E:/python/20190221接口/33.py
user_id,account_no: ('4108102770459648', 'ZH9595100061069003') <class 'tuple'>
user_id2: 4108102770459648
account_no2: ZH9595100061069003
1 {
    "msg": "成功",
    "data": {
        "loanCode": "JKSQ20220710049184DK",
        "payTime": "2022-08-11 10:12:50",
        "period": 1,
        "endPeriod": 1,
        "repayWay": "2",
        "settleStatus": "4",
        "payCode": "4108102770459648",
        "accountNo": "ZH9595100061069003",
        "channelNo": "0014",
        "orderId": "101002_2022081110124980996895558",
        "feeList": []
    },
    "code": 20000
}
2 200
3 {'X-Application-Context': 'repayment-asset-order-web:pre:8399', 'X-Request-Id': 'x-defa0473-70c7-4966-a9a5-2f963d88fab4', 'Content-Type': 'application/json;charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Thu, 11 Aug 2022 02:12:50 GMT'}

Process finished with exit code 0

三、完整的只传入loan_code即完成还款试算、还款接口的调用

import requests
import json
import pymysql

loanCode1="JKSQ20220710049178DK"
'''
# 还款试算接口
'''
#参数化取出user_id,account_no
mydb = pymysql.connect(host="172.31.20.2", port=3306, user="root", passwd="sjroot", db="user", charset='utf8')
mycursor = mydb.cursor()
sql = "select user_id,account_no from repay.t_asset_core where loan_code = '{0}';".format(loanCode1)
# sql = "select user_id,account_no from repay.t_asset_core where loan_code = 'JKSQ20220711049208DK'"
mycursor.execute(sql)
myresult =mycursor.fetchall()
for x in myresult:
    print('user_id,account_no:',x,type(x))
user_id2=x[0]
account_no2=x[1]
print('user_id2:',user_id2)
print('account_no2:',account_no2)
# user_id3='"'+user_id2+'"'
# account_no3='"'+account_no2+'"'
# print('user_id3:',user_id3)
# print('account_no3:',account_no3)#加上这部分会出错的

#调用还款试算接口
ur1='http://10.253.43.83:8399/repayment/order/repaymentTrial'
headers = {'Content-Type':'application/json','Accept': '*/*'}
Value = {
  "accountNo": account_no2,
  "channelNo": "0014",
  "endPeriod": 1,
  "isSettle": "1",
  "loanCode": loanCode1,#参数化
  "payCode": user_id2,#参数化
  "period": 1,
  "repayWay": "2"
}
r = requests.post(url=ur1,json=Value,headers=headers)
repones2 =r.json()
repones3=json.dumps(repones2,indent=4,ensure_ascii=False)#ensure_ascii=False将返回的json汉字化
print('1',repones3)
print('2',r.status_code)
print('3',r.headers)

repones = r.text
print('11',repones)
print(type(repones))#<class 'str'>

s=repones
s_dic=eval(s)
print('s_dic的类型为:',type(s_dic))#自发一串转换为字典
print('s_dic:',s_dic)
payBen=s_dic['data']['payBen']#打印字典中的某个值
payIni=s_dic['data']['payIni']#打印字典中的某个值
payFee=s_dic['data']['payFee']#打印字典中的某个值
payMoney=s_dic['data']['payMoney']#打印字典中的某个值
orderId1=s_dic['data']['orderId']#打印字典中的某个值
print('payBen:',payBen,'类型为:',type(payBen))
print('payIni:',payIni,'类型为:',type(payIni))
print('payFee:',payFee,'类型为:',type(payFee))
print('payMoney:',payMoney,'类型为:',type(payMoney))
print('orderId:',orderId1,'类型为:',type(orderId1))

#判断payMoney的值是否为payBen、payIni、payFee的和
payMoneytotal=payBen+payIni+payFee
print(payMoneytotal)
payMoneytotal2=round(payMoneytotal,2)#取小数点后两位数
print('payMoneytotal2:',payMoneytotal2)
if payMoneytotal2==payMoney:
  print('payMoney的值,PASS:','payBen+payIni+payFee=payMoney')
else:
  print('呵呵哒...')

'''
以下这部分待完善。。。
'''
# 连接数据判断接口返回的值是否和数据表中的一致
mydb = pymysql.connect(host="172.31.20.2", port=3306, user="root", passwd="sjroot", db="user", charset='utf8')
mycursor = mydb.cursor()
sql = "select must_amt from repay.t_external_borrow_repay where loan_code = 'JKSQ20220208048780DK' and period=1"
mycursor.execute(sql)
myresult =mycursor.fetchall()
for x in myresult:
    # print(x,type(x))
    print(x[0])

sql2 = "select must_amt from repay.t_external_borrow_fee_asset where loan_code = 'JKSQ20220208048780DK' and period=1"
mycursor.execute(sql2)
myresult =mycursor.fetchall()
for x in myresult:
    # print(x,type(x))
    print(x[0])



'''
还款接口
'''
#调用还款接口
ur12='http://10.253.43.83:8399/repayment/order/repaymentRequest'
headers2 = {'Content-Type':'application/json','Accept': '*/*'}
Value2 = {
  "accountNo": account_no2,#参数化
  "accountType": "capital",
  "channelNo": "0014",
  "endPeriod": 1,
  "loanCode": loanCode1,#参数化
  "orderId": orderId1,#参数化
  "payCode": user_id2,#参数化
  "payMoney": payMoneytotal2,#参数化
  "period": 1,
  "repayWay": "2",
  "settleStatus": "2"
}
r = requests.post(url=ur12,json=Value2,headers=headers2)
repones2 =r.json()
repones3=json.dumps(repones2,indent=4,ensure_ascii=False)#ensure_ascii=False将返回的json汉字化
print('11',repones3)
print('22',r.status_code)
print('33',r.headers)

repones = r.text
print('11',repones)
print(type(repones))#<class 'str'>

运行后的结果:

D:\Python3\python.exe E:/python/20190221接口/还款试算与还款接口2.py
user_id,account_no: ('4108102770459648', 'ZH9595100061069003') <class 'tuple'>
user_id2: 4108102770459648
account_no2: ZH9595100061069003
1 {
    "msg": "成功",
    "data": {
        "loanCode": "JKSQ20220710049178DK",
        "payBen": 112.3,
        "payIni": 4.67,
        "payFee": 40.0,
        "payMoney": 156.97,
        "payTime": "2022-08-11 10:15:31",
        "period": 1,
        "endPeriod": 1,
        "repayWay": "2",
        "settleStatus": "4",
        "payCode": "4108102770459648",
        "accountNo": "ZH9595100061069003",
        "channelNo": "0014",
        "orderId": "101002_2022081110153112167045558",
        "feeList": [
            {
                "feeName": "靖哲还款时资管费",
                "feeCode": "FC20181102011507002",
                "feeMoney": 40.0
            }
        ]
    },
    "code": 20000
}
2 200
3 {'X-Application-Context': 'repayment-asset-order-web:pre:8399', 'X-Request-Id': 'x-44ddc786-1eaa-423e-ba8f-8b335d497b8c', 'Content-Type': 'application/json;charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Thu, 11 Aug 2022 02:15:31 GMT'}
11 {"msg":"成功","data":{"loanCode":"JKSQ20220710049178DK","payBen":112.3,"payIni":4.67,"payFee":40.0,"payMoney":156.97,"payTime":"2022-08-11 10:15:31","period":1,"endPeriod":1,"repayWay":"2","settleStatus":"4","payCode":"4108102770459648","accountNo":"ZH9595100061069003","channelNo":"0014","orderId":"101002_2022081110153112167045558","feeList":[{"feeName":"靖哲还款时资管费","feeCode":"FC20181102011507002","feeMoney":40.0}]},"code":20000}
<class 'str'>
s_dic的类型为: <class 'dict'>
s_dic: {'msg': '成功', 'data': {'loanCode': 'JKSQ20220710049178DK', 'payBen': 112.3, 'payIni': 4.67, 'payFee': 40.0, 'payMoney': 156.97, 'payTime': '2022-08-11 10:15:31', 'period': 1, 'endPeriod': 1, 'repayWay': '2', 'settleStatus': '4', 'payCode': '4108102770459648', 'accountNo': 'ZH9595100061069003', 'channelNo': '0014', 'orderId': '101002_2022081110153112167045558', 'feeList': [{'feeName': '靖哲还款时资管费', 'feeCode': 'FC20181102011507002', 'feeMoney': 40.0}]}, 'code': 20000}
payBen: 112.3 类型为: <class 'float'>
payIni: 4.67 类型为: <class 'float'>
payFee: 40.0 类型为: <class 'float'>
payMoney: 156.97 类型为: <class 'float'>
orderId: 101002_2022081110153112167045558 类型为: <class 'str'>
156.97
payMoneytotal2: 156.97
payMoney的值,PASS: payBen+payIni+payFee=payMoney
1146.34
49.59
245.0300
11 {
    "msg": "成功",
    "data": {
        "msg": "还款申请已提交",
        "code": 20000
    },
    "code": 20000
}
22 200
33 {'X-Application-Context': 'repayment-asset-order-web:pre:8399', 'X-Request-Id': 'x-d1610eb0-041d-45b7-a079-5855baaa5a8b', 'Content-Type': 'application/json;charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Thu, 11 Aug 2022 02:15:33 GMT'}
11 {"msg":"成功","data":{"msg":"还款申请已提交","code":20000},"code":20000}
<class 'str'>

Process finished with exit code 0