java实现微信支付
package com.hk.wx.pay.service.impl;@Servicepublic class PayServiceImpl implements PayService{ private static final Logger LOG = LoggerFactory.getLogger(PayServiceImpl.class); private static final String PAY_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; @Value("${appid}") private String APPID = ""; @Value("${pay.mch_id}") private String PAY_MACH_ID = ""; @Value("${pay.body}") private String PAY_BODY = ""; @Value("${pay.notify_url}") private String PAY_NOTIFY_URL = ""; @Value("${pay.trade_type}") private String PAY_TYPR = ""; @Value("${pay.key}") private String PAY_KEY = ""; public JSONObject wxPay(String total_fee, String imei, String ip, String openid) { Map pay = new LinkedHashMap(); // 按照 添加顺序 排序 pay.put("appid", this.APPID); //小程序 aapid pay.put("attach", getOrderNum()); //附加数据 pay.put("body", this.PAY_BODY); //商品描述 pay.put("device_info", imei); // pay.put("mch_id", this.PAY_MACH_ID); //商品号 pay.put("nonce_str", getNonceStr()); //随机字符串 pay.put("notify_url", this.PAY_NOTIFY_URL); //返回连接 pay.put("openid", openid); //微信用户 id pay.put("out_trade_no", getOrderNum()); //订单号 pay.put("sign_type", "MD5"); pay.put("spbill_create_ip", ip); pay.put("total_fee", total_fee); pay.put("trade_type", this.PAY_TYPR); pay.put("sign", getSign(pay)); String xml = XmlUtil.mapToXMLString(pay); // 转成 xml 形式 System.out.println(xml); String msg = ""; try { msg = HttpUtils.xmlRequest("https://api.mch.weixin.qq.com/pay/unifiedorder", "POST", xml); // xml 形式 post请求 JSONObject json = XmlUtil.xmlToJSONObject(msg); String ret = json.getString("return_code"); if ((ret != "") && (ret.equals("SUCCESS")) && (!"SUCCESS".equals(json.getString("result_code")))) { return json; } LOG.info("# 统一下单错误...msg:" + json.toString()); return null; } catch (Exception e) { LOG.error("# 支付出错..."); e.printStackTrace(); }return null; } //获取随机数 private static String getNonceStr() { return Md5Utils.encryption(ComApi.getUUID()); } //获取 sign private static String getSign(Map