liujie
2025-12-15 c79a494a58c633deb607f2e880f2ea1f6d9eea64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
    }
 
}