1
zj
8 days ago 7d008bf58373926bbdcf67fa7bcf92510f427fb6
src/main/java/com/nq/service/impl/UserWithdrawServiceImpl.java
@@ -1,43 +1,44 @@
package com.nq.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.nq.dao.*;
import com.nq.enums.EConfigKey;
import com.nq.enums.EUserAssets;
import com.nq.pay.PayUtil;
import com.nq.pojo.*;
import com.nq.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.common.PayV2GatewayKeys;
import com.nq.common.ServerResponse;
import com.nq.utils.*;
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.http.HttpClientUtil;
import com.nq.utils.stock.WithDrawUtils;
import com.nq.utils.pay.PayV2RsaSignUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nq.utils.timeutil.DateTimeUtil;
import com.nq.utils.timeutil.TimeUtil;
import org.apache.commons.collections.map.HashedMap;
import okhttp3.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@@ -49,13 +50,14 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import static com.nq.utils.timeutil.DateTimeUtil.STANDARD_FORMAT;
@Service("iUserWithdrawService")
public class UserWithdrawServiceImpl implements IUserWithdrawService {
    private static final Logger log = LoggerFactory.getLogger(UserWithdrawServiceImpl.class);
    private static final String PAY_V2_PAYOUT_CREATE_URL = "https://pay.kkpay100.com/v2/payOutCreate";
    private static final int PAY_V2_MID = 10724;
    @Autowired
@@ -343,166 +345,351 @@
    @Transactional
    public ServerResponse updateState(Integer withId, Integer state, String authMsg,
                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
        UserWithdraw userWithdraw = this.userWithdrawMapper.selectByPrimaryKey(withId);
        SiteSetting siteSetting = siteSettingService.getSiteSetting();
        if (userWithdraw == null) {
            return ServerResponse.createByErrorMsg("提现订单不存在");
        }
        log.info("当前系统设置  {}", new Gson().toJson(siteSetting));
        if (userWithdraw.getWithStatus().intValue() != 0) {
            return ServerResponse.createByErrorMsg("提现订单已处理,不要重复操作");
        }
        if (state.intValue() == 3 &&
                StringUtils.isBlank(authMsg)) {
            return ServerResponse.createByErrorMsg("失败信息必填");
        }
        User user = this.userMapper.selectById(userWithdraw.getUserId());
        if (user == null) {
            return ServerResponse.createByErrorMsg("用户不存在");
        }
        UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("IN", user.getId());
        if (userAssets == null) {
            return ServerResponse.createByErrorMsg("用户资金账户不存在");
        }
        if (state == 3) {
            if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
                BigDecimal subtract = userWithdraw.getWithAmt().subtract(userAssets.getAmountToBeCovered());
                if(subtract.compareTo(BigDecimal.ZERO) > 0){
                    userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(subtract));
                    userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(subtract));
                    userAssets.setAmountToBeCovered(BigDecimal.ZERO);
    public ServerResponse updateState(Integer withId, Integer state, String authMsg, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            UserWithdraw userWithdraw = this.userWithdrawMapper.selectByPrimaryKey(withId);
            SiteSetting siteSetting = siteSettingService.getSiteSetting();
            if (userWithdraw == null) {
                return ServerResponse.createByErrorMsg("提现订单不存在");
            }
            log.info("当前系统设置  {}", new Gson().toJson(siteSetting));
            if (userWithdraw.getWithStatus().intValue() != 0) {
                return ServerResponse.createByErrorMsg("提现订单已处理,不要重复操作");
            }
            if (state.intValue() == 3 &&
                    StringUtils.isBlank(authMsg)) {
                return ServerResponse.createByErrorMsg("失败信息必填");
            }
            User user = this.userMapper.selectById(userWithdraw.getUserId());
            if (user == null) {
                return ServerResponse.createByErrorMsg("用户不存在");
            }
            UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("IN", user.getId());
            if (userAssets == null) {
                return ServerResponse.createByErrorMsg("用户资金账户不存在");
            }
            if (state == 3) {
                if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
                    BigDecimal subtract = userWithdraw.getWithAmt().subtract(userAssets.getAmountToBeCovered());
                    if(subtract.compareTo(BigDecimal.ZERO) > 0){
                        userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(subtract));
                        userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(subtract));
                        userAssets.setAmountToBeCovered(BigDecimal.ZERO);
                    }else{
                        userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().subtract(userWithdraw.getWithAmt()));
                    }
                }else{
                    userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().subtract(userWithdraw.getWithAmt()));
                    userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(userWithdraw.getWithAmt()));
                    userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(userWithdraw.getWithAmt()));
                }
            }else{
                userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(userWithdraw.getWithAmt()));
                int updateCount = userAssetsMapper.updateById(userAssets);
                if (updateCount > 0) {
                    log.info("提现失败,返还用户资金成功!");
                } else {
                    log.error("返还用户资金出错,抛出异常");
                    throw new Exception("修改用户资金出错,抛出异常");
                }
                userWithdraw.setWithMsg(authMsg);
                userWithdraw.setWithStatus(2);
                userWithdraw.setTransTime(new Date());
                userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
                return ServerResponse.createBySuccessMsg("操作成功!");
            }else if(state == 2){//手动打款
                userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(userWithdraw.getWithAmt()));
                userAssetsMapper.updateById(userAssets);
                userWithdraw.setWithStatus(1);
                userWithdraw.setTransTime(new Date());
                int updateCount = this.userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
                if (updateCount > 0) {
                    return ServerResponse.createBySuccessMsg("操作成功!");
                }
            }else if(state == 1){//走代付
                return getObjectServerResponseTwo(withId, request, response, userWithdraw, user, userAssets);
            }else if(state == 4){//走代付
                return getObjectServerResponseThree(withId, request, response, userWithdraw, user, userAssets);
            }else{
                return ServerResponse.createByErrorMsg("请选择对应的操作!");
            }
            int updateCount = userAssetsMapper.updateById(userAssets);
            if (updateCount > 0) {
                log.info("提现失败,返还用户资金成功!");
            } else {
                log.error("返还用户资金出错,抛出异常");
                throw new Exception("修改用户资金出错,抛出异常");
            }
            userWithdraw.setWithMsg(authMsg);
            userWithdraw.setWithStatus(2);
            userWithdraw.setTransTime(new Date());
            userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
            return ServerResponse.createBySuccessMsg("操作成功!");
        }else if(state == 2){//手动打款
            userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(userWithdraw.getWithAmt()));
            userAssetsMapper.updateById(userAssets);
            userWithdraw.setWithStatus(1);
            userWithdraw.setTransTime(new Date());
            int updateCount = this.userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
            if (updateCount > 0) {
                return ServerResponse.createBySuccessMsg("操作成功!");
            }
        }else if(state == 1){//走代付
            String result = payForAnOrder(request, response, userWithdraw);
            if(StringUtils.isEmpty(result)){
                return ServerResponse.createByErrorMsg("代付打款失败!");
            }
            ObjectMapper objectMapper = new ObjectMapper();
            // 将 JSON 字符串转换为实体类对象
            TransferResponseBo transferResponseBo = objectMapper.readValue(result, TransferResponseBo.class);
            if(transferResponseBo.getRespCode().equals("FAIL")){
                log.error("代付下单失败:"+result);
                return ServerResponse.createByErrorMsg("代付下单失败:",request);
            }
            TransferResponse transferResponse = new TransferResponse();
            transferResponse.setRespCode(transferResponseBo.getRespCode());
            transferResponse.setSignType(transferResponseBo.getSignType());
            transferResponse.setSign(transferResponseBo.getSign());
            transferResponse.setMerTransferId(transferResponseBo.getMerTransferId());
            transferResponse.setTransferAmount(new BigDecimal(transferResponseBo.getTransferAmount()));
            transferResponse.setApplyDate(transferResponseBo.getApplyDate());
            transferResponse.setTradeNo(transferResponseBo.getTradeNo());
            transferResponse.setTradeResult(0);
            transferResponse.setCallbackState(0);
            transferResponse.setUserId(user.getId());
            transferResponse.setCreatedAt(new Date());
            transferResponse.setWithId(withId);
            transferResponseService.save(transferResponse);
            userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(userWithdraw.getWithAmt()));
            userAssetsMapper.updateById(userAssets);
            userWithdraw.setWithStatus(4);
            userWithdraw.setTransTime(new Date());
            int updateCount = this.userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
            if (updateCount > 0) {
                return ServerResponse.createBySuccessMsg("操作成功!");
            }
        }catch (Exception e){
            log.info("提现失败:"+e.getMessage());
            throw e;
        }
        return ServerResponse.createBySuccessMsg("操作失败!");
    }
    public String payForAnOrder(HttpServletRequest request, HttpServletResponse response,UserWithdraw userWithdraw) throws IOException {
        request.setCharacterEncoding("UTF-8");
        Map<String, String> reqMap = new HashMap<String, String>();
        //申请时间 时间格式:yyyy-MM-dd HH:mm:ss
        reqMap.put("apply_date", getOrderTime());
        //收款银行代码   详见附件银行编码或商户后台银行代码表
        reqMap.put("bank_code", "IDPT0001");
        //商户代码 平台分配唯一
        reqMap.put("mch_id", "100789033");
        // 商家转账订单号 保证每笔订单唯一
        reqMap.put("mch_transferId", generatePayOrderId());
        //收款银行账号   银行账号(巴西PIX代付填对应类型的PIX账号)
        reqMap.put("receive_account", userWithdraw.getBankNo());
        //收款银行户名
        reqMap.put("receive_name", userWithdraw.getBankName());
        //转账金额 整数,以元为单位
        reqMap.put("transfer_amount", userWithdraw.getWithAmt().toString());
        //备注
        reqMap.put("remark", userWithdraw.getBankAddress());
        //异步通知地址
        reqMap.put("back_url", "https://api.durocaspitall.com/user/noticePayment.do");
        String signStr = SignUtil.sortData(reqMap);
        //签名方式 固定值MD5,不参与签名
        reqMap.put("sign_type", "MD5");
    //代付二
    private ServerResponse getObjectServerResponseTwo(Integer withId, HttpServletRequest request, HttpServletResponse response, UserWithdraw userWithdraw, User user, UserAssets userAssets) throws Exception {
        String reqUrl = "https://api.watchglbpay.com/pay/transfer";
        String merchant_key = "ZGZY3REWQJLAWRCRTHWQVGWYPMD878KQ";
        String payoutUrl = "https://gateway.kings-pays.com/gateway/payout/init";//正式地址
        String merchantKey = "qqaC1DH/LeR9iPvm";//商户key 需替换
        String aesKey = "ge6vK40fHNZPFJ4p";//商户aesKey 需替换
        String aesIv = "6gJoHTEE1i2O3ovE";//商户aesIv 需替换
        String sign = SignAPI.sign(signStr, merchant_key);
        // 1. 生成商户订单号
        String merchantOrderNo = generatePayoutOrderId(withId);
        reqMap.put("sign", sign);
        // 2. 构建加密前的业务参数
        JSONObject dataObj = new JSONObject();
        dataObj.put("amount", userWithdraw.getWithAmt().intValue()); // 注意金额单位(示例中为整数)
        dataObj.put("transferType", "BANK_TRANSFER");
        dataObj.put("beneficiaryName", user.getNickName());
        dataObj.put("beneficiaryEmail", "null@gmail.com");
        dataObj.put("beneficiaryPhoneNo", user.getPhone());
        dataObj.put("beneficiaryAccount", userWithdraw.getBankNo());
        dataObj.put("beneficiaryIFSC", userWithdraw.getBankAddress());
        dataObj.put("merchantOrderNo", merchantOrderNo);
        dataObj.put("notifyUrl", "https://api.greenbackcaps.top/user/payoutCallbackTwo.do");
        System.out.println("reqMap:" + reqMap.toString().length() + " --> " + reqMap.toString());
        System.out.println("签名参数排序:" + signStr.length() + " --> " + signStr);
        System.out.println("sign值:" + sign.length() + " --> " + sign);
        // 3. AES 加密
        String encryptedData = AesEncryptUtil.encrypt(dataObj.toJSONString(), aesKey, aesIv);
        JSONObject requestObj = new JSONObject();
        requestObj.put("data", encryptedData);
        String result = HttpClientUtil.doPost(reqUrl, reqMap, "utf-8");
        System.out.println("result值:" + result);
        return result;
        // 4. 设置请求头
        Headers headers = new Headers.Builder()
                .add("merchant_key", merchantKey)
                .build();
        // 5. 发送 HTTP 请求
        log.info("代付请求参数:{}", requestObj.toJSONString());
        String respStr = doPost(payoutUrl, requestObj.toJSONString(), headers);
        log.info("代付响应原始数据:{}", respStr);
        // 6. 解析响应(使用 Jackson 或 fastjson,这里以 fastjson 为例)
        JSONObject respJson = JSONObject.parseObject(respStr);
        int code = respJson.getIntValue("code");
        boolean success = respJson.getBooleanValue("success");
        String msg = respJson.getString("msg");
        // 6.1 接口调用失败(code != 0 或 success = false)
        if (code != 0 || !success) {
            handleFailure(userAssets, userWithdraw, "Failure? Please contact customer service.");
            return ServerResponse.createByErrorMsg("代付请求失败:" + msg, request);
        }
        // 6.2 获取 data 部分
        JSONObject data = respJson.getJSONObject("data");
        String status = data.getString("status");       // ACCEPT / FAILURE / PROCESSING
        String failMsg = data.getString("message");     // 失败时的具体原因
        // 7. 根据 status 判断业务是否成功
        if ("ACCEPT".equals(status)) {
            // 代付订单被接受(不一定最终成功,需等待回调)
            String platformOrderNo = data.getString("orderNo");
            // 保存代付记录
            saveTransferRecord(merchantOrderNo, platformOrderNo, userWithdraw.getWithAmt(), user.getId(), withId);
            // 更新提现记录为“已提交”
            userWithdraw.setWithStatus(4); // 4:已提交
            userWithdraw.setTransTime(new Date());
            userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
            log.info("代付下单成功,商户订单号:{}", merchantOrderNo);
            return ServerResponse.createBySuccessMsg("代付申请已提交,请等待处理");
        } else {
            // 业务失败(如 FAILURE)
            String errorMsg = (failMsg != null && !failMsg.isEmpty()) ? failMsg : msg;
            handleFailure(userAssets, userWithdraw, "Withdrawal failed:" + errorMsg);
            return ServerResponse.createByErrorMsg("Withdrawal failed:" + errorMsg, request);
        }
    }
    public String generatePayOrderId() {
        // 获取当前时间戳(毫秒)
        long timestamp = System.currentTimeMillis();
    /**
     * 代付三:v2/payOutCreate(JSON + SHA256withRSA),IMPS
     */
    private ServerResponse getObjectServerResponseThree(Integer withId, HttpServletRequest request, HttpServletResponse response,
                                                        UserWithdraw userWithdraw, User user, UserAssets userAssets) throws Exception {
        String merchantOrderNo = generatePayoutOrderId(withId);
        BigDecimal amount = userWithdraw.getWithAmt().setScale(2, RoundingMode.HALF_UP);
        String notifyUrl = "https://api.greenbackcaps.top/user/payoutCallbackThree.do";
        // 生成一个随机的UUID并截取前8位
        String randomUUID = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
        UserBank bank = userBankMapper.selectOne(new LambdaQueryWrapper<UserBank>()
                .eq(UserBank::getUserId, user.getId())
                .eq(UserBank::getBankNo, userWithdraw.getBankNo())
                .last("limit 1"));
        String email = (bank != null && StringUtils.isNotBlank(bank.getBankEmail()))
                ? bank.getBankEmail().trim() : (user.getId() + "@user.local");
        if (email.length() > 64) {
            email = email.substring(0, 64);
        }
        // 将时间戳和随机UUID结合生成订单号
        return timestamp + randomUUID;
        String userName = StringUtils.defaultIfBlank(userWithdraw.getWithName(), user.getRealName());
        userName = StringUtils.defaultIfBlank(userName, "User");
        if (userName.length() > 16) {
            userName = userName.substring(0, 16);
        }
        String bankName = StringUtils.defaultIfBlank(userWithdraw.getBankName(), "BANK");
        if (bankName.length() > 32) {
            bankName = bankName.substring(0, 32);
        }
        String bankCode = StringUtils.defaultIfBlank(userWithdraw.getBankAddress(), "");
        if (bankCode.length() > 32) {
            bankCode = bankCode.substring(0, 32);
        }
        String bankCardNumber = StringUtils.defaultIfBlank(userWithdraw.getBankNo(), "");
        if (bankCardNumber.length() > 32) {
            bankCardNumber = bankCardNumber.substring(0, 32);
        }
        String address = "India";
        int paymentType = 1;
        Map<String, String> signParams = new TreeMap<>();
        signParams.put("amount", amount.toPlainString());
        signParams.put("address", address);
        signParams.put("bankCardNumber", bankCardNumber);
        signParams.put("bankCode", bankCode);
        signParams.put("bankName", bankName);
        signParams.put("currency", "INR");
        signParams.put("email", email);
        signParams.put("mid", String.valueOf(PAY_V2_MID));
        signParams.put("mobile", user.getPhone());
        signParams.put("notifyUrl", notifyUrl);
        signParams.put("orderId", merchantOrderNo);
        signParams.put("paymentType", String.valueOf(paymentType));
        signParams.put("userName", userName);
        String signBaseString = PayV2RsaSignUtil.buildStringA(signParams);
        String sign = PayV2RsaSignUtil.sign(signBaseString, PayV2GatewayKeys.MERCHANT_PRIVATE_KEY_PEM);
        log.info("代付v2签名串 stringA={}", signBaseString);
        Map<String, Object> body = new LinkedHashMap<>();
        body.put("mid", PAY_V2_MID);
        body.put("orderId", merchantOrderNo);
        body.put("amount", amount.toPlainString());
        body.put("currency", "INR");
        body.put("paymentType", paymentType);
        body.put("bankName", bankName);
        body.put("bankCode", bankCode);
        body.put("bankCardNumber", bankCardNumber);
        body.put("userName", userName);
        body.put("email", email);
        body.put("mobile", user.getPhone());
        body.put("address", address);
        body.put("notifyUrl", notifyUrl);
        body.put("sign", sign);
        String json = new Gson().toJson(body);
        log.info("代付v2请求:{}", json);
        String respStr = HttpClientRequest.doPostJsonBody(PAY_V2_PAYOUT_CREATE_URL, json);
        log.info("代付v2响应:{}", respStr);
        ObjectMapper objectMapper = new ObjectMapper();
        PayV2PayoutCreateResponse resp = objectMapper.readValue(respStr, PayV2PayoutCreateResponse.class);
        if (resp.getStatus() != 1 || resp.getData() == null) {
            handleFailure(userAssets, userWithdraw, "Failure? Please contact customer service.");
            return ServerResponse.createByErrorMsg("代付请求失败:" + (StringUtils.isNotBlank(resp.getMsg()) ? resp.getMsg() : "unknown"), request);
        }
        PayV2PayoutCreateResponse.Data data = resp.getData();
        Map<String, String> respSign = new TreeMap<>();
        if (StringUtils.isNotBlank(data.getAmount())) {
            respSign.put("amount", data.getAmount());
        }
        if (StringUtils.isNotBlank(data.getErrMsg())) {
            respSign.put("err_msg", data.getErrMsg());
        }
        if (data.getMId() != null) {
            respSign.put("m_id", String.valueOf(data.getMId()));
        }
        if (StringUtils.isNotBlank(data.getMOrder())) {
            respSign.put("m_order", data.getMOrder());
        }
        if (StringUtils.isNotBlank(data.getOrderId())) {
            respSign.put("order_id", data.getOrderId());
        }
        if (data.getOrderStatus() != null) {
            respSign.put("order_status", String.valueOf(data.getOrderStatus()));
        }
        if (!PayV2RsaSignUtil.verify(respSign, data.getSign(), PayV2GatewayKeys.PLATFORM_PUBLIC_KEY)) {
            log.warn("代付v2返回签名未通过校验, stringA={}, sign={}",
                    PayV2RsaSignUtil.buildStringA(respSign), data.getSign());
        }
        Integer os = data.getOrderStatus();
        if (os != null && (os == 2 || os == 3)) {
            String em = StringUtils.defaultIfBlank(data.getErrMsg(), resp.getMsg());
            handleFailure(userAssets, userWithdraw, "Failure? Please contact customer service.");
            return ServerResponse.createByErrorMsg("代付失败:" + em, request);
        }
        saveTransferRecordV2(merchantOrderNo, data.getOrderId(), amount, user.getId(), withId, data.getSign());
        userWithdraw.setWithStatus(4);
        userWithdraw.setTransTime(new Date());
        userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
        log.info("代付v2下单成功,商户订单号:{}", merchantOrderNo);
        return ServerResponse.createBySuccessMsg("代付申请已提交,请等待处理");
    }
    public static String getOrderTime() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义输出格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前时间
        return now.format(formatter);
    private static boolean isValidIndiaMobile(String digits10) {
        if (digits10 == null || digits10.length() != 10) {
            return false;
        }
        char c = digits10.charAt(0);
        return c >= '6' && c <= '9';
    }
    private void saveTransferRecordV2(String merchantOrderNo, String platformOrderNo, BigDecimal amount,
                                      Integer userId, Integer withId, String sign) {
        TransferResponse record = new TransferResponse();
        record.setMerTransferId(merchantOrderNo);
        record.setTradeNo(platformOrderNo);
        record.setTransferAmount(amount);
        record.setTradeResult(0);
        record.setCallbackState(0);
        record.setRespCode("SUCCESS");
        record.setSignType("MD5");
        record.setSign(sign);
        record.setApplyDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        record.setUserId(userId);
        record.setWithId(withId);
        record.setCreatedAt(new Date());
        record.setUpdatedAt(new Date());
        transferResponseService.save(record);
    }
    public static String doPost(String url, String data, Headers headers) throws IOException {
        OkHttpClient customClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .headers(headers)
                .post(RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), data))
                .build();
        Response response = customClient.newCall(request).execute();
        String resp = response.body().string();
        return resp;
    }
    private void handleFailure(UserAssets userAssets, UserWithdraw userWithdraw, String errorMsg) {
        userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(userWithdraw.getWithAmt()));
        userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(userWithdraw.getWithAmt()));
        userAssetsMapper.updateById(userAssets);
        userWithdraw.setWithStatus(2); // 2:失败
        userWithdraw.setWithMsg(errorMsg);
        userWithdraw.setTransTime(new Date());
        userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
    }
    private void saveTransferRecord(String merchantOrderNo, String platformOrderNo, BigDecimal amount,
                                    Integer userId, Integer withId) {
        TransferResponse record = new TransferResponse();
        record.setMerTransferId(merchantOrderNo);
        record.setTradeNo(platformOrderNo);
        record.setTransferAmount(amount);
        record.setTradeResult(0);       // 0:已下单
        record.setCallbackState(0);     // 0:未处理
        record.setRespCode("SUCCESS");
        record.setSignType("AES");
        record.setApplyDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        record.setUserId(userId);
        record.setWithId(withId);
        record.setCreatedAt(new Date());
        record.setUpdatedAt(new Date());
        transferResponseService.save(record);
    }
    public int deleteByUserId(Integer userId) {
        return this.userWithdrawMapper.deleteByUserId(userId);
@@ -536,80 +723,118 @@
    }
    @Override
    public void noticePayment(TradeResultVO vo, HttpServletResponse response) throws IOException {
        log.info("代付回调信息:"+vo.toString());
        synchronized (vo.getTradeNo()){
            TransferResponse transferResponse = transferResponseService.getOne(new LambdaQueryWrapper<>(TransferResponse.class)
                    .eq(TransferResponse::getMerTransferId, vo.getMerTransferId())
                    .eq(TransferResponse::getTradeNo, vo.getTradeNo())
                    .eq(TransferResponse::getTradeResult, 0)
                    .eq(TransferResponse::getCallbackState, 0)
            );
            if(ObjectUtils.isNotEmpty(transferResponse)){
                UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("IN", transferResponse.getUserId());
                UserWithdraw userWithdraw = this.userWithdrawMapper.selectByPrimaryKey(transferResponse.getWithId());
                if (transferResponse.getRespCode().equals("SUCCESS")) {
                    transferResponse.setTradeResult(1);
                    transferResponse.setCallbackState(1);
                    transferResponseService.updateById(transferResponse);
                    userWithdraw.setWithStatus(1);
                    userWithdraw.setTransTime(new Date());
                    userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
                    log.info("代付回调----成功");
                    PrintWriter pw = response.getWriter();
                    pw.print("success");
                    pw.flush();
                    pw.close(); // 验签成功,响应SUCCESS
                    return;
                } else {
                    if (userAssets == null) {
                        log.error("用户资金账户不存在!");
                        PrintWriter pw = response.getWriter();
                        pw.print("Signature Error");
                        pw.flush();
                        pw.close();
                        return;
                    }
                    if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
                        BigDecimal subtract = userWithdraw.getWithAmt().subtract(userAssets.getAmountToBeCovered());
                        if(subtract.compareTo(BigDecimal.ZERO) > 0){
                            userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(subtract));
                            userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(subtract));
                            userAssets.setAmountToBeCovered(BigDecimal.ZERO);
                        }else{
                            userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().subtract(userWithdraw.getWithAmt()));
                        }
                    }else{
                        userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(userWithdraw.getWithAmt()));
                        userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(userWithdraw.getWithAmt()));
                    }
                    int updateCount = userAssetsMapper.updateById(userAssets);
                    if (updateCount > 0) {
                        log.info("提现失败,返还用户资金成功!");
                    } else {
                        log.error("返还用户资金出错,抛出异常");
                    }
                    userWithdraw.setWithMsg("代付验证失败");
                    userWithdraw.setWithStatus(2);
                    userWithdraw.setTransTime(new Date());
                    userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
                    transferResponse.setTradeResult(2);
                    transferResponse.setCallbackState(2);
                    transferResponseService.updateById(transferResponse);
                    PrintWriter pw = response.getWriter();
                    pw.print("Signature Error");
                    pw.flush();
                    pw.close();
                }
    /**
     * 代付接口调用
     */
    private ServerResponse getObjectServerResponseOne(Integer withId, HttpServletRequest request,
                                                       HttpServletResponse response, UserWithdraw userWithdraw,
                                                       User user, UserAssets userAssets) throws Exception {
        try {
            // 配置参数(建议放到配置文件中)
            String appId = "db270b73dc384c89ae7241d6465cee03"; // 从商户后台获取
            String key = "PAXkm5ptBhvzNYBSpdm3p7ipwGUQOF8Fm3tYp0"; // 商户密钥
            String payoutUrl = "https://www.copays.net/open-api/create-payout-order"; // 代付接口地址
            // 生成商户订单号
            String merchantOrderId = generatePayoutOrderId(withId);
            // 处理金额,保留两位小数
            BigDecimal amount = userWithdraw.getWithAmt();
            amount = amount.setScale(2, BigDecimal.ROUND_HALF_UP);
            // 构建请求参数
            Map<String, String> params = new HashMap<>();
            params.put("app_id", appId);
            params.put("merchant_order_id", merchantOrderId);
            params.put("amount", amount.toString());
            params.put("payout_mode", "INDIA_IMPS"); // 代付模式,根据实际情况选择
            params.put("customer_account_type", userWithdraw.getBankAddress()); // 账号类型
            params.put("customer_account_no", userWithdraw.getBankNo()); // 收款人账号(银行卡号或UPI ID)
            params.put("notify_url", "https://api.greenbackcaps.top/user/payoutCallback.do"); // 异步通知地址
            // 生成签名
            String sign = PaymentSignUtil.generateSign(params, key);
            params.put("sign", sign);
            log.info("代付请求参数:{}", params);
            log.info("生成的签名:{}", sign);
            // 发送请求
            String result = HttpClientUtil.doPost(payoutUrl, params, "utf-8");
            log.info("代付返回参数:{}", result);
            // 解析响应
            ObjectMapper objectMapper = new ObjectMapper();
            PayoutOrderResponseVo payoutResponse = objectMapper.readValue(result, PayoutOrderResponseVo.class);
            // 检查响应
            if (!Integer.valueOf(200).equals(payoutResponse.getCode())) {
                log.error("代付下单失败,返回码:{},消息:{},请求参数:{}",
                        payoutResponse.getCode(), payoutResponse.getMessage(), params);
                // 代付失败,返还用户资金
                userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(userWithdraw.getWithAmt()));
                userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(userWithdraw.getWithAmt()));
                userAssetsMapper.updateById(userAssets);
                userWithdraw.setWithStatus(2); // 失败
                userWithdraw.setWithMsg("fail:" + payoutResponse.getMessage());
                userWithdraw.setTransTime(new Date());
                userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
                return ServerResponse.createByErrorMsg("fail:" + payoutResponse.getMessage(),request);
            }
            // 保存代付记录
            PayoutOrderResponseVo.PayoutOrderData responseData = payoutResponse.getData();
            TransferResponse transferResponse = new TransferResponse();
            transferResponse.setMerTransferId(merchantOrderId); // 商户订单号
            transferResponse.setTradeNo(responseData.getSystemOrderId()); // 系统订单号
            transferResponse.setTransferAmount(amount);
            transferResponse.setTradeResult(0); // 0:已下单
            transferResponse.setCallbackState(0); // 0:未处理
            transferResponse.setRespCode("SUCCESS"); // 响应状态:下单成功
            transferResponse.setSignType("MD5"); // 签名方式
            transferResponse.setSign(responseData.getSign()); // 签名
            transferResponse.setApplyDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); // 订单时间
            transferResponse.setUserId(user.getId());
            transferResponse.setWithId(withId);
            transferResponse.setCreatedAt(new Date());
            transferResponse.setUpdatedAt(new Date());
            transferResponseService.save(transferResponse);
            // 更新提现记录状态为已提交(4)
            userWithdraw.setWithStatus(4); // 已提交
            userWithdraw.setTransTime(new Date());
            userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
            log.info("代付下单成功,订单号:{}", merchantOrderId);
            return ServerResponse.createBySuccessMsg("代付申请已提交,请等待处理");
        } catch (Exception e) {
            log.error("代付下单异常:", e);
            // 异常时返还用户资金
            userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(userWithdraw.getWithAmt()));
            userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(userWithdraw.getWithAmt()));
            userAssetsMapper.updateById(userAssets);
            userWithdraw.setWithStatus(2); // 失败
            userWithdraw.setWithMsg("代付下单异常:" + e.getMessage());
            userWithdraw.setTransTime(new Date());
            userWithdrawMapper.updateByPrimaryKeySelective(userWithdraw);
            throw e;
        }
    }
    /**
     * 生成代付订单号
     */
    private String generatePayoutOrderId(Integer withId) {
        // 使用提现ID + 时间戳生成唯一订单号
        long timestamp = System.currentTimeMillis();
        return "PAYOUT_" + withId + "_" + timestamp;
    }
}