mirror of
https://github.com/dromara/payment-spring-boot.git
synced 2026-03-19 08:53:43 +08:00
exception handler
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package com.enongm.dianji.payment.alipay;
|
||||
|
||||
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.CertAlipayRequest;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
import com.enongm.dianji.payment.PayException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Dax
|
||||
* @since 14:35
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(AliPayProperties.class)
|
||||
public class AliPayConfiguration {
|
||||
|
||||
@Bean
|
||||
public AlipayClient alipayClient(AliPayProperties aliPayProperties) throws AlipayApiException {
|
||||
|
||||
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
|
||||
AliPayProperties.V1 v1 = aliPayProperties.getV1();
|
||||
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
|
||||
propertyMapper.from(v1::getServerUrl).to(certAlipayRequest::setServerUrl);
|
||||
propertyMapper.from(v1::getAppId).to(certAlipayRequest::setAppId);
|
||||
propertyMapper.from(v1::getAppPrivateKeyPath).as(this::appRSAPrivateKey).to(certAlipayRequest::setPrivateKey);
|
||||
propertyMapper.from(v1::getFormat).to(certAlipayRequest::setFormat);
|
||||
propertyMapper.from(v1::getCharset).to(certAlipayRequest::setCharset);
|
||||
propertyMapper.from(v1::getSignType).to(certAlipayRequest::setSignType);
|
||||
propertyMapper.from(v1::getAppCertPublicKeyPath).as(this::getFileAbsolutePath).to(certAlipayRequest::setCertPath);
|
||||
propertyMapper.from(v1::getAlipayPublicCertPath).as(this::getFileAbsolutePath).to(certAlipayRequest::setAlipayPublicCertPath);
|
||||
propertyMapper.from(v1::getAlipayRootCertPath).as(this::getFileAbsolutePath).to(certAlipayRequest::setRootCertPath);
|
||||
return new DefaultAlipayClient(certAlipayRequest);
|
||||
}
|
||||
|
||||
private String getFileAbsolutePath(String classPath) {
|
||||
try {
|
||||
return new ClassPathResource(classPath).getFile().getAbsolutePath();
|
||||
} catch (IOException e) {
|
||||
log.error("ali pay cert path is not exist ,{}", e.getMessage());
|
||||
throw new PayException("ali pay cert path is not exist");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private String appRSAPrivateKey(String classPath) {
|
||||
try {
|
||||
File file = new ClassPathResource(classPath).getFile();
|
||||
return new BufferedReader(new FileReader(file)).readLine();
|
||||
} catch (IOException e) {
|
||||
log.error("ali pay app private key is required ,{}", e.getMessage());
|
||||
throw new PayException("ali pay app private key is required");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.enongm.dianji.payment.alipay;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
|
||||
/**
|
||||
* The type Ali pay properties.
|
||||
*
|
||||
* @author Dax
|
||||
* @since 14 :13
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties("ali.pay")
|
||||
public class AliPayProperties {
|
||||
/**
|
||||
* alipay api version 1.0
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private V1 v1;
|
||||
|
||||
|
||||
/**
|
||||
* The type V 1.
|
||||
*/
|
||||
@Data
|
||||
public static class V1{
|
||||
/**
|
||||
* alipay server
|
||||
*/
|
||||
private String serverUrl = "https://openapi.alipay.com/gateway.do";
|
||||
/**
|
||||
* your app ID
|
||||
*/
|
||||
private String appId;
|
||||
/**
|
||||
* your app private key, which must be in a single line
|
||||
*/
|
||||
private String appPrivateKeyPath;
|
||||
/**
|
||||
* sign type default RSA2
|
||||
*/
|
||||
private String signType = "RSA2";
|
||||
/**
|
||||
* data format only json now
|
||||
*/
|
||||
private String format ="json";
|
||||
/**
|
||||
* charset default utf-8
|
||||
*/
|
||||
private String charset ="utf-8";
|
||||
/**
|
||||
* alipay public cert path
|
||||
*/
|
||||
private String alipayPublicCertPath;
|
||||
/**
|
||||
* alipay root cert path
|
||||
*/
|
||||
private String alipayRootCertPath;
|
||||
/**
|
||||
* appCertPublicKey
|
||||
*/
|
||||
private String appCertPublicKeyPath;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package com.enongm.dianji.payment.alipay;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* The type Ali pay core.
|
||||
*/
|
||||
public class SignatureProvider {
|
||||
|
||||
/**
|
||||
* 生成签名结果
|
||||
*
|
||||
* @param params 要签名的数组
|
||||
* @param key 签名密钥
|
||||
* @param signType 签名类型
|
||||
* @return 签名结果字符串
|
||||
*/
|
||||
public static String requestSign(Map<String, String> params, String key, String signType) {
|
||||
String preStr = createLinkString(params);
|
||||
/* if (SignType.MD5.getType().equals(signType)) {
|
||||
return SecureUtil.md5(preStr.concat(key));
|
||||
}*/
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成要请求给支付宝的参数数组
|
||||
*
|
||||
* @param params 请求前的参数数组
|
||||
* @param key 商户的私钥
|
||||
* @param signType 签名类型
|
||||
* @return 要请求的参数数组 map
|
||||
*/
|
||||
public static Map<String, String> buildRequestPara(Map<String, String> params, String key, String signType) {
|
||||
// 除去数组中的空值和签名参数
|
||||
Map<String, String> tempMap = paraFilter(params);
|
||||
// 生成签名结果
|
||||
String mySign = requestSign(params, key, signType);
|
||||
|
||||
// 签名结果与签名方式加入请求提交参数组中
|
||||
tempMap.put("sign", mySign);
|
||||
tempMap.put("sign_type", signType);
|
||||
|
||||
return tempMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 除去数组中的空值和签名参数
|
||||
*
|
||||
* @param sArray 签名参数组
|
||||
* @return 去掉空值与签名参数后的新签名参数组 map
|
||||
*/
|
||||
public static Map<String, String> paraFilter(Map<String, String> sArray) {
|
||||
Map<String, String> result = new HashMap<String, String>(sArray.size());
|
||||
if (CollectionUtils.isEmpty(sArray)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
for (String key : sArray.keySet()) {
|
||||
String value = sArray.get(key);
|
||||
if (value == null || "".equals(value) || "sign".equalsIgnoreCase(key)
|
||||
|| "sign_type".equalsIgnoreCase(key)) {
|
||||
continue;
|
||||
}
|
||||
result.put(key, value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数自然排序拼接
|
||||
*
|
||||
* @param params {'k':'b','e':'f','c':'d'} -> {'c':'d','e':'f','k':'b'}
|
||||
* @return 拼接后字符串 c=d&e=f&k=b
|
||||
*/
|
||||
public static String createLinkString(Map<String, String> params) {
|
||||
TreeMap<String, String> treeMap = new TreeMap<>(params);
|
||||
|
||||
Set<String> keySet = treeMap.keySet();
|
||||
StringBuilder content = new StringBuilder();
|
||||
for (String key : keySet) {
|
||||
content.append(key).append("=")
|
||||
.append(treeMap.get(key))
|
||||
.append("&");
|
||||
}
|
||||
return content.substring(0, content.length() - 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user