微信退款

导入相关的jar包
  <dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
  </dependency>
   
  <dependency>
    <groupId>me.chanjar</groupId>
    <artifactId>weixin-java-mp</artifactId>
    <version>1.3.3</version>
  </dependency>
创建退款实体
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;
}
退款的详细代码
      /**
  * 微信退款
  * <p>
  * 微信退款详情信息
  *
  * @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<String, String> packageParams = new TreeMap<String, String>();
  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 = "<xml>" + "<appid>wx84254d2616e3c80d</appid>" + "<mch_id>1275811201</mch_id>" + "<nonce_str>"
    + nonce_str + "</nonce_str>" + "<sign>" + sign + "</sign>" + "<out_trade_no>" + order.getOutTradeNo()
    + "</out_trade_no>" + "<out_refund_no>" + out_refund_no + "</out_refund_no>" + "<total_fee>"
    + packageParams.get("total_fee") + "</total_fee>" + "<refund_fee>" + packageParams.get("refund_fee")
    + "</refund_fee>" + "<refund_fee_type>" + packageParams.get("refund_fee_type") + "</refund_fee_type>"
    + "<op_user_id>1275811201</op_user_id>" + "</xml>";
  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;
 }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值