luodangjia
2024-11-05 0c569ab4b38d972213eeaf7e8965f33f8ec650fd
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.ruoyi.admin.config;
 
import javax.annotation.PostConstruct;
 
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.java.service.refund.RefundService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
 
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
 
import lombok.Getter;
 
import java.security.PrivateKey;
 
/**
 * @desc: 微信config
 * @author: shy
 * @date: 2024/4/9 10:06
 */
@Configuration
@Getter
public class WeChatConfig {
 
    /**
     * 商户号
     */
    @Value("${wx.mchId}")
    public String merchantId;
    /**
     * 商户API私钥路径
     */
    @Value("${wx.privateKeyPath}")
    public String privateKeyPath;
    /**
     * 商户证书序列号
     */
    @Value("${wx.merchantSerialNumber}")
    public String merchantSerialNumber;
    /**
     * 商户APIV3密钥
     */
    @Value("${wx.apiV3Key}")
    public String apiV3Key;
    /**
     * AppId
     */
    @Value("${wx.appId}")
    public String appId;
 
    private Config config;
 
    @Getter
    private PrivateKey privateKey;
 
 
    @PostConstruct
    public void initConfig() {
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置,否则会因为重复的下载任务报错
        config = new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantId)
                .privateKeyFromPath(privateKeyPath)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3Key)
                .build();
    }
 
    @Primary
    @Bean()
    public H5Service h5Service() {
        return new H5Service.Builder()
                .config(config)
                .build();
    }
 
    @Primary
    @Bean()
    public JsapiService jsapiService() {
        return new JsapiService.Builder()
                .config(config)
                .build();
    }
 
    @Primary
    @Bean()
    public NativePayService nativePayService() {
        return new NativePayService.Builder()
                .config(config)
                .build();
    }
    @Primary
    @Bean()
    public RefundService refundService() {
        return new RefundService.Builder()
                .config(config)
                .build();
    }
 
    @Primary
    @Bean
    public NotificationParser notificationParser() {
        return new NotificationParser((NotificationConfig) config);
    }
 
    @Primary
    @Bean
    public PrivateKeySigner privateKeySigner() {
        return new PrivateKeySigner(merchantSerialNumber, privateKey);
    }
 
 
}