依赖
<!-- <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.11.33.ALL</version> </dependency>-->
上代码
package com.yuren.yoga.common.vendor.template; import com.alibaba.fastjson.JSON; import com.alipay.api.*; import com.alipay.api.domain.AlipayTradeAppPayModel; import com.alipay.api.domain.AlipayTradePrecreateModel; import com.alipay.api.domain.AlipayTradeRefundModel; import com.alipay.api.request.*; import com.alipay.api.response.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Random; /** * @author YSM * @user Admin * @data 2021/10/13 * @ProjectName 新建文件夹 */ @Slf4j public class AliPayTemplate { private AliPayProperties aliPayProperties; private String PRODUCT_CODE = "CYCLE_PAY_AUTH"; private String SIGN_SCENE = "INDUSTRY|CARRENTAL"; private String url = "https://openapi.alipay.com/gateway.do"; public AliPayTemplate(AliPayProperties aliPayProperties) { this.aliPayProperties = aliPayProperties; } public CertAlipayRequest getCertAlipayRequest() { CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do"); certAlipayRequest.setAppId(aliPayProperties.getAppId()); certAlipayRequest.setPrivateKey(aliPayProperties.getPrivateKey()); certAlipayRequest.setFormat("json"); certAlipayRequest.setCharset("UTF-8"); certAlipayRequest.setSignType("RSA2"); certAlipayRequest.setCertPath(aliPayProperties.getCertPath() + "appCertPublicKey_" + aliPayProperties.getAppId() + ".crt"); certAlipayRequest.setAlipayPublicCertPath(aliPayProperties.getCertPath() + "alipayCertPublicKey_RSA2.crt"); certAlipayRequest.setRootCertPath(aliPayProperties.getCertPath() + "alipayRootCert.crt"); return certAlipayRequest; } public String getOrder(String name, double price, Double originPrice, String courseId, String userId, String type) { return getOrder(name, price, originPrice, courseId, userId, type, null); } public String getOrder(String name, double price, Double originPrice, String courseId, String userId, String type, String contactPhone) { CertAlipayRequest certAlipayRequest = getCertAlipayRequest(); AlipayClient alipayClient = null; try { alipayClient = new DefaultAlipayClient(certAlipayRequest); } catch (AlipayApiException e) { e.printStackTrace(); } AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); if (originPrice == null || 0 == originPrice) { originPrice = price; } if (StringUtils.isEmpty(contactPhone)) { contactPhone = ""; } model.setBody(courseId + "-" + userId + "-" + type + "-" + originPrice + "-" + contactPhone); model.setSubject("YUREN-" + name); model.setGoodsType("0");//虚拟类商品 String outTradeNo = DateUtils.format(new Date(), "yyyyMMddHHmmss") + new Random().nextInt(8999) + 1000; model.setOutTradeNo(outTradeNo); model.setTimeoutExpress("30m"); model.setTotalAmount(price + ""); model.setProductCode("QUICK_MSECURITY_PAY"); request.setBizModel(model); request.setNotifyUrl(aliPayProperties.getCallbackUrl()); try { AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request); return response.getBody(); } catch (AlipayApiException e) { e.printStackTrace(); } return null; } public AlipayTradePrecreateResponse getQROrder(String name, double price, Double originPrice, String goodsId, String userId, String type) { CertAlipayRequest certAlipayRequest = getCertAlipayRequest(); AlipayClient alipayClient = null; try { alipayClient = new DefaultAlipayClient(certAlipayRequest); } catch (AlipayApiException e) { e.printStackTrace(); } AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); //创建API对应的request类 AlipayTradePrecreateModel model = new AlipayTradePrecreateMode