微信退款

导入相关的jar包
  
    taglibs
    standard
    1.1.2
  
   
  
    me.chanjar
    weixin-java-mp
    1.3.3
  
创建退款实体
public class WxReturnFeeEntity implements Serializable {
 private String return_code;
 private String return_msg;
 private String appid;
 private String mch_id;
 private String nonce_str;
 private String sign;
 private String result_code;
 private String out_refund_no;
 private String err_code;
}
退款的详细代码
      /**
  * 微信退款
  * 

* 微信退款详情信息 * * @param order 订单 * @return WxReturnFeeEntity 退款实体 */ public WxReturnFeeEntity refundFee(OrderEntity order) { String nonce_str = System.currentTimeMillis() + ""; /** 当前时间 yyyyMMddHHmmss */ String currTime = getCurrTime(); /** 8位日期 */ String strTime = currTime.substring(8, currTime.length()); /** 四位随机数 */ String strRandom = buildRandom(4) + ""; /** 订单号 */ String out_refund_no = strTime + strRandom; SortedMap packageParams = new TreeMap(); packageParams.put("appid", "wx84254d2616e3c80d"); packageParams.put("mch_id", "1275811201"); packageParams.put("nonce_str", nonce_str); packageParams.put("out_trade_no", order.getOutTradeNo()); packageParams.put("out_refund_no", out_refund_no); packageParams.put("total_fee", (int) (order.getPayMoney() * hundred) + ""); packageParams.put("refund_fee", (int) (order.getPayMoney() * hundred) + ""); packageParams.put("refund_fee_type", "CNY"); packageParams.put("op_user_id", "1275811201"); String sign = WxCryptUtil.createSign(packageParams, "kuaileguojiang20151212kingberry1"); String xml = "" + "wx84254d2616e3c80d" + "1275811201" + "" + nonce_str + "" + "" + sign + "" + "" + order.getOutTradeNo() + "" + "" + out_refund_no + "" + "" + packageParams.get("total_fee") + "" + "" + packageParams.get("refund_fee") + "" + "" + packageParams.get("refund_fee_type") + "" + "1275811201" + ""; HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund"); try { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(kvConfigs.getValues().get("static") + "/apiclient_cert.p12")); try { keyStore.load(instream, "1275811201".toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1275811201".toCharArray()).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); StringEntity entity = new StringEntity(xml, Consts.UTF_8); httpPost.setEntity(entity); CloseableHttpResponse response = httpclient.execute(httpPost); String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); XStream xstream = XStreamInitializer.getInstance(); xstream.alias("xml", WxReturnFeeEntity.class); WxReturnFeeEntity wxReturnFeeEntity = (WxReturnFeeEntity) xstream.fromXML(responseContent); return wxReturnFeeEntity; } catch (Exception e) { e.printStackTrace(); } return null; }

你可能感兴趣的:(Java技术)