#1、xml格式化请求
def xml_request(self):
body = '''<?xml version="1.0" encoding="utf-8"?>
<payment_request>
<zf_head>
<core_merchant_no>{0}</core_merchant_no>
<version>{1}</version>
<pay_channel>{2}</pay_channel>
<pay_transaction_id>{3}</pay_transaction_id>
<out_trade_no>{4}</out_trade_no>
</zf_head>
<paymentId>{5}</paymentId>
<reqSerialNum>{6}</reqSerialNum>
<memberId>{7}</memberId>
</payment_request>'''\
.format(self.request_data['core_merchant_no'],
self.request_data['version'],
self.request_data['pay_channel'],
self.request_data['pay_transaction_id'],
self.request_data['out_trade_no'],
self.request_data['paymentId'],
self.request_data['reqSerialNum'],
self.request_data['memberId'],)
#请求返回
r = requests.post(self.url, data=body.encode("utf-8"))
return r.text
#2、json请求
def json_request(self,zf_info):
request={
"body":
{"out_trade_no":"{0}".format(zf_info['body']['out_trade_no'])},
"head":
{"service":"front.query",
"version":"1.0",
"partner_id":"10000002048131212",
"core_merchant_no":"{0}".format(zf_info['head']['core_merchant_no']),
"sign":"",
"sign_type":"MD5",
"input_charset":"UTF-8"} }
params = {
"requestJson": json.dumps(request)
}
r = requests.post(self.url, data=params)
return r.text
#3、文件上传请求
def file_request(self):
body={'reqXml':'<?xml version="1.0" encoding="utf-8"?>'
'<payment_request>'
'<zf_head>'
'<core_merchant_no>{0}</core_merchant_no>'
'<version>{1}</version> '
'<pay_channel>{2}</pay_channel>'
'<pay_transaction_id>{3}</pay_transaction_id>'
' <out_trade_no>{4}</out_trade_no>'
'</zf_head> '
'<paymentId>{5}</paymentId>'
'<reqSerialNum>{6}</reqSerialNum>'
'<memberId>{7}</memberId>'
'<photoType>{8}</photoType>'
'<picture>{9}</picture> </payment_request>'
.format(self.request_data['core_merchant_no'],
self.request_data['version'],
self.request_data['pay_channel'],
self.request_data['pay_transaction_id'],
self.request_data['out_trade_no'],
self.request_data['paymentId'],
self.request_data['reqSerialNum'],
self.request_data['memberId'],
self.request_data['photoType'],
self.request_data['picture'])}
f={'picture':open('D:\\aa.jpg','rb')}
#请求返回
r = requests.post(self.url,data=body,files=f)
return r.text