java spring boot 微信支付

    public MsgEntity webPay(
            @RequestParam("title") String title,
            @RequestParam("openId") String openId,
            @RequestParam("ip") String ip,
            @RequestParam("notify_url") String notify_url,
            @RequestParam("money") String money,
            @RequestParam("orderId") String orderId
    ) {
        System.out.println("進入下單..");
        MsgEntity msg = new MsgEntity();
        System.out.println("進入下單..");

        String orderFee1 = money; //充值金額(單位:分)

        String paternerKey = env.getProperty("wxpay.key");
        String appId = env.getProperty("wxlog.appid");
        System.out.println("paternerKey"+paternerKey);
        System.out.println("orderFee1:"+orderFee1);

        try {
            // ---------------生成用戶訂單-----------------
            // ----- 統一下單參數------
            // 注意,參數的順序不能錯!!!!否則無法成功下單
            Map<String, String> paraMap = new HashMap<String, String>();
            paraMap.put("appid", appId);
            paraMap.put("body",  title+":"+orderId);
            paraMap.put("mch_id", env.getProperty("wxpay.mer_id"));
            paraMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paraMap.put("openid", openId);
            paraMap.put("out_trade_no", orderId);//訂單號
            paraMap.put("spbill_create_ip", ip);
            paraMap.put("total_fee", orderFee1);
            paraMap.put("trade_type", "JSAPI");
            paraMap.put("notify_url", notify_url);// 此路徑是微信服務器調用支付結果通知路徑
            String sign = WXPayUtil.generateSignature(paraMap, paternerKey);
            System.out.println("簽名是" + sign);
            paraMap.put("sign", sign);

//            String xml = "<xml>" +
//                    "<appid>"+ appId +"</appid>"+
//                    "<body>"+ title+":"+orderId +"</body>"+
//                    "<mch_id>"+ env.getProperty("wxpay.mer_id") +"</mch_id>"+
//                    "<nonce_str>"+ WXPayUtil.generateNonceStr() +"</nonce_str>"+
//                    "<notify_url>"+ notify_url +"</notify_url>"+
//                    "<openid>"+ openId +"</openid>"+
//                    "<out_trade_no>"+ orderId +"</out_trade_no>"+
//                    "<spbill_create_ip>"+ ip +"</spbill_create_ip>"+
//                    "<total_fee>"+ orderFee1 + "" +"</total_fee>"+
//                    "<trade_type>JSAPI</trade_type>"+
//                    "<sign>"+ sign +"</sign>"+
//                    "</xml>";

            String xml = WXPayUtil.mapToXml(paraMap);//將所有參數(map)轉xml格式
            //xml= new String(xml.getBytes("UTF-8"),"UTF-8");

            // 統一下單接口
            String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            //發送post請求"統一下單接口"返回預支付id:prepay_id
            System.out.println("xml入口是"+xml);
            String xmlStr = MyHttpUtils.doPost(unifiedorder_url, xml,Charset.forName("UTF-8").toString());
            System.out.println("xml出口"+xmlStr);
            //以下內容是返回前端頁面的json數據
            String prepay_id = "";//預支付id
            if (xmlStr.indexOf("SUCCESS") != -1) {
                System.out.println("支付系統返回了prepay_id");
                Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
                prepay_id = map.get("prepay_id");
            } else {
                System.out.println("xmlStr"+xmlStr);
                System.out.println("prepay_id獲取失敗");
            }
            // =============至此已成功獲取到prepay_id================
            //System.out.println("您的prepay_id的值是:"+prepay_id);
            // 將“微信內H5調起支付”需要的參數打包成JSON,發給前端
            Map<String, String> payMap = new HashMap<String, String>();
            payMap.put("appId", appId);
            payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
            payMap.put("nonceStr", WXPayUtil.generateNonceStr());
            payMap.put("signType", "MD5");
            payMap.put("package", "prepay_id=" + prepay_id);
            String paySign = WXPayUtil.generateSignature(payMap, paternerKey);
            payMap.put("paySign", paySign);
            if (payMap != null) {
                msg.setcode(true);
                msg.setobject(payMap);
                msg.setmsg("成功獲得prepay_id,且將數據發送到前端");
            } else {
                msg.setcode(false);
                msg.setmsg("獲取prepayId失敗");
                msg.setobject("0");
            }
        } catch (Exception e) {
            msg.setcode(false);
            msg.setmsg("異常:" + e.toString());
            msg.setobject("000");
        }
        System.out.println(msg.getcode() + "...." + msg.getmsg() + "...." + msg.getobject());

        return msg;
    }
-------------------------------------------------------------------------------
WXPayUtil在這裏  
package com.example.swaggerDemo.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.*;


public class WXPayUtil {

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new SecureRandom();

    public static String generateOrderId() {
        char[] nonceChars = new char[6];  //6個隨機字符
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//設置日期格式
        String orderId1=df.format(new Date());

        String orderI2=new String(nonceChars);
        String orderId =orderId1+orderI2;
        return orderId;
    }
    public static String inputStream2String(InputStream in_st){
        BufferedReader in = new BufferedReader(new InputStreamReader(in_st));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while (true){
            try {
                if (!((line = in.readLine()) != null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            buffer.append(line);
        }
        return buffer.toString();
    }
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打開和URL之間的連接
            URLConnection conn = realUrl.openConnection();
            // 設置通用的請求屬性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 發送POST請求必須設置如下兩行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 獲取URLConnection對象對應的輸出流
            out = new PrintWriter(conn.getOutputStream());
            // 發送請求參數
            out.print(param);
            // flush輸出流的緩衝
            out.flush();
            // 定義BufferedReader輸入流來讀取URL的響應
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("發送 POST 請求出現異常!"+e);
            e.printStackTrace();
        }
        //使用finally塊來關閉輸出流、輸入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * XML格式字符串轉換爲Map
     *
     * @param strXML XML字符串
     * @return XML數據轉換後的Map
     * @throws Exception
     */
    public static Map<String, String> xmlToMap(String strXML) throws Exception {
        try {
            Map<String, String> data = new HashMap<String, String>();
            DocumentBuilder documentBuilder = WXPayXmlUtil.newDocumentBuilder();
            InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
            org.w3c.dom.Document doc = documentBuilder.parse(stream);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getDocumentElement().getChildNodes();
            for (int idx = 0; idx < nodeList.getLength(); ++idx) {
                Node node = nodeList.item(idx);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;
                    data.put(element.getNodeName(), element.getTextContent());
                }
            }
            try {
                stream.close();
            } catch (Exception ex) {
                // do nothing
            }
            return data;
        } catch (Exception ex) {
            WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
            throw ex;
        }

    }

    /**
     * 將Map轉換爲XML格式的字符串
     *
     * @param data Map類型數據
     * @return XML格式的字符串
     * @throws Exception
     */
    public static String mapToXml(Map<String, String> data) throws Exception {
        org.w3c.dom.Document document = WXPayXmlUtil.newDocument();
        org.w3c.dom.Element root = document.createElement("xml");
        document.appendChild(root);
        for (String key: data.keySet()) {
            String value = data.get(key);
            if (value == null) {
                value = "";
            }
            value = value.trim();
            org.w3c.dom.Element filed = document.createElement(key);
            filed.appendChild(document.createTextNode(value));
            root.appendChild(filed);
        }
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
        try {
            writer.close();
        }
        catch (Exception ex) {
        }

        return output;
    }


    /**
     * 生成帶有 sign 的 XML 格式字符串
     *
     * @param data Map類型數據
     * @param key API密鑰
     * @return 含有sign字段的XML
     */
    public static String generateSignedXml(final Map<String, String> data, String key) throws Exception {
        return generateSignedXml(data, key, WXPayConstants.SignType.MD5);
    }

    /**
     * 生成帶有 sign 的 XML 格式字符串
     *
     * @param data Map類型數據
     * @param key API密鑰
     * @param signType 簽名類型
     * @return 含有sign字段的XML
     */
    public static String generateSignedXml(final Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
        String sign = generateSignature(data, key, signType);
        data.put(WXPayConstants.FIELD_SIGN, sign);
        return mapToXml(data);
    }


    /**
     * 判斷簽名是否正確
     *
     * @param xmlStr XML格式數據
     * @param key API密鑰
     * @return 簽名是否正確
     * @throws Exception
     */
    public static boolean isSignatureValid(String xmlStr, String key) throws Exception {
        Map<String, String> data = xmlToMap(xmlStr);
        if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) {
            return false;
        }
        String sign = data.get(WXPayConstants.FIELD_SIGN);
        return generateSignature(data, key).equals(sign);
    }

    /**
     * 判斷簽名是否正確,必須包含sign字段,否則返回false。使用MD5簽名。
     *
     * @param data Map類型數據
     * @param key API密鑰
     * @return 簽名是否正確
     * @throws Exception
     */
    public static boolean isSignatureValid(Map<String, String> data, String key) throws Exception {
        return isSignatureValid(data, key, WXPayConstants.SignType.MD5);
    }

    /**
     * 判斷簽名是否正確,必須包含sign字段,否則返回false。
     *
     * @param data Map類型數據
     * @param key API密鑰
     * @param signType 簽名方式
     * @return 簽名是否正確
     * @throws Exception
     */
    public static boolean isSignatureValid(Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
        if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) {
            return false;
        }
        String sign = data.get(WXPayConstants.FIELD_SIGN);
        return generateSignature(data, key, signType).equals(sign);
    }

    /**
     * 生成簽名
     *
     * @param data 待簽名數據
     * @param key API密鑰
     * @return 簽名
     */
    public static String generateSignature(final Map<String, String> data, String key) throws Exception {
        return generateSignature(data, key, WXPayConstants.SignType.MD5);
    }

    /**
     * 生成簽名. 注意,若含有sign_type字段,必須和signType參數保持一致。
     *
     * @param data 待簽名數據
     * @param key API密鑰
     * @param signType 簽名方式
     * @return 簽名
     */
    public static String generateSignature(final Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
        Set<String> keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (k.equals(WXPayConstants.FIELD_SIGN)) {
                continue;
            }
            if (data.get(k).trim().length() > 0) // 參數值爲空,則不參與簽名
                sb.append(k).append("=").append(data.get(k).trim()).append("&");
        }
        sb.append("key=").append(key);
        if (WXPayConstants.SignType.MD5.equals(signType)) {
            return MD5(sb.toString()).toUpperCase();
        }
        else if (WXPayConstants.SignType.HMACSHA256.equals(signType)) {
            return HMACSHA256(sb.toString(), key);
        }
        else {
            throw new Exception(String.format("Invalid sign_type: %s", signType));
        }
    }


    /**
     * 獲取隨機字符串 Nonce Str
     *
     * @return String 隨機字符串
     */
    public static String generateNonceStr() {
        char[] nonceChars = new char[32];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }


    /**
     * 生成 MD5
     *
     * @param data 待處理數據
     * @return MD5結果
     */
    public static String MD5(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("utf-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 生成 HMACSHA256
     * @param data 待處理數據
     * @param key 密鑰
     * @return 加密結果
     * @throws Exception
     */
    public static String HMACSHA256(String data, String key) throws Exception {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 日誌
     * @return
     */
    public static Logger getLogger() {
        Logger logger = LoggerFactory.getLogger("wxpay java sdk");
        return logger;
    }

    /**
     * 獲取當前時間戳,單位秒
     * @return
     */
    public static long getCurrentTimestamp() {
        return System.currentTimeMillis()/1000;
    }

    /**
     * 獲取當前時間戳,單位毫秒
     * @return
     */
    public static long getCurrentTimestampMs() {
        return System.currentTimeMillis();
    }

}
--------------------------------------------------------------------------
MyHttpUtils 在這裏
package com.example.swaggerDemo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MyHttpUtils {
    /**
     * Get請求
     * @param urlStr
     *   請求的地址
     * @param parameter
     *   請求的參數 格式爲:name=xxx&pwd=xxx
     * @param encoding
     *   服務器端請求編碼。如GBK,UTF-8等
     * @return
     */
    public static String doGet(String urlStr, String parameter, String encoding) {
        URL url = null;
        HttpURLConnection connection = null;
        try {
            url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();  // 新建連接實例
            connection.setConnectTimeout(2000);                     // 設置連接超時時間,單位毫秒
            connection.setReadTimeout(2000);                        // 設置讀取數據超時時間,單位毫秒
            connection.setDoOutput(true);                           // 是否打開輸出流 true|false
            connection.setDoInput(true);                            // 是否打開輸入流true|false
            connection.setRequestMethod("Get");                     // 提交方法GET
            connection.setUseCaches(false);                         // 是否緩存true|false
            connection.connect();                                   // 打開連接端口
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打開輸出流往對端服務器寫數據
            out.writeBytes(parameter);                              // 寫數據,也就是提交你的表單 name=xxx&pwd=xxx
            out.flush();                                            // 刷新
            out.close();                                            // 關閉輸出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));// 往對端寫完數據對端服務器返回數據 ,以BufferedReader流來讀取
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            return buffer.toString();
        } catch (IOException e) {
            // e.printStackTrace();
            System.err.println("MyHttpUtils==>通過url打開一個連接出現異常:" + e);

        } finally {
            if (connection != null) {
                connection.disconnect();// 關閉連接
            }
        }
        return null;
    }
    /**
     * Post請求
     * @param urlStr
     *   請求的地址
     * @param parameter
     *   請求的參數 格式爲:name=xxx&pwd=xxx
     * @param encoding
     *   服務器端請求編碼。如GBK,UTF-8等
     * @return
     */
    public static String doPost(String urlStr, String parameter, String encoding) {
        URL url = null;
        HttpURLConnection connection = null;
        try {
            url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();  // 新建連接實例
            connection.setRequestProperty("Content-Type", "text/xml");
            connection.setRequestProperty("Accept-Charset", "utf-8");
            connection.setConnectTimeout(2500);                     // 設置連接超時時間,單位毫秒
            connection.setReadTimeout(2500);                        // 設置讀取數據超時時間,單位毫秒
            connection.setDoOutput(true);                           // 是否打開輸出流 true|false
            connection.setDoInput(true);                            // 是否打開輸入流true|false
            connection.setRequestMethod("POST");                    // 提交方法POST
            connection.setUseCaches(false);                         // 是否緩存true|false
            connection.connect();                                   // 打開連接端口
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打開輸出流往對端服務器寫數據
            out.writeUTF(parameter);                              // 寫數據,也就是提交你的表單 name=xxx&pwd=xxx
            out.flush();                                            // 刷新
            out.close();                                            // 關閉輸出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));// 往對端寫完數據對端服務器返回數據 ,以BufferedReader流來讀取
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            return buffer.toString();
        } catch (IOException e) {
            // e.printStackTrace();
            System.err.println("MyHttpUtils==>通過url打開一個連接出現異常:" + e);

        } finally {
            if (connection != null) {
                connection.disconnect();// 關閉連接
            }
        }
        return null;
    }

    public static String jsonToXml(String json){
        try {
            StringBuffer buffer = new StringBuffer();
            buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            JSONObject jObj = JSON.parseObject(json);
            jsonToXmlstr(jObj,buffer);
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    /**
     * Json to xmlstr string.
     *
     * @param jObj   the j obj
     * @param buffer the buffer
     * @return the string
     */
    public static String jsonToXmlstr(JSONObject jObj, StringBuffer buffer ){
        Set<Map.Entry<String, Object>> se = jObj.entrySet();
        for(Iterator<Map.Entry<String, Object>> it = se.iterator(); it.hasNext(); )
        {
            Map.Entry<String, Object> en = it.next();
            if(en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONObject")){
                buffer.append("<"+en.getKey()+">");
                JSONObject jo = jObj.getJSONObject(en.getKey());
                jsonToXmlstr(jo,buffer);
                buffer.append("</"+en.getKey()+">");
            }else if(en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONArray")){
                JSONArray jarray = jObj.getJSONArray(en.getKey());
                for (int i = 0; i < jarray.size(); i++) {
                    buffer.append("<"+en.getKey()+">");
                    JSONObject jsonobject =  jarray.getJSONObject(i);
                    jsonToXmlstr(jsonobject,buffer);
                    buffer.append("</"+en.getKey()+">");
                }
            }else if(en.getValue().getClass().getName().equals("java.lang.String")){
                buffer.append("<"+en.getKey()+">"+en.getValue());
                buffer.append("</"+en.getKey()+">");
            }
        }
        return buffer.toString();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章