微信支付(退款為例)


導入證書

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

以在windows為例,解壓之后的文件

雙擊.p12結尾的文件,導入證書,會要求輸入密碼,密碼就是商戶ID,注意一定要是在自己的商戶平台上下載的證書,不然會提示密碼錯誤。

 

導入成功

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<String,String> params=new RefundBuilder().getParams(vo);
    //轉成Xml形式的String
    String xml=XmlParseUtils.assembleXml(params);
    /**
        <xml>
           <appid>wx2421b1c4370ec43b</appid>
           <mch_id>10000100</mch_id>
           <nonce_str>6cefdb308e1e2e8aabd48cf79e546a02</nonce_str>
           <op_user_id>10000100</op_user_id>
           <out_refund_no>1415701182</out_refund_no>
           <out_trade_no>1415757673</out_trade_no>
           <refund_fee>1</refund_fee>
           <total_fee>1</total_fee>
           <transaction_id></transaction_id>
           <sign>FE56DD4AA85C0EECA82C35595A69E153</sign>
        </xml>
    **/
    //調用微信接口
    String  result = HttpClientUtils.executeBySslPost(refundURL,xml,vo.getCertificatePath(),vo.getRefundVo().getMchId());//發送http請求
    //接收xml解析的結果
    Map<String, String> map = new HashMap<String,String>();
    //返回結果為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");
        }
    }
View Code

 

Map構建

public class RefundBuilder extends SignBuilder {

    @Override
    public Map<String, String> getParams(Refund vo) {
        
        Map<String,String> params = new HashMap<String, String>();
        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;
    }
View Code

 

最終的返回結果

System.out.println(JSON.toJSONString(result,true));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM