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(" | ", ""); 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(" | ", ""); 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));
}
})