MrShi
3 天以前 82f707ece09a906007f3d760b6bb025896e3bba6
server/services/src/main/java/com/doumee/config/wx/WxMiniConfig.java
@@ -3,40 +3,43 @@
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.config.mybatis.SpringUtils;
import com.doumee.core.constants.Constants;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.http.HostName;
import com.wechat.pay.java.core.notification.RSAPublicKeyNotificationConfig;
import com.wechat.pay.java.service.billdownload.BillDownloadService;
import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiService;
import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.refund.RefundService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
/**
 * 微信小程序组件
 */
@Slf4j
@Configuration
public class WxMiniConfig {
    /********微信小程序服务**********/
    public static WxMaService wxMaService;
    /********微信小程序支付**********/
    /********微信小程序支付 V2**********/
    public static WxPayService wxPayService;
    /********微信APP支付**********/
    public static WxPayService wxAppPayService;
    /********微信支付 V3**********/
    public static JsapiServiceExtension v3JsapiService;
    public static RefundService v3RefundService;
    public static NotificationParser v3NotificationParser;
    @Autowired
    private WxPayProperties wxPayProperties;
@@ -48,7 +51,8 @@
    @PostConstruct
    void init() {
        this.load_WxMaService();
        this.load_wxPayService();
//        this.load_wxPayService();
        this.load_wxPayV3Service();
//        this.load_wxAppPayService();
    }
    /**
@@ -59,31 +63,56 @@
        config.setAppid(StringUtils.trimToNull(wxPayProperties.getAppId()));
        config.setSecret(StringUtils.trimToNull(wxPayProperties.getAppSecret()));
        config.setMsgDataFormat("JSON");
        //config.setToken("");
        //config.setAesKey("");
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        this.wxMaService = wxMaService;
    }
    /**
     * 初始化微信小程序支付
     * 初始化微信小程序支付 V2
     */
    public void load_wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setTradeType(WxPayConstants.TradeType.JSAPI);
        payConfig.setSignType(WxPayConstants.SignType.MD5);
        payConfig.setAppId(StringUtils.trimToNull(wxPayProperties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(wxPayProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(wxPayProperties.getMchKey()));
        payConfig.setKeyPath(StringUtils.trimToNull(wxPayProperties.getKeyPath()));
        payConfig.setNotifyUrl(StringUtils.trimToNull(wxPayProperties.getNotifyUrl()));
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        this.wxPayService = wxPayService;
//    public void load_wxPayService() {
//        WxPayConfig payConfig = new WxPayConfig();
//        payConfig.setTradeType(WxPayConstants.TradeType.JSAPI);
//        payConfig.setSignType(WxPayConstants.SignType.MD5);
//        payConfig.setAppId(StringUtils.trimToNull(wxPayProperties.getAppId()));
//        payConfig.setMchId(StringUtils.trimToNull(wxPayProperties.getMchId()));
//        payConfig.setMchKey(StringUtils.trimToNull(wxPayProperties.getMchKey()));
//        payConfig.setKeyPath(StringUtils.trimToNull(wxPayProperties.getKeyPath()));
//        payConfig.setNotifyUrl(StringUtils.trimToNull(wxPayProperties.getNotifyUrl()));
//        WxPayService wxPayService = new WxPayServiceImpl();
//        wxPayService.setConfig(payConfig);
//        this.wxPayService = wxPayService;
//    }
    /**
     * 初始化微信支付 V3(JSAPI + 退款 + 回调验签)
     * 使用平台证书模式(自动下载和管理微信平台证书)
     */
    public void load_wxPayV3Service() {
        try {
            // 从 classpath 读取商户私钥
            ClassPathResource keyResource = new ClassPathResource(StringUtils.trimToNull(wxPayProperties.getPrivateKeyPath()));
            InputStream keyStream = keyResource.getInputStream();
            String privateKey = StreamUtils.copyToString(keyStream, StandardCharsets.UTF_8);
            keyStream.close();
            RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(StringUtils.trimToNull(wxPayProperties.getMchId()))
                    .privateKey(privateKey)
                    .merchantSerialNumber(StringUtils.trimToNull(wxPayProperties.getSerialNumer()))
                    .apiV3Key(StringUtils.trimToNull(wxPayProperties.getApiV3Key()))
                    .build();
            v3JsapiService = new JsapiServiceExtension.Builder().config(config).build();
            v3RefundService = new RefundService.Builder().config(config).build();
            v3NotificationParser = new NotificationParser(config);
            log.info("微信支付V3初始化成功(平台证书模式)");
        } catch (Exception e) {
            log.error("微信支付V3初始化失败: {}", e.getMessage(), e);
        }
    }
//    /**
//     * 初始化App支付