|
@@ -1,5 +1,8 @@
|
|
|
package shop.alien.store.strategy.payment.impl;
|
|
package shop.alien.store.strategy.payment.impl;
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alipay.api.AlipayApiException;
|
|
import com.alipay.api.AlipayApiException;
|
|
|
import com.alipay.api.AlipayClient;
|
|
import com.alipay.api.AlipayClient;
|
|
|
import com.alipay.api.AlipayConfig;
|
|
import com.alipay.api.AlipayConfig;
|
|
@@ -7,25 +10,31 @@ import com.alipay.api.DefaultAlipayClient;
|
|
|
import com.alipay.api.domain.*;
|
|
import com.alipay.api.domain.*;
|
|
|
import com.alipay.api.request.AlipayTradeAppPayRequest;
|
|
import com.alipay.api.request.AlipayTradeAppPayRequest;
|
|
|
import com.alipay.api.request.AlipayTradeQueryRequest;
|
|
import com.alipay.api.request.AlipayTradeQueryRequest;
|
|
|
|
|
+import com.alipay.api.request.AlipayTradeRefundRequest;
|
|
|
import com.alipay.api.response.AlipayTradeAppPayResponse;
|
|
import com.alipay.api.response.AlipayTradeAppPayResponse;
|
|
|
import com.alipay.api.response.AlipayTradeQueryResponse;
|
|
import com.alipay.api.response.AlipayTradeQueryResponse;
|
|
|
|
|
+import com.alipay.api.response.AlipayTradeRefundResponse;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.map.HashedMap;
|
|
import org.apache.commons.collections4.map.HashedMap;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import shop.alien.entity.result.R;
|
|
import shop.alien.entity.result.R;
|
|
|
|
|
+import shop.alien.entity.store.RefundRecord;
|
|
|
|
|
+import shop.alien.entity.store.StoreAliPayRefundLog;
|
|
|
import shop.alien.mapper.AlipayZftCreateRecordMapper;
|
|
import shop.alien.mapper.AlipayZftCreateRecordMapper;
|
|
|
|
|
+import shop.alien.store.service.RefundRecordService;
|
|
|
|
|
+import shop.alien.store.service.StoreAliPayRefundLogService;
|
|
|
import shop.alien.store.strategy.payment.PaymentStrategy;
|
|
import shop.alien.store.strategy.payment.PaymentStrategy;
|
|
|
|
|
+import shop.alien.util.common.UniqueRandomNumGenerator;
|
|
|
import shop.alien.util.common.constant.PaymentEnum;
|
|
import shop.alien.util.common.constant.PaymentEnum;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
import java.math.RoundingMode;
|
|
|
-import java.util.Base64;
|
|
|
|
|
|
|
+import java.text.ParseException;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.*;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-import java.util.Random;
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
@@ -36,6 +45,10 @@ public class AlipayPartnerPaymentStrategyImpl implements PaymentStrategy {
|
|
|
|
|
|
|
|
private final AlipayZftCreateRecordMapper alipayZftCreateRecordMapper;
|
|
private final AlipayZftCreateRecordMapper alipayZftCreateRecordMapper;
|
|
|
|
|
|
|
|
|
|
+ private final StoreAliPayRefundLogService storeAliPayRefundLogService;
|
|
|
|
|
+
|
|
|
|
|
+ private final RefundRecordService refundRecordService;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -203,10 +216,22 @@ public class AlipayPartnerPaymentStrategyImpl implements PaymentStrategy {
|
|
|
|
|
|
|
|
Map<String, Object> data = buildAlipayTradeQueryData(response);
|
|
Map<String, Object> data = buildAlipayTradeQueryData(response);
|
|
|
if (response.isSuccess()) {
|
|
if (response.isSuccess()) {
|
|
|
|
|
+ String tradeStatus = response.getTradeStatus();
|
|
|
|
|
+ if ("TRADE_SUCCESS".equals(tradeStatus)) {
|
|
|
|
|
+ return R.success("支付成功");
|
|
|
|
|
+ } else if ("TRADE_CLOSED".equals(tradeStatus)) {
|
|
|
|
|
+ return R.fail("交易已关闭");
|
|
|
|
|
+ } else if ("WAIT_BUYER_PAY".equals(tradeStatus)) {
|
|
|
|
|
+ return R.fail("等待买家付款");
|
|
|
|
|
+ } else if ("TRADE_FINISHED".equals(tradeStatus)) {
|
|
|
|
|
+ return R.success("交易完成");
|
|
|
|
|
+ }
|
|
|
return R.success("订单状态:" + data.get("tradeStatus"));
|
|
return R.success("订单状态:" + data.get("tradeStatus"));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return R.fail("订单未支付");
|
|
|
}
|
|
}
|
|
|
- String sub = response.getSubMsg() != null ? response.getSubMsg() : "";
|
|
|
|
|
- return R.fail("订单查询失败:" + response.getMsg() + "(" + sub + ")");
|
|
|
|
|
|
|
+// String sub = response.getSubMsg() != null ? response.getSubMsg() : "";
|
|
|
|
|
+// return R.fail("订单查询失败:" + response.getMsg() + "(" + sub + ")");
|
|
|
} catch (AlipayApiException e) {
|
|
} catch (AlipayApiException e) {
|
|
|
log.error("支付宝订单查询异常, transactionId={}", transactionId, e);
|
|
log.error("支付宝订单查询异常, transactionId={}", transactionId, e);
|
|
|
return R.fail("查询异常:" + e.getMessage());
|
|
return R.fail("查询异常:" + e.getMessage());
|
|
@@ -266,9 +291,333 @@ public class AlipayPartnerPaymentStrategyImpl implements PaymentStrategy {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public String handleRefund(Map<String, String> params) throws Exception {
|
|
public String handleRefund(Map<String, String> params) throws Exception {
|
|
|
- return "";
|
|
|
|
|
|
|
+ log.info("处理支付宝退款请求,参数:{}", params);
|
|
|
|
|
+ String refundReslut = "";
|
|
|
|
|
+ // 初始化SDK
|
|
|
|
|
+ AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
|
|
|
|
+
|
|
|
|
|
+ // 构造请求参数以调用接口
|
|
|
|
|
+ AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
|
|
|
|
|
+ AlipayTradeRefundModel model = new AlipayTradeRefundModel();
|
|
|
|
|
+
|
|
|
|
|
+ // 设置商户订单号
|
|
|
|
|
+ model.setOutTradeNo(params.get("outTradeNo"));
|
|
|
|
|
+
|
|
|
|
|
+ // 设置支付宝交易号
|
|
|
|
|
+ model.setTradeNo(params.get(""));
|
|
|
|
|
+
|
|
|
|
|
+ // 设置退款金额
|
|
|
|
|
+ model.setRefundAmount(params.get("refundAmount"));
|
|
|
|
|
+
|
|
|
|
|
+ // 设置退款原因说明
|
|
|
|
|
+ model.setRefundReason(params.get("refundReason"));
|
|
|
|
|
+
|
|
|
|
|
+ // 设置退款请求号
|
|
|
|
|
+// model.setOutRequestNo("HZ01RF001");
|
|
|
|
|
+
|
|
|
|
|
+ // 设置退款包含的商品列表信息
|
|
|
|
|
+// List<RefundGoodsDetail> refundGoodsDetail = new ArrayList<RefundGoodsDetail>();
|
|
|
|
|
+// RefundGoodsDetail refundGoodsDetail0 = new RefundGoodsDetail();
|
|
|
|
|
+// refundGoodsDetail0.setOutSkuId("outSku_01");
|
|
|
|
|
+// refundGoodsDetail0.setOutItemId("outItem_01");
|
|
|
|
|
+// refundGoodsDetail0.setGoodsId("apple-01");
|
|
|
|
|
+// refundGoodsDetail0.setRefundAmount("19.50");
|
|
|
|
|
+// List<String> outCertificateNoList = new ArrayList<String>();
|
|
|
|
|
+// outCertificateNoList.add("202407013232143241231243243423");
|
|
|
|
|
+// refundGoodsDetail0.setOutCertificateNoList(outCertificateNoList);
|
|
|
|
|
+// refundGoodsDetail.add(refundGoodsDetail0);
|
|
|
|
|
+// model.setRefundGoodsDetail(refundGoodsDetail);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置退分账明细信息
|
|
|
|
|
+// List<OpenApiRoyaltyDetailInfoPojo> refundRoyaltyParameters = new ArrayList<OpenApiRoyaltyDetailInfoPojo>();
|
|
|
|
|
+// OpenApiRoyaltyDetailInfoPojo refundRoyaltyParameters0 = new OpenApiRoyaltyDetailInfoPojo();
|
|
|
|
|
+// refundRoyaltyParameters0.setAmount("0.1");
|
|
|
|
|
+// refundRoyaltyParameters0.setTransIn("2088101126708402");
|
|
|
|
|
+// refundRoyaltyParameters0.setRoyaltyType("transfer");
|
|
|
|
|
+// refundRoyaltyParameters0.setTransOut("2088101126765726");
|
|
|
|
|
+// refundRoyaltyParameters0.setTransOutType("userId");
|
|
|
|
|
+// refundRoyaltyParameters0.setRoyaltyScene("达人佣金");
|
|
|
|
|
+// refundRoyaltyParameters0.setTransInType("userId");
|
|
|
|
|
+// refundRoyaltyParameters0.setTransInName("张三");
|
|
|
|
|
+// refundRoyaltyParameters0.setDesc("分账给2088101126708402");
|
|
|
|
|
+// refundRoyaltyParameters.add(refundRoyaltyParameters0);
|
|
|
|
|
+// model.setRefundRoyaltyParameters(refundRoyaltyParameters);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置查询选项
|
|
|
|
|
+// List<String> queryOptions = new ArrayList<String>();
|
|
|
|
|
+// queryOptions.add("refund_detail_item_list");
|
|
|
|
|
+// model.setQueryOptions(queryOptions);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置针对账期交易
|
|
|
|
|
+// model.setRelatedSettleConfirmNo("2024041122001495000530302869");
|
|
|
|
|
+
|
|
|
|
|
+ request.setBizModel(model);
|
|
|
|
|
+ // 第三方代调用模式下请设置app_auth_token
|
|
|
|
|
+ // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");
|
|
|
|
|
+
|
|
|
|
|
+ AlipayTradeRefundResponse response = alipayClient.execute(request);
|
|
|
|
|
+ JSONObject responseBody = JSONObject.parseObject(response.getBody());
|
|
|
|
|
+ JSONObject refundResponse = responseBody.getJSONObject("alipay_trade_refund_response");
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isSuccess()) {
|
|
|
|
|
+ refundReslut = "调用成功";
|
|
|
|
|
+ // 保存退款信息进入到支付宝退款记录表(保留原有逻辑)
|
|
|
|
|
+ StoreAliPayRefundLog refundLog = new StoreAliPayRefundLog();
|
|
|
|
|
+ // 响应基本信息
|
|
|
|
|
+ refundLog.setResponseCode(refundResponse.getString("code"));
|
|
|
|
|
+ refundLog.setResponseMsg(refundResponse.getString("msg"));
|
|
|
|
|
+ // 买家信息
|
|
|
|
|
+ refundLog.setBuyerLogonId(refundResponse.getString("buyer_logon_id"));
|
|
|
|
|
+ refundLog.setBuyerOpenId(refundResponse.getString("buyer_open_id"));
|
|
|
|
|
+ // 资金变动信息
|
|
|
|
|
+ refundLog.setFundChange(refundResponse.getString("fund_change"));
|
|
|
|
|
+ // 订单信息
|
|
|
|
|
+ refundLog.setOutTradeNo(refundResponse.getString("out_trade_no"));
|
|
|
|
|
+ refundLog.setTradeNo(refundResponse.getString("trade_no"));
|
|
|
|
|
+ // 退款金额信息
|
|
|
|
|
+ refundLog.setRefundFee(refundResponse.getString("refund_fee"));
|
|
|
|
|
+ refundLog.setSendBackFee(refundResponse.getString("send_back_fee"));
|
|
|
|
|
+ // 退款渠道明细(转换为JSON字符串)
|
|
|
|
|
+ if (refundResponse.containsKey("refund_detail_item_list")) {
|
|
|
|
|
+ JSONArray refundDetailList = refundResponse.getJSONArray("refund_detail_item_list");
|
|
|
|
|
+ if (refundDetailList != null) {
|
|
|
|
|
+ refundLog.setRefundDetailItemList(JSON.toJSONString(refundDetailList));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 退款时间(字符串转Date)
|
|
|
|
|
+ String gmtRefundPayStr = refundResponse.getString("gmt_refund_pay");
|
|
|
|
|
+ if (StringUtils.isNotBlank(gmtRefundPayStr)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+ refundLog.setGmtRefundPay(sdf.parse(gmtRefundPayStr));
|
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
|
+ log.warn("解析退款时间失败: {}", gmtRefundPayStr, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 退款原因和部分退款编号(从请求参数中获取)
|
|
|
|
|
+ refundLog.setRefundReason(params.get("refundReason"));
|
|
|
|
|
+ refundLog.setOutRequestNo(params.get("partialRefundCode"));
|
|
|
|
|
+ // 证书和签名信息
|
|
|
|
|
+ refundLog.setAlipayCertSn(responseBody.getString("alipay_cert_sn"));
|
|
|
|
|
+ refundLog.setSign(responseBody.getString("sign"));
|
|
|
|
|
+ // 标准字段
|
|
|
|
|
+ refundLog.setDeleteFlag(0);
|
|
|
|
|
+ refundLog.setCreatedTime(new Date());
|
|
|
|
|
+ storeAliPayRefundLogService.save(refundLog);
|
|
|
|
|
+
|
|
|
|
|
+ // 保存到通用退款记录表
|
|
|
|
|
+ try {
|
|
|
|
|
+ RefundRecord refundRecord = buildRefundRecordFromAlipayResponse(refundResponse, responseBody, params);
|
|
|
|
|
+ if (refundRecord != null) {
|
|
|
|
|
+ // 检查是否已存在,避免重复插入
|
|
|
|
|
+ long count = refundRecordService.lambdaQuery()
|
|
|
|
|
+ .eq(RefundRecord::getOutRefundNo, refundRecord.getOutRefundNo())
|
|
|
|
|
+ .count();
|
|
|
|
|
+ if (count == 0) {
|
|
|
|
|
+ refundRecordService.save(refundRecord);
|
|
|
|
|
+ log.info("支付宝退款记录已保存到RefundRecord表,商户退款单号:{}", refundRecord.getOutRefundNo());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("支付宝退款记录已存在,跳过插入,商户退款单号:{}", refundRecord.getOutRefundNo());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("保存支付宝退款记录到RefundRecord表失败", e);
|
|
|
|
|
+ // 不抛出异常,避免影响原有逻辑
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("AliPayConfig.processRefund ERROR Msg={}", response.getBody());
|
|
|
|
|
+ refundReslut = refundResponse.getString("sub_msg");
|
|
|
|
|
+
|
|
|
|
|
+ // 保存失败记录到通用退款记录表
|
|
|
|
|
+ try {
|
|
|
|
|
+ RefundRecord refundRecord = buildRefundRecordFromAlipayError(refundResponse, responseBody, params);
|
|
|
|
|
+ if (refundRecord != null) {
|
|
|
|
|
+ // 检查是否已存在,避免重复插入
|
|
|
|
|
+ long count = refundRecordService.lambdaQuery()
|
|
|
|
|
+ .eq(RefundRecord::getOutRefundNo, refundRecord.getOutRefundNo())
|
|
|
|
|
+ .count();
|
|
|
|
|
+ if (count == 0) {
|
|
|
|
|
+ refundRecordService.save(refundRecord);
|
|
|
|
|
+ log.info("支付宝退款失败记录已保存到RefundRecord表,商户退款单号:{}", refundRecord.getOutRefundNo());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("支付宝退款失败记录已存在,跳过插入,商户退款单号:{}", refundRecord.getOutRefundNo());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("保存支付宝退款失败记录到RefundRecord表失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return refundReslut;
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从支付宝退款响应构建RefundRecord对象(成功情况)
|
|
|
|
|
+ */
|
|
|
|
|
+ private RefundRecord buildRefundRecordFromAlipayResponse(JSONObject refundResponse, JSONObject responseBody, Map<String, String> params) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ RefundRecord record = new RefundRecord();
|
|
|
|
|
+
|
|
|
|
|
+ // 基本信息
|
|
|
|
|
+ record.setPayType(PaymentEnum.ALIPAY.getType());
|
|
|
|
|
+ record.setOutTradeNo(refundResponse.getString("out_trade_no"));
|
|
|
|
|
+ record.setTransactionId(refundResponse.getString("trade_no"));
|
|
|
|
|
+ // 部分退款编号作为商户退款单号,如果没有则生成一个
|
|
|
|
|
+ String outRefundNo = params.get("partialRefundCode");
|
|
|
|
|
+ if (StringUtils.isBlank(outRefundNo)) {
|
|
|
|
|
+ outRefundNo = UniqueRandomNumGenerator.generateUniqueCode(19);
|
|
|
|
|
+ }
|
|
|
|
|
+ record.setOutRefundNo(outRefundNo);
|
|
|
|
|
+ record.setRefundId(null); // 支付宝没有单独的退款单号
|
|
|
|
|
+ record.setRefundStatus("SUCCESS");
|
|
|
|
|
+
|
|
|
|
|
+ // 金额信息(支付宝返回的是元,需要转换为分)
|
|
|
|
|
+ String refundFeeStr = refundResponse.getString("refund_fee");
|
|
|
|
|
+ String sendBackFeeStr = refundResponse.getString("send_back_fee");
|
|
|
|
|
+ if (StringUtils.isNotBlank(refundFeeStr)) {
|
|
|
|
|
+ record.setRefundAmount(new BigDecimal(refundFeeStr).multiply(new BigDecimal(100)).longValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isNotBlank(sendBackFeeStr)) {
|
|
|
|
|
+ record.setActualRefundAmount(new BigDecimal(sendBackFeeStr).multiply(new BigDecimal(100)).longValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ // 订单总金额从params获取,如果没有则从退款金额推算
|
|
|
|
|
+ String totalAmountStr = params.get("totalAmount");
|
|
|
|
|
+ if (StringUtils.isNotBlank(totalAmountStr)) {
|
|
|
|
|
+ record.setTotalAmount(new BigDecimal(totalAmountStr).multiply(new BigDecimal(100)).longValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ record.setCurrency("CNY");
|
|
|
|
|
+
|
|
|
|
|
+ // 退款原因
|
|
|
|
|
+ record.setRefundReason(params.get("refundReason"));
|
|
|
|
|
+
|
|
|
|
|
+ // 退款详情
|
|
|
|
|
+ if (refundResponse.containsKey("refund_detail_item_list")) {
|
|
|
|
|
+ JSONArray refundDetailList = refundResponse.getJSONArray("refund_detail_item_list");
|
|
|
|
|
+ if (refundDetailList != null) {
|
|
|
|
|
+ record.setRefundDetail(JSON.toJSONString(refundDetailList));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 用户收到退款账户
|
|
|
|
|
+ record.setUserReceivedAccount(refundResponse.getString("buyer_logon_id"));
|
|
|
|
|
+
|
|
|
|
|
+ // 退款时间
|
|
|
|
|
+ String gmtRefundPayStr = refundResponse.getString("gmt_refund_pay");
|
|
|
|
|
+ if (StringUtils.isNotBlank(gmtRefundPayStr)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+ Date refundTime = sdf.parse(gmtRefundPayStr);
|
|
|
|
|
+ record.setRefundSuccessTime(refundTime);
|
|
|
|
|
+ record.setRefundCreateTime(refundTime); // 支付宝没有单独的创建时间
|
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
|
+ log.warn("解析退款时间失败: {}", gmtRefundPayStr, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 业务信息(从params获取,如果有的话)
|
|
|
|
|
+ if (params.containsKey("userId")) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ record.setUserId(Integer.parseInt(params.get("userId")));
|
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
|
+ log.warn("解析userId失败: {}", params.get("userId"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (params.containsKey("orderId")) {
|
|
|
|
|
+ record.setOrderId(params.get("orderId"));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (params.containsKey("storeId")) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ record.setStoreId(Integer.parseInt(params.get("storeId")));
|
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
|
+ log.warn("解析storeId失败: {}", params.get("storeId"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 响应数据
|
|
|
|
|
+ record.setResponseData(responseBody.toJSONString());
|
|
|
|
|
+
|
|
|
|
|
+ // 标准字段
|
|
|
|
|
+ record.setDeleteFlag(0);
|
|
|
|
|
+ record.setCreatedTime(new Date());
|
|
|
|
|
+
|
|
|
|
|
+ return record;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("构建RefundRecord对象失败", e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从支付宝退款错误响应构建RefundRecord对象(失败情况)
|
|
|
|
|
+ */
|
|
|
|
|
+ private RefundRecord buildRefundRecordFromAlipayError(JSONObject refundResponse, JSONObject responseBody, Map<String, String> params) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ RefundRecord record = new RefundRecord();
|
|
|
|
|
+
|
|
|
|
|
+ // 基本信息
|
|
|
|
|
+ record.setPayType(PaymentEnum.ALIPAY.getType());
|
|
|
|
|
+ record.setOutTradeNo(params.get("outTradeNo"));
|
|
|
|
|
+ // 部分退款编号作为商户退款单号,如果没有则生成一个
|
|
|
|
|
+ String outRefundNo = params.get("partialRefundCode");
|
|
|
|
|
+ if (StringUtils.isBlank(outRefundNo)) {
|
|
|
|
|
+ outRefundNo = UniqueRandomNumGenerator.generateUniqueCode(19);
|
|
|
|
|
+ }
|
|
|
|
|
+ record.setOutRefundNo(outRefundNo);
|
|
|
|
|
+ record.setRefundStatus("ABNORMAL");
|
|
|
|
|
+
|
|
|
|
|
+ // 金额信息
|
|
|
|
|
+ String refundAmountStr = params.get("refundAmount");
|
|
|
|
|
+ if (StringUtils.isNotBlank(refundAmountStr)) {
|
|
|
|
|
+ record.setRefundAmount(new BigDecimal(refundAmountStr).multiply(new BigDecimal(100)).longValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ String totalAmountStr = params.get("totalAmount");
|
|
|
|
|
+ if (StringUtils.isNotBlank(totalAmountStr)) {
|
|
|
|
|
+ record.setTotalAmount(new BigDecimal(totalAmountStr).multiply(new BigDecimal(100)).longValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ record.setCurrency("CNY");
|
|
|
|
|
+
|
|
|
|
|
+ // 退款原因
|
|
|
|
|
+ record.setRefundReason(params.get("refundReason"));
|
|
|
|
|
+
|
|
|
|
|
+ // 错误信息
|
|
|
|
|
+ record.setErrorCode(refundResponse.getString("code"));
|
|
|
|
|
+ record.setErrorMsg(refundResponse.getString("sub_msg"));
|
|
|
|
|
+
|
|
|
|
|
+ // 业务信息
|
|
|
|
|
+ if (params.containsKey("userId")) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ record.setUserId(Integer.parseInt(params.get("userId")));
|
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
|
+ log.warn("解析userId失败: {}", params.get("userId"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (params.containsKey("orderId")) {
|
|
|
|
|
+ record.setOrderId(params.get("orderId"));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (params.containsKey("storeId")) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ record.setStoreId(Integer.parseInt(params.get("storeId")));
|
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
|
+ log.warn("解析storeId失败: {}", params.get("storeId"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 响应数据
|
|
|
|
|
+ record.setResponseData(responseBody.toJSONString());
|
|
|
|
|
+
|
|
|
|
|
+ // 标准字段
|
|
|
|
|
+ record.setDeleteFlag(0);
|
|
|
|
|
+ record.setCreatedTime(new Date());
|
|
|
|
|
+ record.setRefundCreateTime(new Date());
|
|
|
|
|
+
|
|
|
|
|
+ return record;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("构建RefundRecord对象失败", e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public R searchRefundRecordByOutRefundNo(String outRefundNo) throws Exception {
|
|
public R searchRefundRecordByOutRefundNo(String outRefundNo) throws Exception {
|
|
|
return null;
|
|
return null;
|