文档:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1
JSAPI下单
必传参数:
参数名 | 描述 |
---|---|
appid | 应用ID, 注意appid要和mchid关联 |
mchid | 直连商户号 |
openid | openid |
body | 商品描述 |
out_trade_no | 商户订单号 |
notify_url | 通知地址,注意不能携带参数 |
total_fee | 金额,单位分 |
nonce_str | 随机字符串 |
spbill_create_ip | 小程序请求ip |
trade_type | 支付类型,JSAPI |
sign | 签名 |
nonce_str方法:
/**
* 获取32位随机字符串
* @return
*/
public static String getNonceStr() {
Random random = new Random();
return MD5Util.md5(String.valueOf(random.nextInt(10000)));
}
sign生成方法:
/**
* 微信支付sign签名 --> 参数按ascii排序,key=value& 形式拼接,最后拼接上api_key,md5加密转大写
* @param parameters 除了sign以外的请求参数
* @param api_key 商户api key
* @return
*/
public static String createSign(SortedMap<Object, Object> parameters,String api_key) {
StringBuffer sb = new StringBuffer();
Set<Map.Entry<Object, Object>> es = parameters.entrySet();
Iterator<Map.Entry<Object, Object>> it = es.iterator();
while (it.hasNext()) {
Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next();
String k = (String) entry.getKey();
Object v = entry.getValue();
/** 如果参数为key或者sign,则不参与加密签名 */
if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
/** 支付密钥必须参与加密,放在字符串最后面 */
sb.append("key=" + api_key);
/** 记得最后一定要转换为大写 */
String sign = MD5Util.md5(sb.toString()).toUpperCase();
return sign;
}
paySign生成方法
/**
* paySign生成方法 --> 参数按ascii排序,key=value& 形式拼接,最后拼接上api_key,md5加密转大写
* @param parameters 参数包括appId、timeStamp、nonceStr、package、signType
* @param api_key 商户api key
* @return
*/
public static String getPaySign(SortedMap<Object, Object> resultParams){
StringBuilder sb = new StringBuilder();
for(Map.Entry entry : resultParams.entrySet()){
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
sb.append("key=").append(MCH_KEY);
return MD5Util.md5(sb.toString()).toUpperCase();
}
jsapi下单方法
/**
* jsapi方法 --> 将参数以xml格式post
* @param openId openId
* @param money 金额
* @param ip 小程序端ip
* @return 返回小程序支付需要的参数
*/
public static WechatPayModel wechatPay(String openId, int money, String ip){
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
parameters.put("appid", appid);
parameters.put("mch_id", MCH_ID);
parameters.put("openid", openId);
parameters.put("nonce_str", getNonceStr());
parameters.put("body", "智能柜支付");
parameters.put("out_trade_no", createBillNo());
parameters.put("total_fee", money);
parameters.put("spbill_create_ip", ip);
parameters.put("notify_url", "http://test.chinasuperbox.com/notifyUrl");
parameters.put("trade_type", "JSAPI");
String sign = createSign(parameters, MCH_KEY);
parameters.put("sign", sign);
String requestXML = XmlUtil.getRequestXml(parameters);
Map<String,String> resultMap = payPost(wechatPayUrl, requestXML);
WechatPayModel wechatPayModel = new WechatPayModel();
if( resultMap.get("return_code").equals("SUCCESS") && resultMap.get("result_code").equals("SUCCESS") ){
log.info("支付预请求成功");
String prepay_id = resultMap.get("prepay_id");
String nonce_str = resultMap.get("nonce_str");
long timeStamp = System.currentTimeMillis()/1000;
SortedMap<Object, Object> resultParams = new TreeMap<Object, Object>();
resultParams.put("appId", appid);
resultParams.put("timeStamp", timeStamp);
resultParams.put("nonceStr", nonce_str);
resultParams.put("package", "prepay_id="+prepay_id);
resultParams.put("signType", "MD5");
String paySign = getPaySign(resultParams);
wechatPayModel.setPaySign(paySign);
wechatPayModel.setAppId(appid);
wechatPayModel.setNonceStr(nonce_str);
wechatPayModel.setPackageStr("prepay_id="+prepay_id);
wechatPayModel.setSignType("MD5");
wechatPayModel.setTimeStamp(timeStamp);
wechatPayModel.setMsg("支付预请求成功");
return wechatPayModel;
}else{
if( resultMap.containsKey("err_code_des") ){
wechatPayModel.setMsg(resultMap.get("err_code_des"));
}else{
wechatPayModel.setMsg(resultMap.get("return_msg"));
}
log.info("支付预请求失败, return_msg = {}, err_code_des = {}", resultMap.get("return_msg"), resultMap.get("err_code_des"));
return wechatPayModel;
}
}
工具类:
/**
* post方法,将返回的xml解析成map返回
* @param requestUrl 请求地址
* @param requestXML 请求xml字符串
* @return 返回map
*/
public static Map<String,String> payPost(String requestUrl, String requestXML){
CloseableHttpClient httpclient = HttpClients.custom().build();
Map<String,String> result = new HashMap<String,String>();
HttpPost httpPost = new HttpPost(requestUrl);
StringEntity reqEntity = new StringEntity(requestXML, "utf-8");
// 设置content type类型
reqEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(reqEntity);
try{
CloseableHttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
// 从request中取得输入流
InputStream inputStream = entity.getContent();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList) {
result.put(e.getName(), e.getText());
}
}
return result;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
/**
* 将请求参数转换为xml格式的string
* @param parameters
* @return
*/
public static String getRequestXml(SortedMap<Object, Object> parameters) {
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
Set<Map.Entry<Object, Object>> es = parameters.entrySet();
Iterator<Map.Entry<Object, Object>> it = es.iterator();
while (it.hasNext()) {
Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next();
String k = (String) entry.getKey();
String v = entry.getValue() + "";
if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {
sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
} else {
sb.append("<" + k + ">" + v + "</" + k + ">");
}
}
sb.append("</xml>");
return sb.toString();
}
回调xml
<?xml version="1.0" encoding="utf-8"?>
<xml>
<appid><![CDATA[wx303cbfa214a64a4c]]></appid>
<bank_type><![CDATA[OTHERS]]></bank_type>
<cash_fee><![CDATA[1]]></cash_fee>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[N]]></is_subscribe>
<mch_id><![CDATA[1536185011]]></mch_id>
<nonce_str><![CDATA[bb57db42f77807a9c5823bd8c2d9aaef]]></nonce_str>
<openid><![CDATA[o_W745D0ptmzIyj7kmykJqciJPYU]]></openid>
<out_trade_no><![CDATA[075500305202132034782700473]]></out_trade_no>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<sign><![CDATA[4C8B3F62B4212F84BC7C830858D5BFFF]]></sign>
<time_end><![CDATA[20210603093212]]></time_end>
<total_fee>1</total_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>
<transaction_id><![CDATA[4200001039202106031250761263]]></transaction_id>
</xml>
回调方法
/**
* 回调方法 -- 注意接收字符串为xml字符串,返回格式为xml字符串
* @param parameters
* @return
*/
@PostMapping("/notifyUrl")
public String notifyUrl(HttpServletRequest request) {
log.info("into notifyUrl");
try {
//微信返回的请求体
String body = NetUtil.getRequestBody(request);
Map<String, String> xmlMap = XmlUtil.xmlToMap(body);
//业务逻辑处理
payConsumService.paySuccess(xmlMap);
} catch (Exception e) {
e.printStackTrace();
}
return "<xml>"
+"<return_code>SUCCESS</return_code>"
+"<return_msg>OK</return_msg>"
+"</xml>";
}