微信小程序支付服務商版-發起支付


1.准備資料

(1)服務商id

(2)服務商Appid

(3)程序商戶的appid

(4)小程序商戶號ID

(5)服務商的APIKey

(5)證書

(6)證書密碼

 以上資料准備完善 先去看看微信支付的官方的API 根據你的需要找到具體的

    https://pay.weixin.qq.com/wiki/doc/api/index.html

 我用到的是JSAPI支付並且是服務商版下面我的實例均是服務商版本

    從官方的文檔里面我們找到一些必要的信息 

 官方提供的API列表

  1.統一下單 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

  2.查詢訂單 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2

  3.關閉訂單 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_3

  4.申請退款 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4

  5.查詢退款 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_5

  

  以上為一些在正式開始前所需要了解的一些基本內容 下面進入正題

2.代碼部分

 引入依賴

<dependency>
  <groupId>com.github.wxpay</groupId>
  <artifactId>wxpay-sdk</artifactId>
  <version>0.0.3</version>
</dependency>

 在application.yml中配置一些基本的信息

pay:
  #用一下單API地址
  mchPayUrl: https://api.mch.weixin.qq.com/pay/unifiedorder
  #服務商APPID
  appid: --
  #服務商ID
  mch_id: --
  #交易類型
  trade_type: JSAPI
  #小程序APPID
  sub_appid: --
  #小程序商戶號
  sub_mch_id: --
  #異步回調通知地址
  notify_url: http://192.168.1.34:8080/searchNotifyPay
  #證書的密鑰
  serverPayKey: --
  #證書的路徑
  certificate: D://DevelopmentTool//apiclient_cert.p12
  #證書的密碼
  certificatePassword: --
  #用一下單API地址
  seachOrderURL: https://api.mch.weixin.qq.com/pay/orderquery

  創建微信支付參數的compoent主要從application.yml中把參數綁定到實體類中

package com.landimc.compoent.pay;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class WxPayCompoent {

    /***
     *  服務商統一下單 url
     **/
    @Value("${pay.mchPayUrl}")
    public String mchPayUrl = "";

    /***
     * 服務商id
     **/
    @Value("${pay.mch_id}")
    public String mch_id = "";

    /***
     *服務商Appid
     **/
    @Value("${pay.appid}")
    public String appid = "";

    /**
     * 交易類型
     */
    @Value("${pay.trade_type}")
    public String trade_type = "JSAPI";

    /**
     * 小程序商戶的appid
     */
    @Value("${pay.sub_appid}")
    public String sub_appid = "";


    /**
     * 小程序商戶號ID
     */
    @Value("${pay.sub_mch_id}")
    public String sub_mch_id = "";

    /**
     * 異步回調通知地址
     */
    @Value("${pay.notify_url}")
    public String notify_url = "";

    /**
     * 服務商的APIKey
     */
    @Value("${pay.serverPayKey}")
    public String serverPayKey = "";

    /**
     * 證書存放路徑
     */
    @Value("${pay.certificate}")
    public String certificate = "";

    /**
     * 證書密碼 (默認商戶號)
     */
    @Value("${pay.certificatePassword}")
    public String certificatePassword = "";

    /**
     * 查詢訂單的地址
     */
    @Value("${pay.seachOrderURL}")
    public String seachOrderURL = "";


}

  下面是一些用到的輔助工具類

   1.生成簽名

package com.landimc.wxpay.utils;

import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayConstants.SignType;
import com.landimc.compoent.pay.WxPayCompoent;
import com.landimc.tools.MD5;
import com.landimc.tools.SysConfig;
import com.landimc.wxpay.model.WxPayConfig;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.annotation.Resource;
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.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.*;


public class WXPayUtil {

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new SecureRandom();

    @Resource
    private WxPayCompoent wxPayCompoent;

    /**
     * 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, 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, 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, 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, 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, SignType.MD5);
    }

    /**
     * 生成簽名. 注意,若含有sign_type字段,必須和signType參數保持一致。
     *
     * @param data     待簽名數據
     * @param key      API密鑰
     * @param signType 簽名方式
     * @return 簽名
     */
    public static String generateSignature(final Map<String, String> data, String key, 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 (SignType.MD5.equals(signType)) {
            return MD5(sb.toString()).toUpperCase();
        } else if (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();
    }

    //生成簽名
    public static String createSign(SortedMap<String, Object> parameters) {
        StringBuilder sb = new StringBuilder();
        Set es = parameters.entrySet();
        for (Object e : es) {
            Map.Entry entry = (Map.Entry) e;
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k).append("=").append(v).append("&");
            }
        }
        sb.append("key=").append("1234abcd1234abcd1234abcd1234abcd");
        return MD5.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
    }

    /**
     * 第二次簽名專用
     * MD5 加密,轉為指定類型
     *
     * @param text
     * @param key
     * @param input_charset
     * @return
     */
    public static String sign(String text, String key, String input_charset) {
        text = text + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }

    public static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5簽名過程中出現錯誤,指定的編碼集不對,您目前指定的編碼集是:" + charset);
        }
    }


}

  2.統一下單

 

package com.landimc.wxpay;

import com.landimc.tools.ClassToMap;
import com.landimc.wxpay.model.WxPayModel;
import com.landimc.wxpay.utils.HttpClientUtils;
import com.landimc.wxpay.utils.WXPayUtil;
import com.landimc.wxpay.utils.XMLUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.jdom.JDOMException;

import java.io.IOException;
import java.util.*;


/**
 * 統一下單
 *
 * @author Yang
 */
public class UnifiedOrderWxPay {

    /**
     * 發起支付業務
     *
     * @param
     * @return
     * @throws Exception
     */
    public static Map<String, String> toMchPay(String url,SortedMap<String, Object> parameters, String certificate, String pass) throws Exception {
        String s = HttpClientUtils.httpsRequest(url, "POST", XMLUtils.getRequestXml(parameters), certificate, pass);
        Map<String, String> returnMap = new HashMap<>();
        try {
            //將返回的XML結果再次轉換為Map集合
            returnMap = XMLUtils.xmlToMap(s);
            System.out.println(returnMap);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return returnMap;
    }


    /**
     * 生成簽名
     *
     * @param map
     * @return
     */
    public static String getSign(Map<String, String> map) {

        String result = "";
        try {
            List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(map.entrySet());
            // 對所有傳入參數按照字段名的 ASCII 碼從小到大排序(字典序)
            Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {

                @Override
                public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                    return (o1.getKey()).toString().compareTo(o2.getKey());
                }
            });

            // 構造簽名鍵值對的格式
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> item : infoIds) {
                if (item.getKey() != null || item.getKey() != "") {
                    String key = item.getKey();
                    String val = item.getValue();
                    if (!(val == "" || val == null)) {
                        sb.append(key + ":" + val + ":");
                    }
                }

            }
            result = sb.toString();
            //進行MD5加密
            result = DigestUtils.md5Hex(result).toUpperCase();
        } catch (Exception e) {
            return null;
        }
        return result;
    }

}

2.生成簽名 給二次簽名使用 這里你們可以自行優化把兩個簽名方法整合為一個 因為簽名的方法是一樣的

package com.landimc.wxpay.utils;

import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayConstants.SignType;
import com.landimc.compoent.pay.WxPayCompoent;
import com.landimc.tools.MD5;
import com.landimc.tools.SysConfig;
import com.landimc.wxpay.model.WxPayConfig;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.annotation.Resource;
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.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.*;


public class WXPayUtil {

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new SecureRandom();

    @Resource
    private WxPayCompoent wxPayCompoent;

    /**
     * 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, 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, 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, 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, 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, SignType.MD5);
    }

    /**
     * 生成簽名. 注意,若含有sign_type字段,必須和signType參數保持一致。
     *
     * @param data     待簽名數據
     * @param key      API密鑰
     * @param signType 簽名方式
     * @return 簽名
     */
    public static String generateSignature(final Map<String, String> data, String key, 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 (SignType.MD5.equals(signType)) {
            return MD5(sb.toString()).toUpperCase();
        } else if (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();
    }

    //生成簽名
    public static String createSign(SortedMap<String, Object> parameters) {
        StringBuilder sb = new StringBuilder();
        Set es = parameters.entrySet();
        for (Object e : es) {
            Map.Entry entry = (Map.Entry) e;
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k).append("=").append(v).append("&");
            }
        }
        sb.append("key=").append("1234abcd1234abcd1234abcd1234abcd");
        return MD5.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
    }

    /**
     * 第二次簽名專用
     * MD5 加密,轉為指定類型
     *
     * @param text
     * @param key
     * @param input_charset
     * @return
     */
    public static String sign(String text, String key, String input_charset) {
        text = text + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }

    public static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5簽名過程中出現錯誤,指定的編碼集不對,您目前指定的編碼集是:" + charset);
        }
    }


}

下面是controller層 這邊返回的數據正是前端那邊需要的參數 注意下面有個TODO的地方改成你的appid

package com.landimc.controller;

import com.alibaba.fastjson.JSON;
import com.landimc.compoent.pay.WxPayCompoent;
import com.landimc.pojo.TWxjyjl;
import com.landimc.service.WeChartTransactionService;
import com.landimc.tools.PageUtils;
import com.landimc.wxpay.UnifiedOrderWxPay;
import com.landimc.wxpay.model.WxPayModel;
import com.landimc.wxpay.utils.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Yang
 */
@RestController()
public class TestController {

    @Autowired
    private WxPayCompoent wxPayCompoent;

    @Resource
    private WeChartTransactionService weChartTransactionService;

    @RequestMapping("/searchPayList")
    public String searchPayList(TWxjyjl tWxjyjl, PageUtils pageUtils) {
        return JSON.toJSONString(weChartTransactionService.getWechatTransactionsAll(tWxjyjl, pageUtils));
    }

    @RequestMapping("/pay")
    public String test() throws Exception {
        String orderNo = System.currentTimeMillis() + "";
        SortedMap<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appid", wxPayCompoent.appid);
        paramsMap.put("mch_id", wxPayCompoent.mch_id);
        paramsMap.put("sub_appid", wxPayCompoent.sub_appid);
        paramsMap.put("sub_mch_id", wxPayCompoent.sub_mch_id);
        paramsMap.put("nonce_str", UUID.randomUUID().toString().replace("-", ""));
        paramsMap.put("body", "demo-01");
        paramsMap.put("out_trade_no", orderNo);
        paramsMap.put("total_fee", "1");
        paramsMap.put("trade_type", "JSAPI");
        paramsMap.put("sub_openid", "oi0pP5UhaAENr2b66xzb5Rx0a1Do");
        paramsMap.put("spbill_create_ip", "127.0.0.1");
        paramsMap.put("notify_url", "https://www.baidu.com");
        paramsMap.put("sign", WXPayUtil.createSign(paramsMap));
        System.out.println(WXPayUtil.createSign(paramsMap));
        Map<String, String> map = UnifiedOrderWxPay.toMchPay(wxPayCompoent.mchPayUrl, paramsMap, wxPayCompoent.certificate, wxPayCompoent.certificatePassword);
        String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
        // 支付密鑰
        String key = "&key=" + wxPayCompoent.serverPayKey;
        String prepay_id = "";
        prepay_id = map.get("prepay_id");
        String packages = "prepay_id=" + prepay_id;
        //生成32位以下隨機編碼
        String nonceStr1 = UUID.randomUUID().toString().replace("-", "");
        // 開始第二次簽名  //TODO 改成你的APPID
        String mapStr1 = "appId=wx52311f98asdasd&nonceStr=" + nonceStr1 + "&package=prepay_id=" + prepay_id
                + "&signType=MD5&timeStamp=" + timeStamp;
        String paySign = WXPayUtil.sign(mapStr1, key, "utf-8").toUpperCase();
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("timeStamp", timeStamp);
        resultMap.put("nonceStr", nonceStr1);
        resultMap.put("package", packages);
        resultMap.put("paySign", paySign);
        resultMap.put("payno", orderNo);
        return JSON.toJSONString(resultMap);
    }

    @RequestMapping("/search")
    public String search(String tradeNo) throws Exception {
        SortedMap<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appid", wxPayCompoent.appid);
        paramsMap.put("mch_id", wxPayCompoent.mch_id);
        paramsMap.put("sub_mch_id", wxPayCompoent.sub_mch_id);
        paramsMap.put("out_trade_no", tradeNo);
        paramsMap.put("nonce_str", UUID.randomUUID().toString().replace("-", ""));
        paramsMap.put("sign_type", "MD5");
        paramsMap.put("sign", WXPayUtil.createSign(paramsMap));
        Map<String, String> map = UnifiedOrderWxPay.toMchPay(wxPayCompoent.seachOrderURL, paramsMap, wxPayCompoent.certificate, wxPayCompoent.certificatePassword);
        return JSON.toJSONString(map);
    }
}

 

下面提供前端的代碼 這邊是uniapp 可以看一下大概的參數 換成自己的前端

          uni.requestPayment({
                            provider: 'wxpay',
                            timeStamp: res.data.timeStamp,
                            nonceStr: res.data.nonceStr,
                            package: res.data.package,
                            signType: 'MD5',
                            paySign: res.data.paySign,
                            success: function(res) {
                                   //TODO 執行自己的業務代碼 進行查單
                            },
                            fail: function(err) {
                                console.log('fail:' + JSON.stringify(err));
                            }
              })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM