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); } }