提交分账接口问题

dev-discount
袁野 4 years ago
parent 172ca427ff
commit f98946750a
  1. 7
      hai-cweb/src/main/java/com/cweb/controller/pay/WechatPayController.java
  2. 85
      hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java
  3. 12
      hai-service/src/main/java/com/hai/common/utils/WxUtils.java
  4. 1
      hai-service/src/main/java/com/hai/service/pay/PayService.java
  5. BIN
      hai-service/src/main/java/privatekey/apiclient_cert.p12
  6. 23
      hai-service/src/main/java/privatekey/apiclient_cert.pem
  7. 28
      hai-service/src/main/java/privatekey/apiclient_key.pem

@ -163,12 +163,13 @@ public class WechatPayController {
WxSharingReceiversVO receiversVO = new WxSharingReceiversVO(); WxSharingReceiversVO receiversVO = new WxSharingReceiversVO();
receiversVO.setAccount("1609882817"); receiversVO.setAccount("1609882817");
receiversVO.setType("MERCHANT_ID"); receiversVO.setType("MERCHANT_ID");
receiversVO.setName("嗨加油"); receiversVO.setName("个体户黎杨珍");
receiversVO.setRelation_type("SERVICE_PROVIDER"); receiversVO.setRelation_type("SERVICE_PROVIDER");
Map<String , String> map = new HashMap<>(); Map<String , String> map = new HashMap<>();
map.put("mch_id" , "1289663601"); map.put("mch_id" , "1289663601");
map.put("sub_mch_id" , "1609882817");
map.put("appid" , "wx637bd6f7314daa46"); map.put("appid" , "wx637bd6f7314daa46");
map.put("nonce_str" , WxUtils.makeNonStr()); map.put("nonce_str" , WxUtils.makeNonStr());
map.put("sign_type" , "HMAC-SHA256"); map.put("sign_type" , "HMAC-SHA256");
@ -180,7 +181,7 @@ public class WechatPayController {
String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver"; String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
String xm = WxUtils.mapToXml(map); String xm = WxUtils.mapToXml(map);
String rest = HttpsUtils.postData(url, xm); String rest = String.valueOf(HttpsUtils.postData(url, xm));
System.out.println(rest); System.out.println(rest);
log.info("微信支付 -> 添加分账接收方:处理开始"); log.info("微信支付 -> 添加分账接收方:处理开始");
@ -189,4 +190,6 @@ public class WechatPayController {
} }
} }
} }

@ -15,6 +15,7 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -29,10 +30,13 @@ import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.ArrayList; import java.util.ArrayList;
@ -459,12 +463,13 @@ public class HttpsUtils {
} }
/** /**
* @Author Sum1Dream
* * @Description // 不带证书请求xml
* @param url API地址 * @Date 11:42 2021/6/8
* @return * @Param [url, str]
*/ * @return com.alibaba.fastjson.JSONObject
public static String postData(String url, String str) { **/
public static JSONObject postData(String url, String str) {
CloseableHttpClient httpClient = null; CloseableHttpClient httpClient = null;
if (url.startsWith("https")) { if (url.startsWith("https")) {
httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory())
@ -480,13 +485,57 @@ public class HttpsUtils {
httpPost.setConfig(requestConfig); httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(str, "UTF-8");// 解决中文乱码问题 StringEntity stringEntity = new StringEntity(str, "UTF-8");// 解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
httpPost.addHeader("Content-Type", "text/xml"); httpPost.addHeader("Content-Type", "text/xml");
httpPost.setEntity(stringEntity); httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
return JSON.parseObject(httpStr);
} catch (Exception e) {
log.error(e.getMessage(),e);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
log.error(e.getMessage(),e);
}
}
}
return null;
}
HttpResponse httpResponse = httpClient.execute(httpPost); /**
HttpEntity httpEntity = httpResponse.getEntity(); * @Author Sum1Dream
return EntityUtils.toString(httpEntity, "UTF-8"); * @Description // 带证书请求 xml
* @Date 11:45 2021/6/8
* @Param [url, mchId, str]
* @return com.alibaba.fastjson.JSONObject
**/
public static JSONObject postData(String url, String mchId , String str) {
CloseableHttpClient httpClient = null;
if (url.startsWith("https")) {
httpClient = HttpClients.custom().setSSLSocketFactory(readCertificate(mchId))
.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
} else {
httpClient = HttpClients.createDefault();
}
String httpStr = null;
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(str, "UTF-8");// 解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
return JSON.parseObject(httpStr);
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} finally { } finally {
@ -501,7 +550,23 @@ public class HttpsUtils {
return null; return null;
} }
public static SSLConnectionSocketFactory readCertificate(String mchId) {
try {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream stream = new FileInputStream( "F:/mine/hai-server/hai-service/src/main/java/privatekey/apiclient_cert.p12");
keyStore.load(stream , mchId.toCharArray());
stream.close();
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();
return new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1"},
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
} catch (Exception e) {
System.out.println("证书双向认证出现异常" + e);
}
return null;
}
/** /**
* 创建SSL安全连接 * 创建SSL安全连接

@ -1,5 +1,6 @@
package com.hai.common.utils; package com.hai.common.utils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.hai.common.pay.util.sdk.WXPayConstants; import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.pay.util.sdk.WXPayXmlUtil; import com.hai.common.pay.util.sdk.WXPayXmlUtil;
@ -7,15 +8,23 @@ import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NoNameCoder; import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver; import com.thoughtworks.xstream.io.xml.XppDriver;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.security.KeyStore;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.*; import java.util.*;
@ -292,4 +301,7 @@ public class WxUtils {
return output; return output;
} }
} }

@ -18,4 +18,5 @@ public interface PayService {
*/ */
void paySuccess(Map<String, String> map, String payType) throws Exception; void paySuccess(Map<String, String> map, String payType) throws Exception;
} }

@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIID2jCCAsKgAwIBAgIUUPi1hMq87bxzKky00MSigY+1IZswDQYJKoZIhvcNAQEL
BQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsT
FFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3Qg
Q0EwHhcNMjAxMjMxMTAyNDAzWhcNMjUxMjMwMTAyNDAzWjBsMRMwEQYDVQQDDAox
NjA0OTY4MDU1MRswGQYDVQQKDBLlvq7kv6HllYbmiLfns7vnu58xGDAWBgNVBAsM
D+S4quS9k+aIt+aWuea2mzELMAkGA1UEBgwCQ04xETAPBgNVBAcMCFNoZW5aaGVu
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtDjbxytOraNz5QkibnZN
6Q8fBPA11FQyEducTlOxFTXEN2mVIlPrnDJMZ8jQ0fBTHpWsuPMdLY6quEpnBUhf
vzip0giRCo8HAbT8TO2KiQSB0cPiW4HBGvHLI447EXG/xpisHAk6TR1rvdvGtjih
nRpnTLrDTK6TLDKi/hwDGYu2fzGYsXiN++bP45FSLiOn4T29o8PDG2zTpvcrsHEe
lerNltWzPbk5TXnnMDvcoUq/AgvhQjXRpLtOtkhegBBOHJFdTo/ldyV2Y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