KKSU
2025-02-05 9e1d5a2b3ef724a4d08d8b3cda0bfabdbc354650
feat(pay): 优化订单支付描述信息

- 新增 getProductNames 方法获取订单商品名称
- 修改支付参数中的 bill_desc 字段,使用商品名称代替订单号
- 优化商品名称的处理逻辑,支持多商品的名称拼接和长度限制
1 files modified
44 ■■■■■ changed files
src/main/java/cc/mrbird/febs/pay/controller/FIUUController.java 44 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/pay/controller/FIUUController.java
@@ -6,16 +6,19 @@
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.ValidateEntityUtils;
import cc.mrbird.febs.mall.entity.MallOrderInfo;
import cc.mrbird.febs.mall.entity.MallOrderItem;
import cc.mrbird.febs.mall.mapper.MallOrderInfoMapper;
import cc.mrbird.febs.pay.model.FIUUInitPayRequest;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@@ -32,6 +35,7 @@
        MallOrderInfo mallOrderInfo = ValidateEntityUtils.ensureColumnReturnEntity(orderId, MallOrderInfo::getId, mallOrderInfoMapper::selectOne, "订单不存在");
        ValidateEntityUtils.ensureEqual(mallOrderInfo.getPayResult(), "1", "订单已支付");
        String amount = mallOrderInfo.getAmount().toString();
        String productNames = getProductNames(mallOrderInfo.getMemberId(), mallOrderInfo.getId());
        try {
            String merchantId = "e2umart01";
            String verifyKey = "4e3a4ed58e62ddbfacf41f6d5ec56bf2";
@@ -53,7 +57,7 @@
            params.put("bill_name", orderRequest.getBuyerName());
            params.put("bill_email", orderRequest.getBuyerEmail());
            params.put("bill_mobile", orderRequest.getBuyerMobile());
            params.put("bill_desc", mallOrderInfo.getOrderNo());
            params.put("bill_desc", productNames);
            params.put("currency", "MYR"); // 默认 MYR
            params.put("vcode", vcode);
            params.put("returnurl", returnUrl);
@@ -103,4 +107,42 @@
            return new FebsResponse().fail().message("Internal Error");
        }
    }
    /**
     * 根据用户ID和订单ID获取所购买商品名称
     * @return 所含商品名称(多个以","隔开)
     */
    public String getProductNames(Long memberId, Long orderId) {
        MallOrderInfo mallOrderInfo = mallOrderInfoMapper.selectOrderByMemberIdAndId(memberId, orderId);
        List<MallOrderItem> details = mallOrderInfo.getItems();
        if (CollectionUtils.isEmpty(details)) {
            return "";
        }
        StringBuffer productNameBuffer = new StringBuffer();
        Integer maxLength = 30;
        for (int i = 0; i< details.size(); i++) {
            MallOrderItem mallOrderItem = details.get(i);
            String goodsName = mallOrderItem.getGoodsName();
            if (goodsName == null) {
                continue;
            }
            if (i == 0 && goodsName.length() > maxLength) {
                productNameBuffer.append(goodsName.substring(0, maxLength) + "...");
                break;
            }
            if ((productNameBuffer.length() + goodsName.length()) > maxLength) {
                productNameBuffer.append("等");
                break;
            }
            productNameBuffer.append(goodsName + ",");
        }
        String productNames = productNameBuffer.toString();
        if (productNames.endsWith(",")) {
            productNames = productNames.substring(0, productNames.length() - 1);
        }
        if (productNames.endsWith(",等")) {
            productNames = productNames.substring(0, productNames.length() - 2) + "等";
        }
        return productNames;
    }
}