/**
* 支付接口
* @param request
* @param response
* @throws Exception
*/
@SuppressWarnings("rawtypes")
@RequestMapping(value="/payOrderCom",produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
@ResponseBody
public String payOrderCom(HttpServletRequest request, HttpServletResponse response) throws Exception{
Map<String, Object> map=new HashMap<String, Object>();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//小程序appid
String appid = request.getParameter("appid");
//商家id
String mch_id = request.getParameter("mch_id");
//订单id
String indent_id = request.getParameter("indent_id");
/*String bitsecret_key = request.getParameter("bitsecret_key"); //32位秘钥*/
if(!isNull(appid) || !isNull(mch_id) ||!isNull(indent_id)){
map.put("type", false);
map.put("massage", "未获取到参数");
return JsonMapper.getInstance().toJson(map);
}
Indent indent = indentService.findOneByIndent(indent_id);
if(indent == null){
map.put("type", false);
map.put("massage", "未获取订单信息");
return JsonMapper.getInstance().toJson(map);
}
//得到价钱(自定义)
String price = indent.getTotalMoney();
BigDecimal fee = BigDecimal.valueOf(Long.valueOf(price)).divide(new BigDecimal(100));
//订单标题(自定义)
String title = request.getParameter("title");
//时间戳
String times = System.currentTimeMillis() + "";
//获取客户端的ip
String spbill_create_ip = XMLUtil.getIpAddr(request);
//订单编号(自定义 这里以时间戳+随机数)
SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
packageParams.put("appid", appid);//微信小程序ID
packageParams.put("mch_id", mch_id);//商户ID
packageParams.put("nonce_str", times);//随机字符串(32位以内) 这里使用时间戳
packageParams.put("body", title);//支付主体名称 自定义
packageParams.put("out_trade_no", indent_id);//编号 自定义以时间戳+随机数+商品ID(订单id)
packageParams.put("total_fee", fee);//价格 自定义
packageParams.put("spbill_create_ip", spbill_create_ip);//获取客户端ip
packageParams.put("notify_url", "http://192.168.1.108/mkkMoblie/payOrder/buyOrder");//支付返回地址要外网访问的到, localhost不行,调用下面buy方法。(订单存入数据库)
packageParams.put("trade_type", "JSAPI");//这个api有,固定的
//获取sign
String sign = PayCommonUtil.createSign("UTF-8", packageParams);//最后这个是自己在微信商户设置的32位密钥
packageParams.put("sign", sign);
//转成XML
String requestXML = PayCommonUtil.getRequestXml(packageParams);
System.out.println(requestXML);
//得到含有prepay_id的XML
String resXml = HttpUtil.postData("https://api.mch.weixin.qq.com/pay/unifiedorder", requestXML);
Map map_pay = XMLUtil.doXMLParse(resXml);
System.out.println(map_pay);
// String return_code = (String) map.get("return_code");
//得到prepay_id
String prepay_id = (String) map_pay.get("prepay_id");
SortedMap<Object, Object> packageP = new TreeMap<Object, Object>();
packageP.put("appId", appid);//!!!注意,这里是appId,上面是appid
packageP.put("nonceStr", times);//时间戳
packageP.put("package", "prepay_id=" + prepay_id);//必须把package写成 "prepay_id="+prepay_id这种形式,两小时有效
packageP.put("signType", "MD5");//paySign加密
packageP.put("timeStamp", (System.currentTimeMillis() / 1000) + "");
//得到paySign
String paySign = PayCommonUtil.createSign("UTF-8", packageP);
packageP.put("paySign", paySign);
//将packageP数据返回给小程序
/*Gson gson = new Gson();
String json = gson.toJson(packageP);
PrintWriter pw = response.getWriter();
System.out.println(json);
pw.write(json);
pw.close();*/
map.put("packageP", packageP);
map.put("type", true);
map.put("massage", "查询成功");
return JsonMapper.getInstance().toJson(map);
}