微信支付Java版

2022-06-13 17:45:16 浏览数 (2)

微信支付流程梳理

1.小程序微信支付
支付接口
代码语言:javascript复制
/**
 * 支付接口
 * @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);
}
微信支付回调
代码语言:javascript复制
/**
 * 支付回调
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value="/buyOrder",produces = MediaType.APPLICATION_JSON_VALUE   ";charset=UTF-8")
@ResponseBody
public String buyOrder(HttpServletRequest request,HttpServletResponse response) throws Exception{
	Map<String, Object> map=new HashMap<String, Object>();
	BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));  
	String line = null;  
	StringBuilder sb = new StringBuilder();  
	if((line = br.readLine()) != null){  
		sb.append(line);  
	}  
	br.close();  
	//sb为微信返回的xml  
	String notityXml = sb.toString();  
	String resXml = "";  
	@SuppressWarnings("rawtypes")
	Map map_xml = XMLUtil.doXMLParse(notityXml);
	String returnCode = (String) map_xml.get("return_code");  
	System.out.println(map_xml);
	if("SUCCESS".equals(returnCode)){  
		String out_trade_no=(String) map_xml.get("out_trade_no"); //商户订单号
		Indent indent = indentService.findOneByIndent(out_trade_no);
		if(indent == null){
			map.put("type", false); 
			map.put("massage", "未获取订单信息");
			return JsonMapper.getInstance().toJson(map);
		}
		indent.setIndentStatus("4");
		indentService.update(indent);
		resXml = "<xml>"   "<return_code><![CDATA[SUCCESS]]></return_code>"  
				  "<return_msg><![CDATA[OK]]></return_msg>"   "</xml> ";  
	}else {
		resXml = "<xml>"   "<return_code><![CDATA[FAIL]]></return_code>"  
				  "<return_msg><![CDATA[报文为空]]></return_msg>"   "</xml> ";  
	}
	map.put("resXml",resXml.getBytes());
	map.put("type", true);
	map.put("massage", "查询成功");
	return JsonMapper.getInstance().toJson(map);
}

0 人点赞