springboot 整合支付宝支付接口demo

1.引入依赖

添加pom.xml的信息

        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.9.79.ALL</version>
        </dependency>

2.配置文件

配置文件:
在application.yml 或者 application.properties

alipay:
  appId: XXXXXX
  serverUrl: https://openapi.alipay.com/gateway.do
  privateKey: XXXX
  publicKey: XXXXX
  format: json
  charset: UTF-8
  signType: RSA2

编写配置信息类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.Serializable;
@ConfigurationProperties(prefix = "pay")
@Data
public class PayConfigProperties implements Serializable {
    private String appId;
    private String privateKey;
    private String publicKey;
    private String serverUrl;
    private String domain;
    private String format;
    private String charset;
    private String signType;
}  
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AntCertificationUtil;
import com.alipay.api.internal.util.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Configuration
@EnableConfigurationProperties(PayConfigProperties.class)
public class AliPayConfig {
    private String activeEnv;
    @Autowired
    private AliPayConfigProperties aliPayConfigProperties;
    @Bean
    public AlipayClient alipayCertClientService() throws Exception{
        AlipayClient alipayClient=new DefaultAlipayClient(aliPayConfigProperties.getServerUrl(), aliPayConfigProperties.getAppId(),aliPayConfigProperties.getPrivateKey(),aliPayConfigProperties.getFormat(),aliPayConfigProperties.getCharset(),aliPayConfigProperties.getPublicKey(),aliPayConfigProperties.getSignType());
        return alipayClient;
    }

    /**
     * 暂无公私钥验证方式
     * @return
     * @throws Exception
     */
//    @Bean
//    public AlipayClient alipayCertClientService() throws Exception{
//        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//        //设置网关地址
//        certAlipayRequest.setServerUrl(aliPayConfigProperties.getServerUrl());
//        //设置应用Id
//        certAlipayRequest.setAppId(aliPayConfigProperties.getAppId());
//        //设置应用私钥
//        certAlipayRequest.setPrivateKey(aliPayConfigProperties.getPrivateKey());
//        //设置请求格式,固定值json
//        certAlipayRequest.setFormat(aliPayConfigProperties.getFormat());
//        //设置字符集
//        certAlipayRequest.setCharset(aliPayConfigProperties.getCharset());
//        //设置签名类型
//        certAlipayRequest.setSignType(aliPayConfigProperties.getSignType());
//        //如果是生产环境或者预演环境,则使用代理模式
//        if ("prod".equals(activeEnv) || "stage".equals(activeEnv) || "test".equals(activeEnv)) {
//            //设置应用公钥证书路径
//            certAlipayRequest.setCertContent(getCertContentByPath(aliPayConfigProperties.getAppCertPath()));
//            //设置支付宝公钥证书路径
//            certAlipayRequest.setAlipayPublicCertContent(getCertContentByPath(aliPayConfigProperties.getAlipayCertPath()));
//            //设置支付宝根证书路径
//            certAlipayRequest.setRootCertContent(getCertContentByPath(aliPayConfigProperties.getAlipayRootCertPath()));
//            certAlipayRequest.setProxyHost(proxyHost);
//            certAlipayRequest.setProxyPort(proxyPort);
//
//        }else {
//            //local
//            String serverPath = this.getClass().getResource("/").getPath();
//            //设置应用公钥证书路径
//            certAlipayRequest.setCertPath(serverPath+aliPayConfigProperties.getAppCertPath());
//            //设置支付宝公钥证书路径
//            certAlipayRequest.setAlipayPublicCertPath(serverPath+aliPayConfigProperties.getAlipayCertPath());
//            //设置支付宝根证书路径
//            certAlipayRequest.setRootCertPath(serverPath+aliPayConfigProperties.getAlipayRootCertPath());
//        }
//        return new DefaultAlipayClient(certAlipayRequest);
//    }
    public String getCertContentByPath(String name){
        InputStream inputStream = null;
        String content = null;
        try{
//            ClassPathResource classPathResource=new ClassPathResource(name);
//            inputStream=classPathResource.getInputStream();
            inputStream = this.getClass().getClassLoader().getResourceAsStream(name);
            content = new String(FileCopyUtils.copyToByteArray(inputStream));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
            if(inputStream!=null){
                inputStream.close();
            }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }
    //readFileToString读取方法
    private static String readFileToString(String rootCertPath) throws AlipayApiException {
        try {
            File file = new File(rootCertPath);
            String client = FileUtils.readFileToString(file);
            return client;
        }catch (IOException e) {
            throw new AlipayApiException(e);
        }
    }
}
上一篇:SpringBoot项目的Liunx服务器部署(一)


下一篇:Spring Boot 接入支付宝,实战来了!