处理退款带证书请求
This commit is contained in:
@ -20,6 +20,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@ -248,27 +249,35 @@ public class WechatPayService {
|
||||
return errorResult;
|
||||
}
|
||||
|
||||
Map<String, String> reqData = new HashMap<>();
|
||||
reqData.put("transaction_id",String.valueOf(paymentRecord.getTransactionId()));
|
||||
reqData.put("out_trade_no", request.getOutTradeNo());
|
||||
reqData.put("out_refund_no", request.getOutRefundNo());
|
||||
reqData.put("total_fee", String.valueOf(paymentRecord.getTotalFee()));
|
||||
reqData.put("refund_fee", String.valueOf(request.getRefundFee()));
|
||||
reqData.put("notify_url",refundNotifyUrl);
|
||||
|
||||
|
||||
// 创建动态配置
|
||||
DynamicWXPayConfig wxPayConfig = new DynamicWXPayConfig(
|
||||
appId,
|
||||
companyConfig.getMchId(),
|
||||
companyConfig.getApikey(),
|
||||
notifyUrl
|
||||
refundNotifyUrl
|
||||
);
|
||||
|
||||
String certPath = "classpath*:cert/"+companyConfig.getMchId()+".p12";
|
||||
String certAbsolutePath = getCertAbsolutePath(certPath);
|
||||
wxPayConfig.setCertPath(certAbsolutePath);
|
||||
|
||||
|
||||
|
||||
WXPay wxPay = new WXPay(wxPayConfig);
|
||||
|
||||
Map<String, String> reqData = new HashMap<>();
|
||||
reqData.put("out_trade_no", request.getOutTradeNo());
|
||||
reqData.put("out_refund_no", request.getOutRefundNo());
|
||||
reqData.put("total_fee", String.valueOf(paymentRecord.getTotalFee()));
|
||||
reqData.put("refund_fee", String.valueOf(request.getRefundFee()));
|
||||
reqData.put("notify_url", refundNotifyUrl);
|
||||
|
||||
|
||||
if (request.getRefundDesc() != null) {
|
||||
reqData.put("refund_desc", request.getRefundDesc());
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 退款需要证书,这里调用带证书的接口
|
||||
Map<String, String> result = wxPay.refund(reqData);
|
||||
@ -509,4 +518,43 @@ public class WechatPayService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取证书文件的绝对路径
|
||||
*/
|
||||
private String getCertAbsolutePath(String certPath) {
|
||||
try {
|
||||
// 如果certPath是绝对路径,直接返回
|
||||
if (new File(certPath).isAbsolute()) {
|
||||
return certPath;
|
||||
}
|
||||
|
||||
// 如果是相对路径,则从classpath下获取
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
java.net.URL resource = classLoader.getResource(certPath);
|
||||
|
||||
if (resource != null) {
|
||||
return resource.getPath();
|
||||
} else {
|
||||
// 如果在classpath中找不到,则尝试从当前类的classpath下获取
|
||||
resource = WechatPayService.class.getClassLoader().getResource(certPath);
|
||||
if (resource != null) {
|
||||
return resource.getPath();
|
||||
} else {
|
||||
// 如果还找不到,则使用默认路径
|
||||
String defaultPath = "cert/1679965261.p12";
|
||||
resource = classLoader.getResource(defaultPath);
|
||||
if (resource != null) {
|
||||
return resource.getPath();
|
||||
} else {
|
||||
throw new RuntimeException("无法找到证书文件: " + certPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("获取证书路径失败", e);
|
||||
throw new RuntimeException("获取证书路径失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user