import com.alibaba.fastjson.JSONObject; import com.github.wxpay.sdk.WXPayConstants; import com.github.wxpay.sdk.WXPayUtil; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import org.apache.commons.lang3.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.tree.DefaultElement;
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Iterator; import java.util.TreeMap;
public class WeChatPayUtil {
public static String getLocalIp() throws UnknownHostException { InetAddress localHost = InetAddress.getLocalHost(); return localHost.getHostAddress(); }
public static JSONObject xmlToJson(String xmlContent) throws DocumentException { Document document = DocumentHelper.parseText(xmlContent); Element rootElement = document.getRootElement(); Iterator iterator = rootElement.elementIterator(); JSONObject jsonObject = new JSONObject(); while (iterator.hasNext()) { DefaultElement defaultElement = (DefaultElement) iterator.next(); jsonObject.fluentPut(defaultElement.getName(), defaultElement.getStringValue()); } return jsonObject; }
private static String xmlContent(String body, String out_trade_no, String trade_fee) throws Exception { String appid = "改成你微信商户绑定的APPID"; String mchid = "商户账户分配的APPID"; String notifyUrl = "回调通知地址,一定要外网可以访问"; String key = "自己生成的API私钥"; TreeMap<String, String> paramMap = new TreeMap<>(); paramMap.put("appid", appid); paramMap.put("mch_id", mchid); paramMap.put("nonce_str", WXPayUtil.generateNonceStr()); paramMap.put("body", body); paramMap.put("out_trade_no", out_trade_no); paramMap.put("total_fee", trade_fee); paramMap.put("spbill_create_ip", getLocalIp()); paramMap.put("notify_url", notifyUrl); paramMap.put("trade_type", "NATIVE"); String signature = WXPayUtil.generateSignature(paramMap, key); paramMap.put("sign", signature); return WXPayUtil.mapToXml(paramMap); }
public static JSONObject goPay(String body, String out_trade_no, int trade_fee) throws Exception { String sendContent = xmlContent(body, out_trade_no, String.valueOf(trade_fee)); String result = sendPostData(WXPayConstants.UNIFIEDORDER_URL, sendContent, null); return xmlToJson(result); }
public static String sendPostData(String urlParam, String param, String contentType) { try { URL url = new URL(urlParam); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.setConnectTimeout(5 * 60 * 1000); connection.setReadTimeout(5 * 60 * 1000); if (StringUtils.isNotBlank(contentType)) connection.setRequestProperty("content-type", contentType); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "utf-8"); if (param == null) param = ""; writer.write(param); writer.flush(); writer.close(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) { StringBuilder builder = new StringBuilder(); String content = null; while ((content = reader.readLine()) != null) { builder.append(content); builder.append("\r\n"); } return builder.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; }
public static void createQRCode(String content, OutputStream outputStream) { int width = 160; int height = 160; String format = "jpg"; HashMap hashMap = new HashMap(); hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hashMap.put(EncodeHintType.MARGIN, 1); try { BitMatrix matrix = new MultiFormatWriter() .encode(content, BarcodeFormat.QR_CODE, width, height, hashMap); MatrixToImageWriter.writeToStream(matrix, format, outputStream); } catch (Exception e) { e.printStackTrace(); } } }
|