SpringBoot 接入支付宝支付-证书模式

满足一下条件
1、注册支付宝商户,实名认证,获取APPID
2、申请支付宝公钥证书
3、有些复杂具体还是按照官方引导进行操作
产品大全
https://b.alipay.com/signing/productSetV2.htm

目前已对接:转账到支付宝账户
https://b.alipay.com/signing/productDetailV2.htm?productId=I1012000291000001000
<dependency>
	<groupId>com.alipay.sdk</groupId>
	<artifactId>alipay-sdk-java</artifactId>
	<version>4.11.54.ALL</version>
</dependency>
package com.b2c.common.alipay.domain;

import java.math.BigDecimal;

/**
 * Created by htl on 2021/3/8 17:28 <br/>
 * The shortest answer is doing. <br/>
 * 阿里支付对象
 * @author htl
 */
public class AlipayRequest {

    // 商户端的唯一订单号,对于同一笔转账请求,商户需保证该订单号唯一。
    private String outBizNo;
    // 订单总金额,单位为元,精确到小数点后两位,取值范围[0.1,100000000]。
    private BigDecimal transAmount;
    // 销售产品码。单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD。
    private String productCode = "TRANS_ACCOUNT_NO_PWD";
    // 业务场景。单笔无密转账固定为 DIRECT_TRANSFER。
    private String bizScene = "DIRECT_TRANSFER";

    // 收款方信息。
    // 参与方的标识 ID,比如支付宝用户 UID。
    private String identity;
    // 参与方的标识类型,目前支持如下枚举:
    // ALIPAY_USER_ID:支付宝的会员 ID。
    // ALIPAY_LOGON_ID:支付宝登录号,支持邮箱和手机号格式。
    private String identityType = "ALIPAY_LOGON_ID";
    // 参与方真实姓名。如果非空,将校验收款支付宝账号姓名一致性。当 identity_type=ALIPAY_LOGON_ID 时,本字段必填。若传入该属性,则在支付宝回单中将会显示这个属性。
    private String name;

    // 转账业务的标题,用于在支付宝用户的账单里显示。
    private String orderTitle;
    // 业务备注
    private String remark;

    // 支付宝转账订单号 (查询转账结果使用)
    private String orderId;

    public String getOutBizNo() {
        return outBizNo;
    }

    public void setOutBizNo(String outBizNo) {
        this.outBizNo = outBizNo;
    }

    public BigDecimal getTransAmount() {
        return transAmount;
    }

    public void setTransAmount(BigDecimal transAmount) {
        this.transAmount = transAmount;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public String getBizScene() {
        return bizScene;
    }

    public void setBizScene(String bizScene) {
        this.bizScene = bizScene;
    }

    public String getIdentity() {
        return identity;
    }

    public void setIdentity(String identity) {
        this.identity = identity;
    }

    public String getIdentityType() {
        return identityType;
    }

    public void setIdentityType(String identityType) {
        this.identityType = identityType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOrderTitle() {
        return orderTitle;
    }

    public void setOrderTitle(String orderTitle) {
        this.orderTitle = orderTitle;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
}
package com.b2c.common.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransOrderQueryRequest;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransOrderQueryResponse;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.b2c.common.alipay.domain.AlipayRequest;
import com.b2c.common.utils.StrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * Created by htl on 2021/3/8 16:57 <br/>
 * The shortest answer is doing. <br/>
 * 阿里支付工具类
 * @author htl
 */
public class AlipayUtils {
    private static Logger logger = LoggerFactory.getLogger(AlipayUtils.class);

    // 应用ID
    private static final String APPID = "应用ID-60766";
    // 应用私钥-RSA2
    private static final String PRIVATE_KEY = "应用私钥-3me6BgkDXjad16dEFoI=";
    // 支付宝公钥-RSA2
    // private static final String ALIPAY_PUBLIC_KEY = "支付宝公钥-MIIBIjANBgkqhkiG9";
	
	// 支付宝证书存放在linux服务器上,所路径位置根据自己项目进行配置即可
    // 您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt
    private static String appCertPublicKey = "/www/server/nginx/conf/cert/appCertPublicKey_2021002114607966.crt";
    // 您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt
    private static String alipayCertPublicKey = "/www/server/nginx/conf/cert/alipayCertPublicKey_RSA2.crt";
    // 您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt
    private static String alipayRootCert = "/www/server/nginx/conf/cert/alipayRootCert.crt";

    /**
     * 单笔转账接口
     * @param alipayRequest 请求参数
     * @return
     * @throws IOException
     * @throws AlipayApiException
     */
    public static AlipayFundTransUniTransferResponse transUniTransferRequest(AlipayRequest alipayRequest) throws IOException, AlipayApiException {
        // 构造client
        DefaultAlipayClient alipayClient = AlipayUtils.createDefaultAlipayClient();

        // 请求对象
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        JSONObject object = new JSONObject();
        // 商户端的唯一订单号,对于同一笔转账请求,商户需保证该订单号唯一。
        object.put("out_biz_no",alipayRequest.getOutBizNo());
        // 订单总金额,单位为元,精确到小数点后两位,取值范围[0.1,100000000]。
        object.put("trans_amount",alipayRequest.getTransAmount().setScale(2,BigDecimal.ROUND_HALF_DOWN));
        // 销售产品码。单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD。
        object.put("product_code",alipayRequest.getProductCode());
        // 业务场景。单笔无密转账固定为 DIRECT_TRANSFER。
        object.put("biz_scene",alipayRequest.getBizScene());

        // 收款方信息。
        JSONObject payeeInfo = new JSONObject();
        // 参与方的标识 ID,比如支付宝用户 UID。
        payeeInfo.put("identity",alipayRequest.getIdentity());
        // 参与方的标识类型,目前支持如下枚举:
        // ALIPAY_USER_ID:支付宝的会员 ID。
        // ALIPAY_LOGON_ID:支付宝登录号,支持邮箱和手机号格式。
        payeeInfo.put("identity_type",alipayRequest.getIdentityType());
        // 参与方真实姓名。如果非空,将校验收款支付宝账号姓名一致性。当 identity_type=ALIPAY_LOGON_ID 时,本字段必填。若传入该属性,则在支付宝回单中将会显示这个属性。
        payeeInfo.put("name",alipayRequest.getName());
        object.put("payee_info",payeeInfo.toJSONString());

        // 转账业务的标题,用于在支付宝用户的账单里显示。
        object.put("order_title",alipayRequest.getOrderTitle());
        // 业务备注
        object.put("remark",alipayRequest.getRemark());

        // 设置请求参数
        request.setBizContent(object.toJSONString());

        // 发送API请求
        // RSA2密钥请求
        // AlipayFundTransOrderQueryResponse response = alipayClient.execute(request);

        // 公钥证书请求请求
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        if(StrUtils.isNull(response)){
            return null;
        }
        return response;
    }


    /**
     * 查询转账订单接口
     * @param alipayRequest 请求对象
     * @return
     * @throws IOException
     * @throws AlipayApiException
     */
    public static AlipayFundTransOrderQueryResponse rransOrderQueryRequest(AlipayRequest alipayRequest) throws IOException, AlipayApiException {
        //构造client
        DefaultAlipayClient alipayClient = AlipayUtils.createDefaultAlipayClient();

        // 创建查询转账订单对象
        AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest();
        // 创建封装请求参数对象
        JSONObject object = new JSONObject();

        // 判断商户订单号、支付宝转账订单号同时为空处理
        if(StrUtils.isNull(alipayRequest.getOutBizNo()) && StrUtils.isNull(alipayRequest.getOrderId())){
            throw new RuntimeException("商户订单号、支付宝转账订单号 不能同时为空!至少有一个不为空即可!");
        }

        // 赋值: 商户订单号
        if(StrUtils.isNotNull(alipayRequest.getOutBizNo())){
            object.put("out_biz_no",alipayRequest.getOutBizNo());
        }

        // 赋值:支付宝转账订单号
        if(StrUtils.isNotNull(alipayRequest.getOrderId())){
            object.put("order_id",alipayRequest.getOrderId());
        }

        // 赋值请求参数
        request.setBizContent(object.toJSONString());
        // RSA2密钥请求
        // AlipayFundTransOrderQueryResponse response = alipayClient.execute(request);

        // 公钥证书请求请求
        AlipayFundTransOrderQueryResponse response = alipayClient.certificateExecute(request);
        if(StrUtils.isNull(response)){
            return null;
        }
         return response;
    }

    /**
     * 创建DefaultAlipayClient对象
     * @return
     * @throws IOException
     * @throws AlipayApiException
     */
    protected static DefaultAlipayClient createDefaultAlipayClient() throws IOException, AlipayApiException {
        // 构造client
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        certAlipayRequest.setAppId(APPID);
        certAlipayRequest.setPrivateKey(PRIVATE_KEY);
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("GBK");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setCertPath(appCertPublicKey);
        certAlipayRequest.setAlipayPublicCertPath(alipayCertPublicKey);
        certAlipayRequest.setRootCertPath(alipayRootCert);
        // DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
        return alipayClient;
    }

//    public static void main(String[] args) throws AlipayApiException, IOException {
//        AlipayRequest request = new AlipayRequest();
//        request.setOutBizNo("2021030818145965987454");// 业务单号
//        request.setTransAmount(new BigDecimal(1.00)); // 转账金额
//        request.setIdentity("136@12350676958"); // 支付宝账号
//        request.setName("李四");                // 真实名字
//        request.setOrderTitle("余额提现");      // 备注新
//        AlipayFundTransUniTransferResponse response = transUniTransferRequest(request);
//        logger.info("{}",JSONObject.toJSONString(response));
//    }

//    public void main(String[] args) throws FileNotFoundException, ServerException {
//       // AlipayRequest request = new AlipayRequest();
//        //request.setOutBizNo("2021030818145965987454");
//        //request.setOrderId("20210309110070001506520064508636");
//        //AlipayFundTransOrderQueryResponse response = rransOrderQueryRequest(request);
//        //logger.info("{}",JSONObject.toJSONString(response));
//    }

}