微信支付(退款为例)

微信支付退款的官方文档

https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4

导入证书

微信退款是需要证书的
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3

以在windows为例,解压之后的文件

微信支付(退款为例)_第1张图片

双击.p12结尾的文件,导入证书,会要求输入密码,密码就是商户ID,注意一定要是在自己的商户平台上下载的证书,不然会提示密码错误。


微信支付(退款为例)_第2张图片

导入成功

微信支付(退款为例)_第3张图片
Java代码

封装了一个RefundVo对象,字段设定根据官方文档

public class RefundVo {

    private String appid;
    private String mchId;
    private String deviceInfo;
    private String nonceStr;
    private String sign;
    private String signType;
    private String transactionId;
    private String outTradeNo;
    private String outRefundNo;
    private int totalFee;
    private int refundFee;
    private String refundFeeType;
    private String opUserId;
    private String refundAccount;
    //省略get set方法
}

设置各个参数

String key = "xxxxxxx";
RefundVo vo = new RefundVo();
vo.setOutTradeNo("2016006092770333");//商户订单号(微信订单号二选一即可)
vo.setAppid("appid");
vo.setMchId("mchid");
vo.setOutRefundNo("2016006092770333");//某付款的定单号
vo.setTotalFee(1);//订单金额
vo.setRefundFee(1);退款金额
vo.setOpUserId("1410417402");//默认商户号
String certificatePath = "E:/工作/cert/apiclient_cert.p12";//证书的绝对路径
refund(key ,vo,certificatePath );

封装退款的结果

public class RefundResult {

    private String returnCode;
    private String returnMsg;
    private String resultCode;
    private String errCode;
    private String errCodeDes;
    private String appid;
    private String mchId;
    private String deviceInfo;
    private String nonceStr;
    private String sign;
    private String transactionId;
    private String outTradeNo;
    private String outRefundNo;
    private String refundId;
    /**
     * ORIGINAL—原路退款
     * BALANCE—退回到余额
     */
    private String refundChannel;
    /**
     * 申请退款金额
     */
    private int refundFee;
    /**
     * 退款金额
     */
    private int settlementRefundFee;
    private int totalFee;
    private int settlementTotalFee;
    private String feeType;
    private int cashFee;
    private int cashRefundFee;
 //省略set get方法
}

退款的方法

public RefundResult refund(String key,RefundVo vo,String certificatePath){
    RefundResult refundResult = new RefundResult();
    vo.setNonceStr(RandomUtil.wechatRandomString());//设置随机字符串
    vo.setSign(new RefundBuilder().build(vo));//设置签名
    check(vo);//检查参数
    //将参数放入Map中
    Map params=new RefundBuilder().getParams(vo);
    //转成Xml形式的String
    String xml=XmlParseUtils.assembleXml(params);
    /**
        
           wx2421b1c4370ec43b
           10000100
           6cefdb308e1e2e8aabd48cf79e546a02
           10000100
           1415701182
           1415757673
           1
           1
           
           FE56DD4AA85C0EECA82C35595A69E153
        
    **/
    //调用微信接口
    String  result = HttpClientUtils.executeBySslPost(refundURL,xml,vo.getCertificatePath(),vo.getRefundVo().getMchId());//发送http请求
    //接收xml解析的结果
    Map map = new HashMap();
    //返回结果为xml形式,转成map然后封装成refundResult即可
    map = XmlParseUtils.parseXml(result);
    refundResult = new RefundResultBuilder().build(map);
}

参数检查

private void check(RefundVo vo){
       
        if (VerifyUtils.isEmpty(vo.getAppid())) {
             throw new PayException("申请退款参数为空——appid");
        }
        if (VerifyUtils.isEmpty(vo.getMchId())) {
            throw new PayException("申请退款参数为空——mch_id");
        }
        if (VerifyUtils.isEmpty(vo.getNonceStr())) {
            throw new PayException("申请退款参数为空——nonce_str");
        }
        if (VerifyUtils.isEmpty(vo.getSign())) {
            throw new PayException("申请退款参数为空——sign");
        }
        if (VerifyUtils.isEmpty(vo.getTransactionId()) && VerifyUtils.isEmpty(vo.getOutTradeNo())) {
            throw new PayException("申请退款参数为空——transaction_id或者out_trade_no");
        }
        if (VerifyUtils.isEmpty(vo.getOutRefundNo())) {
            throw new PayException("申请退款参数为空——out_refund_no");
        }
        if (VerifyUtils.isEmpty(vo.getTotalFee())) {
            throw new PayException("申请退款参数为空——total_fee");
        }
        if (VerifyUtils.isEmpty(vo.getRefundFee())) {
            throw new PayException("申请退款参数为空——refund_fee");
        }
        if (VerifyUtils.isEmpty(vo.getOpUserId())) {
            throw new PayException("申请退款参数为空——op_user_id");
        }
    }

Map构建

    public class RefundBuilder extends SignBuilder {

    @Override
    public Map getParams(Refund vo) {
        
        Map params = new HashMap();
        if(VerifyUtils.isNotEmpty(vo.getAppid())){
            params.put("appid",vo.getAppid());
        }
        if (VerifyUtils.isNotEmpty(vo.getMchId())) {
            params.put("mch_id", vo.getMchId());
        }
        if (VerifyUtils.isNotEmpty(vo.getDeviceInfo())) {
            params.put("device_info", vo.getDeviceInfo());
        }
        if(VerifyUtils.isNotEmpty(vo.getNonceStr())){
            params.put("nonce_str",vo.getNonceStr());
        }
        if (VerifyUtils.isNotEmpty(vo.getSign())) {
            params.put("sign", vo.getSign());
        }
        if (VerifyUtils.isNotEmpty(vo.getSignType())) {
            params.put("sign_type", vo.getSignType());
        }
        if (VerifyUtils.isNotEmpty(vo.getTransactionId())) {
            params.put("transaction_id", vo.getTransactionId());
        }
        if (VerifyUtils.isNotEmpty(vo.getOutTradeNo())) {
            params.put("out_trade_no", vo.getOutTradeNo());
        }
        if (VerifyUtils.isNotEmpty(vo.getOutRefundNo())) {
            params.put("out_refund_no", vo.getOutRefundNo());
        }
        if (VerifyUtils.isNotEmpty(vo.getTotalFee())) {
            params.put("total_fee",Integer.toString(vo.getTotalFee()));
        }
        if (VerifyUtils.isNotEmpty(vo.getRefundFee())) {
            params.put("refund_fee", Integer.toString(vo.getRefundFee()));
        }
        if (VerifyUtils.isNotEmpty(vo.getRefundFeeType())) {
            params.put("refund_fee_type",vo.getRefundFeeType());
        }
        if (VerifyUtils.isNotEmpty(vo.getOpUserId())) {
            params.put("op_user_id",vo.getOpUserId());
        }
        if (VerifyUtils.isNotEmpty(vo.getRefundAccount())) {
            params.put("refund_account",vo.getRefundAccount());
        }
        return params;
    }

}

http执行的方法

public static String executeBySslPost(String url, String body,String certificatePath,String password) throws Exception {
        String result = "";
        //商户id
        //指定读取证书格式为PKCS12
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        //读取本机存放的PKCS12证书文件
        FileInputStream instream = new FileInputStream(new File(certificatePath));
        try {
            //指定PKCS12的密码(商户ID)
            keyStore.load(instream, password.toCharArray());
        } finally {
            instream.close();
        }
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, password.toCharArray()).build();
        //指定TLS版本
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        //设置httpclient的SSLSocketFactory
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        try {
            HttpPost httppost = new HttpPost(url);
            StringEntity reqEntity = new StringEntity(body, "UTF-8");
            httppost.setEntity(reqEntity);

            System.out.println("Executing request: " + httppost.getRequestLine());
            CloseableHttpResponse response = null;
            try {
                response = httpclient.execute(httppost);
                result = EntityUtils.toString(response.getEntity(),"UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("请求失败", e);
                throw new RuntimeException(e);
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("请求失败", e);
            throw new RuntimeException(e);
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

最终的返回结果

System.out.println(JSON.toJSONString(result,true));
微信支付(退款为例)_第4张图片

欢迎大家讨论~我的博客地址 http://blog.doublez.cc

你可能感兴趣的:(微信支付(退款为例))