增加支付宝支付功能
This commit is contained in:
		
							
								
								
									
										113
									
								
								src/main/java/com/sczx/pay/config/AlipayConf.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								src/main/java/com/sczx/pay/config/AlipayConf.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,113 @@ | ||||
| package com.sczx.pay.config; | ||||
|  | ||||
|  | ||||
| import com.alipay.api.AlipayApiException; | ||||
| import com.alipay.api.AlipayClient; | ||||
| import com.alipay.api.AlipayConfig; | ||||
| import com.alipay.api.DefaultAlipayClient; | ||||
| import com.sczx.pay.entity.CompanyAlipayConfig; | ||||
| import com.sczx.pay.mapper.CompanyAlipayConfigMapper; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.stereotype.Component; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.Objects; | ||||
|  | ||||
| @Component | ||||
| public class AlipayConf { | ||||
|  | ||||
|     @Value("${ali.pay.app-id}") | ||||
|     private String appId; | ||||
|  | ||||
|     @Value("${alipay.gateway-url}") | ||||
|     private String gatewayUrl; | ||||
|  | ||||
|     @Value("${ali.pay.privateKey}") | ||||
|     private String privateKey; | ||||
|  | ||||
| //    @Value("${alipay.miniapp.publicKey}") | ||||
| //    private String publicKey; | ||||
|  | ||||
|     @Value("${ali.pay.appCertPath}") | ||||
|     private String appCertPath; | ||||
|  | ||||
|     @Value("${ali.pay.alipayCertPath}") | ||||
|     private String alipayCertPath; | ||||
|  | ||||
|     @Value("${ali.pay.alipayRootCertPath}") | ||||
|     private String alipayRootCertPath; | ||||
|  | ||||
|  | ||||
|     @Autowired | ||||
|     private CompanyAlipayConfigMapper companyAlipayConfigMapper; | ||||
|  | ||||
|     private static final Map<String,AlipayClient> apiClientMap = new HashMap<>(); | ||||
|  | ||||
|     private static AlipayClient alipayClient; | ||||
|  | ||||
|     public AlipayClient alipayClient() throws AlipayApiException { | ||||
|  | ||||
|         if(Objects.nonNull(alipayClient)){ | ||||
|             AlipayConfig alipayConfig = new AlipayConfig(); | ||||
|             //设置网关地址 | ||||
|             alipayConfig.setServerUrl(gatewayUrl); | ||||
|             //设置应用ID | ||||
|             alipayConfig.setAppId(appId); | ||||
|             //设置应用私钥 | ||||
|             alipayConfig.setPrivateKey(privateKey); | ||||
|             //设置请求格式,固定值json | ||||
|             alipayConfig.setFormat("JSON"); | ||||
|             //设置字符集 | ||||
|             alipayConfig.setCharset("UTF-8"); | ||||
|             //设置签名类型 | ||||
|             alipayConfig.setSignType("RSA2"); | ||||
|             //设置应用公钥证书路径 | ||||
|             alipayConfig.setAppCertPath(appCertPath); | ||||
|             //设置支付宝公钥证书路径 | ||||
|             alipayConfig.setAlipayPublicCertPath(alipayCertPath); | ||||
|             //设置支付宝根证书路径 | ||||
|             alipayConfig.setRootCertPath(alipayRootCertPath); | ||||
|  | ||||
|             alipayClient = new DefaultAlipayClient(alipayConfig); | ||||
|         } | ||||
|         return alipayClient; | ||||
|     } | ||||
|  | ||||
|     public String getCompanyAppid(Long companyId) throws AlipayApiException { | ||||
|         CompanyAlipayConfig companyAlipayConfig = companyAlipayConfigMapper.getConfigByCompanyId(companyId); | ||||
|         if(Objects.nonNull(companyAlipayConfig)){ | ||||
|             return companyAlipayConfig.getMchId(); | ||||
|         }else { | ||||
|             throw new AlipayApiException("未找到公司对应的支付宝支付配置"); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * 获取支付宝客户端 | ||||
|      * @param companyId | ||||
|      * @param privateKey | ||||
|      * @param alipayPublicKey | ||||
|      * @return | ||||
|      * @throws AlipayApiException | ||||
|      */ | ||||
|     public AlipayClient alipayClient(Long companyId, String privateKey, String alipayPublicKey) throws AlipayApiException { | ||||
|         AlipayClient alipayClient = apiClientMap.get(companyId.toString()); | ||||
|         if(Objects.isNull(alipayClient)){ | ||||
|             AlipayConfig alipayConfig = new AlipayConfig(); | ||||
|             alipayConfig.setServerUrl(gatewayUrl); | ||||
|             alipayConfig.setAppId(appId); | ||||
|             alipayConfig.setPrivateKey(privateKey); | ||||
|             alipayConfig.setFormat("json"); | ||||
|             alipayConfig.setAlipayPublicKey(alipayPublicKey); | ||||
|             alipayConfig.setCharset("UTF-8"); | ||||
|             alipayConfig.setSignType("RSA2"); | ||||
|             alipayClient = new DefaultAlipayClient(alipayConfig); | ||||
|             apiClientMap.put(companyId.toString(),alipayClient); | ||||
|         } | ||||
|  | ||||
|         return alipayClient; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										182
									
								
								src/main/java/com/sczx/pay/controller/AliPaymentController.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										182
									
								
								src/main/java/com/sczx/pay/controller/AliPaymentController.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,182 @@ | ||||
| package com.sczx.pay.controller; | ||||
|  | ||||
| import com.sczx.pay.dto.*; | ||||
| import com.sczx.pay.service.AlipayService; | ||||
| 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; | ||||
|  | ||||
| @Slf4j | ||||
| @RestController | ||||
| @RequestMapping("/api/alipay") | ||||
| public class AliPaymentController { | ||||
|  | ||||
|     @Autowired | ||||
|     private AlipayService alipayService; | ||||
|  | ||||
|     /** | ||||
|      * 支付宝统一下单接口 | ||||
|      */ | ||||
|     @PostMapping("/unifiedOrder") | ||||
|     public AlipayCreateResponse unifiedOrder(@RequestBody AlipayCreateRequest request) { | ||||
|         log.info("收到支付宝支付请求: {}", request); | ||||
|         return alipayService.unifiedOrder(request); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 查询订单接口 | ||||
|      */ | ||||
|     @GetMapping("/query/{companyId}/{outTradeNo}") | ||||
|     public AlipayQueryResponse orderQuery(@PathVariable Long companyId, @PathVariable String outTradeNo) { | ||||
|         log.info("收到支付宝订单查询请求,公司ID: {}, 订单号: {}", companyId, outTradeNo); | ||||
|         return alipayService.orderQuery(companyId, outTradeNo); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 关闭订单接口 | ||||
|      */ | ||||
|     @PostMapping("/close") | ||||
|     public AlipayResponse closeOrder(@RequestBody AlipayCloseRequest request) { | ||||
|         log.info("收到支付宝关闭订单请求: {}", request); | ||||
|         return alipayService.closeOrder(request); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 申请退款接口 | ||||
|      */ | ||||
|     @PostMapping("/refund") | ||||
|     public AlipayResponse refund(@RequestBody AlipayRefundRequest request) { | ||||
|         log.info("收到支付宝退款请求: {}", request); | ||||
|         return alipayService.refund(request); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 查询退款接口 | ||||
|      */ | ||||
|     @PostMapping("/refundQuery") | ||||
|     public AlipayResponse refundQuery(@RequestBody AlipayRefundRequest request) { | ||||
|         log.info("收到支付宝退款查询请求: {}", request); | ||||
|         return alipayService.refundQuery(request); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 支付宝支付结果通知 | ||||
|      */ | ||||
|     @PostMapping("/notify") | ||||
|     public String notify(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"); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 支付宝退款结果通知 | ||||
|      */ | ||||
|     @PostMapping("/refundNotify") | ||||
|     public String refundNotify(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; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayCloseRequest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayCloseRequest.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| package com.sczx.pay.dto; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| // 支付宝查询请求 | ||||
| @Data | ||||
| public class AlipayCloseRequest { | ||||
|     private Long companyId; | ||||
|     private String outTradeNo;      // 商户订单号 | ||||
|     private String reasonCode; //原因编码 | ||||
|     private String reasonDesc; | ||||
|     private String openId;  //买家支付宝用户唯一标识 | ||||
|  | ||||
| } | ||||
							
								
								
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayCreateRequest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayCreateRequest.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| package com.sczx.pay.dto; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| // 支付宝支付请求 | ||||
| @Data | ||||
| public class AlipayCreateRequest { | ||||
|     private Long companyId;         // 公司ID | ||||
|     private String outTradeNo;      // 商户订单号 | ||||
|     private String subject;         // 订单标题 | ||||
|     private String totalAmount;     // 订单总金额 | ||||
|     private String body;            // 订单描述 | ||||
|     private String openId;          // 用户ID | ||||
| } | ||||
							
								
								
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayCreateResponse.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayCreateResponse.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| package com.sczx.pay.dto; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| // 支付宝支付响应 | ||||
| @Data | ||||
| public class AlipayCreateResponse { | ||||
|     private boolean success; | ||||
|     private String message; | ||||
|     private String tradeNo;         // 支付宝交易号 | ||||
|     private String outTradeNo;      // 商户订单号 | ||||
|     private String orderStr;        // 支付串(用于前端调起支付) | ||||
|     private String code;            // 状态码 | ||||
| } | ||||
							
								
								
									
										11
									
								
								src/main/java/com/sczx/pay/dto/AlipayQueryRequest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/main/java/com/sczx/pay/dto/AlipayQueryRequest.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | ||||
| package com.sczx.pay.dto; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| // 支付宝查询请求 | ||||
| @Data | ||||
| public class AlipayQueryRequest { | ||||
|     private Long companyId; | ||||
|     private String outTradeNo;      // 商户订单号 | ||||
|     private String tradeNo;         // 支付宝交易号(可选,与outTradeNo二选一) | ||||
| } | ||||
							
								
								
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayQueryResponse.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayQueryResponse.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| package com.sczx.pay.dto; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| // 支付宝订单查询响应 | ||||
| @Data | ||||
| public class AlipayQueryResponse { | ||||
|     private boolean success; | ||||
|     private String message; | ||||
|     private String tradeNo;         // 支付宝交易号 | ||||
|     private String outTradeNo;      // 商户订单号 | ||||
|     private String tradeStatus;        // 交易状态 | ||||
|     private String code;            // 状态码 | ||||
| } | ||||
							
								
								
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayRefundRequest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/main/java/com/sczx/pay/dto/AlipayRefundRequest.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| package com.sczx.pay.dto; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| // 支付宝退款请求 | ||||
| @Data | ||||
| public class AlipayRefundRequest { | ||||
|     private Long companyId; | ||||
|     private String outTradeNo;      // 商户订单号 | ||||
|     private String refundAmount;    // 退款金额 | ||||
|     private String refundReason;    // 退款原因 | ||||
|     private String outRequestNo;    // 退款请求号 | ||||
|     private String tradeNo;         // 支付宝交易号(可选,与outTradeNo二选一) | ||||
| } | ||||
							
								
								
									
										10
									
								
								src/main/java/com/sczx/pay/dto/AlipayResponse.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/main/java/com/sczx/pay/dto/AlipayResponse.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| package com.sczx.pay.dto; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| @Data | ||||
| public class AlipayResponse { | ||||
|     private boolean success; | ||||
|     private String message; | ||||
|     private String code;            // 状态码 | ||||
| } | ||||
							
								
								
									
										44
									
								
								src/main/java/com/sczx/pay/entity/CompanyAlipayConfig.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/main/java/com/sczx/pay/entity/CompanyAlipayConfig.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,44 @@ | ||||
| package com.sczx.pay.entity; | ||||
|  | ||||
| /** | ||||
|  * 公司支付宝支付配置实体类 | ||||
|  */ | ||||
| public class CompanyAlipayConfig { | ||||
|     private Long id; | ||||
|     private String mchId; | ||||
|     private String apikey; | ||||
|  | ||||
|     // 构造函数 | ||||
|     public CompanyAlipayConfig() {} | ||||
|  | ||||
|     public CompanyAlipayConfig(Long id, String mchId, String apikey) { | ||||
|         this.id = id; | ||||
|         this.mchId = mchId; | ||||
|         this.apikey = apikey; | ||||
|     } | ||||
|      | ||||
|     // Getter和Setter方法 | ||||
|     public Long getId() { | ||||
|         return id; | ||||
|     } | ||||
|      | ||||
|     public void setId(Long id) { | ||||
|         this.id = id; | ||||
|     } | ||||
|      | ||||
|     public String getMchId() { | ||||
|         return mchId; | ||||
|     } | ||||
|      | ||||
|     public void setMchId(String mchId) { | ||||
|         this.mchId = mchId; | ||||
|     } | ||||
|      | ||||
|     public String getApikey() { | ||||
|         return apikey; | ||||
|     } | ||||
|      | ||||
|     public void setApikey(String apikey) { | ||||
|         this.apikey = apikey; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,22 @@ | ||||
| package com.sczx.pay.mapper; | ||||
|  | ||||
| import com.sczx.pay.entity.CompanyAlipayConfig; | ||||
| import org.apache.ibatis.annotations.Mapper; | ||||
| import org.apache.ibatis.annotations.Param; | ||||
| import org.apache.ibatis.annotations.Select; | ||||
|  | ||||
| @Mapper | ||||
| public interface CompanyAlipayConfigMapper { | ||||
|      | ||||
|     /** | ||||
|      * 根据公司ID获取支付宝支付配置 | ||||
|      * @param companyId 公司ID | ||||
|      * @return 微信支付配置信息 | ||||
|      */ | ||||
|     @Select("SELECT id, ali_receiving_account AS mchId, ali_key AS apikey FROM zc_company WHERE id = #{companyId}") | ||||
|     CompanyAlipayConfig getConfigByCompanyId(@Param("companyId") Long companyId); | ||||
|  | ||||
|  | ||||
|     @Select("SELECT id,ali_receiving_account AS mchId, ali_key AS apikey FROM zc_company WHERE ali_receiving_account = #{mchId} limit 1") | ||||
|     CompanyAlipayConfig getCompanyIdByMchId(@Param("mchId") String mchId); | ||||
| } | ||||
							
								
								
									
										47
									
								
								src/main/java/com/sczx/pay/service/AlipayService.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/main/java/com/sczx/pay/service/AlipayService.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,47 @@ | ||||
| package com.sczx.pay.service; | ||||
|  | ||||
| import com.sczx.pay.dto.*; | ||||
|  | ||||
| import java.util.Map; | ||||
|  | ||||
| public interface AlipayService { | ||||
|     /** | ||||
|      * 支付宝统一下单 | ||||
|      */ | ||||
|     AlipayCreateResponse unifiedOrder(AlipayCreateRequest request); | ||||
|  | ||||
|     /** | ||||
|      * 查询订单 | ||||
|      */ | ||||
|     AlipayQueryResponse orderQuery(Long companyId, String outTradeNo); | ||||
|  | ||||
|     /** | ||||
|      * 关闭订单 | ||||
|      */ | ||||
|     AlipayResponse closeOrder(AlipayCloseRequest alipayCloseRequest); | ||||
|  | ||||
|     /** | ||||
|      * 申请退款 | ||||
|      */ | ||||
|     AlipayResponse refund(AlipayRefundRequest request); | ||||
|  | ||||
|     /** | ||||
|      * 查询退款 | ||||
|      */ | ||||
|     AlipayResponse refundQuery(AlipayRefundRequest refundQueryRequest); | ||||
|  | ||||
|     /** | ||||
|      * 验证通知签名 | ||||
|      */ | ||||
|     boolean verifyNotifySign(Long companyId, Map<String, String> params); | ||||
|  | ||||
|     /** | ||||
|      * 处理支付成功通知 | ||||
|      */ | ||||
|     boolean processPaySuccessNotify(Long companyId, Map<String, String> notifyMap); | ||||
|  | ||||
|     /** | ||||
|      * 处理退款通知 | ||||
|      */ | ||||
|     boolean processRefundNotify(Map<String, String> notifyMap); | ||||
| } | ||||
							
								
								
									
										479
									
								
								src/main/java/com/sczx/pay/service/impl/AlipayServiceImpl.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										479
									
								
								src/main/java/com/sczx/pay/service/impl/AlipayServiceImpl.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,479 @@ | ||||
| package com.sczx.pay.service.impl; | ||||
|  | ||||
|  | ||||
| import com.alipay.api.AlipayApiException; | ||||
| import com.alipay.api.AlipayClient; | ||||
| import com.alipay.api.domain.*; | ||||
| import com.alipay.api.internal.util.AlipaySignature; | ||||
| import com.alipay.api.request.*; | ||||
| import com.alipay.api.response.*; | ||||
| import com.sczx.pay.config.AlipayConf; | ||||
| import com.sczx.pay.dto.*; | ||||
| import com.sczx.pay.entity.OrderMain; | ||||
| import com.sczx.pay.entity.PayStatus; | ||||
| import com.sczx.pay.entity.PaymentRecord; | ||||
| import com.sczx.pay.entity.RefundRecord; | ||||
| import com.sczx.pay.mapper.OrderPayMapper; | ||||
| import com.sczx.pay.mapper.PaymentRecordMapper; | ||||
| import com.sczx.pay.mapper.RefundRecordMapper; | ||||
| import com.sczx.pay.service.AlipayService; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.stereotype.Service; | ||||
|  | ||||
| import java.math.BigDecimal; | ||||
| import java.util.Date; | ||||
| import java.util.Map; | ||||
|  | ||||
| @Slf4j | ||||
| @Service | ||||
| public class AlipayServiceImpl implements AlipayService { | ||||
|  | ||||
|     @Autowired | ||||
|     private AlipayConf alipayConf; | ||||
|  | ||||
|     @Value("${ali.pay.app-id}") | ||||
|     private String appId; | ||||
|  | ||||
|     @Value("${ali.pay.notify-url}") | ||||
|     private String notifyUrl; | ||||
|  | ||||
|     @Value("${ali.pay.refund-notify-url}") | ||||
|     private String refundNotifyUrl; | ||||
|  | ||||
|     @Value("${ali.pay.privateKey}") | ||||
|     private String privateKey; | ||||
|  | ||||
| //    @Value("${alipay.miniapp.publicKey}") | ||||
| //    private String publicKey; | ||||
|  | ||||
|     @Value("${ali.pay.appCertPath}") | ||||
|     private String appCertPath; | ||||
|  | ||||
|     @Value("${ali.pay.alipayCertPath}") | ||||
|     private String alipayCertPath; | ||||
|  | ||||
|     @Value("${ali.pay.alipayRootCertPath}") | ||||
|     private String alipayRootCertPath; | ||||
|  | ||||
|  | ||||
|     @Autowired | ||||
|     private PaymentRecordMapper paymentRecordMapper; | ||||
|  | ||||
|     @Autowired | ||||
|     private RefundRecordMapper refundRecordMapper; | ||||
|  | ||||
|     @Autowired | ||||
|     private OrderPayMapper orderPayMapper; | ||||
|  | ||||
|     @Override | ||||
|     public AlipayCreateResponse unifiedOrder(AlipayCreateRequest alipayCreateRequest) { | ||||
|         AlipayCreateResponse response = new AlipayCreateResponse(); | ||||
|         try { | ||||
|             AlipayClient alipayClient = alipayConf.alipayClient(); | ||||
|  | ||||
|             // 构造请求参数以调用接口 | ||||
|             AlipayTradeCreateRequest request = new AlipayTradeCreateRequest(); | ||||
|             AlipayTradeCreateModel model = new AlipayTradeCreateModel(); | ||||
|  | ||||
|             // 设置商户订单号 | ||||
|             model.setOutTradeNo(alipayCreateRequest.getOutTradeNo()); | ||||
|  | ||||
|             // 设置订单总金额 | ||||
|             model.setTotalAmount(alipayCreateRequest.getTotalAmount()); | ||||
|  | ||||
|             // 设置订单标题 | ||||
|             model.setSubject(alipayCreateRequest.getSubject()); | ||||
|  | ||||
|             // 设置订单相对超时时间 | ||||
| //            model.setTimeoutExpress("90m"); | ||||
|  | ||||
|             // uid参数未来计划废弃,存量商户可继续使用,新商户请使用openid。请根据应用-开发配置-openid配置选择支持的字段。 | ||||
|             // model.setBuyerId("2088102146225135"); | ||||
|  | ||||
|             // 设置买家支付宝用户唯一标识 | ||||
|             model.setBuyerOpenId(alipayCreateRequest.getOpenId()); | ||||
|  | ||||
|             model.setOpAppId(alipayConf.getCompanyAppid(alipayCreateRequest.getCompanyId())); | ||||
|  | ||||
|             // 设置产品码 | ||||
|             model.setProductCode("JSAPI_PAY"); | ||||
|  | ||||
|  | ||||
|             request.setBizModel(model); | ||||
|             //设置回调地址 | ||||
|             request.setNotifyUrl(notifyUrl); | ||||
|  | ||||
|             // 第三方代调用模式下请设置app_auth_token | ||||
|             // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->"); | ||||
|  | ||||
|             AlipayTradeCreateResponse alipayResponse = alipayClient.certificateExecute(request); | ||||
|  | ||||
|             if (alipayResponse.isSuccess()) { | ||||
|                 response.setSuccess(true); | ||||
|                 response.setOrderStr(alipayResponse.getBody()); | ||||
|                 response.setOutTradeNo(alipayCreateRequest.getOutTradeNo()); | ||||
|                 response.setTradeNo(alipayResponse.getTradeNo()); | ||||
|                 response.setCode("SUCCESS"); | ||||
|                 response.setMessage("下单成功"); | ||||
|                 //保存支付单 | ||||
|                 recordPaymentInfo(alipayCreateRequest); | ||||
|  | ||||
|             } else { | ||||
|                 response.setSuccess(false); | ||||
|                 response.setCode("FAIL"); | ||||
|                 response.setMessage("下单失败: " + alipayResponse.getMsg() + ":" + alipayResponse.getSubMsg()); | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             log.error("支付宝下单异常,公司ID: {}, 订单号: {}", alipayCreateRequest.getCompanyId(), alipayCreateRequest.getOutTradeNo(), e); | ||||
|             response.setSuccess(false); | ||||
|             response.setCode("FAIL"); | ||||
|             response.setMessage("下单异常: " + e.getMessage()); | ||||
|         } | ||||
|  | ||||
|         return response; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public AlipayQueryResponse orderQuery(Long companyId, String outTradeNo) { | ||||
|         AlipayQueryResponse response = new AlipayQueryResponse(); | ||||
|         try { | ||||
|             AlipayClient alipayClient = alipayConf.alipayClient(); | ||||
|  | ||||
|             // 构造请求参数以调用接口 | ||||
|             // 构造请求参数以调用接口 | ||||
|             AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); | ||||
|             AlipayTradeQueryModel model = new AlipayTradeQueryModel(); | ||||
|  | ||||
|             // 设置订单支付时传入的商户订单号 | ||||
|             model.setOutTradeNo(outTradeNo); | ||||
|  | ||||
|             request.setBizModel(model); | ||||
|  | ||||
|             // 第三方代调用模式下请设置app_auth_token | ||||
|             // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->"); | ||||
|  | ||||
|             AlipayTradeQueryResponse alipayResponse = alipayClient.certificateExecute(request); | ||||
|  | ||||
|             if (alipayResponse.isSuccess()) { | ||||
|                 response.setSuccess(true); | ||||
|                 response.setOutTradeNo(alipayResponse.getOutTradeNo()); | ||||
|                 response.setTradeNo(alipayResponse.getTradeNo()); | ||||
|                 response.setTradeStatus(alipayResponse.getTradeStatus()); | ||||
|                 response.setCode("SUCCESS"); | ||||
|                 response.setMessage("查询支付单成功"); | ||||
|             } else { | ||||
|                 response.setSuccess(false); | ||||
|                 response.setCode("FAIL"); | ||||
|                 response.setMessage("查询支付单失败: " + alipayResponse.getMsg() + ":" + alipayResponse.getSubMsg()); | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             log.error("支付宝订单查询异常,公司ID: {}, 订单号: {}", companyId, outTradeNo, e); | ||||
|             response.setSuccess(false); | ||||
|             response.setCode("FAIL"); | ||||
|             response.setMessage("支付宝订单查询败异常: " + e.getMessage()); | ||||
|         } | ||||
|         return response; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public AlipayResponse closeOrder(AlipayCloseRequest alipayCloseRequest) { | ||||
|         AlipayResponse response = new AlipayResponse(); | ||||
|         try { | ||||
|             AlipayClient alipayClient = alipayConf.alipayClient(); | ||||
|             // 构造请求参数以调用接口 | ||||
|             AlipayTradeCloseRequest request = new AlipayTradeCloseRequest(); | ||||
|             AlipayTradeCloseModel model = new AlipayTradeCloseModel(); | ||||
|  | ||||
|  | ||||
|             // 设置订单支付时传入的商户订单号 | ||||
|             model.setOutTradeNo(alipayCloseRequest.getOutTradeNo()); | ||||
|  | ||||
|             request.setBizModel(model); | ||||
|             // 第三方代调用模式下请设置app_auth_token | ||||
|             // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->"); | ||||
|  | ||||
|             AlipayTradeCloseResponse closeResponse = alipayClient.certificateExecute(request); | ||||
|             log.info("关闭订单响应 : {}",closeResponse.getBody()); | ||||
|  | ||||
|             if (closeResponse.isSuccess()) { | ||||
|                 response.setSuccess(true); | ||||
|                 response.setCode("SUCCESS"); | ||||
|                 response.setMessage("关闭支付单成功"); | ||||
|             } else { | ||||
|                 response.setSuccess(false); | ||||
|                 response.setCode("FAIL"); | ||||
|                 response.setMessage("关闭支付单失败: " + closeResponse.getMsg() + ":" + closeResponse.getSubMsg()); | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             log.error("支付宝关闭异常,公司ID: {}, 订单号: {}", alipayCloseRequest.getCompanyId(), alipayCloseRequest.getOutTradeNo(), e); | ||||
|             response.setSuccess(false); | ||||
|             response.setCode("FAIL"); | ||||
|             response.setMessage("关闭支付单失败异常: " + e.getMessage()); | ||||
|         } | ||||
|         return response; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public AlipayResponse refund(AlipayRefundRequest request) { | ||||
|         AlipayResponse response = new AlipayResponse(); | ||||
|         try { | ||||
|             AlipayClient alipayClient = alipayConf.alipayClient(); | ||||
|             // 构造请求参数以调用接口 | ||||
|             AlipayTradeRefundRequest refundRequest = new AlipayTradeRefundRequest(); | ||||
|             AlipayTradeRefundModel model = new AlipayTradeRefundModel(); | ||||
|  | ||||
|             // 设置商户订单号 | ||||
|             model.setOutTradeNo(request.getOutTradeNo()); | ||||
| // | ||||
| //            // 设置支付宝交易号 | ||||
| //            model.setTradeNo("2014112611001004680073956707"); | ||||
|  | ||||
|             // 设置退款金额 | ||||
|             model.setRefundAmount(request.getRefundAmount()); | ||||
|  | ||||
|             // 设置退款原因说明 | ||||
|             model.setRefundReason(request.getRefundReason()); | ||||
|  | ||||
|             // 设置退款请求号 | ||||
|             model.setOutRequestNo(request.getOutRequestNo()); | ||||
|             refundRequest.setBizModel(model); | ||||
|  | ||||
|             AlipayTradeRefundResponse refundResponse = alipayClient.certificateExecute(refundRequest); | ||||
|             log.info("退款响应 : {}",refundResponse.getBody()); | ||||
|  | ||||
|             if (refundResponse.isSuccess()) { | ||||
|                 response.setSuccess(true); | ||||
|                 response.setCode("SUCCESS"); | ||||
|                 response.setMessage("发起退款成功"); | ||||
|                 //保存退款信息 | ||||
|                 recordRefundInfo(request); | ||||
|             } else { | ||||
|                 response.setSuccess(false); | ||||
|                 response.setCode("FAIL"); | ||||
|                 response.setMessage("退款失败: " + refundResponse.getMsg() + ":" + refundResponse.getSubMsg()); | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             log.error("支付宝退款异常,公司ID: {}, 订单号: {}", request.getCompanyId(), request.getOutTradeNo(), e); | ||||
|             response.setSuccess(false); | ||||
|             response.setCode("FAIL"); | ||||
|             response.setMessage("退款失败异常: " + e.getMessage()); | ||||
|         } | ||||
|         return response; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public AlipayResponse refundQuery(AlipayRefundRequest refundQueryRequest) { | ||||
|         AlipayResponse response = new AlipayResponse(); | ||||
|         try { | ||||
|             AlipayClient alipayClient = alipayConf.alipayClient(); | ||||
|             // 构造请求参数以调用接口 | ||||
|             AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest(); | ||||
|             AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel(); | ||||
|  | ||||
|             // 设置商户订单号 | ||||
|             model.setOutTradeNo(refundQueryRequest.getOutTradeNo()); | ||||
|  | ||||
|             // 设置退款请求号 | ||||
|             model.setOutRequestNo(refundQueryRequest.getOutRequestNo()); | ||||
|  | ||||
|  | ||||
|             request.setBizModel(model); | ||||
|             // 第三方代调用模式下请设置app_auth_token | ||||
|             // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->"); | ||||
|  | ||||
|             AlipayTradeFastpayRefundQueryResponse refundQueryResponse = alipayClient.certificateExecute(request); | ||||
|             log.info("退款查询响应 : {}",refundQueryResponse.getBody()); | ||||
|  | ||||
|             if (refundQueryResponse.isSuccess() && "REFUND_SUCCESS".equals(refundQueryResponse.getRefundStatus())) { | ||||
|                 response.setSuccess(true); | ||||
|                 response.setCode("SUCCESS"); | ||||
|                 response.setMessage("退款成功"); | ||||
|             } else { | ||||
|                 response.setSuccess(false); | ||||
|                 response.setCode("FAIL"); | ||||
|                 response.setMessage("退款查询失败: " + refundQueryResponse.getMsg() + ":" + refundQueryResponse.getSubMsg()); | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             log.error("支付宝退款异常,公司ID: {}, 订单号: {}", refundQueryRequest.getCompanyId(), refundQueryRequest.getOutTradeNo(), e); | ||||
|             response.setSuccess(false); | ||||
|             response.setCode("FAIL"); | ||||
|             response.setMessage("支付宝退款失败异常: " + e.getMessage()); | ||||
|         } | ||||
|         return response; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean verifyNotifySign(Long companyId, Map<String, String> params) { | ||||
|         try { | ||||
|             return AlipaySignature.rsaCertCheckV1(params, alipayCertPath, "UTF-8", "RSA2"); | ||||
|         } catch (AlipayApiException e) { | ||||
|             log.error("支付宝通知签名验证异常,公司ID: {}", companyId, e); | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean processPaySuccessNotify(Long companyId, Map<String, String> notifyMap) { | ||||
|         try { | ||||
|             // 这里实现您的业务逻辑 | ||||
|             // 例如:更新订单状态、发送通知等 | ||||
|  | ||||
|             String outTradeNo = notifyMap.get("out_trade_no"); | ||||
|             String tradeNo = notifyMap.get("trade_no"); | ||||
|             String totalAmount = notifyMap.get("total_amount"); | ||||
|  | ||||
|             log.info("处理支付宝支付成功通知,公司ID: {}, 订单号: {}, 交易号: {}, 金额: {}", | ||||
|                     companyId, outTradeNo, tradeNo, totalAmount); | ||||
|  | ||||
|             // 更新支付记录状态 | ||||
|             int updated = paymentRecordMapper.updateToSuccess( | ||||
|                     outTradeNo, | ||||
|                     tradeNo, | ||||
|                     new Date(), // 支付时间 | ||||
|                     new Date()  // 更新时间 | ||||
|             ); | ||||
|             if (updated > 0) { | ||||
|                 log.info("支付宝支付记录状态已更新,订单号: {}, 支付宝交易号: {}", outTradeNo, tradeNo); | ||||
|                 //更新主订单状态 | ||||
|                 OrderMain orderMain = orderPayMapper.getOrderStatusByOrderNo(outTradeNo); | ||||
|                 String OrderStatus = orderMain.getOrderStatus(); | ||||
|  | ||||
|                 if(OrderStatus.equals("WAIT_PAY")){ | ||||
|                     orderPayMapper.updateOrderStatus(orderMain.getOrderNo(),"WAIT_PICK"); | ||||
|                 }else if (OrderStatus.equals("RERENT_WAIT_PAY")){ | ||||
|                     orderPayMapper.updateOrderStatus(orderMain.getOrderNo(),"RENT_ING"); | ||||
|                 } | ||||
|                 orderPayMapper.updateSubOrderPaymentStatus(outTradeNo,"ZFB_PAY", PayStatus.SUCCESS.getCode(),tradeNo); | ||||
|                 return true; | ||||
|             } else { | ||||
|                 log.warn("未找到对应的支付宝支付记录,订单号: {}", outTradeNo); | ||||
|                 return false; | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             log.error("处理支付宝支付成功通知异常,公司ID: {}", companyId, e); | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean processRefundNotify(Map<String, String> notifyMap) { | ||||
|         try { | ||||
|             String outTradeNo = notifyMap.get("out_trade_no"); | ||||
|             String outRequestNo = notifyMap.get("out_request_no"); | ||||
|             String refundAmount = notifyMap.get("refund_amount"); | ||||
|             String refundStatus = notifyMap.get("refund_status"); | ||||
|  | ||||
|             log.info("处理支付宝退款通知,订单号: {}, 退款请求号: {}, 退款金额: {}", | ||||
|                     outTradeNo, outRequestNo, refundAmount); | ||||
| // | ||||
| // | ||||
| //            String outRefundNo = notifyData.get("out_refund_no"); | ||||
| //            String refundId = notifyData.get("refund_id"); | ||||
| //            String refundStatus = notifyData.get("refund_status"); | ||||
| //            String outTradeNo = notifyData.get("out_trade_no"); | ||||
| //            BigDecimal refundFee = new BigDecimal(notifyData.get("refund_fee")); | ||||
|  | ||||
|             // 根据退款状态更新退款记录 | ||||
|             String statusDesc = ""; | ||||
|             String payStatus = ""; | ||||
|             statusDesc = "退款成功"; | ||||
|             payStatus = "REFUND_SUCCESS"; | ||||
| //            switch (refundStatus) { | ||||
| //                case "SUCCESS": | ||||
| //                    statusDesc = "退款成功"; | ||||
| //                    payStatus = "REFUND_SUCCESS"; | ||||
| //                    break; | ||||
| //                case "REFUNDCLOSE": | ||||
| //                    statusDesc = "退款关闭"; | ||||
| //                    payStatus = "REFUND_SUCCESS"; | ||||
| //                    break; | ||||
| //                case "PROCESSING": | ||||
| //                    statusDesc = "退款处理中"; | ||||
| //                    payStatus = "REFUNDING"; | ||||
| //                    break; | ||||
| //                case "CHANGE": | ||||
| //                    statusDesc = "退款异常"; | ||||
| //                    payStatus = "REFUND_ERROR"; | ||||
| //                    break; | ||||
| //                default: | ||||
| //                    statusDesc = "未知状态"; | ||||
| //                    payStatus = "REFUND_ERROR"; | ||||
| //            } | ||||
|             orderPayMapper.updateSubOrderRefundStatus(outTradeNo,payStatus,new BigDecimal(refundAmount),new Date(),outRequestNo); | ||||
|             int updated = refundRecordMapper.updateRefundStatus( | ||||
|                     outRequestNo, | ||||
|                     refundStatus, | ||||
|                     statusDesc, | ||||
|                     outRequestNo, | ||||
|                     "SUCCESS".equals(refundStatus) ? new Date() : null, // 退款成功时间 | ||||
|                     new Date() // 更新时间 | ||||
|             ); | ||||
|  | ||||
|             if (updated > 0) { | ||||
|                 log.info("支付宝退款记录状态已更新,退款单号: {}, 支付宝退款单号: {}, 状态: {}", outRequestNo, outRequestNo, refundStatus); | ||||
|                 return true; | ||||
|             } else { | ||||
|                 log.warn("未找到对应的微信退款记录,退款单号: {}", outRequestNo); | ||||
|                 return false; | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             log.error("处理微信退款通知异常,退款单号: {}", notifyMap.get("out_request_no"), e); | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 记录支付信息到数据库 | ||||
|      */ | ||||
|     private void recordPaymentInfo(AlipayCreateRequest request) { | ||||
|         try { | ||||
|             PaymentRecord paymentRecord = new PaymentRecord(); | ||||
|             paymentRecord.setCompanyId(request.getCompanyId()); | ||||
|             paymentRecord.setOutTradeNo(request.getOutTradeNo()); | ||||
|             paymentRecord.setTotalFee(new BigDecimal(request.getTotalAmount())); // 转换为元 | ||||
|             paymentRecord.setBody(request.getBody()); | ||||
|             paymentRecord.setOpenid(request.getOpenId()); | ||||
|             paymentRecord.setTradeState("NOTPAY"); // 未支付 | ||||
|             paymentRecord.setTradeStateDesc("未支付"); | ||||
|             paymentRecord.setCreateTime(new Date()); | ||||
|             paymentRecord.setUpdateTime(new Date()); | ||||
| //            paymentRecord.setAttach(request.getAttach()); | ||||
|             paymentRecord.setPayChannel(PaymentRecord.PayChannel.ALIPAY.name()); // 设置支付渠道为支付宝支付 | ||||
|  | ||||
|             paymentRecordMapper.insertPaymentRecord(paymentRecord); | ||||
|             log.info("支付记录已保存,订单号: {}", request.getOutTradeNo()); | ||||
|         } catch (Exception e) { | ||||
|             log.error("保存支付记录异常,订单号: {}", request.getOutTradeNo(), e); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 记录退款信息到数据库 | ||||
|      */ | ||||
|     private void recordRefundInfo(AlipayRefundRequest request) { | ||||
|         try { | ||||
|             RefundRecord refundRecord = new RefundRecord(); | ||||
|             refundRecord.setCompanyId(request.getCompanyId()); | ||||
|             refundRecord.setOutTradeNo(request.getOutTradeNo()); | ||||
|             refundRecord.setOutRefundNo(request.getOutRequestNo()); | ||||
| //            refundRecord.setTotalFee(totalFee); | ||||
|             refundRecord.setRefundFee(new BigDecimal(request.getRefundAmount())); // 转换为元 | ||||
|             refundRecord.setRefundDesc(request.getRefundReason()); | ||||
|             refundRecord.setCreateTime(new Date()); | ||||
|             refundRecord.setUpdateTime(new Date()); | ||||
|  | ||||
|             refundRecord.setRefundStatus("PROCESSING"); // 退款处理中 | ||||
|             refundRecord.setRefundStatusDesc("退款处理中"); | ||||
|             refundRecord.setRefundId(request.getOutRequestNo()); | ||||
|             refundRecord.setPayChannel(PaymentRecord.PayChannel.ALIPAY.name()); | ||||
|  | ||||
|  | ||||
|             refundRecordMapper.insertRefundRecord(refundRecord); | ||||
|             log.info("退款记录已保存,退款单号: {}", request.getOutRequestNo()); | ||||
|         } catch (Exception e) { | ||||
|             log.error("保存退款记录异常,退款单号: {}", request.getOutRequestNo(), e); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user