容灾
package com.itsoft.hotel.commons.utils;
public abstract interface IWXPayDomain {
abstract void report(final String domain, long elapsedTimeMillis, final Exception ex);
abstract DomainInfo getDomain(final WXPayConfig config);
static class DomainInfo{
public String domain;
public boolean primaryDomain;
public DomainInfo(String domain, boolean primaryDomain) {
this.domain = domain;
this.primaryDomain = primaryDomain;
}
@Override
public String toString() {
return "DomainInfo{" +
"domain='" + domain + '\'' +
", primaryDomain=" + primaryDomain +
'}';
}
}
}
微信sdk方法
package com.itsoft.hotel.commons.utils;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class WeiXinOrderUtil {
/**
* 组装统一下单参数
* @return
*
public String buildWechatOrderParam(WechatOrder wechatOrder,HttpServletRequest request) throws Exception{
String payKey = wechatConfig.getPaykey();
String signType = wechatConfig.getSignType();
SortedMap<String, String> signMap = new TreeMap<String, String>();
long timeStamp = PayUtils.getUnixTime(new Date());
wechatOrder.setAppid(wechatConfig.getWechatAppId());
wechatOrder.setTrade_type(wechatConfig.getTradeType());
String prepayId = createWechatOrder(wechatOrder,request);
signMap.put("appId",wechatOrder.getAppid());
signMap.put("timeStamp", timeStamp+"");
signMap.put("nonceStr", wechatOrder.getNonce_str());
signMap.put("package", "prepay_id="+prepayId);
signMap.put("signType", signType);
String paySign = PayUtils.getSign(signMap, payKey);
signMap.put("pg", prepayId);
signMap.put("paySign", paySign);
signMap.put("result", "success");
String json = JSON.toJSONString(signMap);
JSONObject returnJson=JSONObject.parseObject(json);
return returnJson.toJSONString();
}
*
* 创建下单签名 包含商品信息
* @return
*
public String createWechatSign(WechatOrder wechatOrder){
String mch_id = wechatConfig.getMchId();
String notify_url = wechatConfig.getNotifyUrl();
String device_info = wechatConfig.getDeviceInfo();
String payKey = wechatConfig.getPaykey();
SortedMap<String, String> parameters = new TreeMap<String, String>();
parameters.put("appid", wechatOrder.getAppid());
parameters.put("mch_id",mch_id);
parameters.put("device_info", device_info);
parameters.put("body", wechatOrder.getOrderName());
parameters.put("trade_type", wechatOrder.getTrade_type());
parameters.put("nonce_str", wechatOrder.getNonce_str());
parameters.put("notify_url", notify_url);
parameters.put("out_trade_no", wechatOrder.getOut_trade_no());
parameters.put("total_fee", wechatOrder.getTotal_fee()+"");
parameters.put("openid", wechatOrder.getOpenid());
return PayUtils.getSign(parameters, payKey);
}
*
* 创建微信订单 请求微信接口下单
* @return
*
public String createWechatOrder(WechatOrder wechatOrder,HttpServletRequest request) throws Exception{
String mch_id = wechatConfig.getMchId();
String notify_url = wechatConfig.getNotifyUrl();
String device_info = wechatConfig.getDeviceInfo();
String createOrderURL = wechatConfig.getCreateOrderUrl();
String xml = "<xml>"+
"<appid>"+wechatOrder.getAppid()+"</appid>"+
"<body>"+wechatOrder.getOrderName()+"</body>"+
"<device_info>"+device_info+"</device_info>"+
"<mch_id>"+mch_id+"</mch_id>"+
"<nonce_str>"+wechatOrder.getNonce_str()+"</nonce_str>"+
"<notify_url>"+notify_url+"</notify_url>"+
"<openid>"+wechatOrder.getOpenid()+"</openid>"+
"<out_trade_no>"+wechatOrder.getOut_trade_no()+"</out_trade_no>"+
"<total_fee>"+wechatOrder.getTotal_fee()+"</total_fee>"+
"<trade_type>"+wechatOrder.getTrade_type()+"</trade_type>"+
"<sign>"+createWechatSign(wechatOrder)+"</sign>"+
"</xml>";
String result = PayUtils.httpsRequest(createOrderURL, "POST", xml);
System.out.println("-----------------------------统一下单结果---------------------------");
System.out.println(result);
Map<String, String> resultMap = null;
resultMap=PayUtils.getH5PayMap(result,request);
return resultMap.get("prepay_id");
}
*
* 获取 WeixinOauth2Token
* @return
*
public WeixinOauth2Token getWeixinOauth2Token(String code){
String appId = wechatConfig.getWechatAppId();
String appSecret = wechatConfig.getWechatAppSecret();
return AdvancedUtil.getOauth2AccessToken(appId, appSecret, code);
}*/
public static String getOrderReturnStream(HttpServletRequest request) throws IOException {
InputStream inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return new String(outSteam.toByteArray(),"utf-8");
}
}
package com.itsoft.hotel.commons.utils;
import java.util.HashMap;
import java.util.Map;
public class WXPay {
private WXPayConfig config;
private WXPayConstants.SignType signType;
private boolean autoReport;
private boolean useSandbox;
private String notifyUrl;
private WXPayRequest wxPayRequest;
public WXPay(final WXPayConfig config) throws Exception {
this(config, null, true, false);
}
public WXPay(final WXPayConfig config, final boolean autoReport) throws Exception {
this(config, null, autoReport, false);
}
public WXPay(final WXPayConfig config, final boolean autoReport, final boolean useSandbox) throws Exception{
this(config, null, autoReport, useSandbox);
}
public WXPay(final WXPayConfig config, final String notifyUrl) throws Exception {
this(config, notifyUrl, true, false);
}
public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport) throws Exception {
this(config, notifyUrl, autoReport, false);
}
public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception {
this.config = config;
this.notifyUrl = notifyUrl;
this.autoReport = autoReport;
this.useSandbox = useSandbox;
if (useSandbox) {
this.signType = WXPayConstants.SignType.MD5;
}
else {
this.signType = WXPayConstants.SignType.MD5;
}
this.wxPayRequest = new WXPayRequest(config);
}
private void checkWXPayConfig() throws Exception {
if (this.config == null) {
throw new Exception("config is null");
}
if (this.config.getAppID() == null || this.config.getAppID().trim().length() == 0) {
throw new Exception("appid in config is empty");
}
if (this.config.getMchID() == null || this.config.getMchID().trim().length() == 0) {
throw new Exception("appid in config is empty");
}
if (this.config.getCertStream() == null) {
throw new Exception("cert stream in config is empty");
}
if (this.config.getWXPayDomain() == null){
throw new Exception("config.getWXPayDomain() is null");
}
if (this.config.getHttpConnectTimeoutMs() < 10) {
throw new Exception("http connect timeout is too small");
}
if (this.config.getHttpReadTimeoutMs() < 10) {
throw new Exception("http read timeout is too small");
}
}
public Map<String, String> fillRequestData(Map<String, String> reqData) throws Exception {
reqData.put("appid", config.getAppID());
reqData.put("mch_id", config.getMchID());
reqData.put("nonce_str", WXPayUtil.generateNonceStr());
if (WXPayConstants.SignType.MD5.equals(this.signType)) {
reqData.put("sign_type", WXPayConstants.MD5);
}
else if (WXPayConstants.SignType.HMACSHA256.equals(this.signType)) {
reqData.put("sign_type", WXPayConstants.HMACSHA256);
}
reqData.put("sign", WXPayUtil.generateSignature(reqData, config.getKey(), this.signType));
return reqData;
}
public boolean isResponseSignatureValid(Map<String, String> reqData) throws Exception {
return WXPayUtil.isSignatureValid(reqData, this.config.getKey(), this.signType);
}
public boolean isPayResultNotifySignatureValid(Map<String, String> reqData) throws Exception {
String signTypeInData = reqData.get(WXPayConstants.FIELD_SIGN_TYPE);
WXPayConstants.SignType signType;
if (signTypeInData == null) {
signType = WXPayConstants.SignType.MD5;
}
else {
signTypeInData = signTypeInData.trim();
if (signTypeInData.length() == 0) {
signType = WXPayConstants.SignType.MD5;
}
else if ( WXPayConstants.MD5.equals(signTypeInData)) {
signType = WXPayConstants.SignType.MD5;
}
else if ( WXPayConstants.HMACSHA256.equals(signTypeInData)) {
signType = WXPayConstants.SignType.HMACSHA256;
}
else {
throw new Exception(String.format("Unsupported sign_type: %s", signTypeInData));
}
}
return WXPayUtil.isSignatureValid(reqData, this.config.getKey(), signType);
}
public String requestWithoutCert(String urlSuffix, Map<String, String> reqData,
int connectTimeoutMs, int readTimeoutMs) throws Exception {
String msgUUID = reqData.get("nonce_str");
String reqBody = WXPayUtil.mapToXml(reqData);
String resp = this.wxPayRequest.requestWithoutCert(urlSuffix, msgUUID, reqBody, connectTimeoutMs, readTimeoutMs, autoReport);
return resp;
}
public String requestWithCert(String urlSuffix, Map<String, String> reqData,
int connectTimeoutMs, int readTimeoutMs) throws Exception {
String msgUUID= reqData.get("nonce_str");
String reqBody = WXPayUtil.mapToXml(reqData);
String resp = this.wxPayRequest.requestWithCert(urlSuffix, msgUUID, reqBody, connectTimeoutMs, readTimeoutMs, this.autoReport);
return resp;
}
public Map<String, String> processResponseXml(String xmlStr) throws Exception {
String RETURN_CODE = "return_code";
String return_code;
Map<String, String> respData = WXPayUtil.xmlToMap(xmlStr);
if (respData.containsKey(RETURN_CODE)) {
return_code = respData.get(RETURN_CODE);
}
else {
throw new Exception(String.format("No `return_code` in XML: %s", xmlStr));
}
if (return_code.equals(WXPayConstants.FAIL)) {
return respData;
}
else if (return_code.equals( WXPayConstants.SUCCESS)) {
if (this.isResponseSignatureValid(respData)) {
return respData;
}
else {
throw new Exception(String.format("Invalid sign value in XML: %s", xmlStr));
}
}
else {
throw new Exception(String.format("return_code value %s is invalid in XML: %s", return_code, xmlStr));
}
}
public Map<String, String> microPay(Map<String, String> reqData) throws Exception {
return this.microPay(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> microPay(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_MICROPAY_URL_SUFFIX;
}
else {
url = WXPayConstants.MICROPAY_URL_SUFFIX;
}
String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
public Map<String, String> microPayWithPos(Map<String, String> reqData) throws Exception {
return this.microPayWithPos(reqData, this.config.getHttpConnectTimeoutMs());
}
public Map<String, String> microPayWithPos(Map<String, String> reqData, int connectTimeoutMs) throws Exception {
int remainingTimeMs = 60*1000;
long startTimestampMs = 0;
Map<String, String> lastResult = null;
Exception lastException = null;
while (true) {
startTimestampMs = WXPayUtil.getCurrentTimestampMs();
int readTimeoutMs = remainingTimeMs - connectTimeoutMs;
if (readTimeoutMs > 1000) {
try {
lastResult = this.microPay(reqData, connectTimeoutMs, readTimeoutMs);
String returnCode = lastResult.get("return_code");
if (returnCode.equals("SUCCESS")) {
String resultCode = lastResult.get("result_code");
String errCode = lastResult.get("err_code");
if (resultCode.equals("SUCCESS")) {
break;
}
else {
if (errCode.equals("SYSTEMERROR") || errCode.equals("BANKERROR") || errCode.equals("USERPAYING")) {
remainingTimeMs = remainingTimeMs - (int)( WXPayUtil.getCurrentTimestampMs() - startTimestampMs);
if (remainingTimeMs <= 100) {
break;
}
else {
WXPayUtil.getLogger().info("microPayWithPos: try micropay again");
if (remainingTimeMs > 5*1000) {
Thread.sleep(5*1000);
}
else {
Thread.sleep(1*1000);
}
continue;
}
}
else {
break;
}
}
}
else {
break;
}
}
catch (Exception ex) {
lastResult = null;
lastException = ex;
}
}
else {
break;
}
}
if (lastResult == null) {
throw lastException;
}
else {
return lastResult;
}
}
public Map<String, String> unifiedOrder(Map<String, String> reqData) throws Exception {
return this.unifiedOrder(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> unifiedOrder(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_UNIFIEDORDER_URL_SUFFIX;
}
else {
url = WXPayConstants.UNIFIEDORDER_URL_SUFFIX;
}
if(this.notifyUrl != null) {
reqData.put("notify_url", this.notifyUrl);
}
String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
public Map<String, String> orderQuery(Map<String, String> reqData) throws Exception {
return this.orderQuery(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> orderQuery(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_ORDERQUERY_URL_SUFFIX;
}
else {
url = WXPayConstants.ORDERQUERY_URL_SUFFIX;
}
String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
public Map<String, String> reverse(Map<String, String> reqData) throws Exception {
return this.reverse(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> reverse(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_REVERSE_URL_SUFFIX;
}
else {
url = WXPayConstants.REVERSE_URL_SUFFIX;
}
String respXml = this.requestWithCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
public Map<String, String> closeOrder(Map<String, String> reqData) throws Exception {
return this.closeOrder(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> closeOrder(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_CLOSEORDER_URL_SUFFIX;
}
else {
url = WXPayConstants.CLOSEORDER_URL_SUFFIX;
}
String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
public Map<String, String> refund(Map<String, String> reqData) throws Exception {
return this.refund(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> refund(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_REFUND_URL_SUFFIX;
}
else {
url = WXPayConstants.REFUND_URL_SUFFIX;
}
String respXml = this.requestWithCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
public Map<String, String> refundQuery(Map<String, String> reqData) throws Exception {
return this.refundQuery(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> refundQuery(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_REFUNDQUERY_URL_SUFFIX;
}
else {
url = WXPayConstants.REFUNDQUERY_URL_SUFFIX;
}
String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
public Map<String, String> downloadBill(Map<String, String> reqData) throws Exception {
return this.downloadBill(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> downloadBill(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_DOWNLOADBILL_URL_SUFFIX;
}
else {
url = WXPayConstants.DOWNLOADBILL_URL_SUFFIX;
}
String respStr = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs).trim();
Map<String, String> ret;
if (respStr.indexOf("<") == 0) {
ret = WXPayUtil.xmlToMap(respStr);
}
else {
ret = new HashMap<String, String>();
ret.put("return_code", WXPayConstants.SUCCESS);
ret.put("return_msg", "ok");
ret.put("data", respStr);
}
return ret;
}
public Map<String, String> report(Map<String, String> reqData) throws Exception {
return this.report(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> report(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_REPORT_URL_SUFFIX;
}
else {
url = WXPayConstants.REPORT_URL_SUFFIX;
}
String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return WXPayUtil.xmlToMap(respXml);
}
public Map<String, String> shortUrl(Map<String, String> reqData) throws Exception {
return this.shortUrl(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> shortUrl(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_SHORTURL_URL_SUFFIX;
}
else {
url = WXPayConstants.SHORTURL_URL_SUFFIX;
}
String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
public Map<String, String> authCodeToOpenid(Map<String, String> reqData) throws Exception {
return this.authCodeToOpenid(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> authCodeToOpenid(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = WXPayConstants.SANDBOX_AUTHCODETOOPENID_URL_SUFFIX;
}
else {
url = WXPayConstants.AUTHCODETOOPENID_URL_SUFFIX;
}
String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
return this.processResponseXml(respXml);
}
}
package com.itsoft.hotel.commons.utils;
import org.apache.http.client.HttpClient;
public class WXPayConstants {
public enum SignType {
MD5, HMACSHA256
}
public static final String DOMAIN_API = "api.mch.weixin.qq.com";
public static final String DOMAIN_API2 = "api2.mch.weixin.qq.com";
public static final String DOMAIN_APIHK = "apihk.mch.weixin.qq.com";
public static final String DOMAIN_APIUS = "apius.mch.weixin.qq.com";
public static final String FAIL = "FAIL";
public static final String SUCCESS = "SUCCESS";
public static final String HMACSHA256 = "HMAC-SHA256";
public static final String MD5 = "MD5";
public static final String FIELD_SIGN = "sign";
public static final String FIELD_SIGN_TYPE = "sign_type";
public static final String WXPAYSDK_VERSION = "WXPaySDK/3.0.9";
public static final String USER_AGENT = WXPAYSDK_VERSION +
" (" + System.getProperty("os.arch") + " " + System.getProperty("os.name") + " " + System.getProperty("os.version") +
") Java/" + System.getProperty("java.version") + " HttpClient/" + HttpClient.class.getPackage().getImplementationVersion();
public static final String MICROPAY_URL_SUFFIX = "/pay/micropay";
public static final String UNIFIEDORDER_URL_SUFFIX = "/pay/unifiedorder";
public static final String ORDERQUERY_URL_SUFFIX = "/pay/orderquery";
public static final String REVERSE_URL_SUFFIX = "/secapi/pay/reverse";
public static final String CLOSEORDER_URL_SUFFIX = "/pay/closeorder";
public static final String REFUND_URL_SUFFIX = "/secapi/pay/refund";
public static final String REFUNDQUERY_URL_SUFFIX = "/pay/refundquery";
public static final String DOWNLOADBILL_URL_SUFFIX = "/pay/downloadbill";
public static final String REPORT_URL_SUFFIX = "/payitil/report";
public static final String SHORTURL_URL_SUFFIX = "/tools/shorturl";
public static final String AUTHCODETOOPENID_URL_SUFFIX = "/tools/authcodetoopenid";
public static final String SANDBOX_MICROPAY_URL_SUFFIX = "/sandboxnew/pay/micropay";
public static final String SANDBOX_UNIFIEDORDER_URL_SUFFIX = "/sandboxnew/pay/unifiedorder";
public static final String SANDBOX_ORDERQUERY_URL_SUFFIX = "/sandboxnew/pay/orderquery";
public static final String SANDBOX_REVERSE_URL_SUFFIX = "/sandboxnew/secapi/pay/reverse";
public static final String SANDBOX_CLOSEORDER_URL_SUFFIX = "/sandboxnew/pay/closeorder";
public static final String SANDBOX_REFUND_URL_SUFFIX = "/sandboxnew/secapi/pay/refund";
public static final String SANDBOX_REFUNDQUERY_URL_SUFFIX = "/sandboxnew/pay/refundquery";
public static final String SANDBOX_DOWNLOADBILL_URL_SUFFIX = "/sandboxnew/pay/downloadbill";
public static final String SANDBOX_REPORT_URL_SUFFIX = "/sandboxnew/payitil/report";
public static final String SANDBOX_SHORTURL_URL_SUFFIX = "/sandboxnew/tools/shorturl";
public static final String SANDBOX_AUTHCODETOOPENID_URL_SUFFIX = "/sandboxnew/tools/authcodetoopenid";
}
package com.itsoft.hotel.commons.utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
public class WXPayReport {
public static class ReportInfo {
private String version = "v1";
private String sdk = WXPayConstants.WXPAYSDK_VERSION;
private String uuid;
private long timestamp;
private long elapsedTimeMillis;
private String firstDomain;
private boolean primaryDomain;
private int firstConnectTimeoutMillis;
private int firstReadTimeoutMillis;
private int firstHasDnsError;
private int firstHasConnectTimeout;
private int firstHasReadTimeout;
public ReportInfo(String uuid, long timestamp, long elapsedTimeMillis, String firstDomain, boolean primaryDomain, int firstConnectTimeoutMillis, int firstReadTimeoutMillis, boolean firstHasDnsError, boolean firstHasConnectTimeout, boolean firstHasReadTimeout) {
this.uuid = uuid;
this.timestamp = timestamp;
this.elapsedTimeMillis = elapsedTimeMillis;
this.firstDomain = firstDomain;
this.primaryDomain = primaryDomain;
this.firstConnectTimeoutMillis = firstConnectTimeoutMillis;
this.firstReadTimeoutMillis = firstReadTimeoutMillis;
this.firstHasDnsError = firstHasDnsError?1:0;
this.firstHasConnectTimeout = firstHasConnectTimeout?1:0;
this.firstHasReadTimeout = firstHasReadTimeout?1:0;
}
@Override
public String toString() {
return "ReportInfo{" +
"version='" + version + '\'' +
", sdk='" + sdk + '\'' +
", uuid='" + uuid + '\'' +
", timestamp=" + timestamp +
", elapsedTimeMillis=" + elapsedTimeMillis +
", firstDomain='" + firstDomain + '\'' +
", primaryDomain=" + primaryDomain +
", firstConnectTimeoutMillis=" + firstConnectTimeoutMillis +
", firstReadTimeoutMillis=" + firstReadTimeoutMillis +
", firstHasDnsError=" + firstHasDnsError +
", firstHasConnectTimeout=" + firstHasConnectTimeout +
", firstHasReadTimeout=" + firstHasReadTimeout +
'}';
}
public String toLineString(String key) {
String separator = ",";
Object[] objects = new Object[] {
version, sdk, uuid, timestamp, elapsedTimeMillis,
firstDomain, primaryDomain, firstConnectTimeoutMillis, firstReadTimeoutMillis,
firstHasDnsError, firstHasConnectTimeout, firstHasReadTimeout
};
StringBuffer sb = new StringBuffer();
for(Object obj: objects) {
sb.append(obj).append(separator);
}
try {
String sign = WXPayUtil.HMACSHA256(sb.toString(), key);
sb.append(sign);
return sb.toString();
}
catch (Exception ex) {
return null;
}
}
}
private static final String REPORT_URL = "http://report.mch.weixin.qq.com/wxpay/report/default";
private static final int DEFAULT_CONNECT_TIMEOUT_MS = 6*1000;
private static final int DEFAULT_READ_TIMEOUT_MS = 8*1000;
private LinkedBlockingQueue<String> reportMsgQueue = null;
private WXPayConfig config;
private ExecutorService executorService;
private volatile static WXPayReport INSTANCE;
private WXPayReport(final WXPayConfig config) {
this.config = config;
reportMsgQueue = new LinkedBlockingQueue<String>(config.getReportQueueMaxSize());
executorService = Executors.newFixedThreadPool(config.getReportWorkerNum(), new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
});
if (config.shouldAutoReport()) {
WXPayUtil.getLogger().info("report worker num: {}", config.getReportWorkerNum());
for (int i = 0; i < config.getReportWorkerNum(); ++i) {
executorService.execute(new Runnable() {
public void run() {
while (true) {
try {
StringBuffer sb = new StringBuffer();
String firstMsg = reportMsgQueue.take();
WXPayUtil.getLogger().info("get first report msg: {}", firstMsg);
String msg = null;
sb.append(firstMsg);
int remainNum = config.getReportBatchSize() - 1;
for (int j=0; j<remainNum; ++j) {
WXPayUtil.getLogger().info("try get remain report msg");
msg = reportMsgQueue.take();
WXPayUtil.getLogger().info("get remain report msg: {}", msg);
if (msg == null) {
break;
}
else {
sb.append("\n");
sb.append(msg);
}
}
WXPayReport.httpRequest(sb.toString(), DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS);
}
catch (Exception ex) {
WXPayUtil.getLogger().warn("report fail. reason: {}", ex.getMessage());
}
}
}
});
}
}
}
public static WXPayReport getInstance(WXPayConfig config) {
if (INSTANCE == null) {
synchronized (WXPayReport.class) {
if (INSTANCE == null) {
INSTANCE = new WXPayReport(config);
}
}
}
return INSTANCE;
}
public void report(String uuid, long elapsedTimeMillis,
String firstDomain, boolean primaryDomain, int firstConnectTimeoutMillis, int firstReadTimeoutMillis,
boolean firstHasDnsError, boolean firstHasConnectTimeout, boolean firstHasReadTimeout) {
long currentTimestamp = WXPayUtil.getCurrentTimestamp();
ReportInfo reportInfo = new ReportInfo(uuid, currentTimestamp, elapsedTimeMillis,
firstDomain, primaryDomain, firstConnectTimeoutMillis, firstReadTimeoutMillis,
firstHasDnsError, firstHasConnectTimeout, firstHasReadTimeout);
String data = reportInfo.toLineString(config.getKey());
WXPayUtil.getLogger().info("report {}", data);
if (data != null) {
reportMsgQueue.offer(data);
}
}
@Deprecated
private void reportSync(final String data) throws Exception {
httpRequest(data, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS);
}
@Deprecated
private void reportAsync(final String data) throws Exception {
new Thread(new Runnable() {
public void run() {
try {
httpRequest(data, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS);
}
catch (Exception ex) {
WXPayUtil.getLogger().warn("report fail. reason: {}", ex.getMessage());
}
}
}).start();
}
private static String httpRequest(String data, int connectTimeoutMs, int readTimeoutMs) throws Exception{
BasicHttpClientConnectionManager connManager;
connManager = new BasicHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build(),
null,
null,
null
);
HttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(connManager)
.build();
HttpPost httpPost = new HttpPost(REPORT_URL);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();
httpPost.setConfig(requestConfig);
StringEntity postEntity = new StringEntity(data, "UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.addHeader("User-Agent", WXPayConstants.USER_AGENT);
httpPost.setEntity(postEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity, "UTF-8");
}
}
package com.itsoft.hotel.commons.utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.SecureRandom;
public class WXPayRequest {
private WXPayConfig config;
public WXPayRequest( WXPayConfig config) throws Exception{
this.config = config;
}
private String requestOnce(final String domain, String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs, boolean useCert) throws Exception {
BasicHttpClientConnectionManager connManager;
if (useCert) {
char[] password = config.getMchID().toCharArray();
InputStream certStream = config.getCertStream();
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(certStream, password);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, password);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslContext,
new String[]{"TLSv1"},
null,
new DefaultHostnameVerifier());
connManager = new BasicHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslConnectionSocketFactory)
.build(),
null,
null,
null
);
}
else {
connManager = new BasicHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build(),
null,
null,
null
);
}
HttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(connManager)
.build();
String url = "https://" + domain + urlSuffix;
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();
httpPost.setConfig(requestConfig);
StringEntity postEntity = new StringEntity(data, "UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.addHeader("User-Agent", WXPayConstants.USER_AGENT + " " + config.getMchID());
httpPost.setEntity(postEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity, "UTF-8");
}
private String request(String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs, boolean useCert, boolean autoReport) throws Exception {
Exception exception = null;
long elapsedTimeMillis = 0;
long startTimestampMs = WXPayUtil.getCurrentTimestampMs();
boolean firstHasDnsErr = false;
boolean firstHasConnectTimeout = false;
boolean firstHasReadTimeout = false;
IWXPayDomain.DomainInfo domainInfo = config.getWXPayDomain().getDomain(config);
if(domainInfo == null){
throw new Exception("WXPayConfig.getWXPayDomain().getDomain() is empty or null");
}
try {
String result = requestOnce(domainInfo.domain, urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs, useCert);
elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
config.getWXPayDomain().report(domainInfo.domain, elapsedTimeMillis, null);
WXPayReport.getInstance(config).report(
uuid,
elapsedTimeMillis,
domainInfo.domain,
domainInfo.primaryDomain,
connectTimeoutMs,
readTimeoutMs,
firstHasDnsErr,
firstHasConnectTimeout,
firstHasReadTimeout);
return result;
}
catch (UnknownHostException ex) {
exception = ex;
firstHasDnsErr = true;
elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
WXPayUtil.getLogger().warn("UnknownHostException for domainInfo {}", domainInfo);
WXPayReport.getInstance(config).report(
uuid,
elapsedTimeMillis,
domainInfo.domain,
domainInfo.primaryDomain,
connectTimeoutMs,
readTimeoutMs,
firstHasDnsErr,
firstHasConnectTimeout,
firstHasReadTimeout
);
}
catch (ConnectTimeoutException ex) {
exception = ex;
firstHasConnectTimeout = true;
elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
WXPayUtil.getLogger().warn("connect timeout happened for domainInfo {}", domainInfo);
WXPayReport.getInstance(config).report(
uuid,
elapsedTimeMillis,
domainInfo.domain,
domainInfo.primaryDomain,
connectTimeoutMs,
readTimeoutMs,
firstHasDnsErr,
firstHasConnectTimeout,
firstHasReadTimeout
);
}
catch (SocketTimeoutException ex) {
exception = ex;
firstHasReadTimeout = true;
elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
WXPayUtil.getLogger().warn("timeout happened for domainInfo {}", domainInfo);
WXPayReport.getInstance(config).report(
uuid,
elapsedTimeMillis,
domainInfo.domain,
domainInfo.primaryDomain,
connectTimeoutMs,
readTimeoutMs,
firstHasDnsErr,
firstHasConnectTimeout,
firstHasReadTimeout);
}
catch (Exception ex) {
exception = ex;
elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
WXPayReport.getInstance(config).report(
uuid,
elapsedTimeMillis,
domainInfo.domain,
domainInfo.primaryDomain,
connectTimeoutMs,
readTimeoutMs,
firstHasDnsErr,
firstHasConnectTimeout,
firstHasReadTimeout);
}
config.getWXPayDomain().report(domainInfo.domain, elapsedTimeMillis, exception);
throw exception;
}
public String requestWithoutCert(String urlSuffix, String uuid, String data, boolean autoReport) throws Exception {
return this.request(urlSuffix, uuid, data, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs(), false, autoReport);
}
public String requestWithoutCert(String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs, boolean autoReport) throws Exception {
return this.request(urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs, false, autoReport);
}
public String requestWithCert(String urlSuffix, String uuid, String data, boolean autoReport) throws Exception {
return this.request(urlSuffix, uuid, data, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs(), true, autoReport);
}
public String requestWithCert(String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs, boolean autoReport) throws Exception {
return this.request(urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs, true, autoReport);
}
}
package com.itsoft.hotel.commons.utils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
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.security.MessageDigest;
import java.security.SecureRandom;
import java.security.Security;
import java.util.*;
public class WXPayUtil {
private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final Random RANDOM = new SecureRandom();
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) {
}
return data;
} catch (Exception ex) {
WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
throw ex;
}
}
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();
try {
writer.close();
}
catch (Exception ex) {
}
return output;
}
public static String generateSignedXml(final Map<String, String> data, String key) throws Exception {
return generateSignedXml(data, key, WXPayConstants.SignType.MD5);
}
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);
}
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);
}
public static boolean isSignatureValid(Map<String, String> data, String key) throws Exception {
return isSignatureValid(data, key, WXPayConstants.SignType.MD5);
}
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);
}
public static String generateSignature(final Map<String, String> data, String key) throws Exception {
return generateSignature(data, key, WXPayConstants.SignType.MD5);
}
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));
}
}
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);
}
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();
}
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();
}
public static Logger getLogger() {
Logger logger = LoggerFactory.getLogger("wxpay java sdk");
return logger;
}
public static long getCurrentTimestamp() {
return System.currentTimeMillis()/1000;
}
public static long getCurrentTimestampMs() {
return System.currentTimeMillis();
}
}
package com.itsoft.hotel.commons.utils;
import org.w3c.dom.Document;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public final class WXPayXmlUtil {
public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
documentBuilderFactory.setXIncludeAware(false);
documentBuilderFactory.setExpandEntityReferences(false);
return documentBuilderFactory.newDocumentBuilder();
}
public static Document newDocument() throws ParserConfigurationException {
return newDocumentBuilder().newDocument();
}
}
package com.itsoft.hotel.commons.utils;
import java.io.InputStream;
public abstract class WXPayConfig {
abstract String getAppID();
abstract String getMchID();
abstract String getKey();
abstract InputStream getCertStream();
public int getHttpConnectTimeoutMs() {
return 6*1000;
}
public int getHttpReadTimeoutMs() {
return 8*1000;
}
abstract IWXPayDomain getWXPayDomain();
public boolean shouldAutoReport() {
return true;
}
public int getReportWorkerNum() {
return 6;
}
public int getReportQueueMaxSize() {
return 10000;
}
public int getReportBatchSize() {
return 10;
}
}
继承 WXPayConfing 配置 支付信息
package com.itsoft.hotel.commons.utils;
import com.itsoft.framework.core.utils.StringUtils;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
@Service
public class MyWxPayConfig extends WXPayConfig {
private byte[] certData;
private String appId ;
private String wxPayKey ;
private String wxPayMchId ;
public byte[] getCertData() {
return certData;
}
public void setCertData(byte[] certData) {
this.certData = certData;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getWxPayKey() {
return wxPayKey;
}
public void setWxPayKey(String wxPayKey) {
this.wxPayKey = wxPayKey;
}
public String getWxPayMchId() {
return wxPayMchId;
}
public void setWxPayMchId(String wxPayMchId) {
this.wxPayMchId = wxPayMchId;
}
private String linuxOS ;
public String getLinuxOS() {
return linuxOS;
}
public void setLinuxOS(String linuxOS) {
this.linuxOS = linuxOS;
}
@Override
public String getAppID() {
return appId;
}
@Override
public String getMchID() {
return wxPayMchId;
}
@Override
public String getKey() {
return wxPayKey;
}
@Override
public InputStream getCertStream() {
return new ByteArrayInputStream(this.certData);
}
@Override
public IWXPayDomain getWXPayDomain() {
IWXPayDomain iwxPayDomain = new IWXPayDomain() {
@Override
public void report(String domain, long elapsedTimeMillis, Exception ex) {
}
@Override
public DomainInfo getDomain(WXPayConfig config) {
return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true);
}
};
return iwxPayDomain;
}
public void MyWxPayConfigInit() throws Exception {
if (StringUtils.isNotBlank(getLinuxOS())){
String property = System.getProperty("os.name");
File file = null;
if (property.contains("Windows") || property.contains("windows")) {
file = new File(getLinuxOS());
} else {
file = new File(getLinuxOS());
}
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
}
}
MD5工具
package com.itsoft.hotel.commons.utils;
import java.security.MessageDigest;
public class MD5Util {
public final static String MD5(String s) {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
}
下单,退款 控制层
package com.itsoft.hotel.controller;
import com.itsoft.framework.core.data.Result;
import com.itsoft.hotel.entity.ManagerHotelInfoVO;
import com.itsoft.hotel.entity.ManagerHotelOrderVO;
import com.itsoft.hotel.service.ManagerHotelInfoService;
import com.itsoft.hotel.service.ManagerHotelOrderService;
import com.itsoft.hotel.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/payRefund", produces = "application/json;charset=UTF-8")
@Api(value = "支付回调", tags = ":支付回调")
public class PayRefundController extends CommonController{
@Autowired
private PayService payService;
@Autowired
private ManagerHotelInfoService managerHotelInfoService;
@Autowired
private ManagerHotelOrderService managerHotelOrderService;
@ApiOperation(notes = "微信退款", value = "微信退款")
@ApiImplicitParam(name = "orderId", required = true, value = "订单id", dataType = "String")
@RequestMapping(value = "/refund",method = RequestMethod.POST)
public Result refund(String orderId) throws Exception {
return payService.refund(orderId);
}
@ApiOperation(notes = "微信支付", value = "微信支付")
@ApiImplicitParams({
@ApiImplicitParam(name = "orderId", required = true, value = "订单id", dataType = "String"),
@ApiImplicitParam(name = "payType", required = true, value = "支付类型 NATIVE 扫码 JSAPI 公众号", dataType = "String"),
@ApiImplicitParam(name = "openid", required = true, value = "openid", dataType = "String"),
})
@RequestMapping(value = "/pay",method = RequestMethod.POST)
public Result pay(String orderId,String payType,String openid) throws Exception {
ManagerHotelOrderVO hotelOrderVO= managerHotelOrderService.getById(orderId);
if (hotelOrderVO.getStatus()!=0) return Result.error("订单状态错误!");
hotelOrderVO.setClientIp(getIp2(request));
ManagerHotelInfoVO hotelInfoVO= managerHotelInfoService.getById(hotelOrderVO.getHotelId());
return payService.pay(hotelOrderVO,hotelInfoVO,payType,openid);
}
}
service层
package com.itsoft.hotel.service;
import com.itsoft.framework.core.data.DataMap;
import com.itsoft.framework.core.data.Result;
import com.itsoft.hotel.entity.ManagerHotelInfoVO;
import com.itsoft.hotel.entity.ManagerHotelOrderVO;
import com.itsoft.hotel.entity.PaySettingVO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface PayService {
Result pay(ManagerHotelOrderVO managerHotelOrderVO, ManagerHotelInfoVO mchInfo, String payType, String openid);
String wxNotifyuUrl(HttpServletRequest request, HttpServletResponse response);
Result refund(String orderId) throws Exception;
String wxRefundNotifyuUrl(HttpServletRequest request, HttpServletResponse response);
}
serviceImpl 层
package com.itsoft.hotel.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itsoft.framework.core.data.Result;
import com.itsoft.framework.core.logger.LoggerHelper;
import com.itsoft.framework.core.utils.IdUtils;
import com.itsoft.hotel.commons.utils.*;
import com.itsoft.hotel.entity.ManagerHotelInfoVO;
import com.itsoft.hotel.entity.ManagerHotelOrderVO;
import com.itsoft.hotel.mapper.ManagerHotelInfoMapper;
import com.itsoft.hotel.mapper.ManagerHotelOrderMapper;
import com.itsoft.hotel.service.ManagerHotelOrderService;
import com.itsoft.hotel.service.PayService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
@Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
public class PayServiceImpl implements PayService {
public static Logger log = LoggerFactory.getLogger(LoggerHelper.class);
@Autowired
private MyWxPayConfig myWxPayConfig;
@Autowired
private ManagerHotelOrderMapper managerHotelOrderMapper;
@Autowired
private ManagerHotelInfoMapper managerHotelInfoMapper;
@Autowired
private ManagerHotelOrderService managerHotelOrderService;
@Override
public Result pay(ManagerHotelOrderVO managerHotelOrderVO, ManagerHotelInfoVO mchInfo, String payType, String openid) {
if (StringUtils.isNotBlank(openid))managerHotelOrderVO.setOpenid(openid);
return wxPayV2SubmitOrder(managerHotelOrderVO,mchInfo,payType);
}
@Override
public String wxNotifyuUrl(HttpServletRequest request, HttpServletResponse response) {
try {
InputStream is = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String resXml = sb.toString();
log.info("微信支付异步通知请求包------");
log.info(resXml);
return wechatPayBack(resXml);
} catch (Exception e) {
log.error("微信支付回调通知失败", e);
String result = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml> ";
return result;
}
}
@Override
public Result refund(String orderId) throws Exception {
ManagerHotelOrderVO managerHotelOrderVO= managerHotelOrderMapper.selectById(orderId);
QueryWrapper<ManagerHotelInfoVO> managerHotelInfoVOQueryWrapper =new QueryWrapper<>();
managerHotelInfoVOQueryWrapper.eq("id",managerHotelOrderVO.getHotelId());
ManagerHotelInfoVO mchInfo =managerHotelInfoMapper.selectOne(managerHotelInfoVOQueryWrapper);
myWxPayConfig.setWxPayKey(mchInfo.getHotelKey());
myWxPayConfig.setAppId(mchInfo.getAppId());
myWxPayConfig.setWxPayMchId(mchInfo.getMchId());
myWxPayConfig.setLinuxOS(mchInfo.getApiclientCert());
myWxPayConfig.MyWxPayConfigInit();
Map<String, String> map = new HashMap<>();
map.put("appid", mchInfo.getAppId());
map.put("mch_id", mchInfo.getMchId());
map.put("out_trade_no", orderId);
map.put("out_refund_no", IdUtils.fastSimpleUUID());
map.put("total_fee", OrderNoUtil.changeY2F(managerHotelOrderVO.getTotalAmount()));
map.put("refund_fee", OrderNoUtil.changeY2F(managerHotelOrderVO.getTotalAmount()));
map.put("refund_fee_type", "CNY");
map.put("notify_url", mchInfo.getRefundNotifyUrl());
WXPay wxpay = new WXPay(myWxPayConfig);
Map<String, String> refundMap = wxpay.refund(map);
log.info("====微信退款参数====={}", JSONObject.toJSON(refundMap));
String returnCode = refundMap.get("return_code");
String errCodeDes = refundMap.get("err_code_des");
String resultCode = refundMap.get("result_code");
if (returnCode.equals("SUCCESS") && resultCode.equals("SUCCESS")) {
return Result.success("申请退款成功");
} else {
managerHotelOrderService.updateStatus(orderId,"2");
return Result.error("申请退款失败");
}
}
@Override
public String wxRefundNotifyuUrl(HttpServletRequest request, HttpServletResponse response) {
try {
InputStream is = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String resXml = sb.toString();
log.info("微信退款异步通知请求包------");
log.info(resXml);
return wechatRefund(resXml);
} catch (Exception e) {
log.error("微信退款回调通知失败", e);
String result = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml> ";
return result;
}
}
public String wechatRefund(String xmlStr) {
String xmlBack = "";
Map<String, String> notifyMap = null;
String lockKey = "refund_back_";
try {
notifyMap = WXPayUtil.xmlToMap(xmlStr);
QueryWrapper<ManagerHotelInfoVO> managerHotelInfoVOQueryWrapper =new QueryWrapper<>();
managerHotelInfoVOQueryWrapper.eq("mchId",notifyMap.get("mch_id"));
ManagerHotelInfoVO managerHotelInfoVO= managerHotelInfoMapper.selectOne(managerHotelInfoVOQueryWrapper);
myWxPayConfig.setWxPayKey(managerHotelInfoVO.getHotelKey());
myWxPayConfig.setAppId(managerHotelInfoVO.getAppId());
myWxPayConfig.setWxPayMchId(managerHotelInfoVO.getMchId());
myWxPayConfig.setLinuxOS(managerHotelInfoVO.getApiclientCert());
myWxPayConfig.MyWxPayConfigInit();
String reqInfo = AESUtil.decryptData(notifyMap.get("req_info"),managerHotelInfoVO.getHotelKey());
Map<String, String> map = WXPayUtil.xmlToMap(reqInfo);
if (null == map || map.size() == 0) {
log.info("解密异常");
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";
}
String transactionId = map.get("transaction_id");
String outTradeNo = map.get("out_trade_no");
String refundId = map.get("refund_id");
String outRefundNo = map.get("out_refund_no");
String totalFee = map.get("total_fee");
String settlementTotalFee = map.get("settlement_total_fee");
String refundFee = map.get("refund_fee");
String settlementRefundFee = map.get("settlement_refund_fee");
String refundStatus = map.get("refund_status");
String successTime = map.get("success_time");
String refundRecvAccout = map.get("refund_recv_accout");
String refundAccount = map.get("refund_account");
String refundRequestSource = map.get("refund_request_source");
if (null == settlementRefundFee) {
log.info("退款金额为空");
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";
}
if (null == outRefundNo) {
log.info("商户退款单号为空");
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";
}
if (null == refundId) {
log.info("微信退款单号为空");
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";
}
log.info("\n\t----------------------------------------------------------\n\t" +
"订单退款成功" +
"\n\t----------------------------------------------------------");
managerHotelOrderService.updateStatus(outTradeNo,"1");
return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
} catch (Exception e) {
e.printStackTrace();
log.info("退款回调失败");
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";
}
}
public String wechatPayBack(String xmlStr) {
String xmlBack = "";
Map<String, String> notifyMap = null;
try {
notifyMap = WXPayUtil.xmlToMap(xmlStr);
QueryWrapper<ManagerHotelInfoVO> managerHotelInfoVOQueryWrapper =new QueryWrapper<>();
managerHotelInfoVOQueryWrapper.eq("mchId",notifyMap.get("mch_id"));
ManagerHotelInfoVO mchInfo= managerHotelInfoMapper.selectOne(managerHotelInfoVOQueryWrapper);
myWxPayConfig.setWxPayKey(mchInfo.getHotelKey());
myWxPayConfig.setAppId(mchInfo.getAppId());
myWxPayConfig.setWxPayMchId(mchInfo.getMchId());
WXPay wxpay = new WXPay(myWxPayConfig);
if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {
log.info("签名成功 \r\n" + JSONObject.toJSONString(notifyMap));
String returnCode = notifyMap.get("return_code").trim();
String outTradeNo = notifyMap.get("out_trade_no").trim();
if (null == outTradeNo) {
log.info("微信支付回调失败订单号: {}", notifyMap);
xmlBack = "<xml><return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg></xml> ";
return xmlBack;
}
String tradeType = notifyMap.get("trade_type").trim();
String bankType = notifyMap.get("bank_type").trim();
String transactionId = notifyMap.get("transaction_id").trim();
String cashFee = notifyMap.get("cash_fee").trim();
String totalFee = notifyMap.get("total_fee").trim();
String timeEnd = notifyMap.get("time_end").trim();
String isSubscribe = notifyMap.get("is_subscribe").trim();
SimpleDateFormat format = new SimpleDateFormat("yyyy-HH-dd HH:mm:ss");
Date date = new SimpleDateFormat("yyyy-HH-dd HH:mm:ss").parse(format.format(new SimpleDateFormat("yyyyHHddHHmmss").parse(timeEnd)));
log.info("微信支付回调成功订单号: {}", notifyMap);
managerHotelOrderService.updateStatus(outTradeNo,"0");
xmlBack = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
return xmlBack;
} else {
log.error("微信支付回调通知签名错误");
xmlBack = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml> ";
return xmlBack;
}
} catch (Exception e) {
log.error("微信支付回调通知失败", e);
xmlBack = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml> ";
}
return xmlBack;
}
public Result wxPayV2SubmitOrder(ManagerHotelOrderVO managerHotelOrderVO,ManagerHotelInfoVO mchInfo, String payType) {
try {
myWxPayConfig.setWxPayKey(mchInfo.getHotelKey());
myWxPayConfig.setAppId(mchInfo.getAppId());
myWxPayConfig.setWxPayMchId(mchInfo.getMchId());
WXPay wxpay = new WXPay(myWxPayConfig);
Map<String, String> data = new HashMap<String, String>();
data.put("body", managerHotelOrderVO.getHotelRoomName());
data.put("out_trade_no", managerHotelOrderVO.getId());
data.put("total_fee", OrderNoUtil.changeY2F(managerHotelOrderVO.getTotalAmount()));
data.put("spbill_create_ip", managerHotelOrderVO.getClientIp());
if (StringUtils.equals(payType,"NATIVE")){
data.put("trade_type", "NATIVE");
}
if (StringUtils.equals(payType,"JSAPI")){
data.put("trade_type", "JSAPI");
data.put("openid", managerHotelOrderVO.getOpenid());
}
data.put("notify_url", mchInfo.getNotifyUrl());
Map<String, String> response = wxpay.unifiedOrder(data);
log.info("===微信唤醒成功==={}",JSONObject.toJSONString(response));
String returnCode = response.get("return_code");
String resultCode = response.get("result_code");
String returnMsg = response.get("return_msg");
String prepayId = response.get("prepay_id");
if (CollUtil.isEmpty(response) || !returnCode.equals("SUCCESS") ||
!resultCode.equals("SUCCESS") || !returnMsg.equals("OK") || null == prepayId) {
log.error("========微信支付======{}",response.get("err_code_des"));
return Result.error(response.get("err_code_des"));
}
if (StringUtils.equals(payType,"NATIVE")){
String codeUrl = response.get("code_url");
return Result.success(codeUrl);
}
if (StringUtils.equals(payType,"JSAPI")){
Map<String, String> map = wechatCreatePay(prepayId);
log.info(JSONObject.toJSONString(map));
if (CollUtil.isEmpty(map) || map.size() == 0) {
return Result.error("支付调起参数异常");
}
return Result.success(map);
}
} catch (Exception e) {
e.printStackTrace();
log.error("========微信支付异常======{}",e);
}
return Result.error("微信唤醒失败");
}
public Map<String, String> wechatCreatePay(String prepayId) {
try {
Map<String, String> wxPayMap = new HashMap<String, String>();
wxPayMap.put("appId", myWxPayConfig.getAppID());
wxPayMap.put("timeStamp", String.valueOf(WXPayUtil.getCurrentTimestamp()));
wxPayMap.put("nonceStr", WXPayUtil.generateNonceStr());
wxPayMap.put("package", "prepay_id=" + prepayId);
wxPayMap.put("signType", "MD5");
String sign = WXPayUtil.generateSignature(wxPayMap, myWxPayConfig.getKey());
Map<String, String> map = new HashMap<>();
map.put("prepay_id", prepayId);
map.put("paySign", sign);
map.putAll(wxPayMap);
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
成功回调 控制层
package com.itsoft.hotel.controller;
import com.alibaba.fastjson.JSONObject;
import com.itsoft.framework.core.data.DataMap;
import com.itsoft.hotel.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping(value = "/frontendPayNotify")
@Api(value = "支付回调", tags = ":支付回调")
public class FrontendPayNotifyController extends CommonController{
@Autowired
private PayService payService;
@ApiOperation(notes = "微信回调通知", value = "微信回调通知")
@ApiImplicitParams({
})
@RequestMapping(value = "/wxNotifyuUrl",method = RequestMethod.POST)
public String wxNotifyuUrl(HttpServletRequest request,HttpServletResponse response) throws IOException {
logger.info("==========微信回调参数request.getInputStream()======={}", JSONObject.toJSONString(request.getInputStream()));
return payService.wxNotifyuUrl(request,response);
}
@ApiOperation(notes = "微信退款回调通知", value = "微信退款回调通知")
@ApiImplicitParams({
})
@RequestMapping(value = "/wxRefundNotifyuUrl",method = RequestMethod.POST)
public String wxRefundNotifyuUrl(HttpServletRequest request,HttpServletResponse response) throws IOException {
logger.info("==========微信退款回调通知getInputStream()======={}",JSONObject.toJSONString(request.getInputStream()));
return payService.wxRefundNotifyuUrl(request,response);
}
}
pom文件
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!--httpUtil工具集成 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.7.11</version>
</dependency>