package com.tzy.framework.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tzy.common.utils.DateUtils;
import com.tzy.common.utils.LogSyncUtil;
import com.tzy.common.utils.StringUtils;
import com.tzy.framework.pay.Entity.*;
import com.tzy.framework.pay.config.BaoFooPayConfig;
import com.tzy.framework.pay.dto.*;
import com.tzy.framework.pay.service.BaoFooPayService;
import com.tzy.framework.pay.util.HttpUtil;
import com.tzy.framework.pay.util.bofoo.rsa.SignatureUtils;
import com.tzy.framework.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 宝付支付
 */
@Slf4j
@Service
public class BaoFooPayServiceImpl implements BaoFooPayService {

//    public static final String URL = "https://mch-juhe.baofoo.com/api";//test
    public static final String URL = "https://juhe.baofoo.com/api";
    @Value("${tzy.app-version:dev}")
    private String appversion;
    @Autowired
    private BaoFooPayConfig baoFooPayConfig ;

    public static void main(String[] args) throws Exception{
        BaoFooPayServiceImpl service = new BaoFooPayServiceImpl();
        service.baoFooPayConfig = new BaoFooPayConfig();
        service.appversion = "prod1";
        PaymentRequestDto payRequestBody = new PaymentRequestDto();
        payRequestBody.setBody("测试支付");
        payRequestBody.setPaySubWay("ALIPAY_NATIVE");
        payRequestBody.setTotalAmount(new BigDecimal("1000"));
        payRequestBody.setPayWay("QUICK_PASS_NATIVE");//"ALIPAY_NATIVE";//"WECHAT_JSAPI";//QUICK_PASS_NATIVE
        log.info("下单结果>>>>"+service.unifiedOrder(payRequestBody));
        //查询
//        PaymentRequestDto paymentRequestDto = new PaymentRequestDto();
//        paymentRequestDto.setOrderNo("GU202304141637244105");//GU202304141528385870
//        log.info("查询结果>>>>"+service.queryOrder(paymentRequestDto));
        //退款
//        RefundRequestDto refundRequestDto = new RefundRequestDto();
//        refundRequestDto.setTradeNo("GU202304141637244105");
//        refundRequestDto.setRefundAmount(new BigDecimal(1000));
//        refundRequestDto.setRefundMsg("退款测试！");//RN202304141640122746
//        log.info("退款申请>>>>"+service.refund(refundRequestDto));
        //退款查询
//        RefundRequestDto refundRequestDto = new RefundRequestDto();
//        refundRequestDto.setRefundNo("RN202304141640122746");//RN202304141601525321
//        log.info("退款查询>>>>"+service.refundQuery(refundRequestDto));
    }
    /**
     * 下单
     * @param payRequestBody
     * @throws Exception
     */
    /*public ResultMap unifiedOrder(PaymentRequestDto payRequestBody){
        if(payRequestBody.getPayWay().contains("alipay")){
            return alipayUnifiedOrder(payRequestBody);
        }


        return ResultMap.error("未找到支付方式");
    }*/

    /**
     * 下单-支付宝
     * @param payRequestBody
     * @throws Exception
     */
    public ResultMap unifiedOrder(PaymentRequestDto payRequestBody) {
        ResultMap resultMap = new ResultMap();
        try {
            String appScheme = "";
            baoFooPayConfig = initPayConfig(payRequestBody);
            String pfxpath = baoFooPayConfig.getPrivateKey();//商户私钥
            String cerpath = baoFooPayConfig.getPublicKey();//宝付公钥
            String pfxpwd = baoFooPayConfig.getPrivateKeyPwd();//私钥密码
            String txnAmt = String.valueOf(payRequestBody.getTotalAmount().multiply(new BigDecimal(100)).intValue());//用户实际付款金额
            String totalAmt = String.valueOf(payRequestBody.getTotalAmount().multiply(new BigDecimal(100)).intValue());//订单总金额
            String txnTime = getTxnTime();//报文发送日期时间
            String payCode = payRequestBody.getPaySubWay(); //"ALIPAY_NATIVE"; //"ALIPAY_NATIVE";//"WECHAT_JSAPI";//QUICK_PASS_NATIVE
            String subMchId = StringUtils.isNotEmpty(appversion) && appversion.equals("prod") ? "SIMID": "";//测试传空，生产传SIMID
            //569520824 微信
            ///微信支付参数。
            String sub_appid = "";
            String sub_openid = "";
            ///支付宝参数
            String buyer_id = "";
            //buyer_id，auth_code
            String trade_no = RandomUtil.getRandomNew(RandomUtil.GROUP_USER_ORDER);
            Map<String, Object> Biz_Content = new HashMap<String, Object>();
            Biz_Content.put("agentMerId", "");
            Biz_Content.put("agentTerId", "");
            Biz_Content.put("merId", baoFooPayConfig.getMid());
            Biz_Content.put("terId", baoFooPayConfig.getTid());
            Biz_Content.put("outTradeNo", trade_no);
            Biz_Content.put("txnAmt", txnAmt);
            Biz_Content.put("txnTime", txnTime);
            Biz_Content.put("totalAmt", totalAmt);
            int i = DateUtils.differentMinutesByMillisecond(payRequestBody.getExpireTime(), DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, txnTime));
            Biz_Content.put("timeExpire", String.valueOf(i));//订单支付的有效时间，单位：分钟
            Biz_Content.put("prodType", "ORDINARY");//SHARING:分账产品,ORDINARY:普通产品
            Biz_Content.put("payCode", payCode);
            /**
             * WECHAT_JSAPI	微信JSAPI
             * ALIPAY_NATIVE8 支付宝扫码支付（主扫）
             * ALIPAY_JSAPI	支付宝生活号支付
             * QUICK_PASS_NATIVE 云闪付主扫
             * QUICK_PASS_NATIVE_JS	云闪付主扫JS
             *
             */
            Map<String, Object> Pay_Extend = new HashMap<String, Object>();
            if (payCode.startsWith("WECHAT")) {
                ///微信支付属性-公共属性
                Pay_Extend.put("name", "");
                Pay_Extend.put("number", "");
                Pay_Extend.put("type", "");
                Pay_Extend.put("body",  payRequestBody.getBody());
                Pay_Extend.put("area_info", "");
                ////terminal_info 终端信息参数     支付方式为：WECHAT_MICROPAY该字段必填
                String Deviceid = "";///终端设备号
                TerminalInfoEntity Terminal_Info = new TerminalInfoEntity(payCode, Deviceid);
                Pay_Extend.put("terminal_info", Terminal_Info);
                ///场景信息	scene_info
                Map<String, String> Scene_Info = new HashMap<String, String>();
                Scene_Info.put("id", "");///门店唯一标识
                Scene_Info.put("name", "");
                Scene_Info.put("area_code", "");
                Scene_Info.put("address", "");
                Pay_Extend.put("scene_info", Scene_Info);
                if (payCode.equals("WECHAT_JSAPI")) {
                    Pay_Extend.put("sub_appid", sub_appid);
                    Pay_Extend.put("sub_openid", sub_openid);
                } else if (payCode.equals("WECHAT_MICROPAY")) {
                }
                subMchId = baoFooPayConfig.getWxSimid(); //"569520824";
            } else if (payCode.startsWith("ALIPAY")) {
                Pay_Extend.put("subject", payRequestBody.getBody());
                Pay_Extend.put("area_info", "156"+"2901");
                ////terminal_info 终端信息参数     支付方式为：ALIPAY_MICROPAY该字段必填
                String Deviceid = "";///终端设备号
                TerminalInfoEntity Terminal_Info = new TerminalInfoEntity(payCode, Deviceid);
                Pay_Extend.put("terminal_info", Terminal_Info);
                Pay_Extend.put("store_id", "");
                Pay_Extend.put("hb_fq_num", "");
                Pay_Extend.put("hb_fq_seller_percent", "");
                Pay_Extend.put("enable_pay_channels", "");
                Pay_Extend.put("disable_pay_channels", "");
                if (payCode.equals("ALIPAY_JSAPI")) {
                    Pay_Extend.put("buyer_id", buyer_id);
                } else if (payCode.equals("ALIPAY_MICROPAY")) {

                }
                subMchId = baoFooPayConfig.getAlipaySimid(); //"2088620820601391";//二级商户
                appScheme = "alipays://platformapi/startapp?appId=20000067&url=";
            } else if (payCode.startsWith("QUICK_PASS_NATIVE")) {
                subMchId = baoFooPayConfig.getUacSimid(); //"844290053110005";
                Pay_Extend.put("areaInfo", "156"+"2901");
            }
            Biz_Content.put("payExtend", Pay_Extend);
            Biz_Content.put("feeMerId", "");
            Biz_Content.put("subMchId", subMchId);
            Biz_Content.put("notifyUrl", baoFooPayConfig.getPayNotifyUrl());
            Biz_Content.put("pageUrl", "");
            Biz_Content.put("forbidCredit", "0");//1：禁止,0：不禁止;不传默认为0
            Biz_Content.put("attach", "");
            Biz_Content.put("reqReserved", payRequestBody.getPayWay());
            ///风控参数
            RiskInfoEntity rie = new RiskInfoEntity();
            rie.setLocationPoint("");
            rie.setClientIp(payRequestBody.getCreateip());
            Biz_Content.put("riskInfo", rie);
            PostMasterEntity pme = new PostMasterEntity();
            String BizContent = JSONObject.toJSONString(Biz_Content);
            log.info("签名参数 >>> " + BizContent);
            String BizContentEn = SignatureUtils.encryptByRSA(BizContent, pfxpath, pfxpwd);
            pme.setBizContent(BizContent);
            pme.setSignStr(BizContentEn);
            log.info("签名 >>> " + BizContentEn);
            pme.setMerId(baoFooPayConfig.getMid());
            pme.setTerId(baoFooPayConfig.getTid());
            pme.setMethod(PayConstants.BFPAY_METHOD_UNIFIED_ORDER);
            log.info("pme.getParmFormat >>> " + pme.getParmFormat());
            LogSyncUtil.logSync("宝付支付 预下单,交易编号:{},请求参数:{}",payRequestBody.getOrderNo(),pme);
            String PostString = HttpUtil.RequestForm(URL, pme.getParmFormat());
            LogSyncUtil.logSync("宝付支付 预下单,交易编号:{},返回结果:{}",payRequestBody.getOrderNo(),PostString);
            log.info("请求连接 >>> " + PostString);
            ResultMasterEntity rme = JSONObject.parseObject(PostString, ResultMasterEntity.class);
            log.info("下单返回结果 >>> " + rme.toString());
            if (SignatureUtils.verifySignature(cerpath, rme.getDataContent(), rme.getSignStr())) {
                if("SUCCESS".equals(rme.getReturnCode())){
                    JSONObject jsonObject = JSONObject.parseObject(rme.getDataContent());
                    if(null != jsonObject ){
                        String resultCode = jsonObject.getString("resultCode");
                        if("SUCCESS".equals(resultCode)){
                            log.info("[交易结果]交易成功，成功金额："+totalAmt.toString()+"分。");
                            Map<String ,String > data = new HashMap<>();
                            data.put("orderNo",jsonObject.getString("outTradeNo"));
                            data.put("tradeNo",jsonObject.getString("tradeNo"));
                            if (payCode.startsWith("QUICK_PASS_NATIVE")){
                                String string = jsonObject.getJSONObject("chlRetParam").getString("qrCode");
                                string = string.replace("https://", "upwallet://html/");
                                data.put("url", string );
                            } else {
                                data.put("url",appScheme+jsonObject.getJSONObject("chlRetParam").getString("qr_code"));
                            }
                            resultMap.put("data",data);
                            resultMap.put("merOrderId",trade_no);
                            return resultMap;
                        }else{
                           return ResultMap.error( jsonObject.getString("errMsg"));
                        }
                    }
                } else {
                    return ResultMap.error(rme.getReturnMsg());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
        }
        return ResultMap.error("下单失败");
    }

    /**
     * 查询
     * @param paymentRequestDto
     * @return
     * @throws Exception
     */
    public PaymentInfoResponseDto queryOrder(PaymentRequestDto paymentRequestDto){
        PaymentInfoResponseDto paymentInfoResponseDto = new PaymentInfoResponseDto();
        try {
            baoFooPayConfig = initPayConfig(paymentRequestDto);
            String pfxpath = baoFooPayConfig.getPrivateKey();//商户私钥
            String cerpath = baoFooPayConfig.getPublicKey();//宝付公钥
            String pfxpwd = baoFooPayConfig.getPrivateKeyPwd();//私钥密码
            String merId = baoFooPayConfig.getMid();
            String terId = baoFooPayConfig.getTid();
            String tradeNo = "";//宝付订单号
            String outTradeNo = paymentRequestDto.getOrderNo();//商户系统内部订单号，同一个商户号下唯
            Map<String, Object> Biz_Content = new HashMap<String, Object>();
            Biz_Content.put("agentMerId", "");
            Biz_Content.put("agentTerId", "");
            Biz_Content.put("merId", merId);
            Biz_Content.put("terId", terId);
            Biz_Content.put("tradeNo", tradeNo);
            Biz_Content.put("outTradeNo", outTradeNo);
            PostMasterEntity pme = new PostMasterEntity();
            String BizContent = JSONObject.toJSONString(Biz_Content).toString();
            log.info(BizContent);
            String BizContentEn = SignatureUtils.encryptByRSA(BizContent, pfxpath, pfxpwd);
            pme.setBizContent(BizContent);
            pme.setSignStr(BizContentEn);
            pme.setMerId(merId);
            pme.setTerId(terId);
            pme.setMethod(PayConstants.BFPAY_METHOD_ORDER_QUERY);
            log.info(pme.getParmFormat());
            LogSyncUtil.logSync("宝付支付 订单查询,交易编号:{},请求参数:{}",paymentRequestDto.getOrderNo(),pme);
            String PostString = HttpUtil.RequestForm(URL, pme.getParmFormat());
            LogSyncUtil.logSync("宝付支付 订单查询,交易编号:{},返回结果:{}",paymentRequestDto.getOrderNo(),PostString);
            log.info(PostString);
            ResultMasterEntity rme = JSONObject.parseObject(PostString, ResultMasterEntity.class);
            log.info(rme.toString());
            if (SignatureUtils.verifySignature(cerpath, rme.getDataContent(), rme.getSignStr())) {
                if("SUCCESS".equals(rme.getReturnCode())){
                    JSONObject jsonObject = JSONObject.parseObject(rme.getDataContent());
                    log.info("解析结果：{}",jsonObject);
                    if(null != jsonObject ){
                        String resultCode = jsonObject.getString("resultCode");
                        if("SUCCESS".equals(resultCode)){
                            if("SUCCESS".equals(jsonObject.getString("txnState"))){
                                //下单成功
                                paymentInfoResponseDto.setOrderNo(outTradeNo);
                                paymentInfoResponseDto.setPayWay(paymentRequestDto.getPayWay());
                                paymentInfoResponseDto.setPaymentNo(StringUtils.isNotEmpty(jsonObject.getString("tradeNo")) ? jsonObject.getString("tradeNo") : null);
                                Integer orderAmt = null != jsonObject.getInteger("succAmt") ? jsonObject.getInteger("succAmt") : 0;
                                paymentInfoResponseDto.setPaymentPrice(new BigDecimal(orderAmt).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_FLOOR));
                                paymentInfoResponseDto.setActualPayment(new BigDecimal(orderAmt).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_FLOOR));
                                Date finishedDate = StringUtils.isNotEmpty(jsonObject.getString("finishTime")) ? DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, jsonObject.getString("finishTime")) : null;
                                paymentInfoResponseDto.setPaymentSuccessTime(finishedDate);
                                paymentInfoResponseDto.setSuccess(true);
                                return paymentInfoResponseDto;
                            }else{
                                return paymentInfoResponseDto.error(-1,jsonObject.getString("txnState"));
                            }
                        }else{
                            return paymentInfoResponseDto.error(-1,jsonObject.getString("errMsg"));
                        }
                    }
                } else {
                    return paymentInfoResponseDto.error(-1,rme.getReturnMsg());
                }
            } else {
                log.info("验签失败");
                return PaymentInfoResponseDto.error(-1,"验签失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            return PaymentInfoResponseDto.error(-1,"签名生成异常！");
        }
        return PaymentInfoResponseDto.error(-1,"查询失败！");
    }

    /**
     * 退款
     * @param refundRequestDto
     * @throws Exception
     */
    public ResultMap refund(RefundRequestDto refundRequestDto){
        try {
            baoFooPayConfig = initRefundConfig(refundRequestDto);
            String pfxpath = baoFooPayConfig.getPrivateKey();//商户私钥
            String cerpath = baoFooPayConfig.getPublicKey();//宝付公钥
            String pfxpwd = baoFooPayConfig.getPrivateKeyPwd();//私钥密码
            String merId = baoFooPayConfig.getMid();
            String terId = baoFooPayConfig.getTid();
            String txnTime = getTxnTime();//报文发送日期时间
            String originOutTradeNo = refundRequestDto.getOrderNo();//原支付订单商户订单号 [与原支付订单宝付交易号二选一比传]
            String outTradeNo = RandomUtil.getRandomNew(RandomUtil.REFUND_NO);
            String refundAmt = String.valueOf(refundRequestDto.getRefundAmount().multiply(new BigDecimal(100)).intValue());//退款金额
            String totalAmt = String.valueOf(refundRequestDto.getRefundAmount().multiply(new BigDecimal(100)).intValue());//订单总金额
            Map<String, Object> Biz_Content = new HashMap<String, Object>();
            Biz_Content.put("agentMerId", "");
            Biz_Content.put("agentTerId", "");
            Biz_Content.put("merId", merId);
            Biz_Content.put("terId", terId);
            Biz_Content.put("originTradeNo", "");
            Biz_Content.put("originOutTradeNo", originOutTradeNo);
            Biz_Content.put("outTradeNo", outTradeNo);
            Biz_Content.put("notifyUrl", baoFooPayConfig.getRefundNotifyUrl());
            Biz_Content.put("merchantName", baoFooPayConfig.getMerchantName());
            Biz_Content.put("refundAmt", refundAmt);
            Biz_Content.put("totalAmt", totalAmt);
            Biz_Content.put("txnTime", txnTime);
            Biz_Content.put("attach", "");//预留字段
            Biz_Content.put("reqReserved", refundRequestDto.getPayWay());//请求方保留域
            Biz_Content.put("refundReason", refundRequestDto.getRefundMsg());
            PostMasterEntity pme = new PostMasterEntity();
            String BizContent = JSONObject.toJSONString(Biz_Content).toString();
            log.info(BizContent);
            String BizContentEn = SignatureUtils.encryptByRSA(BizContent, pfxpath, pfxpwd);
            pme.setBizContent(BizContent);
            pme.setSignStr(BizContentEn);
            pme.setMerId(merId);
            pme.setTerId(terId);
            pme.setMethod(PayConstants.BFPAY_METHOD_ORDER_REFUND);
            log.info(pme.getParmFormat());
            LogSyncUtil.logSync("宝付支付 退款,交易编号:{},请求参数:{}",originOutTradeNo,pme);
            String PostString = HttpUtil.RequestForm(URL, pme.getParmFormat());
            LogSyncUtil.logSync("宝付支付 退款,交易编号:{},返回结果:{}",originOutTradeNo,PostString);
            log.info(PostString);
            ResultMasterEntity rme = JSONObject.parseObject(PostString, ResultMasterEntity.class);
            log.info("响应] 响应内容：{}",rme);
            if (SignatureUtils.verifySignature(cerpath, rme.getDataContent(), rme.getSignStr())) {
                log.info("解析结果：{}",rme.getDataContent());
                if("SUCCESS".equals(rme.getReturnCode())){
                    JSONObject jsonObject = JSONObject.parseObject(rme.getDataContent());
                    if(null != jsonObject ){
                        String resultCode = jsonObject.getString("resultCode");
                        if("SUCCESS".equals(resultCode)) {
                            if("REFUND".equals(jsonObject.getString("refundState"))){
                                log.info("[申请退款结果]交易成功，成功金额：" + totalAmt.toString() + "分。");
                                //下单成功
                                Map<String, String> stringStringMap = new HashMap<>();
                                stringStringMap.put("out_refund_no", outTradeNo);
                                stringStringMap.put("refund_id", jsonObject.getString("tradeNo"));
                                stringStringMap.put("refund_fee", String.valueOf(jsonObject.getInteger("refundAmt")));
                                stringStringMap.put("fund_change", "Y");
//                                stringStringMap.put("finishedDate", result.get("finishedDate"));
                                return ResultMap.ok(jsonObject.getString("refundState"))
                                        .put("data", stringStringMap)
                                        ;
                            }else{
                                return ResultMap.error(-1,jsonObject.getString("errMsg"));
                            }
                        }else{
                            return ResultMap.error(-1,jsonObject.getString("errMsg"));
                        }
                    }else{
                        return ResultMap.error(rme.getReturnMsg());
                    }
                }
            } else {
                return ResultMap.error(-1,"验签失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultMap.error(-1,"签名生成异常！");
        }
        return ResultMap.error(-1,"退款失败！");
    }

    /**
     * 退款查询
     * @param refundRequestDto
     * @return
     */
    public ResultMap refundQuery( RefundRequestDto refundRequestDto){
        try {
            baoFooPayConfig = initRefundConfig(refundRequestDto);
            String pfxpath = baoFooPayConfig.getPrivateKey();//商户私钥
            String cerpath = baoFooPayConfig.getPublicKey();//宝付公钥
            String pfxpwd = baoFooPayConfig.getPrivateKeyPwd();//私钥密码
            String merId = baoFooPayConfig.getMid();
            String terId = baoFooPayConfig.getTid();
            String tradeNo = "";//宝付订单号
            String outTradeNo = refundRequestDto.getRefundNo();//商户系统内部订单号，同一个商户号下唯
            Map<String, Object> Biz_Content = new HashMap<String, Object>();
            Biz_Content.put("agentMerId", "");
            Biz_Content.put("agentTerId", "");
            Biz_Content.put("merId", merId);
            Biz_Content.put("terId", terId);
            Biz_Content.put("outTradeNo", outTradeNo);
            Biz_Content.put("tradeNo", tradeNo);
            PostMasterEntity pme = new PostMasterEntity();
            String BizContent = JSONObject.toJSONString(Biz_Content).toString();
            log.info(BizContent);
            String BizContentEn = SignatureUtils.encryptByRSA(BizContent, pfxpath, pfxpwd);
            pme.setBizContent(BizContent);
            pme.setSignStr(BizContentEn);
            pme.setMerId(merId);
            pme.setTerId(terId);
            pme.setMethod(PayConstants.BFPAY_METHOD_REFUND_QUERY);
            log.info(pme.getParmFormat());
            LogSyncUtil.logSync("宝付支付 退款查询,交易编号:{},请求参数:{}",outTradeNo,pme);
            String PostString = HttpUtil.RequestForm(URL, pme.getParmFormat());
            LogSyncUtil.logSync("宝付支付 退款查询,交易编号:{},返回结果:{}",outTradeNo,PostString);
            log.info(PostString);
            ResultMasterEntity rme = JSONObject.parseObject(PostString, ResultMasterEntity.class);
            log.info("响应] 响应内容：{}",rme);
            if (SignatureUtils.verifySignature(cerpath, rme.getDataContent(), rme.getSignStr())) {
                log.info("解析结果：{}",rme.getDataContent());
                if("SUCCESS".equals(rme.getReturnCode())) {
                    JSONObject jsonObject = JSONObject.parseObject(rme.getDataContent());
                    if (null != jsonObject) {
                        String resultCode = jsonObject.getString("resultCode");
                        if ("SUCCESS".equals(resultCode)) {
                            if ("SUCCESS".equals(jsonObject.getString("refundState"))) {
                                log.info("[申请退款结果]交易成功，金额："+jsonObject.getInteger("succAmt")+"分。");
                                Map<String, String> stringStringMap = new HashMap<>();
                                stringStringMap.put("out_refund_no", outTradeNo);
                                stringStringMap.put("refund_id",  jsonObject.getString("tradeNo"));
                                stringStringMap.put("refund_fee", String.valueOf(jsonObject.getInteger("succAmt")));
                                stringStringMap.put("fund_change", "Y");
                                stringStringMap.put("finishedDate", jsonObject.getString("finishTime"));
                                return ResultMap.ok()
                                        .put("data",stringStringMap)
                                        ;
                            } else {
                                return ResultMap.error(-1,jsonObject.getString("errMsg"));
                            }
                        }else{
                            return ResultMap.error(-1,jsonObject.getString("errMsg"));
                        }
                    }
                }
            } else {
                return ResultMap.error(-1,"验签失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultMap.error(-1,"签名生成异常！");
        }
        return ResultMap.error(-1,"退款查询失败！");
    }

    private static String getTxnTime() {
        return DateUtils.dateTimeNow(DateUtils.YYYYMMDDHHMMSS);
    }

    private BaoFooPayConfig initPayConfig(PaymentRequestDto paymentRequestDto){
        return StringUtils.isNotEmpty(appversion) && appversion.equals("prod")
                ? baoFooPayConfig = (BaoFooPayConfig) paymentRequestDto.getPayConfigByClass(BaoFooPayConfig.class) : new BaoFooPayConfig();
    }
    private BaoFooPayConfig initRefundConfig(RefundRequestDto refundRequestDto){
        return StringUtils.isNotEmpty(appversion) && appversion.equals("prod")
                ? baoFooPayConfig = (BaoFooPayConfig) refundRequestDto.getPayConfigByClass(BaoFooPayConfig.class) : new BaoFooPayConfig();
    }

}
