微信支付退款

1 微信支付退款接口 https://api.mch.weixin.qq.com/secapi/pay/refund

2 退款接口需要双向证书,商户平台可下载

3 拼接xml

public static String getRefundXml(String orderNo, String total_amount) throws Exception {
Integer total=(int) (Double.valueOf(total_amount)*100);
String nonce_str = getNonceStr();
String out_refund_no=(orderNo+0);
String signSoure = "appid=" + appid 
+ "&mch_id=" + mch_id 
+ "&nonce_str=" + nonce_str
+ "&out_refund_no="+out_refund_no
+ "&out_trade_no=" + orderNo 
+ "&refund_fee=" + total
+ "&total_fee=" + total;
String stringSignTemp = signSoure + "&key=" + key;
String sign = MD5Util.MD5Encode(stringSignTemp,"utf-8").toUpperCase();
String xml="<xml><appid>"+appid+"</appid>"+
   "<mch_id>"+mch_id+"</mch_id>"+
   "<nonce_str>"+nonce_str+"</nonce_str>"+ 
   "<out_refund_no>"+out_refund_no+"</out_refund_no>"+
   "<out_trade_no>"+orderNo +"</out_trade_no>"+
   "<refund_fee>"+total+"</refund_fee>"+
   "<total_fee>"+total+"</total_fee>"+
   "<transaction_id></transaction_id>"+
   "<sign>"+sign+"</sign></xml>";
return xml;

}

4 发送请求

package com.common.https;


import java.io.FileInputStream;
import java.security.KeyStore;


import javax.net.ssl.SSLContext;


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;


public class ClientCustomSSL {
public static String doRefund(String url,String data,String keystoreFile,String pass) throws Exception {
    /**
    * 注意PKCS12证书 是从微信商户平台-》账户设置-》 API安全 中下载的
    */
   
        KeyStore keyStore  = KeyStore.getInstance("PKCS12");
        FileInputStream instream = new FileInputStream(keystoreFile);//P12文件目录
        try {
        /**
        * 此处要改
        * */
            keyStore.load(instream, pass.toCharArray());//这里写密码..默认是你的MCHID
        } finally {
            instream.close();
        }
 
        // Trust own CA and all self-signed certs
        /**
    * 此处要改
    * */
        SSLContext sslcontext = SSLContexts.custom()
                .loadKeyMaterial(keyStore, pass.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();
        }
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值