ZQN
2024-06-17 b1ff19545212508d3f65741ab889f0b6df82a511
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
package com.project.common.alipay;
 
import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
 
import java.io.IOException;
 
 
//@Configuration
@Slf4j
//@EnableConfigurationProperties(AlipayProperties.class)
public class AlipayConfiguration {
 
//    @Autowired
    private AlipayProperties properties;
 
 
    /**
     * 支付宝接口加签模式为公钥证书时使用
     */
//    @Bean
    public DefaultAlipayClient alipayClient() throws AlipayApiException, IOException {
        log.info("---------创建支付宝客户端--------");
 
        //判断系统环境
        String osName = System.getProperty("os.name");
        log.info("-------系统环境--------" + osName);
        String serverpath = null;
        if (osName.startsWith("Windows")) {
            // windows
            serverpath = ResourceUtils.getURL("classpath:").getPath();
            log.info("-------------win文件路径----------" + serverpath);
//        } else if (osName.startsWith("Mac OS")) {
//            // 苹果
        } else {
            // unix or linux
            serverpath = System.getProperty("user.dir");
            log.info("-------------unix or linux文件路径----------" + serverpath);
        }
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(properties.getGatewayUrl());
        certAlipayRequest.setAppId(properties.getAppid());
        certAlipayRequest.setFormat(properties.getFormate());
        certAlipayRequest.setPrivateKey(properties.getAppPrivateKey());
        certAlipayRequest.setCharset(properties.getCharset());
        certAlipayRequest.setSignType(properties.getSignType());
        certAlipayRequest.setCertPath(serverpath + properties.getAppCertPath());
        certAlipayRequest.setAlipayPublicCertPath(serverpath + properties.getAlipayCertPath());
        certAlipayRequest.setRootCertPath(serverpath + properties.getAlipayRootCertPath());
        return new DefaultAlipayClient(certAlipayRequest);
    }
}