修改通知接口
This commit is contained in:
@ -0,0 +1,144 @@
|
||||
package com.sczx.pay.controller;
|
||||
|
||||
import com.sczx.pay.service.AlipayService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
@Api(value = "支付宝通知接口", tags = "支付宝支付接口")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/alipay")
|
||||
public class AliPayNotifyController {
|
||||
|
||||
@Autowired
|
||||
private AlipayService alipayService;
|
||||
|
||||
|
||||
/**
|
||||
* 支付宝支付结果通知
|
||||
*/
|
||||
@ApiOperation(value = "支付宝支付结果通知")
|
||||
@PostMapping("/pay/notify")
|
||||
public String alipayNotify(HttpServletRequest request) {
|
||||
try {
|
||||
// 读取支付宝回调数据
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String[]> requestParams = request.getParameterMap();
|
||||
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
|
||||
String name = iter.next();
|
||||
String[] values = requestParams.get(name);
|
||||
String valueStr = "";
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
|
||||
}
|
||||
params.put(name, valueStr);
|
||||
}
|
||||
|
||||
String outTradeNo = params.get("out_trade_no");
|
||||
String companyIdStr = params.get("passback_params"); // 通过回传参数获取公司ID
|
||||
Long companyId = companyIdStr != null ? Long.parseLong(companyIdStr) : null;
|
||||
|
||||
log.info("收到支付宝支付通知,数据: {}, 公司ID: {}, 订单号: {}", params, companyId, outTradeNo);
|
||||
|
||||
// 验证签名
|
||||
if (!alipayService.verifyNotifySign(companyId, params)) {
|
||||
log.warn("支付宝支付通知签名验证失败,公司ID: {}", companyId);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
|
||||
String tradeStatus = params.get("trade_status");
|
||||
if (!"TRADE_SUCCESS".equals(tradeStatus) && !"TRADE_FINISHED".equals(tradeStatus)) {
|
||||
log.warn("支付宝支付通知状态失败,公司ID: {}: {}", companyId, tradeStatus);
|
||||
return buildResponse("success"); // 状态不是成功时也返回success,避免重复通知
|
||||
}
|
||||
|
||||
// 处理支付成功的业务逻辑
|
||||
String tradeNo = params.get("trade_no");
|
||||
String totalAmount = params.get("total_amount");
|
||||
|
||||
// 更新数据库中的订单状态
|
||||
boolean success = alipayService.processPaySuccessNotify(companyId, params);
|
||||
if (success) {
|
||||
log.info("支付宝支付成功,公司ID: {}, 订单号: {}, 支付宝交易号: {}, 金额: {}",
|
||||
companyId, outTradeNo, tradeNo, totalAmount);
|
||||
return buildResponse("success");
|
||||
} else {
|
||||
log.error("更新支付宝支付状态失败,公司ID: {}, 订单号: {}", companyId, outTradeNo);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理支付宝支付通知异常", e);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付宝退款结果通知
|
||||
*/
|
||||
@ApiOperation(value = "支付宝退款结果通知")
|
||||
@PostMapping("/refund/notify")
|
||||
public String alipayRefundNotify(HttpServletRequest request) {
|
||||
try {
|
||||
// 读取支付宝退款回调数据
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String[]> requestParams = request.getParameterMap();
|
||||
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
|
||||
String name = iter.next();
|
||||
String[] values = requestParams.get(name);
|
||||
String valueStr = "";
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
|
||||
}
|
||||
params.put(name, valueStr);
|
||||
}
|
||||
|
||||
log.info("收到支付宝退款通知, 数据: {}", params);
|
||||
|
||||
// 验证签名
|
||||
// 注意:支付宝退款通知的签名验证方式可能与支付通知不同
|
||||
// 这里简化处理,实际应根据支付宝文档实现
|
||||
|
||||
String outTradeNo = params.get("out_trade_no");
|
||||
String refundStatus = params.get("refund_status");
|
||||
|
||||
if (!"REFUND_SUCCESS".equals(refundStatus)) {
|
||||
log.warn("支付宝退款通知状态失败: {}", refundStatus);
|
||||
return buildResponse("success");
|
||||
}
|
||||
|
||||
// 处理退款通知的业务逻辑
|
||||
String outRequestNo = params.get("out_request_no");
|
||||
String refundAmount = params.get("refund_amount");
|
||||
|
||||
// 更新数据库中的退款状态
|
||||
boolean success = alipayService.processRefundNotify(params);
|
||||
if (success) {
|
||||
log.info("支付宝退款处理完成,订单号: {}, 退款请求号: {}, 退款金额: {}, 状态: {}",
|
||||
outTradeNo, outRequestNo, refundAmount, refundStatus);
|
||||
return buildResponse("success");
|
||||
} else {
|
||||
log.error("更新支付宝退款状态失败,订单号: {}, 退款请求号: {}", outTradeNo, outRequestNo);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理支付宝退款通知异常", e);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
}
|
||||
|
||||
private String buildResponse(String result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -8,11 +8,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
@Api(value = "支付宝支付接口", tags = "支付宝支付接口")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@ -72,121 +67,4 @@ public class AliPaymentController {
|
||||
return alipayService.refundQuery(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付宝支付结果通知
|
||||
*/
|
||||
@ApiOperation(value = "支付宝支付结果通知")
|
||||
@PostMapping("/notify")
|
||||
public String alipayNotify(HttpServletRequest request) {
|
||||
try {
|
||||
// 读取支付宝回调数据
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String[]> requestParams = request.getParameterMap();
|
||||
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
|
||||
String name = iter.next();
|
||||
String[] values = requestParams.get(name);
|
||||
String valueStr = "";
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
|
||||
}
|
||||
params.put(name, valueStr);
|
||||
}
|
||||
|
||||
String outTradeNo = params.get("out_trade_no");
|
||||
String companyIdStr = params.get("passback_params"); // 通过回传参数获取公司ID
|
||||
Long companyId = companyIdStr != null ? Long.parseLong(companyIdStr) : null;
|
||||
|
||||
log.info("收到支付宝支付通知,数据: {}, 公司ID: {}, 订单号: {}", params, companyId, outTradeNo);
|
||||
|
||||
// 验证签名
|
||||
if (!alipayService.verifyNotifySign(companyId, params)) {
|
||||
log.warn("支付宝支付通知签名验证失败,公司ID: {}", companyId);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
|
||||
String tradeStatus = params.get("trade_status");
|
||||
if (!"TRADE_SUCCESS".equals(tradeStatus) && !"TRADE_FINISHED".equals(tradeStatus)) {
|
||||
log.warn("支付宝支付通知状态失败,公司ID: {}: {}", companyId, tradeStatus);
|
||||
return buildResponse("success"); // 状态不是成功时也返回success,避免重复通知
|
||||
}
|
||||
|
||||
// 处理支付成功的业务逻辑
|
||||
String tradeNo = params.get("trade_no");
|
||||
String totalAmount = params.get("total_amount");
|
||||
|
||||
// 更新数据库中的订单状态
|
||||
boolean success = alipayService.processPaySuccessNotify(companyId, params);
|
||||
if (success) {
|
||||
log.info("支付宝支付成功,公司ID: {}, 订单号: {}, 支付宝交易号: {}, 金额: {}",
|
||||
companyId, outTradeNo, tradeNo, totalAmount);
|
||||
return buildResponse("success");
|
||||
} else {
|
||||
log.error("更新支付宝支付状态失败,公司ID: {}, 订单号: {}", companyId, outTradeNo);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理支付宝支付通知异常", e);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付宝退款结果通知
|
||||
*/
|
||||
@ApiOperation(value = "支付宝退款结果通知")
|
||||
@PostMapping("/refundNotify")
|
||||
public String alipayRefundNotify(HttpServletRequest request) {
|
||||
try {
|
||||
// 读取支付宝退款回调数据
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String[]> requestParams = request.getParameterMap();
|
||||
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
|
||||
String name = iter.next();
|
||||
String[] values = requestParams.get(name);
|
||||
String valueStr = "";
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
|
||||
}
|
||||
params.put(name, valueStr);
|
||||
}
|
||||
|
||||
log.info("收到支付宝退款通知, 数据: {}", params);
|
||||
|
||||
// 验证签名
|
||||
// 注意:支付宝退款通知的签名验证方式可能与支付通知不同
|
||||
// 这里简化处理,实际应根据支付宝文档实现
|
||||
|
||||
String outTradeNo = params.get("out_trade_no");
|
||||
String refundStatus = params.get("refund_status");
|
||||
|
||||
if (!"REFUND_SUCCESS".equals(refundStatus)) {
|
||||
log.warn("支付宝退款通知状态失败: {}", refundStatus);
|
||||
return buildResponse("success");
|
||||
}
|
||||
|
||||
// 处理退款通知的业务逻辑
|
||||
String outRequestNo = params.get("out_request_no");
|
||||
String refundAmount = params.get("refund_amount");
|
||||
|
||||
// 更新数据库中的退款状态
|
||||
boolean success = alipayService.processRefundNotify(params);
|
||||
if (success) {
|
||||
log.info("支付宝退款处理完成,订单号: {}, 退款请求号: {}, 退款金额: {}, 状态: {}",
|
||||
outTradeNo, outRequestNo, refundAmount, refundStatus);
|
||||
return buildResponse("success");
|
||||
} else {
|
||||
log.error("更新支付宝退款状态失败,订单号: {}, 退款请求号: {}", outTradeNo, outRequestNo);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理支付宝退款通知异常", e);
|
||||
return buildResponse("failure");
|
||||
}
|
||||
}
|
||||
|
||||
private String buildResponse(String result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,6 +40,9 @@ public class AlipayServiceImpl implements AlipayService {
|
||||
@Value("${alipay.notify-url}")
|
||||
private String notifyUrl;
|
||||
|
||||
@Value("${alipay.refund-notify-url}")
|
||||
private String refundNotifyUrl;
|
||||
|
||||
@Value("${alipay.alipay-public-cert-path}")
|
||||
private String alipayPublicCertPath;
|
||||
|
||||
@ -225,6 +228,7 @@ public class AlipayServiceImpl implements AlipayService {
|
||||
// 设置退款请求号
|
||||
model.setOutRequestNo(request.getOutRequestNo());
|
||||
refundRequest.setBizModel(model);
|
||||
refundRequest.setNotifyUrl(refundNotifyUrl);
|
||||
|
||||
AlipayTradeRefundResponse refundResponse = alipaySdkUtil.execute(refundRequest);
|
||||
log.info("退款响应 : {}",refundResponse.getBody());
|
||||
|
||||
@ -89,4 +89,5 @@ alipay:
|
||||
alipay-public-cert-path: /root/cert/appCertPublicKey_2021005174658269.crt
|
||||
ali-public-cert-path: /root/cert/alipayCertPublicKey_RSA2.crt
|
||||
alipay-root-cert-path: /root/cert/alipayRootCert.crt
|
||||
notify-url: https://www.minbo.wang:8020/api/alipay/notify
|
||||
notify-url: http://115.190.8.52:8018/alipay/pay/notify
|
||||
refund-notify-url: http://115.190.8.52:8018/alipay/refund/notify
|
||||
|
||||
Reference in New Issue
Block a user