微信小程序支付->退款
微信小程序退款的时候如果是线上,就会涉及到Linux读取打包后项目存放文件路径失败问题,获取不到其中的微信退款证书,在这里就需要使用流的方式进行读取路径,经大佬指点才最终上线可以退款成功读取证书。
本地测试如何读取微信支付退款证书以及安装就不解释了,大家去百度就可以看到一大堆,这里只讲解如何在项目上线后使用流读取退款证书。
1.PayService
/**
* 微信支付退款
* @param response
* @param wechatReturnRequestParam
* @return
*/
Map<String, Object> refund(HttpServletResponse response, WechatReturnRequestParam wechatReturnRequestParam);
2.WechatReturnRequestParam实体类 ->自行需求更换
public class WechatReturnRequestParam {
private String appId;//公众账号ID
private String mchId;//商户号
private String nonceStr;//随机字符串
private String sign;//签名
private String signType = "MD5";//签名类型
private String transactionId;//微信支付返回的订单号
private String outTradeNo;//商户订单号
private String outRefundNo;//退款单号
private String totalFee;//订单金额
private String refundFee;//退款金额
private String refundFeeType = "CNY";//货币类型
private String refundDesc;//退款原因
private String notifyUrl;//通知地址
private String mchSecret;
}
3.PayServiceImpl
@Override
public synchronized Map<String, Object> refund(HttpServletResponse response, WechatReturnRequestParam we) {
//返回参数
Map<String, Object> resMap = new HashMap<>();
String resXml = "";
try {
//拼接统一下单地址参数
Map<String, String> paraMap = new HashMap<>();
//商户订单号 outTradeNo
String outTradeNo = we.getOutTradeNo();
//商户退款单号 transaction_id
String outRefundNo = we.getOutRefundNo();
//同上,也是填写商户退款单号 transaction_id
String transactionId = we.getTransactionId();
//订单金额 这两笔金额一定要一致,都是支付的金额
String totalFee = we.getTotalFee().toString();
//退款金额 CommUtils此工具类中方法可进行元->分的转换
String refundFee = we.getRefundFee().toString();
System.out.println("订单号="+outTradeNo);
paraMap.put("appid",WechatConfig.Appid);
paraMap.put("mch_id",WechatConfig.mch_id);
//生成的随机字符串
paraMap.put("nonce_str",WXPayUtil.generateNonceStr());
paraMap.put("out_trade_no",outTradeNo);//商户订单号id
paraMap.put("out_refund_no",outRefundNo);//商户退款单号
paraMap.put("total_fee",totalFee);
paraMap.put("refund_fee",refundFee);
String sign = WXPayUtil.generateSignature(paraMap, WechatConfig.key);
//生成签名
paraMap.put("sign",sign);
//将所有参数map转化成xml
String xml = WXPayUtil.mapToXml(paraMap);
System.out.println("xml"+xml);
//退款路径《https://api.mch.weixin.qq.com/secapi/pay/refund》
String refund_url = WechatConfig.refund_url;
System.out.println("refund_url" + refund_url);
System.out.println("------------------------------分------------------------------割------------------------------线------------------------------");
//发送post请求申请退款
String xmlStr = HttpClientUtil.doRefund(refund_url, xml);
System.out.println("退款xmlStr=========="+xmlStr);
System.out.println("------------------------------分------------------------------割------------------------------线------------------------------");
/* 退款成功回调修改订单状态 */
if (xmlStr.indexOf("SUCCESS") != -1){
Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
if (map.get("return_code").equals("SUCCESS")){
resMap.put("success",true);//此步说明退款成功
resMap.put("data","退款成功");
System.out.println("退款成功");
System.out.println("------------------------------分------------------------------割------------------------------线------------------------------");
try {
//在这里执行自己呃业务逻辑,在退款之后要对自己订单进行一个状态的修改(已退款)
//业务逻辑没有什么
//
//
//通知微信服务器不要再进行回调action
resXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(resXml.getBytes());
out.flush();
out.close();
System.out.println("返回给微信的值:"+resXml.getBytes());
} catch (IOException e) {
resMap.put("fail","订单状态修改失败");
}
}
}else {
resMap.put("success","fail");//此步说明退款成功
}
} catch (Exception e) {
e.printStackTrace();
}
return resMap;
}
4.CommUtils
在进行退款时候穿的金额参数需要进行一次以分为单位的转换
/**
* 元转换成分
* @param amount
* @return
*/
public static String getMoney(String amount) {
if(amount==null){
return "";
}
// 金额转化为分为单位
// 处理包含, ¥ 或者$的金额
String currency = amount.replaceAll("\\$|\\¥|\\,", "");
int index = currency.indexOf(".");
int length = currency.length();
Long amLong = 0l;
if(index == -1){
amLong = Long.valueOf(currency+"00");
}else if(length - index >= 3){
amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));
}else if(length - index == 2){
amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
}else{
amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
}
return amLong.toString();
}
- WXPayUtil (这里补全一下代码,之前的工具类中方法可能有所缺失)
生成的随机字符串用于拼接统一下单地址并生成签名返回给微信通知,而后将所有参数map转化成xml
public class WXPayUtil {
private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final Random RANDOM = new SecureRandom();
/**
* 获取随机字符串 (采用截取8位当前日期数 + 4位随机整数)
* @return
*/
public static String getNonceStr() {
//获得当前日期
Date now = new Date();
SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String currTime = outFormat.format(now);
//截取8位
String strTime = currTime.substring(8, currTime.length());
//得到4位随机整数
int num = 1;
double random = Math.random();
if (random < 0.1) {
random = random + 0.1;
}
for (int i = 0; i < 4; i++) {
num = num * 10;
}
num = (int)random * num;
return strTime + num;
}
/**
* 解析xml得到 prepay_id 预支付id
* @param result
* @return
* @throws DocumentException
*/
public static String getPayNo(String result) throws DocumentException {
Map<String, String> map = new HashMap<String, String>();
InputStream in = new ByteArrayInputStream(result.getBytes());
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());
}
//返回码
String return_code = map.get("return_code");
//返回信息
String result_code = map.get("result_code");
//预支付id
String prepay_id = "";
//return_code 和result_code 都为SUCCESS 的时候返回 预支付id
if(return_code.equals("SUCCESS")&&result_code.equals("SUCCESS")){
prepay_id = map.get("prepay_id");
}
return prepay_id;
}
/**
*
// * @param requestUrl请求地址
// * @param requestMethod请求方法
// * @param outputStr参数
*/
public static String httpRequest(String requestUrl,String requestMethod,String outputStr){
// 创建SSLContext
StringBuffer buffer = null;
try{
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(requestMethod);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
//往服务器端写内容
if(null !=outputStr){
OutputStream os=conn.getOutputStream();
os.write(outputStr.getBytes("utf-8"));
os.close();
}
// 读取服务器端返回的内容
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
buffer = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}
public static String urlEncodeUTF8(String source){
String result=source;
try {
result=java.net.URLEncoder.encode(source, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 签名字符串
* @param key 密钥
* @return 签名结果
*/
public static String sign(String text, String key, String input_charset) {
text = text + "&key=" + key;
return DigestUtils.md5Hex(getContentBytes(text, input_charset));
}
/**
* 除去数组中的空值和签名参数
* @param sArray 签名参数组
* @return 去掉空值与签名参数后的新签名参数组
*/
public static Map<String, String> paraFilter(Map<String, String> sArray) {
Map<String, String> result = new HashMap<String, String>();
if (sArray == null || sArray.size() <= 0) {
return result;
}
for (String key : sArray.keySet()) {
String value = sArray.get(key);
if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
|| key.equalsIgnoreCase("sign_type")) {
continue;
}
result.put(key, value);
}
return result;
}
/**
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
* @param params 需要排序并参与字符拼接的参数组
* @return 拼接后字符串
*/
public static String createLinkString(Map<String, String> params) {
List<String> keys = new ArrayList<String>(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;
}
/**
* XML格式字符串转换为Map
*
* @param strXML XML字符串
* @return XML数据转换后的Map
* @throws Exception
*/
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) {
// do nothing
}
return data;
} catch (Exception ex) {
WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
throw ex;
}
}
/**
* 将Map转换为XML格式的字符串
*
* @param data Map类型数据
* @return XML格式的字符串
* @throws Exception
*/
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(); //.replaceAll("\n|\r", "");
try {
writer.close();
}
catch (Exception ex) {
}
return output;
}
/**
* 生成签名
*
* @param data 待签名数据
* @param key API密钥
* @return 签名
*/
public static String generateSignature(final Map<String, String> data, String key) throws Exception {
return generateSignature(data, key, SignType.MD5);
}
/**
* 生成签名. 注意,若含有sign_type字段,必须和signType参数保持一致。
*
* @param data 待签名数据
* @param key API密钥
* @param signType 签名方式
* @return 签名
*/
public static String generateSignature(final Map<String, String> data, String key, 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 (SignType.MD5.equals(signType)) {
return MD5(sb.toString()).toUpperCase();
}
else if (SignType.HMACSHA256.equals(signType)) {
return HMACSHA256(sb.toString(), key);
}
else {
throw new Exception(String.format("Invalid sign_type: %s", signType));
}
}
/**
* 获取随机字符串 Nonce Str
*
* @return String 随机字符串
*/
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);
}
/**
* 生成 MD5
*
* @param data 待处理数据
* @return MD5结果
*/
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();
}
/**
* 生成 HMACSHA256
* @param data 待处理数据
* @param key 密钥
* @return 加密结果
* @throws Exception
*/
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();
}
/**
* 日志
* @return
*/
public static Logger getLogger() {
Logger logger = LoggerFactory.getLogger("wxpay java sdk");
return logger;
}
}
6.退款路径
此退款路径在前两篇支付与回调中都提及过此配置类,关于微信配置都在WechatConfig中,如有需要可看前两篇文章。
/*退款路径*/
public static final String refund_url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
7.此时发送post请求申请退款就需要读取《微信商户证书->退款证书》
在这里我只写对于上线Linux的打包项目读取当中static文件证书方式
(1)需要去pay.weixin.com 下载证书
(2)解压 apiclient_cert.p12
将这两个文件放到项目中resources中,static名字随意取什么,要的是代码中一定要路径一致。准备工作做好之后就开始读取文件的方式。
注意:想要读取此路径,需要使用流的写法。或许有其他方法,但是我能力有限,确实不太清楚。
/**
* https双向签名认证,用于支付申请退款
*/
//证书的密码 -> 自己的商户号
public static String SSLCERT_PASSWORD = "***商户号";
@SuppressWarnings("deprecation")
public static String doRefund(String url, String data) throws Exception {
byte[] certData = null;
try {
InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/apiclient_cert.p12");
System.out.println(certStream + "有没有");
certData = IOUtils.toByteArray(certStream);
certStream.close();
} catch (Exception e) {
e.printStackTrace();
}
ByteArrayInputStream certBis = new ByteArrayInputStream(certData);
System.out.println(certBis + "有没有");
//注意PKCS12证书 是从微信商户平台-》账户设置-》 API安全 中下载的
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(certBis, SSLCERT_PASSWORD.toCharArray());
//下载证书时的密码、默认密码是你的MCHID mch_id
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, SSLCERT_PASSWORD.toCharArray())//这里也是写密码的
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
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();
}
}
到此微信支付的退款就全部写完,大家共同进步,关注一下吧。