zoukankan      html  css  js  c++  java
  • 微信退款---服务端

    微信退款比较容易按照DEMO进行操作。

      首先去下载微信安全证书,里面有微信安全证书使用说明:

        微信支付API共四份(证书pkcs12格式、证书pem格式、证书密钥pem格式、CA证书),为接口中强制要求时需携带的证书文件。

        退款接口只需要使用 证书pkcs12格式(apiclient_cert.p12)

      第二:查看微信申请退款接口可以看出退款传参同样需要进行签名验证,以xml的形式传送

    代码如下:

     /**
         * 
         * @方法名称:payRefund
         * @内容摘要: <退款>
         * @param transaction_id
         *            微信支付订单号
         * @param out_refund_no
         *            商户退款订单号
         * @param total_fee
         *            总金额
         * @param refund_fee
         *            退款金额
         * @param op_user_id
         *            操作人
         * @return String
         */
        public String wxPayRefundRequest(Order order ,String transaction_id) throws Exception{
            String strResponse = "";
            
            KeyStore keyStore  = KeyStore.getInstance("PKCS12");
             
         // 读取微信安全证书

         String file_s = Thread.currentThread().getContextClassLoader().getResource("").toString();
         FileInputStream instream = new FileInputStream(new File(file_s.substring(5)+"/com/conf/apiclient_cert.p12"));
    
            try {
                keyStore.load(instream, Constant.mchID.toCharArray());
            } finally {
                instream.close();
            }
    
            // Trust own CA and all self-signed certs
            SSLContext sslcontext = SSLContexts.custom()
                    .loadKeyMaterial(keyStore, Constant.mchID.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 {
           //设置访问地址,也可以用GET方式  
           //退款接口地址:refund_url=https://api.mch.weixin.qq.com/secapi/pay/refund HttpPost httpPost
    = new HttpPost(Constant.refund_url); Double amount = elandOrder.getOrderAmount().doubleValue(); String oAmount = new TenPayManager().moneyToFen(amount); // oAmount = "1"; String nonce_str = new TenPayManager().createNonceStr(); String stringA = "appid=" + Constant.appID + "&mch_id=" + Constant.mchID + "&nonce_str=" + nonce_str + "&op_user_id=" +Constant.mchID + "&out_refund_no=" + order.getOrderSn().toString() + "&out_trade_no=" + Order.getOrderSn().toString() + "&refund_fee=" + oAmount + "&total_fee=" + oAmount; String stringSignTemp = stringA + "&key=" + Constant.key; System.out.println(stringA); System.out.println(stringSignTemp); // 签名最后全部转为大写 String sign = MD5Weixin.MD5Encode(stringSignTemp).toUpperCase(); System.out.println(sign); StringBuffer xml = new StringBuffer(); xml.append("<xml>"); xml.append("<appid>").append(Constant.appID).append("</appid>"); xml.append("<mch_id>").append(Constant.mchID).append("</mch_id>"); xml.append("<nonce_str>").append(nonce_str).append("</nonce_str>"); xml.append("<op_user_id>").append(Constant.mchID).append("</op_user_id>"); xml.append("<out_refund_no>").append(order.getOrderSn().toString()).append("</out_refund_no>"); xml.append("<out_trade_no>").append(order.getOrderSn().toString()).append("</out_trade_no>"); xml.append("<refund_fee>").append(oAmount).append("</refund_fee>"); xml.append("<total_fee>").append(oAmount).append("</total_fee>"); xml.append("<transaction_id>").append("").append("</transaction_id>"); xml.append("<sign><![CDATA[").append(sign).append("]]></sign>"); xml.append("</xml>"); StringEntity se = new StringEntity(xml.toString()); httpPost.setEntity(se); logger.info("‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’executing request" + httpPost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity entity = response.getEntity(); logger.info("------------------------------------response.getStatusLine():"+response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); String text = ""; String retXmlStr = ""; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); retXmlStr = retXmlStr + text; logger.info("‘’‘’‘’‘’‘’-----------------text:"+text); } logger.info("‘’‘’‘’‘’‘’-----------------retXmlStr:"+retXmlStr); if (!"".equals(retXmlStr)) {
                  //解析xml为MAP Map
    <String,Object> resultMap = XmlParser.xmlStrParser(retXmlStr); strResponse = resultMap.get("return_code").toString(); logger.info("---weixin Message-code:" + resultMap.get("return_code").toString()); logger.info("---weixin Message:" + resultMap.get("return_msg").toString()); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } return strResponse; }
  • 相关阅读:
    Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()
    @@ROWCOUNT (Transact-SQL)
    C#调用存储过程简单完整例子
    C# Ajax 手机发送短信验证码 校验验证码 菜鸟级别实现方法
    C#反射技术的简单操作(读取和设置类的属性)
    .NET调用Java写的WebService
    蓝牙错误提示
    sql开启xp_cmdshell
    网页手机宽度
    对称加密算法比较
  • 原文地址:https://www.cnblogs.com/doufuquanjia/p/5960287.html
Copyright © 2011-2022 走看看