写在前面:标红的地方是需要注意的,这里很容易搞混到底使用平台证书还是商户证书。
【服务商模式】
请求头
POST https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Referer: https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586
Content-Type: application/json
Authorization: WECHATPAY2-SHA256-RSA2048 mchid="服务商商户号ID",nonce_str="dxt55guj.rny",timestamp="1611040484",serial_no="服务商证书序列号",signature="使用服务商证书加密的字符串(格式:HTTP请求方法\nURL\n请求时间戳\n请求随机串\n请求报文主体\n)"
Host: api.mch.weixin.qq.com
Content-Length: 360
Connection: Keep-Alive
请求体:
{"sp_appid":"服务商服务号APPID","sp_mchid":"服务商商户号ID","sub_appid":"子商户对应appid","sub_mchid":"子商户商户号ID","payer":{"sub_openid":"子商户对应appid对应的openid"},"description":"描述","out_trade_no":"61213d2c3c48436f9de68b0455e5bde0","notify_url":"http://www.mirsh.cn","amount":{"total":1,"currency":"CNY"},"scene_info":{"payer_client_ip":"127.0.0.1"}}
【直连模式】
请求头
POST https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Referer: https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586
Content-Type: application/json
Authorization: WECHATPAY2-SHA256-RSA2048 mchid="商户号ID",nonce_str="jlujyhrk.vxs",timestamp="1611041033",serial_no="商户号证书序列号",signature="使用商户证书加密的字符串(格式:HTTP请求方法\nURL\n请求时间戳\n请求随机串\n请求报文主体\n)"
Host: api.mch.weixin.qq.com
Content-Length: 292
Connection: Keep-Alive
请求体
{"appid":"商户号对应的appid","mchid":"商户号ID","payer":{"openid":"oXY6B4sjZO4svmWVuLktyDT9mlsU"},"description":"描述","out_trade_no":"fcf799e5632b46e38ce6f25a8b57aa3d","notify_url":"http://www.mirsh.cn","amount":{"total":1,"currency":"CNY"},"scene_info":{"payer_client_ip":"127.0.0.1"}}
文档里都给这样的示例,官方的东西还要到处查怎么用
为什么文档里,从没提到,需要加“请求头”呀
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
写的不清不楚的,得让我们自己猜,自己去查。
大佬,有个问题折腾了好几天,就是401,未经授权。
哪位大佬遇到过!!!急!!!
几点需要确定的:
1.服务器是否需要安装P12证书,代码里用不用增加证书
2.商户平台注册的支付接口,然后绑定公众号的APPID,这里申请统一支付接口用的公众号的APPID有问题么。
悬红解答,解决问题,50元微信红包奉上。。。
2.看你的写法你应该是调用v3版本接口,这个要说明下你调用哪个接口,是否模式调用(直连|服务商),然后看一下authorizationString内容与请求参数内容。
请求体 body数据:{"amount": {"total": 1,"currency": "CNY"},"mchid": "1608128xxx3","description": "商家","notify_url": "http://xxxxxx.com/order/notifyWxPay","payer": {"openid": "oxxxt1Z4qWQxCbvAnAytg3XG0Gkw"},"out_trade_no": "20210520000003483","appid": "wx80c6046666675022"}
请求头设置
HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Authorization","WECHATPAY2-SHA256-RSA2048" + " " + token); System.out.println("WECHATPAY2-SHA256-RSA2048 "+token); StringEntity entity = new StringEntity(reqdata,"UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); httpPost.setHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
请求头的Authorization WECHATPAY2-SHA256-RSA2048 mchid="1608128xxx",nonce_str="0000041514",timestamp="1621440004",serial_no="6207B9E166618239AC04AD7F35BB3DF84EDA03A0",signature="Z4NSq1M54u9NyMnPKQolN7uTeJI46Xnm4Qo77kSUwjNdhZfBTYeV2ZKWY2lxWBVR59twjmzhdUVLEZgAnOx7fBAHxFfY+fV+YCI/bwvfjqzjw4ARHqFXWsoJIG1tX1uDrgQuLOmPkdtELx9VBDIXgC26Lz61CuYDEW6jJJ1UuMPZWzCw4SX3k3WZ3D7lXL8fMvXDYROmoLmYlvkeGtXllN7uy1kV4HRdmpB4T6FQAQyEAR/blZwtFD0DPWknTY9rYHuCL9wN2tGcPheZsnlZqW10ScCi8kPyKJBUnKN466DaJ/zeHy2mwNKzXtZSEwcMvz/hYGyeyfgBHM9qkSHTug=="
赞!