支付宝退款--服务端

1、

/**
     * 
     * @方法名称:alipayRefundRequest
     * @内容摘要: <支付宝退款请求>
     * @param out_trade_no 订单支付时传入的商户订单号,不能和 trade_no同时为空。
     * @param trade_no 支付宝交易号,和商户订单号不能同时为空
     * @param refund_amount 需要退款的金额,该金额不能大于订单金额,单位为元,支持两位小数
     */
    public String alipayRefundRequest(String out_trade_no,String trade_no,double refund_amount){

        // 发送请求
        String strResponse = null;
        try {
            AlipayClient alipayClient = new DefaultAlipayClient
                    (AlipayConfig.alipay_url,AlipayConfig.app_id,
                            AlipayConfig.private_key,AlipayConfig.content_type,AlipayConfig.input_charset,AlipayConfig.public_key);
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            
            AlipayRefundInfo alidata= new AlipayRefundInfo();
            alidata.setOut_trade_no(out_trade_no);
//            alidata.setRefund_amount(0.01);  //用于测试
            alidata.setRefund_amount(refund_amount);
            alidata.setTrade_no(trade_no);
            
            request.setBizContent(JSONObject.fromObject(alidata).toString());
            logger.info("////////////////////////////////////////////////----BizContent:"+JSONObject.fromObject(alidata).toString());
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            strResponse=response.getCode();
            logger.info("////////////////////////////////////////////////----strResponse:"+strResponse);
            if ("10000".equals(response.getCode())) {
                strResponse="退款成功";
            }else {
                strResponse=response.getSubMsg();
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("///////////////////////////----exception:"+e);
//            Logger.getLogger(getClass()).error("alipayRefundRequest", e);
        }
        return strResponse;

    }

2、AlipayConfig.java

package com.eland.util;

/* *
 *类名:AlipayConfig
 *功能:基础配置类
 *详细:设置帐户有关信息及返回路径
 *版本:3.3
 *日期:2012-08-10
 *说明:
 *以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
 *该代码仅供学习和研究支付宝接口使用,只是提供一个参考。

 *提示:如何获取安全校验码和合作身份者ID
 *1.用您的签约支付宝账号登录支付宝网站(www.alipay.com)
 *2.点击“商家服务”(https://b.alipay.com/order/myOrder.htm)
 *3.点击“查询合作者身份(PID)”、“查询安全校验码(Key)”

 *安全校验码查看时,输入支付密码后,页面呈灰色的现象,怎么办?
 *解决方法:
 *1、检查浏览器配置,不让浏览器做弹框屏蔽设置
 *2、更换浏览器或电脑,重新登录查询。
 */

public class AlipayConfig {

    // 合作身份者ID,以2088开头由16位纯数字组成的字符串
    public static String partner = "2088xxxxxxxxxxxx";
    // 商户的私钥
    public static String private_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    // 支付宝的公钥(固定)--取消
    public static String public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";


    //支付宝的公钥(支付)
    public static String ali_public_key =xxx";
                                           
    //支付宝网关
    public static String alipay_url = "https://openapi.alipay.com/gateway.do";
    // 支付宝分配给开发者的应用ID
    public static String app_id = "2016xxxxxxxxxxx";
    
    public static String content_type = "JSON";
    
    
    // 调试用,创建TXT日志文件夹路径
    public static String log_path = "D:\\";
    // 字符编码格式 目前支持 gbk 或 utf-8
    public static String input_charset = "utf-8";
    // 签名方式 不需修改
    public static String sign_type = "RSA";

}

 

posted @ 2017-08-10 10:13  豆腐全家  阅读(340)  评论(0编辑  收藏  举报