package com.framework.loippi.plugins.aliapppay.reFund;

import com.alipay.api.response.AlipayTradeRefundResponse;
import com.framework.loippi.cache.ConfigCache;
import com.framework.loippi.plugins.aliapppay.reFund.entity.AliPayRefund;
import com.framework.loippi.plugins.aliapppay.reFund.entity.AlipaySubmit;

public class AlipayRefundUtil {


    /**
     * 支付宝退款
     *
     * @param aliPayRefund
     * @return
     */
    public static AlipayTradeRefundResponse toRefund(AliPayRefund aliPayRefund) {
        //建立请求
        AlipayTradeRefundResponse sHtmlText = AlipaySubmit.buildRequest(aliPayRefund.getTradeNo(), aliPayRefund.getRefundAmount(),
                aliPayRefund.getRRefundReason(), ConfigCache.getConfig("alipay.user.appid"), ConfigCache.getConfig("alipay.user.privateKey"), ConfigCache.getConfig("alipay.user.alipayPublicKey"));
        return sHtmlText;
    }

}

-------------------------------------------------------------------
package com.framework.loippi.plugins.aliapppay.reFund.entity;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;

/* *
 *类名:AlipaySubmit
 *功能:支付宝各接口请求提交类
 *详细:构造支付宝各接口表单HTML文本,获取远程HTTP数据
 *版本:3.3
 *日期:2012-08-13
 *说明:
 *以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
 *该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
 */

public class AlipaySubmit {

    /**
     * 支付宝提供给商户的服务接入网关URL(新)
     */
    private static final String ALIPAY_GATEWAY_NEW = "https://mapi.alipay.com/gateway.do?";

    /**
     * 建立请求,以表单HTML形式构造(默认)
     *
     * @return 提交表单HTML文本
     */
    public static AlipayTradeRefundResponse buildRequest(String outTradeNo, BigDecimal amount, String reason, String appId, String privateRsa, String publicRsa) {
        AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_GATEWAY_NEW, appId, privateRsa, "json", "utf-8", publicRsa, "RSA2");
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizContent("{" +
                "\"out_trade_no\":\"" + outTradeNo + "\"," +
                "\"out_request_no\":\"" +"TK"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "\"," +
                "\"refund_amount\":" + amount.setScale(2, RoundingMode.HALF_UP).toString() + "}");
        //"\"refund_reason\":\"" + reason + "\"" + "}");
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 建立错误的跳转请求,以表单HTML形式构造(默认)
     *
     * @param orderSn  订单号
     * @param errorMsg 错误信息
     * @return 提交表单HTML文本
     */
    public static String errorRedirect(String orderSn, String errorMsg, String payResult) {
        StringBuffer sbHtml = new StringBuffer();
        sbHtml.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><html>"
                + "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
                + "<title>支付失败/title></head><body>");

        //页面加载完后自动跳转
        sbHtml.append("<script>location.href='" + payResult + "'</script>");

        sbHtml.append("</body></html>");
        return sbHtml.toString();
    }

}



--------------------------------------
package com.framework.loippi.plugins.aliapppay.reFund.entity;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 支付宝退款所需实体
 *
 * @类名称:AliPayRefund
 * @类描述:
 * @修改备注:
 */
@Data
@ToString
public class AliPayRefund implements Serializable {
    /**
     * 交易号(如2015102821001004900005578936)
     */
    private String tradeNo;
    /**
     * 退款金额
     */
    private BigDecimal refundAmount;
    /**
     * 退款理由 “退款理由”长度不能大于256字节,“退款理由”中不能有“^”、“|”、“$”、“#”等影响detail_data格式的特殊字符;
     */
    private String rRefundReason;
    /**
     * 退款当天日期 格式 2007-10-01 13:13:13
     */
    private String refundDate;
    /**
     * 退款笔数 最大支持1000笔
     */
    private Integer refundAmountNum;
    /**
     * 批次号 退款日期(8位)+流水号(3~24位)不可重复 如(201511130002)
     */
    private String batchNumber;
    /**
     * 单笔数据集 单条数据
     * 原付款支付宝交易号^退款总金额^退款理由
     * 格式为:第一笔交易退款数据集#第二笔交易退款数据集#第三笔交易退款数据集…#第N笔交易退款数据集
     * 2015102821001004900005578936^0.01^协商退款#2015101621001004650091502622^0.01^协商退款
     */
    private String detaildata;

    public void setDetaildata(String tradeNo, BigDecimal refundAmount, String rRefundReason) {
        if ("".equals(rRefundReason)) {
            rRefundReason = "协商退款";
        }
        this.detaildata = tradeNo + "^" + refundAmount + "^" + rRefundReason;
    }

    public String getDetaildata() {
        return detaildata;
    }

    /**
     * 退款成功后回调路径
     */
    private String notifyurl;

    /**
     * 批次号
     */
    private String batchNo;
// public static void main(String[] args) {
//    String str="2010031906272929^80^SUCCESS$jax_chuanhang@alipay.com^2088101003147483^0.01^SUCCESS";
//    System.out.println("hahh"+str.substring(0, str.indexOf("^")));
//    
// }

    public AliPayRefund() {

    }

    public AliPayRefund(String tradeNo, BigDecimal refundAmount, String rRefundReason) {
        this.tradeNo = tradeNo;
        this.refundAmount = refundAmount;
        this.rRefundReason = rRefundReason;
    }
}