实现Java (springMVC)后台端接入微信app支付,回调。
最近去实现Java端实现微信app的后台代码,自己也是查看了很多文档,最终实现了后台端代码的预订单生成,已经前段支付成功后的回调校验,好了废话不多说,直接编写我所实现的路程。(写的不好,大神们不要喷我,如果有雷同请联系我 qq:47092202)
微信APP支付实现
首先需要导入必须的jar包,根据个人而定,我是用了一下jar包
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
首先先准备好,微信支付接口的申请,具体操作可以看官网的文档
配置类
public class ConfigUtil {
public final static String APPID = "";// 服务号的应用号
public final static String MCH_ID = "";// 商户号
public final static String API_KEY = "";// API密钥
public final static String SIGN_TYPE = "MD5";// 签名加密方式
/**微信支付url 不用修改*/
public final static String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
/**微信订单查询url 不用修改*/
public final static String ORDER_QUERY = "https://api.mch.weixin.qq.com/pay/orderquery";
/**回调方法,是我们后台给微信回调的接口,下面会写到*/
public final static String NOTIFY_URL = "xxxxx/pay/wxNotify";
}
controller 代码 (让安卓,ios调用的接口)
@ResponseBody
@RequestMapping("/wxAppPay")
public Map<String, Object> wxpay(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> resultMap = new HashMap<String, Object>();
Order order = orderService.queryOrder(Long.valueOf(request.getParameter("orderNo"))); //根据唯一订单号,查询订单 //业务代码,根据自己情况实现
BigDecimal price = order.getPayPrice();
int priceStr = price.multiply(new BigDecimal(100)).intValue(); //微信 金额 单位是分 所以要*100转化为元
if (priceStr <= 0) {
resultMap.put("msg", 金额不能为0!");
resultMap.put("code", "500");
return resultMap;
}
String requestXML = OrderUtil.createOrder(request, order, price);//生成有签字的预订单
// 调用统一下单接口
String result = PayCommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, "POST", requestXML);
logger.info("\n"+result);
try {
SortedMap<Object, Object> paramMap = createPayInfo(result);
resultMap.put("code", "200");
resultMap.put("payInfo", paramMap);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultMap;
}
微信回调地址 controller (注意回调地址接口不允许传参)
@RequestMapping("/wxNotify")
@ResponseBody
public void wxNotify(HttpServletRequest request, HttpServletResponse response) throws IOException, JDOMException, ParseException {
logger.info("微信回调方法");
// 读取参数
InputStream inputStream;
StringBuffer sb = new StringBuffer();
inputStream = request.getInputStream();
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((s = in.readLine()) != null) {
sb.append(s);
}
in.close();
inputStream.close();
// 解析xml成map
Map<String, Object> m = new HashMap<String, Object>();
m = XMLUtil.doXMLParse(sb.toString());
// 过滤空 设置 TreeMap
SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
Iterator<String> it = m.keySet().iterator();
while (it.hasNext()) {
String parameter = (String) it.next();
Object parameterValue = m.get(parameter);
String v = "";
if (null != parameterValue) {
v = ((String) parameterValue).trim();
}
packageParams.put(parameter, v);
}
// 判断签名是否正确
String resultXml = "";
if (PayCommonUtil.isWxSign("UTF-8", packageParams)) {
if ("SUCCESS".equals((String) packageParams.get("result_code"))) {
logger.info("