PC端,也就是Native支付
统一下单api:
必须参数
Appid,mch_id,nonce_str,sign,body,out_trade_no,total_fee,spbill_create_ip,notify_url,trade_type
@RequestMapping("/wxpayPC")
@ResponseBody
public Map<String, String> wxpayPC(@RequestParam("orderNumber") String orderNumber, HttpServletResponse response, HttpServletRequest request) throws Exception {
WxpayConfig wxpayConfig = new WxpayConfig();
/**
* PC并不需要openId
* */
OrderInfo info = orderInfoService.selectOne(new EntityWrapper<OrderInfo>()
.eq("order_number", orderNumber));
Map<String,String> paraMap = new HashMap<>();
paraMap.put("appid",wxpayConfig.getApp_id());
paraMap.put("body",info.getOrderProject());
paraMap.put("mch_id",wxpayConfig.getMch_id());
paraMap.put("nonce_str", WXPayUtil.generateNonceStr());
String randomNum = (int)((Math.random()*9+1)*100000)+"";
String orderNo =randomNum+orderNumber;
paraMap.put("out_trade_no",orderNo);
paraMap.put("spbill_create_ip",wxpayConfig.getSpbill_create_ip());
String total_fee = info.getOrderMoney().multiply(new BigDecimal("100")).intValue()+"";
paraMap.put("total_fee",total_fee);
paraMap.put("trade_type","NATIVE");
paraMap.put("notify_url","http://XXXX.com/XXXX/api/wxnotifyPC");
String apiKey = wxpayConfig.getApi_key();
String sign = WXPayUtil.generateSignature(paraMap,apiKey);
paraMap.put("sign",sign);
String xml = WXPayUtil.mapToXml(paraMap);
//统一下单 https://api.mch.weixin.qq.com/pay/unifiedorder
String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
String xmlStr = HttpUtil.sendPost(unifiedorder_url,xml,false);
System.out.println(xmlStr);
//以下内容是返回前端页面的json数据
String prepay_id = "";//预支付id
String code_url = "";
if (xmlStr.indexOf("SUCCESS") != -1) {
Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
prepay_id = (String) map.get("prepay_id");
code_url = (String)map.get("code_url");
}
Map<String, String> payMap = new HashMap<String, String>();
payMap.put("appId", wxpayConfig.getApp_id());
payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp()+"");
payMap.put("nonceStr", WXPayUtil.generateNonceStr());
payMap.put("signType", "MD5");
payMap.put("code_url",code_url);//将code_url返回给前台
payMap.put("package", "prepay_id=" + prepay_id);
String paySign = WXPayUtil.generateSignature(payMap, wxpayConfig.getApi_key());
payMap.put("paySign", paySign);
return payMap;
}
将数据发送给https://api.mch.weixin.qq.com/pay/unifiedorder,你可以判断返回是否成功,成功时候有一个code_url,也就是二维码的url。
一般格式weixin://wxpay/bizpayurl?pr=ByrALdk
生成二维码,可以调用qrcode.js库
new QRCode(document.getElementById("qrcode"), "weixin://wxpay/bizpayurl?pr=ByrALdk");
调用这个方法会生成支付二维码
后台回调方法
- 获取信息
- 成功后,修改订单状态,返回success给微信
@RequestMapping("/wxnotifyPC")
public String wxnotifyPC(HttpServletRequest request,HttpServletResponse response){
InputStream is = null;
try{
is = request.getInputStream();
String xml = WXPayUtil.inputStream2String(is);
Map<String,String> notifyMap = WXPayUtil.xmlToMap(xml);
String orderNumber = "";
if(notifyMap.get("return_code").equals("SUCCESS")) {
orderNumber = notifyMap.get("out_trade_no").substring(6);
OrderInfo info = orderInfoService.selectOne(new EntityWrapper<OrderInfo>()
.eq("order_number", orderNumber));
info.setOrderState(1);
orderInfoService.updateById(info);
String amount = notifyMap.get("total_fee");
System.out.println("实际付款:"+amount);
}
response.getWriter().write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
思考,前台
前台,在扫码后如何跳转支付页面,因为二维码出现后,我并不知道这个二维码是否被手机扫码,所以我在这个扫码界面
设置了定时器1,二维码生成后。1秒执行查询的ajax,对这个order查询是否成功,如果成功,返回订单界面。
定时器2,1小时刷新二维码,如果这个用户停留在这个界面,防止二维码失效