JAVA实现微信小程序支付退款功能
- 本如亲测有效(代码复制直接可以用的),退款的前提是必须有小程序的appid、商户号、商户密匙、和证书、
- 这个是微信小程序退款的官网大家可以去看看:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_9.shtml
- 这个是微信官网里面的签名信息和API证书如何申请可以去看看:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_0.shtml
- 吧上面4个东西全部弄到保存好,并吧证书放在项目的resources层下,一般情况下证书名称就叫:apiclient_cert.p12 .
- 全部填写好了之后就可以将下面代码复制到你的项目里面就可直接运行,返回类型根据你的项目需求进行修改就行了。
- 注意:这里退款金额和订单金额都是以分为单位的
// 有需要的话可以取这段代码进行金额转换,将金额转换成分
BigDecimal payMoney = new BigDecimal(BigDecimal.valueOf(Double.parseDouble(BigDecimalUtil.mulBig(money, new BigDecimal(100)).toString())).stripTrailingZeros().toPlainString());
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.security.KeyStore;
import java.util.*;
public class PayUtil {
private static String APPID = ""; //小程序的appid
private static String MCH_ID = ""; //商户号
private static String KEY = ""; //商户秘钥
private byte [] certData;
private String getRandomStringByLength(int length) {
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
private static String mapToXml(Map<String, String> param) {
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
for (Map.Entry<String, String> entry : param.entrySet()) {
sb.append("<" + entry.getKey() + ">");
sb.append(entry.getValue());
sb.append("</" + entry.getKey() + ">");
}
sb.append("</xml>");
return sb.toString();
}
private static Map xmlToMap(String strxml) throws Exception {
Map<String, String> map = new HashMap<>();
if (null == strxml || "".equals(strxml)) {
return null;
}
InputStream in = String2Inputstream(strxml);
SAXReader read = new SAXReader();
Document doc = read.read(in);
//得到xml根元素
Element root = doc.getRootElement();
//遍历 得到根元素的所有子节点
@SuppressWarnings("unchecked")
List<Element> list = root.elements();
for (Element element : list) {
//装进map
map.put(element.getName(), element.getText());
}
//关闭流
in.close();
return map;
}
private static InputStream String2Inputstream(String strxml) throws IOException {
return new ByteArrayInputStream(strxml.getBytes("UTF-8"));
}
/**
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
*
* @param params 需要排序并参与字符拼接的参数组
* @return 拼接后字符串
*/
private static String createLinkString(Map<String, String> params) {
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
String preStr = "";
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = params.get(key);
if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
preStr = preStr + key + "=" + value;
} else {
preStr = preStr + key + "=" + value + "&";
}
}
return preStr;
}
/**
* 签名字符串
*
* @param text 需要签名的字符串
* @param key 密钥
* @param input_charset 编码格式
* @return 签名结果
*/
public static String sign(String text, String key, String input_charset) {
text = text + "&key=" + key;
return DigestUtils.md5Hex(getContentBytes(text, input_charset));
}
private static byte[] getContentBytes(String content, String charset) {
if (charset == null || "".equals(charset)) {
return content.getBytes();
}
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
}
}
/**
* 调用微信退款接口
*/
private String doRefund(String url, String data) throws Exception {
KeyStore keyStore = KeyStore.getInstance("PKCS12"); //证书格式
try {
InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("apiclient_cert.p12");
this.certData = IOUtils.toByteArray(certStream);
certStream.close();
} catch (Exception e) {
e.printStackTrace();
}
ByteArrayInputStream is = new ByteArrayInputStream(this.certData);
try {
keyStore.load(is, PayUtil.MCH_ID.toCharArray());
} finally {
is.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(
keyStore,
PayUtil.MCH_ID.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 httpost = new HttpPost(url); // 设置响应头信息
httpost.addHeader("Connection", "keep-alive");
httpost.addHeader("Accept", "*/*");
httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpost.addHeader("Host", "api.mch.weixin.qq.com");
httpost.addHeader("X-Requested-With", "XMLHttpRequest");
httpost.addHeader("Cache-Control", "max-age=0");
httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
httpost.setEntity(new StringEntity(data, "UTF-8"));
CloseableHttpResponse response = httpclient.execute(httpost);
try {
HttpEntity entity = response.getEntity();
String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(entity);
return jsonStr;
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
/**
* 申请退款
*
* @param orderId 商户订单号
* @param refundId 商户退款单号
* @param totalFee 订单金额
* @param refundFee 退款金额
* @param refundAccount 退款资金来源(默认传 "REFUND_SOURCE_UNSETTLED_FUNDS")
* 注: 退款金额不能大于订单金额
*/
public Map<String, String> refund(String orderId, String refundId, String totalFee,
String refundFee, String refundAccount) {
Map<String, String> params = new HashMap<>();
params.put("appid", PayUtil.APPID);
params.put("mch_id", PayUtil.MCH_ID);
params.put("nonce_str", getRandomStringByLength(32));
params.put("out_trade_no", orderId); //商户订单号和微信订单号二选一(我这里选的是商户订单号)
params.put("out_refund_no", refundId);
params.put("total_fee", totalFee);
params.put("refund_fee", refundFee);
params.put("refund_account", refundAccount);
params.put("sign_type", "MD5");
String preStr = PayUtil.createLinkString(params);
//签名算法
String sign = (sign(preStr, PayUtil.KEY, "utf-8")).toUpperCase();
params.put("sign", sign);
Map<String, String> map = new HashMap<>();
try {
String xml = mapToXml(params);
String xmlStr = doRefund("https://api.mch.weixin.qq.com/secapi/pay/refund", xml);
map = xmlToMap(xmlStr);
// 这里是将返回类型转换成了 map 如果要判断是否退款成功可以获取 map里面的 result_code 值,如果这个值等于 SUCCESS就代表退款成功,这里就可以写退款之后的逻辑,比如修改订单状态之类的,
if ("SUCCESS".equals(status)){
// 退款成功
// 这里就写逻辑,当然,如果不需要写逻辑的话,可以直接吧if else 删掉也是可以直接跑起来的
}else {
// 退款失败
return Result.fail("退款失败");
}
} catch (Exception e) {
}
return map;
}
}