//支付交易訂單號,amount 退款金額,orderAmount 訂單金額,refundSn 退貨單號
public Map<String, Object> refunds(String orderSn, BigDecimal amount,BigDecimal orderAmount,String refundSn) throws Exception {
Map<String, Object> parameterMap = new TreeMap<>();
parameterMap.put("appid", getAppId());//公衆賬號ID
parameterMap.put("mch_id", getMchId());//商戶號
parameterMap.put("nonce_str", DigestUtils.md5Hex(UUID.randomUUID() + RandomStringUtils.randomAlphabetic(30)));//隨機字符串
parameterMap.put("out_refund_no",refundSn);//退單號
parameterMap.put("out_trade_no", orderSn);//支付單號
parameterMap.put("total_fee", orderAmount.intValue());//訂單金額
parameterMap.put("refund_fee", amount.intValue());//退款金額
parameterMap.put("sign", generateSign(parameterMap));
//String result = WebUtils.post(service_url, XmlUtils.toXml(parameterMap));
String result = RefundRequest.ClientCustomSSL(XmlUtils.toXml(parameterMap), service_url, getMchId());
Map<String, String> resultMap = XmlUtils.toObject(result, new TypeReference<Map<String, String>>() {
});
String return_code = resultMap.get("return_code");
String return_msg = resultMap.get("return_msg");
String result_code = resultMap.get("result_code");
Map<String, Object> map = new HashMap<>();
if("SUCCESS".equals(return_code)&&result_code!=null&&"SUCCESS".equals(result_code)){
map.put("return_code", return_code);
map.put("return_msg", return_msg);
map.put("result_code", result_code);
map.put("out_refund_no", resultMap.get("out_refund_no"));
map.put("refund_fee", resultMap.get("refund_fee"));
}else{
map.put("return_code", return_code);
map.put("return_msg", return_msg);
}
return map;
}
XmlUtils.toXml
/**
* 將對象轉換爲XML字符串
*
* @param value
* 對象
* @return XML字符串
*/
public static String toXml(Object value) {
Assert.notNull(value, "[Assertion failed] - value is required; it must not be null");
try {
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(true);
XmlMapper xmlMapper = new XmlMapper(module);
return xmlMapper.writer().withRootName("xml").writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
XmlUtils.toObject
/**
* 將XML字符串轉換爲對象
*
* @param xml
* XML字符串
* @param valueType
* 類型
* @return 對象
*/
public static <T> T toObject(String xml, Class<T> valueType) {
Assert.hasText(xml, "[Assertion failed] - xml must have text; it must not be null, empty, or blank");
Assert.notNull(valueType, "[Assertion failed] - valueType is required; it must not be null");
try {
return XML_MAPPER.readValue(xml, valueType);
} catch (JsonParseException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 生成簽名
*
* @param parameterMap
* 參數
* @return 簽名
*/
private String generateSign(Map<String, ?> parameterMap) {
return StringUtils.upperCase(DigestUtils.md5Hex(joinKeyValue(new TreeMap<>(parameterMap), null, "&key=" + getApiKey(), "&", true)));
}
getApiKey() 是獲取商戶賬號裏設置的密碼
/**
* 連接Map鍵值對
*
* @param map
* Map
* @param prefix
* 前綴
* @param suffix
* 後綴
* @param separator
* 連接符
* @param ignoreEmptyValue
* 忽略空值
* @param ignoreKeys
* 忽略Key
* @return 字符串
*/
protected String joinKeyValue(Map<String, Object> map, String prefix, String suffix, String separator, boolean ignoreEmptyValue, String... ignoreKeys) {
List<String> list = new ArrayList<>();
if (map != null) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
String value = ConvertUtils.convert(entry.getValue());
if (StringUtils.isNotEmpty(key) && !ArrayUtils.contains(ignoreKeys, key) && (!ignoreEmptyValue || StringUtils.isNotEmpty(value))) {
list.add(key + "=" + (value != null ? value : StringUtils.EMPTY));
}
}
}
return (prefix != null ? prefix : StringUtils.EMPTY) + StringUtils.join(list, separator) + (suffix != null ? suffix : StringUtils.EMPTY);
}
RefundRequest.ClientCustomSSL
public static String ClientCustomSSL(String xmlStr,String path,String mchid) throws Exception{
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(SystemUtils.getWeiXinFile());
try {
keyStore.load(instream, mchid.toCharArray());
} finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, mchid.toCharArray())
.build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
HttpPost httpPost = new HttpPost(path);
StringEntity entityStr = new StringEntity(xmlStr);
entityStr.setContentType("text/xml");
System.out.println("entityStr--------------"+entityStr);
httpPost.setEntity(entityStr);
CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity httpEntity = null;
try {
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
return EntityUtils.toString(httpEntity, "UTF-8");
}
EntityUtils.consume(httpEntity);
} finally {
EntityUtils.consume(httpEntity);
IOUtils.closeQuietly(httpResponse);
httpResponse.close();
}
} finally {
httpclient.close();
}
return null;
}