• 微信退款和支付宝退款接口调用(java版)


    项目中需要使用到微信和支付宝的退款功能,在这两天研究了一下这两个平台的退款,有很多坑,在开发中需要留意
    1、微信退款接口
    相对来说我感觉微信的退款接口还是比较好调用的,直接发送httppost请求即可;

    /**
         * 
         * @方法名称:payRefund
         * @内容摘要: <退款>
         * @param transaction_id
         *            微信支付订单号
         * @param out_refund_no
         *            商户订单号
         * @param total_fee
         *            总金额
         * @param refund_fee
         *            退款金额
         * @param op_user_id
         *            操作人
         * @return String
         * @exception
         * @author:鹿伟伟
         * @创建日期:2016年4月11日-上午11:07:04
         */
        public String wxPayRefundRequest(String transaction_id, String out_refund_no,
                int total_fee, int refund_fee, String op_user_id) {
            CloseableHttpClient httpclient = null;
            CloseableHttpResponse response = null;
            String strResponse = null;
            try {
                httpclient = ClientCustomSSL.getCloseableHttpClient();
                // 构造HTTP请求
                HttpPost httpPost = new HttpPost(Configure.PAY_REFUND_API);
                // PayRefundReqData wxdata = new PayRefundReqData(
                // "1004720096201602263541023415", "16371", 30, 30, "19417");
                PayRefundReqData wxdata = new PayRefundReqData(transaction_id,
                        out_refund_no, total_fee, refund_fee, op_user_id);
                String requestStr = Util.ConvertObj2Xml(wxdata);
                StringEntity se = new StringEntity(requestStr.toString());
                httpPost.setEntity(se);
                // 发送请求
                response = httpclient.execute(httpPost);
    
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    SAXReader saxReader = new SAXReader();
                    Document document = saxReader.read(entity.getContent());
                    Element rootElt = document.getRootElement();
                    // 结果码
                    String returnCode = rootElt.elementText("return_code");
                    String resultCode = rootElt.elementText("result_code");
                    if ("SUCCESS".equals(returnCode)&&"SUCCESS".equals(resultCode)) {
                        strResponse=returnCode;
                    }else {
                        strResponse=rootElt.elementText("err_code_des");
                    }
                }
                 EntityUtils.consume(entity); 
            } catch (Exception e) {
                Logger.getLogger(getClass()).error("payRefundRequest", e);
            } finally {
                try {
                    response.close();
                    httpclient.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Logger.getLogger(getClass()).error("payRefundRequest关闭异常:", e);
                }
            }
            return strResponse;
        }

    报错的话请检查加密的sign是否正确,还有就是调用的接口地址是否正确,有问题找我
    2、支付宝退款接口
    支付宝直接导入支付宝封装好的jar包直接调用即可,官网下载地址:https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1
    在弄支付宝退款的时候有一个插曲:就是支付宝调用时是客户端直接调用的,当时没有通过后台,后台只做了一个回调地址的使用,本来想着按照微信的思路写一个支付出来的,没想到怎么也调试不通,直接通过网址可以访问,在方法里面不行,后来只好使用支付宝的jar了。
    调用方法:

    /**
         * 
         * @方法名称:alipayRefundRequest
         * @内容摘要: <支付宝退款请求>
         * @param out_trade_no 订单支付时传入的商户订单号,不能和 trade_no同时为空。
         * @param trade_no 支付宝交易号,和商户订单号不能同时为空
         * @param refund_amount 需要退款的金额,该金额不能大于订单金额,单位为元,支持两位小数
         * @return 
         * String
         * @exception 
         * @author:鹿伟伟
         * @创建日期:2016年4月12日-下午4:53:30
         */
        public String alipayRefundRequest(String out_trade_no,String trade_no,double refund_amount){
    
            // 发送请求
            String strResponse = null;
            try {
                AlipayClient alipayClient = new DefaultAlipayClient
                        (AlipayConfig.alipayurl,AlipayConfig.appid,
                                AlipayConfig.private_key,AlipayConfig.content_type,AlipayConfig.input_charset,AlipayConfig.ali_public_key);
                AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
                AlipayRefundInfo alidata= new AlipayRefundInfo();
                alidata.setOut_trade_no(out_trade_no);
                alidata.setRefund_amount(refund_amount);
                alidata.setTrade_no(trade_no);
                request.setBizContent(JsonUtils.convertToString(alidata));
                AlipayTradeRefundResponse response = alipayClient.execute(request);
                strResponse=response.getCode();
                if ("10000".equals(response.getCode())) {
                    strResponse="退款成功";
                }else {
                    strResponse=response.getSubMsg();
                }
            } catch (Exception e) {
                Logger.getLogger(getClass()).error("alipayRefundRequest", e);
            }
            return strResponse;
    
        }

    不管怎么滴吧,搞定了,还有一个疑问就是官网上文档里面也没有具体说返回的状态码code的含义,网上找了一遍也没有找到,哪位大神知道的话发我一份。小弟在此谢过了。
    微信下单接口
    支付宝错误码:
    https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.Z87Tfg&treeId=58&articleId=103599&docType=1

  • 相关阅读:
    普通图片转ascii码字符图
    基于RAF的一个小动画框
    HTML5给我们带来了什么
    记坑
    以后的博客可能会偏向游戏开发方向
    总结:js中4类修改样式的方法
    小记:css特殊性
    css小记
    探究绑定事件的this指向以及event传参的小问题
    手把手教你图片转ASCII码图
  • 原文地址:https://www.cnblogs.com/luweiwei/p/5968500.html
Copyright © 2020-2023  润新知