liujie
2025-12-15 85ed5ccbecbcb6e129fb9e5d08c9f7feb157a29c
ruoyi-system/src/main/java/com/ruoyi/system/utils/ICBCPayUtil.java
@@ -1,80 +1,80 @@
package com.ruoyi.system.utils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.IcbcApiException;
import com.icbc.api.request.CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1;
import com.icbc.api.response.CardbusinessAggregatepayB2cOnlineConsumepurchaseResponseV1;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.system.config.IcbcConfig;
import com.ruoyi.system.enums.ICBCRespOrderErrorEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@Slf4j
@Component
public class ICBCPayUtil {
    @Autowired
    private IcbcConfig icbcConfig;
    public JSONObject wechatPay(String outTradeNo, BigDecimal totalPrice, String goodsName,String openid){
        CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1.CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1Biz bizContent = new
                CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1.CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1Biz();
        bizContent.setMer_id(icbcConfig.getMerId());
        bizContent.setMer_prtcl_no(icbcConfig.getMerPrtclNo());
        bizContent.setDecive_info(icbcConfig.getDeciveInfo());
        bizContent.setOut_trade_no(outTradeNo);
        bizContent.setOrig_date_time(DateUtils.getCurrentDateTimeWithT());
        //交易币种,目前工行只支持使用人民币(001)支付
        bizContent.setFee_type("001");
        bizContent.setSpbill_create_ip(IpUtils.getIpAddr(WebUtils.request()));
        //金额,单位分
        bizContent.setTotal_fee(String.valueOf(totalPrice.multiply(new BigDecimal(100)).intValue()));
        //支付成功后回调url
        bizContent.setMer_url(icbcConfig.getNotifyUrl());
        bizContent.setBody(goodsName);
        //收单接入方式,5-APP,7-微信公众号,8-支付宝生活号,9-微信小程序
        bizContent.setAccess_type("9");
        //支付方式,9-微信;10-支付宝;
        bizContent.setPay_mode("9");
        //商户在微信开放平台注册的APPID,支付方式为微信时不能为空
        bizContent.setShop_appid(icbcConfig.getWxAppId());
        //第三方用户标识,商户在微信公众号内或微信小程序内接入时必送,即access_type为7或9时,上送用户在商户APPID下的唯一标识;商户通过支付宝生活号接入时不送
        bizContent.setOpen_id(openid);
        bizContent.setIcbc_appid(icbcConfig.getAppId());
        bizContent.setExpire_time(icbcConfig.getExpireTime());
        //通知类型,表示在交易处理完成后把交易结果通知商户的处理模式。取值“HS”:在交易完成后将通知信息,主动发送给商户,发送地址为notify_url指定地址; 取值“AG”:在交易完成后不通知商户。不送或送空,默认为"HS"
        bizContent.setNotify_type("HS");
        //结果发送类型,通知方式为HS时有效。取值“0”:无论支付成功或者失败,银行都向商户发送交易通知信息;取值“1”,银行只向商户发送交易成功的通知信息。默认是"0"
        bizContent.setResult_type("0");
        CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1 request = new CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1();
        request.setServiceUrl(icbcConfig.getApiPayUrl());
        request.setBizContent(bizContent);
        DefaultIcbcClient client = IcbcConfig.getInstance(icbcConfig);
        CardbusinessAggregatepayB2cOnlineConsumepurchaseResponseV1 response;
        JSONObject responsePackage = new JSONObject();
        try {
            log.info("准备调用工行支付成功,请求参数:{}", JSON.toJSONString(request));
            response = client.execute(request);
            if (response.isSuccess()) {
                log.info("调用工行支付成功,返回信息:{}", JSON.toJSONString(response));
                responsePackage = JSONObject.parseObject(response.getWx_data_package());
            } else {
                log.info("调用工行支付失败,返回信息:{}", JSON.toJSONString(response));
                throw new ServiceException(ICBCRespOrderErrorEnum.fromCode(String.valueOf(response.getReturnCode())).getErrorMsg());
            }
        } catch (IcbcApiException e) {
            log.error(e.toString(), e);
        }
        return responsePackage;
    }
}
//package com.ruoyi.system.utils;
//
//import com.alibaba.fastjson2.JSON;
//import com.alibaba.fastjson2.JSONObject;
//import com.icbc.api.DefaultIcbcClient;
//import com.icbc.api.IcbcApiException;
//import com.icbc.api.request.CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1;
//import com.icbc.api.response.CardbusinessAggregatepayB2cOnlineConsumepurchaseResponseV1;
//import com.ruoyi.common.exception.ServiceException;
//import com.ruoyi.common.utils.DateUtils;
//import com.ruoyi.common.utils.WebUtils;
//import com.ruoyi.common.utils.ip.IpUtils;
//import com.ruoyi.system.config.IcbcConfig;
//import com.ruoyi.system.enums.ICBCRespOrderErrorEnum;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.math.BigDecimal;
//
//@Slf4j
//@Component
//public class ICBCPayUtil {
//
//    @Autowired
//    private IcbcConfig icbcConfig;
//
//    public JSONObject wechatPay(String outTradeNo, BigDecimal totalPrice, String goodsName,String openid){
//        CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1.CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1Biz bizContent = new
//                CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1.CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1Biz();
//        bizContent.setMer_id(icbcConfig.getMerId());
//        bizContent.setMer_prtcl_no(icbcConfig.getMerPrtclNo());
//        bizContent.setDecive_info(icbcConfig.getDeciveInfo());
//        bizContent.setOut_trade_no(outTradeNo);
//        bizContent.setOrig_date_time(DateUtils.getCurrentDateTimeWithT());
//        //交易币种,目前工行只支持使用人民币(001)支付
//        bizContent.setFee_type("001");
//        bizContent.setSpbill_create_ip(IpUtils.getIpAddr(WebUtils.request()));
//        //金额,单位分
//        bizContent.setTotal_fee(String.valueOf(totalPrice.multiply(new BigDecimal(100)).intValue()));
//        //支付成功后回调url
//        bizContent.setMer_url(icbcConfig.getNotifyUrl());
//        bizContent.setBody(goodsName);
//        //收单接入方式,5-APP,7-微信公众号,8-支付宝生活号,9-微信小程序
//        bizContent.setAccess_type("9");
//        //支付方式,9-微信;10-支付宝;
//        bizContent.setPay_mode("9");
//        //商户在微信开放平台注册的APPID,支付方式为微信时不能为空
//        bizContent.setShop_appid(icbcConfig.getWxAppId());
//        //第三方用户标识,商户在微信公众号内或微信小程序内接入时必送,即access_type为7或9时,上送用户在商户APPID下的唯一标识;商户通过支付宝生活号接入时不送
//        bizContent.setOpen_id(openid);
//        bizContent.setIcbc_appid(icbcConfig.getAppId());
//        bizContent.setExpire_time(icbcConfig.getExpireTime());
//        //通知类型,表示在交易处理完成后把交易结果通知商户的处理模式。取值“HS”:在交易完成后将通知信息,主动发送给商户,发送地址为notify_url指定地址; 取值“AG”:在交易完成后不通知商户。不送或送空,默认为"HS"
//        bizContent.setNotify_type("HS");
//        //结果发送类型,通知方式为HS时有效。取值“0”:无论支付成功或者失败,银行都向商户发送交易通知信息;取值“1”,银行只向商户发送交易成功的通知信息。默认是"0"
//        bizContent.setResult_type("0");
//        CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1 request = new CardbusinessAggregatepayB2cOnlineConsumepurchaseRequestV1();
//        request.setServiceUrl(icbcConfig.getApiPayUrl());
//        request.setBizContent(bizContent);
//        DefaultIcbcClient client = IcbcConfig.getInstance(icbcConfig);
//        CardbusinessAggregatepayB2cOnlineConsumepurchaseResponseV1 response;
//        JSONObject responsePackage = new JSONObject();
//        try {
//            log.info("准备调用工行支付成功,请求参数:{}", JSON.toJSONString(request));
//            response = client.execute(request);
//            if (response.isSuccess()) {
//                log.info("调用工行支付成功,返回信息:{}", JSON.toJSONString(response));
//                responsePackage = JSONObject.parseObject(response.getWx_data_package());
//            } else {
//                log.info("调用工行支付失败,返回信息:{}", JSON.toJSONString(response));
//                throw new ServiceException(ICBCRespOrderErrorEnum.fromCode(String.valueOf(response.getReturnCode())).getErrorMsg());
//            }
//        } catch (IcbcApiException e) {
//            log.error(e.toString(), e);
//        }
//        return responsePackage;
//    }
//
//}