商品品类
This commit is contained in:
21
src/main/java/com/sczx/pay/utils/AlipayApiCallback.java
Normal file
21
src/main/java/com/sczx/pay/utils/AlipayApiCallback.java
Normal file
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2024 All Rights Reserved.
|
||||
*/
|
||||
package com.sczx.pay.utils;
|
||||
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayResponse;
|
||||
|
||||
/**
|
||||
* @author jishupei.jsp
|
||||
* @version : AlipayApiCallback, v0.1 2024年03月27日 5:46 下午 jishupei.jsp Exp $
|
||||
*/
|
||||
public interface AlipayApiCallback<T, R extends AlipayResponse> {
|
||||
|
||||
R process() throws AlipayApiException;
|
||||
|
||||
T getData(R response);
|
||||
|
||||
String getApiName();
|
||||
}
|
||||
50
src/main/java/com/sczx/pay/utils/AlipayApiTemplate.java
Normal file
50
src/main/java/com/sczx/pay/utils/AlipayApiTemplate.java
Normal file
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2024 All Rights Reserved.
|
||||
*/
|
||||
package com.sczx.pay.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayResponse;
|
||||
import com.sczx.pay.alipay.vo.OpenResponse;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/**
|
||||
* @author jishupei.jsp
|
||||
* @version : AlipayApiTemplate, v0.1 2024年03月27日 5:48 下午 jishupei.jsp Exp $
|
||||
*/
|
||||
public class AlipayApiTemplate {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(AlipayApiTemplate.class);
|
||||
|
||||
public static <T, R extends AlipayResponse> OpenResponse<T> execute(AlipayApiCallback<T, R> callback) {
|
||||
try {
|
||||
//执行
|
||||
R response = callback.process();
|
||||
OpenResponse<T> tOpenResponse = new OpenResponse<>(response);
|
||||
if (response.isSuccess()) {
|
||||
//获取data
|
||||
T data = callback.getData(response);
|
||||
tOpenResponse.setData(data);
|
||||
logger.info(callback.getApiName() + "调用成功:" + response.getBody());
|
||||
} else {
|
||||
logger.error(callback.getApiName() + "调用失败:" + JSON.toJSONString(response));
|
||||
}
|
||||
return tOpenResponse;
|
||||
} catch (AlipayApiException e) {
|
||||
//异常处理
|
||||
logger.error(callback.getApiName() + "调用失败", e);
|
||||
return new OpenResponse<>(e);
|
||||
} catch (Exception e) {
|
||||
//异常处理
|
||||
logger.error(callback.getApiName() + "调用失败", e);
|
||||
OpenResponse<T> response = new OpenResponse<>();
|
||||
response.setCode("SYSTEM_ERROR");
|
||||
response.setMsg("系统错误");
|
||||
response.setSubMsg(e.getMessage());
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
242
src/main/java/com/sczx/pay/utils/AlipaySdkUtil.java
Normal file
242
src/main/java/com/sczx/pay/utils/AlipaySdkUtil.java
Normal file
@ -0,0 +1,242 @@
|
||||
package com.sczx.pay.utils;
|
||||
|
||||
import com.alipay.api.*;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@Configuration
|
||||
public class AlipaySdkUtil {
|
||||
|
||||
private AlipayClient alipayClient;
|
||||
|
||||
@Value("${alipay.appid}")
|
||||
private String appId;
|
||||
|
||||
// 应用私钥
|
||||
@Value("${alipay.privatekey}")
|
||||
private String privateKey;
|
||||
|
||||
// 商户ID(alipay.pid)
|
||||
@Value("${alipay.pid}")
|
||||
private String pid;
|
||||
|
||||
// 是否使用OpenId(alipay.use_open_id)
|
||||
@Value("${alipay.use_open_id}")
|
||||
private boolean useOpenId;
|
||||
|
||||
// 使用OpenId时必填(alipay.openid)
|
||||
@Value("${alipay.openid}")
|
||||
private String openid;
|
||||
|
||||
// 用户ID,和OpenId二选一(alipay.userid)
|
||||
@Value("${alipay.userid}")
|
||||
private String userid;
|
||||
|
||||
// 是否是服务商(alipay.is_isv)
|
||||
@Value("${alipay.is_isv}")
|
||||
private boolean isIsv;
|
||||
|
||||
// 服务商必填(alipay.app_auth_token)
|
||||
@Value("${alipay.app_auth_token}")
|
||||
private String appAuthToken;
|
||||
|
||||
@Value("${alipay.alipay-public-cert-path}")
|
||||
private String alipayPublicCertPath;
|
||||
|
||||
@Value("${alipay.ali-public-cert-path}")
|
||||
private String aliPublicCertPath;
|
||||
|
||||
@Value("${alipay.alipay-root-cert-path}")
|
||||
private String alipayRootCertPath;
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws AlipayApiException {
|
||||
// 初始化v2 SDK
|
||||
this.alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
}
|
||||
|
||||
public <T extends AlipayResponse> T execute(AlipayRequest<T> request) throws AlipayApiException {
|
||||
if (isIsv) {
|
||||
return alipayClient.certificateExecute(request, null, appAuthToken);
|
||||
}
|
||||
return alipayClient.certificateExecute(request);
|
||||
}
|
||||
|
||||
private AlipayConfig getAlipayConfig() {
|
||||
AlipayConfig alipayConfig = new AlipayConfig();
|
||||
alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
|
||||
alipayConfig.setAppId(appId);
|
||||
alipayConfig.setPrivateKey(privateKey);
|
||||
//设置应用公钥证书路径
|
||||
alipayConfig.setAppCertPath(alipayPublicCertPath);
|
||||
//设置支付宝公钥证书路径
|
||||
alipayConfig.setAlipayPublicCertPath(aliPublicCertPath);
|
||||
//设置支付宝根证书路径
|
||||
alipayConfig.setRootCertPath(alipayRootCertPath);
|
||||
alipayConfig.setFormat("json");
|
||||
alipayConfig.setCharset("UTF-8");
|
||||
alipayConfig.setSignType("RSA2");
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("alipay-sdk-demo", "app-item-0.0.1");
|
||||
alipayConfig.setCustomHeaders(headers);
|
||||
|
||||
|
||||
|
||||
return alipayConfig;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>pid</tt>.
|
||||
*
|
||||
* @return property value of pid
|
||||
*/
|
||||
public String getPid() {
|
||||
return pid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>pid</tt>.
|
||||
*
|
||||
* @param pid value to be assigned to property pid
|
||||
*/
|
||||
public void setPid(String pid) {
|
||||
this.pid = pid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>appId</tt>.
|
||||
*
|
||||
* @return property value of appId
|
||||
*/
|
||||
public String getAppId() {
|
||||
return appId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>appId</tt>.
|
||||
*
|
||||
* @param appId value to be assigned to property appId
|
||||
*/
|
||||
public void setAppId(String appId) {
|
||||
this.appId = appId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>openid</tt>.
|
||||
*
|
||||
* @return property value of openid
|
||||
*/
|
||||
public String getOpenid() {
|
||||
return openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>openid</tt>.
|
||||
*
|
||||
* @param openid value to be assigned to property openid
|
||||
*/
|
||||
public void setOpenid(String openid) {
|
||||
this.openid = openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>userid</tt>.
|
||||
*
|
||||
* @return property value of userid
|
||||
*/
|
||||
public String getUserid() {
|
||||
return userid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>userid</tt>.
|
||||
*
|
||||
* @param userid value to be assigned to property userid
|
||||
*/
|
||||
public void setUserid(String userid) {
|
||||
this.userid = userid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>useOpenId</tt>.
|
||||
*
|
||||
* @return property value of useOpenId
|
||||
*/
|
||||
public boolean isUseOpenId() {
|
||||
return useOpenId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>useOpenId</tt>.
|
||||
*
|
||||
* @param useOpenId value to be assigned to property useOpenId
|
||||
*/
|
||||
public void setUseOpenId(boolean useOpenId) {
|
||||
this.useOpenId = useOpenId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>isIsv</tt>.
|
||||
*
|
||||
* @return property value of isIsv
|
||||
*/
|
||||
public boolean isv() {
|
||||
return isIsv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>isIsv</tt>.
|
||||
*
|
||||
* @param isv value to be assigned to property isIsv
|
||||
*/
|
||||
public void setIsv(boolean isv) {
|
||||
isIsv = isv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>appAuthToken</tt>.
|
||||
*
|
||||
* @return property value of appAuthToken
|
||||
*/
|
||||
public String getAppAuthToken() {
|
||||
return appAuthToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>appAuthToken</tt>.
|
||||
*
|
||||
* @param appAuthToken value to be assigned to property appAuthToken
|
||||
*/
|
||||
public void setAppAuthToken(String appAuthToken) {
|
||||
this.appAuthToken = appAuthToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>privateKey</tt>.
|
||||
*
|
||||
* @return property value of privateKey
|
||||
*/
|
||||
public String getPrivateKey() {
|
||||
return privateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>privateKey</tt>.
|
||||
*
|
||||
* @param privateKey value to be assigned to property privateKey
|
||||
*/
|
||||
public void setPrivateKey(String privateKey) {
|
||||
this.privateKey = privateKey;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user