提交接口

dev-discount
袁野 4 years ago
parent 5e52085c5e
commit 9766ebdf06
  1. 37
      hai-cweb/src/main/java/com/cweb/controller/pay/WechatPayController.java
  2. 2
      hai-service/src/main/java/com/hai/common/pay/util/XmlUtil.java
  3. 13
      hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java
  4. 116
      hai-service/src/main/java/com/hai/common/utils/WxUtils.java

@ -1,12 +1,10 @@
package com.cweb.controller.pay;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cweb.config.SysConst;
import com.hai.common.pay.WechatPayUtil;
import com.hai.common.pay.util.IOUtil;
import com.hai.common.pay.util.SignatureUtil;
import com.hai.common.pay.util.XmlUtil;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.utils.HttpsUtils;
import com.hai.common.utils.WxUtils;
import com.hai.model.WxSharingOrderRequestModel;
@ -35,6 +33,8 @@ public class WechatPayController {
private static Logger log = LoggerFactory.getLogger(WechatPayController.class);
private WXPayConstants.SignType signType;
@Resource
private NotifyService notifyService;
@Resource
@ -160,30 +160,29 @@ public class WechatPayController {
public void addLedgerReceiver(HttpServletRequest request, HttpServletResponse response) {
try {
WxSharingOrderRequestModel wxSharingOrderRequest = new WxSharingOrderRequestModel();
wxSharingOrderRequest.setMch_id("1289663601");
wxSharingOrderRequest.setSub_mch_id("1609882817");
wxSharingOrderRequest.setAppid("wx637bd6f7314daa46");
wxSharingOrderRequest.setNonce_str(WxUtils.makeNonStr());
WxSharingReceiversVO receiversVO = new WxSharingReceiversVO();
receiversVO.setAccount(wxSharingOrderRequest.getSub_mch_id());
receiversVO.setAccount("1603942866");
receiversVO.setType("MERCHANT_ID");
receiversVO.setName("嗨加油");
receiversVO.setName("重庆惠昕石化有限责任公司");
receiversVO.setRelation_type("SERVICE_PROVIDER");
wxSharingOrderRequest.setReceiver(JSONObject.toJSONString(receiversVO));
Map<String , String> map = new HashMap<>();
map.put("mch_id" , "1289663601");
map.put("appid" , "wx637bd6f7314daa46");
map.put("nonce_str" , WxUtils.makeNonStr());
map.put("sign_type" , "HMAC-SHA256");
map.put("receiver" , JSONObject.toJSONString(receiversVO));
String sign = WxUtils.generateSignature(map, "aSDGSrthkiDf8558wRKfd57S875S4DHF" , WXPayConstants.SignType.HMACSHA256);
BeanMap beanMap = BeanMap.create(wxSharingOrderRequest);
wxSharingOrderRequest.setSign(WxUtils.makeSign(beanMap, "ASDfksdrgg55dDJoe5fky5u8trylDs5u" ,"SHA256"));
map.put("sign" , sign);
String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
String xm = XmlUtil.toSplitXml(wxSharingOrderRequest);
JSONObject jsonObject = HttpsUtils.postData(url, xm);
String xm = WxUtils.mapToXml(map);
String rest = HttpsUtils.postData(url, xm);
System.out.println(jsonObject);
System.out.println(rest);
log.info("微信支付 -> 添加分账接收方:处理开始");
} catch (Exception e) {
log.error("WechatPayController --> wechatNotify() error!", e);

@ -219,7 +219,7 @@ public class XmlUtil {
XStream xstream = XStreamFactroy.initSplitLine();
xstream.alias("xml", obj.getClass());
result = xstream.toXML(obj);
return result;
return result.replaceAll("&quot;","\"");
}
/**

@ -464,7 +464,7 @@ public class HttpsUtils {
* @param url API地址
* @return
*/
public static JSONObject postData(String url, String str) {
public static String postData(String url, String str) {
CloseableHttpClient httpClient = null;
if (url.startsWith("https")) {
httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory())
@ -480,14 +480,13 @@ public class HttpsUtils {
httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(str, "UTF-8");// 解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
return JSON.parseObject(httpStr);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity, "UTF-8");
} catch (Exception e) {
log.error(e.getMessage(),e);
} finally {

@ -1,6 +1,8 @@
package com.hai.common.utils;
import com.google.common.collect.Maps;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.pay.util.sdk.WXPayXmlUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;
@ -8,6 +10,12 @@ import net.sf.cglib.beans.BeanMap;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
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.StringWriter;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.*;
@ -18,6 +26,7 @@ public class WxUtils {
private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* 生成签名
*
@ -62,7 +71,7 @@ public class WxUtils {
// 拼接key
sb.append("key=").append(mchKey);
// MD5加密
// HMACSHA256加密
String sign = HMACSHA256(sb.toString(), mchKey).toUpperCase();
return sign;
}
@ -133,17 +142,15 @@ public class WxUtils {
* @throws Exception
*/
public static String HMACSHA256(String data, String key) throws Exception {
String hash = "";
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] bytes = sha256_HMAC.doFinal(data.getBytes());
hash = byteArrayToHexString(bytes);
} catch (Exception e) {
System.out.println("Error HmacSHA256 ===========" + e.getMessage());
}
return hash.toUpperCase();
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();
}
/**
@ -199,5 +206,90 @@ public class WxUtils {
}
/**
* 生成签名. 注意若含有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));
}
}
/**
* 生成 MD5
*
* @param data 待处理数据
* @return MD5结果
*/
public static String MD5(String data) throws Exception {
java.security.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();
}
/**
* 将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;
}
}

Loading…
Cancel
Save