controller
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {
@Autowired
private PayService payService;
@ApiOperation(value = "付款码收款(扫客户)-服务费管理,需要服务费查询的参数外加收款的快递公司参数", notes = "已安全检查")
@PostMapping("/serviceFeeManagement/toPayment")
public Object toPayment(@RequestBody LittleBeeDTO json, HttpServletRequest req) {
Map map2 = new HashMap(8);
map2.put("mchId", "aaa");
map2.put("key", "aaa");
map2.put("orderNo", "aaa");
map2.put("money", 100);
map2.put("authCode", "");
Object o = payService.toPayment(map2);
Re.success("ss", o);
return o;
}
}
service
package com.cc.service.impl;
import com.cc.model.dto.Re;
import java.util.Map;
public interface PayService {
Re toPayment(Map<String, Object> map);
Re sweepCodePayment(Map<String, Object> map);
Re queryOrderState(Map<String, Object> map);
}
serviceImpl
package com.cc.service.impl;
import com.cc.model.dto.*;
import com.cc.utils.OkHttp3Utils;
import com.cc.utils.WeChatPayUtils;
import com.thoughtworks.xstream.XStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
@Component
@Slf4j
@Service
public class PayServiceImpl implements PayService {
@Autowired
private WeChatPayUtils wxPayUtil;
final String str = "butler-sit";
@Value("${wechat.callbackUrl}")
private String weChatCallbackUrl;
@Value("${wechat.appId}")
private String weChatAppId;
@Value("${wechat.mchId}")
private String weChatMchId;
@Value("${wechat.key}")
private String weChatKey;
@Override
public Re toPayment(Map<String, Object> map) {
Map<String, String> requestMap = new HashMap<String, String>(16);
String appId = weChatAppId;
requestMap.put("appid", appId);
String mchId = null;
String key = null;
String callbackUrl = weChatCallbackUrl;
if (callbackUrl.contains(str)) {
mchId = weChatMchId;
requestMap.put("mch_id", mchId);
key = weChatKey;
} else {
mchId = map.get("mchId").toString();
requestMap.put("mch_id", map.get("mchId").toString());
key = map.get("key").toString();
}
requestMap.put("total_fee", map.get("money").toString());
String noncestr = WeChatPayUtils.generateNonceStr();
requestMap.put("nonce_str", noncestr);
requestMap.put("body", "付款码支付");
String orderNo = map.get("orderNo") + "";
requestMap.put("out_trade_no", orderNo);
String createIp = "";
try {
InetAddress addr = InetAddress.getLocalHost();
createIp = addr.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
requestMap.put("spbill_create_ip", createIp);
requestMap.put("auth_code", map.get("authCode").toString());
requestMap.put("sign_type", "HMAC-SHA256");
String sign = null;
try {
sign = wxPayUtil.generateSignature(requestMap, key);
} catch (Exception e) {
throw new RuntimeException("生成签名错误");
}
requestMap.put("sign", sign);
log.info("请求付款码支付接口参数requestMap:" + requestMap.toString());
PaymentCodeDTO paymentCodeDTO = null;
try {
paymentCodeDTO = new PaymentCodeDTO();
BeanUtils.populate(paymentCodeDTO, requestMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
XStream xStream = new XStream();
XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByWildcard(new String[]{
"com.sf.sfkdgj.service.impl.**"
});
xStream.alias("xml", PaymentCodeDTO.class);
String xml = xStream.toXML(paymentCodeDTO);
String xmlToString = xml.replace("__", "_");
log.info("付款码支付请求参数为-----------:" + xmlToString);
UnifiedPayResposeDTO unifiedPayResposeDTO = WeChatPayUtils.httpOrder(xmlToString, WeChatConstant.TOPAYMENT_URL);
String returnCode = "SUCCESS";
String resultCode = "SUCCESS";
String userPaying = "USERPAYING";
log.info("付款码支付返回的xml转换为对象:" + unifiedPayResposeDTO.toString());
Map<String, Object> map1 = new HashMap<>(16);
map1.put("orderRespose", unifiedPayResposeDTO);
try {
if (returnCode.equals(unifiedPayResposeDTO.getReturn_code()) && resultCode.equals(unifiedPayResposeDTO.getResult_code())) {
log.info("*******付款码**********" + "接口请求成功!");
log.info("unifiedPayResposeDTO------------:" + unifiedPayResposeDTO.toString());
return Re.success(map1);
} else if (userPaying.equals(unifiedPayResposeDTO.getErr_code())) {
UnifiedPayResposeDTO unifiedPayResposeDTO1 = null;
int num = 3;
for (int i = 0; i < num; i++) {
Thread.sleep(10000);
Map<String, String> data = new HashMap<>(16);
data.put("appid", appId);
data.put("mch_id", mchId);
String noncestr1 = WeChatPayUtils.generateNonceStr();
data.put("nonce_str", noncestr1);
data.put("out_trade_no", map.get("orderNo").toString());
data.put("sign_type", "HMAC-SHA256");
String sign1 = null;
try {
sign1 = wxPayUtil.generateSignature(data, key);
} catch (Exception e) {
throw new RuntimeException("生成签名错误");
}
data.put("sign", sign1);
QueryOrderStateDTO queryOrderStateDTO = null;
try {
queryOrderStateDTO = new QueryOrderStateDTO();
BeanUtils.populate(queryOrderStateDTO, data);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
XStream xStream1 = new XStream();
XStream.setupDefaultSecurity(xStream1);
xStream1.allowTypesByWildcard(new String[]{
"com.sf.sfkdgj.service.impl.**"
});
xStream1.alias("xml", QueryOrderStateDTO.class);
String xml1 = xStream1.toXML(queryOrderStateDTO);
String xmlToString1 = xml1.replace("__", "_");
log.info("付款码支付请求参数为-----------:" + xmlToString1);
unifiedPayResposeDTO1 = WeChatPayUtils.httpOrder(xmlToString1, WeChatConstant.QUERYORDER_URL);
if (resultCode.equals(unifiedPayResposeDTO1.getTrade_state())) {
log.info("微信加密支付成功!");
return Re.success(map1);
}
log.info("正在支付" + unifiedPayResposeDTO1);
}
unifiedPayResposeDTO1.setReturn_msg("支付时间超时,请重新支付");
map1.put("orderRespose", unifiedPayResposeDTO1);
return Re.error(map1, "支付时间超时,请重新支付");
}
return Re.error(map1, unifiedPayResposeDTO.getErr_code_des());
} catch (Exception e) {
String text = "调用微信支付出错,返回状态码:" + unifiedPayResposeDTO.getReturn_code() + ",返回信息:" + unifiedPayResposeDTO.getResult_code();
if (unifiedPayResposeDTO.getErr_code() != null && !"".equals(unifiedPayResposeDTO.getErr_code())) {
text = text + ",错误码:" + unifiedPayResposeDTO.getErr_code() + ",错误描述:" + unifiedPayResposeDTO.getErr_code_des();
}
log.info("*******付款码支付失败**********" + text);
map1.put("responseMap", unifiedPayResposeDTO);
log.info("支付失败参数responseMap:" + unifiedPayResposeDTO.toString());
return Re.error(map1, unifiedPayResposeDTO.getErr_code_des());
}
}
private void reverseOrder(String certificateToString, String mchId, String certificate) {
try {
reverseOrder(certificateToString, mchId, certificate);
HttpPost httpPost = new HttpPost(WeChatConstant.REVERSE_URL);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
httpPost.setConfig(requestConfig);
StringEntity postEntity = new StringEntity(certificateToString, "UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.addHeader("User-Agent", WeChatConstant.USER_AGENT + " " + mchId);
httpPost.setEntity(postEntity);
CloseableHttpClient httpClient = OkHttp3Utils.createCloseableHttpClient(mchId, certificate);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String result = EntityUtils.toString(httpEntity, "UTF-8");
log.info(result);
Map<String, String> responseMap = WeChatPayUtils.xmlStr2Map(result);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Re sweepCodePayment(Map<String, Object> map) {
Map<String, String> requestMap = new HashMap<String, String>(16);
String appId = weChatAppId;
requestMap.put("appid", appId);
String mchId = null;
String key = null;
String callbackUrl = weChatCallbackUrl;
if (callbackUrl.contains(str)) {
mchId = weChatMchId;
requestMap.put("mch_id", mchId);
key = weChatKey;
} else {
mchId = map.get("mchId").toString();
requestMap.put("mch_id", mchId);
key = map.get("key").toString();
}
requestMap.put("total_fee", map.get("money").toString());
String noncestr = WeChatPayUtils.generateNonceStr();
requestMap.put("nonce_str", noncestr);
requestMap.put("body", "扫码支付");
String orderNo = String.valueOf(map.get("orderNo"));
requestMap.put("out_trade_no", orderNo);
String createIp = "";
try {
InetAddress addr = InetAddress.getLocalHost();
createIp = addr.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
requestMap.put("spbill_create_ip", createIp);
requestMap.put("notify_url", callbackUrl);
requestMap.put("trade_type", "NATIVE");
requestMap.put("sign_type", "HMAC-SHA256");
String sign = null;
try {
sign = wxPayUtil.generateSignature(requestMap, key);
} catch (Exception e) {
throw new RuntimeException("生成签名错误");
}
requestMap.put("sign", sign);
log.info("请求付款码支付接口参数requestMap:" + requestMap.toString());
SweepCodeDTO sweepCodeDTO = null;
try {
sweepCodeDTO = new SweepCodeDTO();
BeanUtils.populate(sweepCodeDTO, requestMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
XStream xStream = new XStream();
XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByWildcard(new String[]{
"com.sf.sfkdgj.service.impl.**"
});
xStream.alias("xml", SweepCodeDTO.class);
String xml = xStream.toXML(sweepCodeDTO);
String xmlToString = xml.replace("__", "_");
log.info("付款码支付请求参数为-----------:" + xmlToString);
UnifiedPayResposeDTO unifiedPayResposeDTO = WeChatPayUtils.httpOrder(xmlToString, WeChatConstant.UNIFIEDORDER_URL);
Map<String, Object> map1 = new HashMap<>(16);
map1.put("unifiedPayRespose", unifiedPayResposeDTO);
if (null != unifiedPayResposeDTO && WeChatConstant.SUCCESS.equals(unifiedPayResposeDTO.getReturn_code()) && WeChatConstant.SUCCESS.equals(unifiedPayResposeDTO.getResult_code())) {
String timestamp = String.valueOf(WeChatPayUtils.getCurrentTimestamp());
map1.put("unifiedPayRespose", unifiedPayResposeDTO);
SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appId", appId);
packageParams.put("signType", "HMACSHA256");
packageParams.put("nonceStr", noncestr);
packageParams.put("timeStamp", timestamp);
String packages = "prepay_id=" + unifiedPayResposeDTO.getPrepay_id();
packageParams.put("package", packages);
String sign1 = null;
try {
sign1 = wxPayUtil.generateSignature(packageParams, key);
} catch (Exception e) {
log.info("生成签名错误");
return Re.error(map1, "生成签名错误");
}
if (sign1 != null && !"".equals(sign1)) {
log.info("成功!");
return Re.success(map1);
} else {
return Re.error(map1, "生成签名失败");
}
} else {
String text = "调用微信扫码支付出错,返回状态码:" + unifiedPayResposeDTO.getReturn_code() + ",返回信息:" + unifiedPayResposeDTO.getReturn_msg();
if (unifiedPayResposeDTO.getErr_code() != null && !"".equals(unifiedPayResposeDTO.getErr_code())) {
text = text + ",错误码:" + unifiedPayResposeDTO.getErr_code() + ",错误描述:" + unifiedPayResposeDTO.getErr_code_des();
}
log.info("*******微信扫码支付失败**********" + text);
return Re.error(map1, unifiedPayResposeDTO.getErr_code_des());
}
}
@Override
public Re queryOrderState(Map<String, Object> map) {
Map<String, String> requestMap = new HashMap<String, String>(16);
String appId = weChatAppId;
requestMap.put("appid", appId);
String mchId = null;
String key = null;
String callbackUrl = weChatCallbackUrl;
if (callbackUrl.contains(str)) {
mchId = weChatMchId;
requestMap.put("mch_id", mchId);
requestMap.put("total_fee", "1");
key = weChatKey;
} else {
requestMap.put("total_fee", map.get("money").toString());
mchId = map.get("mchId").toString();
requestMap.put("mch_id", mchId);
key = map.get("key").toString();
}
Map<String, String> data = new HashMap<>(16);
data.put("appid", appId);
data.put("mch_id", mchId);
String noncestr1 = WeChatPayUtils.generateNonceStr();
data.put("nonce_str", noncestr1);
data.put("out_trade_no", map.get("orderNo").toString());
data.put("sign_type", "HMAC-SHA256");
String sign1 = null;
try {
sign1 = wxPayUtil.generateSignature(data, key);
} catch (Exception e) {
throw new RuntimeException("生成签名错误");
}
data.put("sign", sign1);
QueryOrderStateDTO queryOrderStateDTO = null;
try {
queryOrderStateDTO = new QueryOrderStateDTO();
BeanUtils.populate(queryOrderStateDTO, data);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
XStream xStream = new XStream();
XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByWildcard(new String[]{
"com.sf.sfkdgj.service.impl.**"
});
xStream.alias("xml", QueryOrderStateDTO.class);
String xml = xStream.toXML(queryOrderStateDTO);
String xmlToString = xml.replace("__", "_");
log.info("付款码支付请求参数为-----------:" + xmlToString);
UnifiedPayResposeDTO unifiedPayResposeDTO = WeChatPayUtils.httpOrder(xmlToString, WeChatConstant.QUERYORDER_URL);
Map<String, Object> map1 = new HashMap<>(16);
map1.put("unifiedPayRespose", unifiedPayResposeDTO);
if (null != unifiedPayResposeDTO && WeChatConstant.SUCCESS.equals(unifiedPayResposeDTO.getReturn_code())) {
return Re.success(map1);
} else {
String text = "调用微信扫码支付出错,返回状态码:" + unifiedPayResposeDTO.getReturn_code() + ",返回信息:" + unifiedPayResposeDTO.getReturn_msg();
if (unifiedPayResposeDTO.getErr_code() != null && !"".equals(unifiedPayResposeDTO.getErr_code())) {
text = text + ",错误码:" + unifiedPayResposeDTO.getErr_code() + ",错误描述:" + unifiedPayResposeDTO.getErr_code_des();
log.info("*******微信扫码支付失败**********" + text);
}
return Re.error(map1, unifiedPayResposeDTO.getErr_code_des());
}
}
public String callback(HttpServletRequest request) throws Exception {
log.info("进入接口callback");
ServletInputStream instream = null;
StringBuffer sb = new StringBuffer();
Map<String, String> returnData = new HashMap<String, String>(4);
try {
instream = request.getInputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = instream.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len));
}
} catch (Exception e) {
log.error(e.getMessage());
log.error("读取微信请求参数错误");
} finally {
instream.close();
}
Map<String, String> map = WeChatPayUtils.xmlStr2Map(sb.toString());
if (map != null) {
String outTradeNo = map.get("out_trade_no");
String openid = map.get("openid");
BigDecimal money = new BigDecimal(map.get("total_fee")).divide(new BigDecimal("100"));
}
String xml = WeChatPayUtils.GetMapToXml(returnData);
log.info("不可以");
log.info(xml);
return xml;
}
}
WeChatConstant
package com.cc.model.dto;
import org.apache.http.client.HttpClient;
public class WeChatConstant {
public static final String ZERO = "0";
public static final String ONE = "1";
public static final String NULL = "null";
public static final String DETAIL = "TestDetail";
public static final String SUCCESS = "SUCCESS";
public static final String FAIL = "FAIL";
public static final String OK = "OK";
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 FENZ_URL ="https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
public static final String TOPAYMENT_URL = "https://api.mch.weixin.qq.com/pay/micropay";
public static final String QUERYORDER_URL = "https://api.mch.weixin.qq.com/pay/orderquery";
public static final String REVERSE_URL = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
public static final String UNIFIEDORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
public static final String RETURN_CODE ="return_code";
public static final String RETURN_MSG ="return_msg";
public static final String RESULT_CODE ="result_code";
public static final String ERR_CODE ="err_code";
public static final String ERR_CODE_DES ="err_code_des";
public static final String MCH_ID ="mch_id";
public static final String SUB_MCH_ID ="sub_mch_id";
public static final String APPID ="appid";
public static final String SUB_APPID ="sub_appid";
public static final String NONCE_STR ="nonce_str";
public static final String SIGN ="sign";
public static final String TRANSACTION_ID ="transaction_id";
public static final String OUT_ORDER_NO ="out_order_no";
public static final String ORDER_ID ="order_id";
public enum SignType {
MD5,
HMACSHA256
}
public static final String APP_ID = "wx485a4fb5bd28a751";
public static final String APP_SECRET = "a6792f30037cf7efaa02748df188d369";
public static final String TOKEN = "jishipei";
public static final String CERTIFICATE_PATH="C:/tools/apiclient_cert.p12";
public static final String REFUND_URL ="https://api.mch.weixin.qq.com/secapi/pay/refund";
public static final String BASE_URL = "http://weixin.xinfor.com";
public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
public static final String PAY_SUCCESS_MESSAGE_ID = "rVUVeBgQI8LalswUQ9NiaQ_8OZBM8nge35wzVYOeDkA";
public static final String SEND_TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
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 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 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";
}
PaymentCodeDTO
package com.cc.model.dto;
import lombok.Data;
@Data
public class PaymentCodeDTO {
private String appid;
private String mch_id;
private String total_fee;
private String nonce_str;
private String body;
private String out_trade_no;
private String spbill_create_ip;
private String auth_code;
private String sign;
private String sign_type;
}
QueryOrderStateDTO
package com.cc.model.dto;
import lombok.Data;
@Data
public class QueryOrderStateDTO {
private String appid;
private String mch_id;
private String out_trade_no;
private String nonce_str;
private String sign;
private String sign_type;
}
Re
package com.cc.model.dto;
import com.cc.model.ResponseConstant;
public class Re<T> {
private Integer status;
private String msg;
private T data;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Re(Integer status, String msg, T data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public Re(T data) {
this.status = ResponseConstant.SUCCESS_STATUS;
this.msg = ResponseConstant.SUCCESS_MSG;
this.data = data;
}
public Re() {
this.status = ResponseConstant.ERROR_STATUS;
this.msg = "error";
this.data = null;
}
public static <T> Re<T> success() {
return new Re<T>(ResponseConstant.SUCCESS_STATUS, ResponseConstant.SUCCESS_MSG, null);
}
public static <T> Re<T> success(T data) {
return new Re<T>(ResponseConstant.SUCCESS_STATUS, ResponseConstant.SUCCESS_MSG, data);
}
public static <T> Re<T> success(String msg, T data) {
return new Re<T>(ResponseConstant.SUCCESS_STATUS, msg, data);
}
public static <T> Re<T> error(T data) {
return new Re<T>(ResponseConstant.ERROR_STATUS, "error", data);
}
public static <T> Re<T> errorMsg(String msg) {
return new Re<T>(ResponseConstant.ERROR_STATUS, msg, null);
}
public static <T> Re<T> error(T data, String msg) {
return new Re<T>(ResponseConstant.ERROR_STATUS, msg, data);
}
@Override
public String toString() {
return "ResultUtil [status=" + status + ", msg=" + msg + ", data=" + data + "]";
}
}
SweepCodeDTO
package com.cc.model.dto;
import lombok.Data;
@Data
public class SweepCodeDTO {
private String appid;
private String mch_id;
private String total_fee;
private String nonce_str;
private String body;
private String out_trade_no;
private String spbill_create_ip;
private String notify_url;
private String trade_type;
private String sign;
private String sign_type;
}
UnifiedPayResposeDTO
package com.cc.model.dto;
import lombok.Data;
@Data
public class UnifiedPayResposeDTO {
private String return_code;
private String return_msg;
private String appid;
private String mch_id;
private String device_info;
private String nonce_str;
private String sign;
private String result_code;
private String err_code;
private String err_code_des;
private String openid;
private String is_subscribe;
private String trade_type;
private String prepay_id;
private String code_url;
private String bank_type;
private String total_fee;
private String cash_fee;
private String transaction_id;
private String out_trade_no;
private String time_end;
private String trade_state;
private String fee_type;
private String attach;
private String promotion_detail;
private String cash_fee_type;
private String settlement_total_fee;
private String coupon_fee;
private String trade_state_desc;
}
OkHttp3Utils
package com.cc.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import javax.net.ssl.SSLContext;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
public class OkHttp3Utils {
private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
private static String SF_MAP_URL ;
private static String KEY ;
private static String URL ;
private static String ACCESS_Id ;
private static String ACCESS_TOKEN ;
@Value("${dm.gis.url}")
public void setGisUrl(String url) {
SF_MAP_URL = url;
}
@Value("${dm.gis.ak}")
public void setKEY(String ak) {
KEY = ak;
}
@Value("${email.url}")
public void setURL(String url) {
URL = url;
}
@Value("${ds.sms.accessId}")
public void setAccessId(String accessId) {
ACCESS_Id = accessId;
}
@Value("${ds.sms.accessToken}")
public void setAccessToken(String accessToken) {
ACCESS_TOKEN = accessToken;
}
public static String sendByGetUrl(String addres) {
String url = SF_MAP_URL + KEY
+ "&address=" + addres;
String result;
OkHttpClient client = new OkHttpClient();
client.newBuilder().connectTimeout(3, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url(url)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
assert response.body() != null;
result = response.body().string();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String sendByGet(String url,String params) {
String result;
OkHttpClient client = new OkHttpClient();
client.newBuilder().connectTimeout(3, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url(url+"?"+params)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
assert response.body() != null;
result = response.body().string();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String sendByPostJson(String url, String json) {
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
assert response.body() != null;
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String sendByPostJsonByUrlencoded(String url, String json) {
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(MEDIA_TYPE_TEXT, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
assert response.body() != null;
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static boolean sendEmailByPost(JSONObject templateJson, String email, String templateCode) throws Exception{
JSONObject params = new JSONObject();
params.put("userId", email);
params.put("templateCode", templateCode);
params.put("templateParam", templateJson);
params.put("subject", "企业邮箱验证码");
params.put("accessId", ACCESS_Id);
params.put("accessToken", ACCESS_TOKEN);
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, params.toJSONString());
Request request = new Request.Builder()
.url(URL)
.post(body)
.build();
Response response = null;
try {
boolean isSuccess = false;
response = client.newCall(request).execute();
assert response.body() != null;
String string = response.body().string();
JSONObject result = JSON.parseObject(string);
log.info("发邮件返回"+result);
if(result != null){
isSuccess = result.getBooleanValue("success");
String requestId = result.getString("requestId");
if(!isSuccess){
log.error(email+"邮件发送失败:"+result.getString("errorMessage"));
}
}
return isSuccess;
} catch (Exception e) {
log.error(email+"邮件发送失败:"+e.getMessage());
return false;
}
}
public static String sendByPostMap(String url, Map<String, String> params) {
String result;
OkHttpClient client = new OkHttpClient();
StringBuilder content = new StringBuilder();
Set<Map.Entry<String, String>> entrys = params.entrySet();
Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
content.append(entry.getKey()).append("=").append(entry.getValue());
if (iterator.hasNext()) {
content.append("&");
}
}
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_TEXT, content.toString());
Request request = new Request.Builder().url(url).post(requestBody).build();
Response response = null;
try {
response = client.newCall(request).execute();
assert response.body() != null;
result = response.body().string();
System.out.println("result = " + result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static CloseableHttpClient createCloseableHttpClient(String mchId, String certificate )throws Exception{
KeyStore keyStore = KeyStore.getInstance("PKCS12");
Resource resource = new ClassPathResource(certificate);
InputStream in = resource.getInputStream();
DataInputStream instream = new DataInputStream(in);
try {
keyStore.load(instream, mchId.toCharArray());
} finally {
instream.close();
}
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
return httpClient;
}
}
WeChatPayUtils
package com.cc.utils;
import com.cc.model.dto.UnifiedPayResposeDTO;
import com.cc.model.dto.WeChatConstant;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
@Component
public class WeChatPayUtils {
private static Logger log = LoggerFactory.getLogger(WeChatPayUtils.class);
public static final String ENCRYPTION_TYPE = "MD5";
public static XStream xstream = new XStream(new XppDriver() {
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
boolean cdata = true;
String nodeName = "";
@SuppressWarnings("unchecked")
@Override
public void startNode(String name, Class clazz) {
nodeName = name;
super.startNode(name, clazz);
}
@Override
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
String detail="detail";
if (!detail.equals(nodeName)) {
writer.write(text);
} else {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
}
} else {
writer.write(text);
}
}
};
}
});
public static UnifiedPayResposeDTO httpOrder(String orderInfo, String url) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
BufferedOutputStream buffOutStr = new BufferedOutputStream(conn.getOutputStream());
buffOutStr.write(orderInfo.getBytes("UTF-8"));
buffOutStr.flush();
buffOutStr.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
xstream.alias("xml", UnifiedPayResposeDTO.class);
UnifiedPayResposeDTO unifiedPayResposeDTO = (UnifiedPayResposeDTO) xstream.fromXML(sb.toString());
return unifiedPayResposeDTO;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Map<String,String> xmlStr2Map(String xmlStr){
Map<String,String> map = new HashMap<String,String>(5);
org.dom4j.Document doc;
try {
doc = DocumentHelper.parseText(xmlStr);
org.dom4j.Element root = doc.getRootElement();
List children = root.elements();
if(children != null && children.size() > 0) {
for(int i = 0; i < children.size(); i++) {
org.dom4j.Element child = (org.dom4j.Element)children.get(i);
map.put(child.getName(), child.getTextTrim());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return map;
}
public static String mapToXml(Map<String, String> data) throws Exception {
return null;
}
public String generateSignedXml(final Map<String, String> data, String key, WeChatConstant.SignType signType) throws Exception {
String sign = generateSignature(data, key, signType);
data.put(WeChatConstant.FIELD_SIGN, sign);
return mapToXml(data);
}
public boolean isSignatureValid(Map<String, String> data, String key) throws Exception {
return isSignatureValid(data, key, WeChatConstant.SignType.MD5);
}
public boolean isSignatureValid(Map<String, String> data, String key, WeChatConstant.SignType signType) throws Exception {
if (!data.containsKey(WeChatConstant.FIELD_SIGN)) {
return false;
}
String sign = data.get(WeChatConstant.FIELD_SIGN);
return generateSignature(data, key, signType).equals(sign);
}
public String generateSignature(final Map<String, String> data, String key) throws Exception {
return generateSignature(data, key, WeChatConstant.SignType.HMACSHA256);
}
public String signature(final Map<String, String> data, String key) throws Exception {
return generateSignature(data, key, WeChatConstant.SignType.HMACSHA256);
}
public String generateSignature(final Map<String, String> data, String key, WeChatConstant.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(WeChatConstant.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 (WeChatConstant.SignType.MD5.equals(signType)) {
log.info("获取签名原串:"+sb.toString());
throw new Exception(String.format("Invalid sign_type: %s", signType));
} else if (WeChatConstant.SignType.HMACSHA256.equals(signType)) {
return hmacsha256(sb.toString(), key);
} else {
log.error("获取签名失败,失败原因:" + String.format("Invalid sign_type: %s", signType));
throw new Exception(String.format("Invalid sign_type: %s", signType));
}
}
public static String hmacsha256(String data, String key) throws Exception {
Mac sha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256.init(secretKey);
byte[] array = sha256.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 String generateNonceStr() {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
}
public static String GetMapToXml(Map<String, String> param) {
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
for (Map.Entry<String, String> entry : param.entrySet()) {
String str = "<" + entry.getKey() + ">";
sb.append(str);
sb.append(entry.getValue());
String str1 = "</" + entry.getKey() + ">";
sb.append(str1);
}
sb.append("</xml>");
return sb.toString();
}
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();
}
public static String generateUuid() {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
}
}
配置文件
#微信配置文件
wechat:
callbackUrl: aaa
appId: bbbb
mchId: cccc
key: ddd
email:
url: ccceee
dm:
gis:
url: bbbb
ak: ssssssss
ds:
sms:
accessId: dddddd
accessToken: ffffffffff
ResponseConstant
package com.cc.model;
public class ResponseConstant {
public static final String MSG = "错误,当前没有可连接的服务";
public static final int ERROR_STATUS = 500;
public static final int SUCCESS_STATUS = 200;
public static final String SUCCESS_MSG = "success";
}
pom
<!--微信支付相关依赖start-->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
<!--微信支付相关依赖end-->
退款证书
![微信退款证书](https://i-blog.csdnimg.cn/blog_migrate/50f37e559735ed33b9ef63a7c1ffbb17.png)