参考:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=4_2
统筹: 因为是做的微信支付,商户嵌入微信支付,所以不是被扫支付,系统先调用该统一下单接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易回话标识后,再调用支付接口返回到手机端,然后等待回调即可;
统一下单接口:
URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder
不要证书;
成功后,返回:状态码 return_code : SUCCESS/FAIL
1. 手机向后台交互
入参:商品描述:body;总金额:total_fee;终端IP:spbill_create_ip;购买的商品ID:自定义;用户ID:自定义;(存储到自己的数据库中)
2. 后台准备数据向微信直接请求;
将存储好的公众账号ID:appid;商户号:mch_id,取出来;自己生成随机字符串建议格式为:WX年月日时分秒随机数;按照签名规则生成签名;
SortedMap<String, Object> map = new TreeMap<String, Object>();
// 公众账号ID
map.put("appid", Configure.getAppid());
// 商户号
map.put("mch_id", Configure.getMchid());
// 随机字符串
map.put("nonce_str", randomString);
// 总金额(单位是分没有小数点)
int money = new BigDecimal(amount).multiply(new BigDecimal(100)).intValue();
map.put("total_fee", money + "");
map.put("out_trade_no", out_trade_no);
// 交易类型
map.put("trade_type", Configure.getTradeType());
// 商品描述
map.put("body", body);
map.put("spbill_create_ip", spbillCreateIp);
// 回掉地址
map.put("notify_url", Configure.NOTIFY_URL);
map.put("sign", Signature.getSign(map));
String requestXML = PayCommonUtil.getRequestXml(map); // xml格式的字符串
public static String getRequestXml(SortedMap<String, Object> parameters) {
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
Set es = parameters.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) 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>
<appid>wx2421b1c4370ec43b</appid>
<attach>支付测试</attach>
<body>JSAPI支付测试</body>
<mch_id>10000100</mch_id>
<nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
<notify_url>http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php</notify_url>
<openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
<out_trade_no>1415659990</out_trade_no>
<spbill_create_ip>14.23.150.211</spbill_create_ip>
<total_fee>1</total_fee>
<trade_type>JSAPI</trade_type>
<sign>0CB01533B8C1EF103065174F50BCA001</sign>
</xml>
向微信的统一下单接口发送请求
public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
return buffer.toString();
} catch (ConnectException ce) {
log.error("连接超时:{}", ce);
} catch (Exception e) {
log.error("https请求异常:{}", e);
}
return null;
}
3. 获取到微信的相应,拼装数据格式为吊起支付接口;然后返回给手机;
String result = CommonUtil.httpsRequest(Configure.REFUND_QUERY_API, "POST", requestXML);
Map<String, Object> resultMap = XMLParser.getMapFromXML(result);
SortedMap<String, Object> map2 = new TreeMap<String, Object>();
String code = resultMap.get("return_code").toString();
if (StringUtils.equals("SUCCESS", code)) {
String nonce_str = MD5.MD5Encode(System.currentTimeMillis() / 1000 + "", "utf-8");
nonce_str = String.valueOf(System.currentTimeMillis() / 1000);
map2.put("appid", resultMap.get("appid").toString());
map2.put("noncestr", randomString);
map2.put("package", "Sign=WXPay");
map2.put("partnerid", resultMap.get("mch_id").toString());
map2.put("prepayid", resultMap.get("prepay_id").toString());
map2.put("timestamp", nonce_str);
resultMap.clear(); // 很重要哦
resultMap.put("sign", Signature.getSign(map2));
resultMap.put("noncestr", randomString);
resultMap.put("package", "Sign=WXPay");
resultMap.put("timestamp", nonce_str);
resultMap.put("appid", map2.get("appid").toString());
resultMap.put("partnerid", map2.get("partnerid").toString());
resultMap.put("prepayid", map2.get("prepayid").toString());
}
好多人在这一步出现问题,原因是文档中没有提现这一步数据是怎么返回的,没有明确说明,后台返回给手机端数据,需要按照调起支付接口的请求参数格式;当然我也在这步卡了n久;无良的微信支付接口;
这一步应该是后台按照调起支付接口拼装数据包括sign,然后传给手机端,手机端调用该接口;
当然需要注意一点,就是这个过程中,不要传多余的值,只要这些数据;
值得一提的是微信的返回结果-2是用户取消,我只想说一句,用户取消你妹啊,明明是参数有问题,还用户取消,然后一点提示没有,我已经无力吐槽;大公司不在乎小人物的感受~~~~你不用拉倒~~~有的是人用~~~我们不在乎~~~
有人说:随机字符串,第二次传的要和第一次的是一个,反正我是这样做的,成功了;
4. 准备回调方法
@RequestMapping(value = "/weixinRes", method = RequestMethod.POST)
@ControllerSystemLog(description = "微信支付请求回调", type = SystemLogTypeEnum.USER)
public void weixinRes() throws Exception {
LoggerUtil.info(getClass(), getRequest().getParameterMap().toString());
InputStream inStream = getRequest().getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
LoggerUtil.info(getClass(), "~~~~~~~~~~~~~~~~付款成功~~~~~~~~~");
outSteam.close();
inStream.close();
String result = new String(outSteam.toByteArray(), "utf-8");// 获取微信调用我们notify_url的返回信息
Map<String, Object> map = XMLParser.getMapFromXML(result);
for (Object keyValue : map.keySet()) {
LoggerUtil.info(getClass(), keyValue + "=" + map.get(keyValue));
}
if (map.get("result_code").toString().equalsIgnoreCase("SUCCESS")) {
try {
this.orderService.save(map);
getResponse().getWriter().write(PayCommonUtil.setXML("SUCCESS", "")); // 告诉微信服务器,我收到信息了,不要在调用回调action了
LoggerUtil.info(getClass(), "-------------" + PayCommonUtil.setXML("SUCCESS", ""));
} catch (Exception e) {
e.printStackTrace();
}
} else {
LoggerUtil.info(getClass(), "~~~~~~~~~~~~~~~~微信付款失败了~~~~~~~~~" + map.get("return_msg").toString());
}
}