1.controller 层
@RequestMapping(value = “/wx_pay_buy”)
@ResponseBody
public Map<String, Object> wxPay(@RequestBody WxPayRequest wxPayRequest) {
return wxPayService.wxPay(wxPayRequest);
}
2.service 层
这个参数是查询订单的方法直接返回过来的参数 orderNum 和openId
public Map<String, Object> wxPay(WxPayRequest wxPayRequest) {
Map<String, Object> response = new HashMap<String, Object>();// 返回给小程序端需要的参数
String orderNum = wxPayRequest.getOrderNum();
String openId = wxPayRequest.getOpenId();
String appid = "";
try {
// 生成的随机字符串
String nonce_str = StringUtils.getRandomStringByLength(32);
// 获取客户端的ip地址
String spbill_create_ip = "127.0.0.1";
// 组装参数,用户生成统一下单接口的签名
Map<String, String> packageParams = new HashMap<String, String>();
// 商品名称
String body = null;
String notifyUrl = null;
String price = null;
body = "秒杀商品支付";
notifyUrl = notifyKillUrl;//支付回调
appid = mainAppid; //小程序的appid
ShoppingKillTradingRecord order = shoppingKillTradingRecordService.findByOrderRecord(orderNum);
if (order == null) {
response.put("msg", "未查询到秒杀订单");
return response;
}
price = FinancialUtils.getFeng(order.getPrice()).toString();//价格
packageParams.put("appid", appid);// 商户appid
packageParams.put("mch_id", mchid);// 商户id
packageParams.put("nonce_str", nonce_str);// 32位随机字符串
packageParams.put("body", body);// 商品名称
packageParams.put("out_trade_no", orderNum);// 商户订单号
packageParams.put("total_fee", price);// 支付金额,这边需要转成字符串类型,否则后面的签名会失败
packageParams.put("spbill_create_ip", spbill_create_ip);// 客户端的ip地址
packageParams.put("notify_url", notifyUrl);// 支付成功后的回调地址
packageParams.put("trade_type", tradeType);// 支付方式
packageParams.put("openid", openId);// 微信openid
String now = DateUtils.getDateAfterMinute(0);
String after = DateUtils.getDateAfterMinute(2);
packageParams.put("time_start", now);
packageParams.put("time_expire", after);
String prestr = PayUtils.createLinkString(packageParams); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
// MD5运算生成签名,这里是第一次签名,用于调用统一下单接口
String mysign = PayUtils.sign(prestr, secert, "utf-8").toUpperCase();
// 拼接统一下单接口使用的xml数据,要将上一步生成的签名一起拼接进去
String xml = "<xml>" + "<appid>" + appid + "</appid>" + "<body><![CDATA[" + body + "]]></body>"
+ "<mch_id>" + mchid + "</mch_id>" + "<nonce_str>" + nonce_str + "</nonce_str>"
+ "<notify_url>" + notifyUrl + "</notify_url>" + "<openid>" + openId
+ "</openid>" + "<out_trade_no>" + orderNum + "</out_trade_no>"
+ "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>" + "<total_fee>" + price
+ "</total_fee>" + "<trade_type>" + tradeType + "</trade_type>" + "<sign>" + mysign
+ "</sign>" + "<time_start>" + now + "</time_start>"
+ "<time_expire>" + after + "</time_expire>" + "</xml>";
System.out.println("调试模式_统一下单接口 请求XML数据:" + xml);
// 调用统一下单接口,并接受返回的结果
String result = PayUtils.httpRequest(payUrl, "POST", xml);
System.out.println("调试模式_统一下单接口 返回XML数据:" + result);
// 将解析结果存储在HashMap中
@SuppressWarnings("rawtypes")
Map map = PayUtils.doXMLParse(result);
String return_code = (String) map.get("return_code");// 返回状态码
if (return_code.equals("SUCCESS")) {
String prepay_id = (String) map.get("prepay_id");// 返回的预付单信息
response.put("nonceStr", nonce_str);
response.put("package", "prepay_id=" + prepay_id);
Long timeStamp = System.currentTimeMillis() / 1000;
response.put("timeStamp", timeStamp + "");// 这边要将返回的时间戳转化成字符串,不然小程序端调用wx.requestPayment方法会报签名错误
// 拼接签名需要的参数
String stringSignTemp = "appId=" + appid + "&nonceStr=" + nonce_str + "&package=prepay_id="
+ prepay_id + "&signType=MD5&timeStamp=" + timeStamp;
// 再次签名,这个签名用于小程序端调用wx.requesetPayment方法
String paySign = PayUtils.sign(stringSignTemp, secert, "utf-8").toUpperCase();
response.put("paySign", paySign);
response.put("signType", "MD5");
}
response.put("appId", appid);
return response;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
将微信支付统一下单接口中所必要的参数进行设置后 ,去调用微信统一下单接口. 然会返回给前端调用支付控件.