2
0

feat: payment module support notify

This commit is contained in:
zhangsz
2025-01-07 19:35:38 +08:00
parent b47e403fbd
commit 03e6bd043a
29 changed files with 621 additions and 73 deletions

View File

@@ -4,7 +4,11 @@ import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@@ -12,6 +16,7 @@ import org.wfc.payment.alipay.service.IAlipayPaymentService;
import org.wfc.payment.alipay.service.IAlipayQueryOrderService;
import org.wfc.payment.alipay.service.IAlipayTradeCloseService;
import org.wfc.payment.alipay.service.IAlipayRefundService;
import org.wfc.payment.alipay.service.IAlipayNotifyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
@@ -72,4 +77,17 @@ public class AlipayController {
return null;
}
}
private final IAlipayNotifyService alipayNotifyService;
@PostMapping("/notify")
public String handleAlipayNotify(@RequestParam Map<String, String> params) {
try {
boolean result = alipayNotifyService.handleAlipayNotify(params);
return result ? "success" : "failure";
} catch (AlipayApiException e) {
e.printStackTrace();
return "failure";
}
}
}

View File

@@ -0,0 +1,17 @@
package org.wfc.payment.alipay.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.wfc.payment.alipay.model.UAlipayOrderModel;
@Mapper
public interface UAlipayOrderMapper {
void insertOrder(UAlipayOrderModel uAlipayOrder);
void updateOrder(UAlipayOrderModel uAlipayOrder);
UAlipayOrderModel selectOrderByOutTradeNo(@Param("outTradeNo") String outTradeNo);
UAlipayOrderModel selectOrderById(@Param("id") Long id);
}

View File

@@ -0,0 +1,26 @@
package org.wfc.payment.alipay.model;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
import lombok.Data;
@Data
@Entity
public class UAlipayOrderModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;
private String outTradeNo;
private String tradeStatus;
private BigDecimal totalAmount;
private String subject;
private String body;
private String createBy;
private Date createTime;
private String updateBy;
private Date updateTime;
// Getters and Setters
}

View File

@@ -0,0 +1,9 @@
package org.wfc.payment.alipay.service;
import java.util.Map;
import com.alipay.api.AlipayApiException;
public interface IAlipayNotifyService {
boolean handleAlipayNotify(Map<String, String> params) throws AlipayApiException;
}

View File

@@ -0,0 +1,60 @@
package org.wfc.payment.alipay.service.impl;
import java.math.BigDecimal;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wfc.payment.alipay.config.AlipayConfig;
import org.wfc.payment.alipay.mapper.UAlipayOrderMapper;
import org.wfc.payment.alipay.model.UAlipayOrderModel;
import org.wfc.payment.alipay.service.IAlipayNotifyService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import java.util.Date;
@Service
public class AlipayNotifyServiceImpl implements IAlipayNotifyService {
private final AlipayConfig alipayConfig;
private final UAlipayOrderMapper alipayOrderMapper;
@Autowired
public AlipayNotifyServiceImpl(UAlipayOrderMapper alipayOrderMapper, AlipayConfig alipayConfig) {
this.alipayOrderMapper = alipayOrderMapper;
this.alipayConfig = alipayConfig;
}
public boolean handleAlipayNotify(Map<String, String> params) throws AlipayApiException {
boolean signVerified = AlipaySignature.rsaCheckV1(params,
alipayConfig.getPublicKey(),
alipayConfig.getCharset(),
alipayConfig.getSignType());
if (signVerified) {
// 处理业务逻辑,例如更新订单状态
String outTradeNo = params.get("out_trade_no");
String tradeStatus = params.get("trade_status");
BigDecimal totalAmount = new BigDecimal(params.get("total_amount"));
String subject = params.get("subject");
String body = params.get("body");
UAlipayOrderModel order = alipayOrderMapper.selectOrderByOutTradeNo(outTradeNo);
if (order == null) {
order = new UAlipayOrderModel();
order.setOutTradeNo(outTradeNo);
order.setCreateTime(new Date());
alipayOrderMapper.insertOrder(order);
}
order.setTradeStatus(tradeStatus);
order.setTotalAmount(totalAmount);
order.setSubject(subject);
order.setBody(body);
order.setUpdateTime(new Date());
alipayOrderMapper.updateOrder(order);
return "TRADE_SUCCESS".equals(tradeStatus);
}
return false;
}
}

View File

@@ -1,6 +1,5 @@
package org.wfc.payment.alipay.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
@@ -8,12 +7,13 @@ import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.wfc.payment.alipay.config.AlipayConfig;
import org.wfc.payment.alipay.service.IAlipayPaymentService;
import lombok.AllArgsConstructor;
@Service
@AllArgsConstructor
public class AlipayPaymentServiceImpl implements IAlipayPaymentService {
@Autowired
private AlipayConfig alipayConfig;
private final AlipayConfig alipayConfig;
@Override
public String createPayment(String outTradeNo, String totalAmount, String subject, String body)

View File

@@ -1,20 +1,22 @@
package org.wfc.payment.alipay.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import lombok.AllArgsConstructor;
import org.wfc.payment.alipay.config.AlipayConfig;
import org.wfc.payment.alipay.service.IAlipayQueryOrderService;
@Service
@AllArgsConstructor
public class AlipayQueryOrderServiceImpl implements IAlipayQueryOrderService {
@Autowired
private AlipayConfig alipayConfig;
private final AlipayConfig alipayConfig;
@Override
public AlipayTradeQueryResponse queryOrder(String outTradeNo) throws AlipayApiException {

View File

@@ -7,14 +7,17 @@ import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import lombok.AllArgsConstructor;
import org.wfc.payment.alipay.config.AlipayConfig;
import org.wfc.payment.alipay.service.IAlipayRefundService;
@Service
@AllArgsConstructor
public class AlipayRefundServiceImpl implements IAlipayRefundService {
@Autowired
private AlipayConfig alipayConfig;
private final AlipayConfig alipayConfig;
@Override
public AlipayTradeRefundResponse refundOrder(String outTradeNo, String refundAmount, String refundReason)

View File

@@ -1,6 +1,5 @@
package org.wfc.payment.alipay.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
@@ -9,12 +8,13 @@ import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import org.wfc.payment.alipay.config.AlipayConfig;
import org.wfc.payment.alipay.service.IAlipayTradeCloseService;
import lombok.AllArgsConstructor;
@Service
@AllArgsConstructor
public class AlipayTradeCloseServiceImpl implements IAlipayTradeCloseService {
@Autowired
private AlipayConfig alipayConfig;
private final AlipayConfig alipayConfig;
@Override
public AlipayTradeCloseResponse closeOrder(String outTradeNo) throws AlipayApiException {

View File

@@ -8,7 +8,7 @@ import org.wfc.payment.ccpay.model.PaymentResponse;
import org.wfc.payment.ccpay.model.UCreditCardToken;
import org.wfc.payment.ccpay.model.RefundResponse;
import org.wfc.payment.ccpay.model.RefundRequest;
import org.wfc.payment.ccpay.service.IUCreditCardPaymentService;
import org.wfc.payment.ccpay.service.IUCreditCardOrderService;
import org.wfc.payment.ccpay.service.IUCreditCardTokenService;
/**
@@ -19,11 +19,11 @@ import org.wfc.payment.ccpay.service.IUCreditCardTokenService;
@RequestMapping("/ccpay")
public class CcpayController {
private final IUCreditCardPaymentService paymentService;
private final IUCreditCardOrderService paymentService;
private final IUCreditCardTokenService tokenService;
@Autowired
public CcpayController(IUCreditCardPaymentService paymentService, IUCreditCardTokenService tokenService) {
public CcpayController(IUCreditCardOrderService paymentService, IUCreditCardTokenService tokenService) {
this.paymentService = paymentService;
this.tokenService = tokenService;
}

View File

@@ -0,0 +1,15 @@
package org.wfc.payment.ccpay.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.wfc.payment.ccpay.model.UCreditCardOrder;
@Mapper
public interface UCreditCardOrderMapper {
void insertOrder(UCreditCardOrder order);
UCreditCardOrder selectOrderById(@Param("id") Long id);
// 其他需要的方法
}

View File

@@ -1,15 +0,0 @@
package org.wfc.payment.ccpay.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.wfc.payment.ccpay.model.UCreditCardPayment;
@Mapper
public interface UCreditCardPaymentMapper {
void insertPayment(UCreditCardPayment payment);
UCreditCardPayment selectPaymentById(@Param("id") Long id);
// 其他需要的方法
}

View File

@@ -5,8 +5,8 @@ import java.math.BigDecimal;
import java.time.LocalDateTime;
@Entity
@Table(name = "u_credit_card_payment")
public class UCreditCardPayment {
@Table(name = "u_credit_card_order")
public class UCreditCardOrder {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

View File

@@ -4,9 +4,9 @@ import org.wfc.payment.ccpay.model.PaymentRequest;
import org.wfc.payment.ccpay.model.PaymentResponse;
import org.wfc.payment.ccpay.model.RefundRequest;
import org.wfc.payment.ccpay.model.RefundResponse;
import org.wfc.payment.ccpay.model.UCreditCardPayment;
import org.wfc.payment.ccpay.model.UCreditCardOrder;
public interface IUCreditCardPaymentService {
public interface IUCreditCardOrderService {
PaymentResponse processPayment(PaymentRequest paymentRequest);
@@ -14,9 +14,9 @@ public interface IUCreditCardPaymentService {
RefundResponse refundOrder(RefundRequest refundRequest);
void savePayment(UCreditCardPayment payment);
void saveOrder(UCreditCardOrder order);
UCreditCardPayment getPaymentById(Long id);
UCreditCardOrder getOrderById(Long id);
String getPaymentToken(PaymentRequest paymentRequest);

View File

@@ -9,41 +9,41 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.wfc.payment.ccpay.config.CcpayConfig;
import org.wfc.payment.ccpay.mapper.UCreditCardPaymentMapper;
import org.wfc.payment.ccpay.mapper.UCreditCardOrderMapper;
import org.wfc.payment.ccpay.model.PaymentRequest;
import org.wfc.payment.ccpay.model.PaymentResponse;
import org.wfc.payment.ccpay.model.RefundRequest;
import org.wfc.payment.ccpay.model.RefundResponse;
import org.wfc.payment.ccpay.model.UCreditCardPayment;
import org.wfc.payment.ccpay.service.IUCreditCardPaymentService;
import org.wfc.payment.ccpay.model.UCreditCardOrder;
import org.wfc.payment.ccpay.service.IUCreditCardOrderService;
import java.util.HashMap;
import java.util.Map;
@Service
public class UCreditCardPaymentServiceImpl implements IUCreditCardPaymentService {
public class UCreditCardOrderServiceImpl implements IUCreditCardOrderService {
private final UCreditCardPaymentMapper paymentMapper;
private final UCreditCardOrderMapper paymentMapper;
private final CcpayConfig ccpayConfig;
private static final String CONTENT_TYPE_HEADER = "Content-Type";
private static final String APPLICATION_JSON = "application/json";
@Autowired
public UCreditCardPaymentServiceImpl(UCreditCardPaymentMapper paymentMapper, CcpayConfig ccpayConfig) {
public UCreditCardOrderServiceImpl(UCreditCardOrderMapper paymentMapper, CcpayConfig ccpayConfig) {
this.paymentMapper = paymentMapper;
this.ccpayConfig = ccpayConfig;
}
@Override
public void savePayment(UCreditCardPayment payment) {
paymentMapper.insertPayment(payment);
public void saveOrder(UCreditCardOrder order) {
paymentMapper.insertOrder(order);
}
@Override
public UCreditCardPayment getPaymentById(Long id) {
return paymentMapper.selectPaymentById(id);
public UCreditCardOrder getOrderById(Long id) {
return paymentMapper.selectOrderById(id);
}
@Override

View File

@@ -1,5 +1,6 @@
package org.wfc.payment.wxpay.controller;
import com.alipay.api.AlipayApiException;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
@@ -15,22 +16,26 @@ import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wfc.payment.wxpay.service.IWxPayNotifyOrderService;
import org.wfc.payment.wxpay.service.IWxPayNotifyRefundService;
import org.wfc.payment.wxpay.service.IWxPayNotifyScanpayService;
import java.io.File;
import java.util.Date;
import java.util.Map;
@Tag(name = "WeChat Pay")
@RestController
@RequestMapping("/wxpay")
// @AllArgsConstructor
@AllArgsConstructor
public class WxPayController {
private final WxPayService wxService;
@Autowired
public WxPayController(WxPayService wxService) {
this.wxService = wxService;
}
// @Autowired
// public WxPayController(WxPayService wxService) {
// this.wxService = wxService;
// }
/**
* <pre>
@@ -166,28 +171,55 @@ public class WxPayController {
return this.wxService.refundQuery(wxPayRefundQueryRequest);
}
private final IWxPayNotifyOrderService wxPayNotifyOrderService;
private static final String MSG_SUCCESS = "success";
private static final String MSG_FAILURE = "failure";
@Operation(summary = "支付回调通知处理")
@PostMapping("/notify/order")
public String parseOrderNotifyResult(@RequestBody String xmlData) throws WxPayException {
final WxPayOrderNotifyResult notifyResult = this.wxService.parseOrderNotifyResult(xmlData);
// TODO 根据自己业务场景需要构造返回对象
return WxPayNotifyResponse.success("成功");
// 根据自己业务场景需要构造返回对象
try {
Map<String, String> notifyResultMap = notifyResult.toMap();
boolean result = wxPayNotifyOrderService.handleWxPayNotifyOrder(notifyResultMap);
return result ? MSG_SUCCESS : MSG_FAILURE;
} catch (Exception e) {
e.printStackTrace();
return MSG_FAILURE;
}
}
private final IWxPayNotifyRefundService wxPayNotifyRefundService;
@Operation(summary = "退款回调通知处理")
@PostMapping("/notify/refund")
public String parseRefundNotifyResult(@RequestBody String xmlData) throws WxPayException {
final WxPayRefundNotifyResult result = this.wxService.parseRefundNotifyResult(xmlData);
// TODO 根据自己业务场景需要构造返回对象
return WxPayNotifyResponse.success("成功");
final WxPayRefundNotifyResult notifyResult = this.wxService.parseRefundNotifyResult(xmlData);
// 根据自己业务场景需要构造返回对象
try {
Map<String, String> notifyResultMap = notifyResult.toMap();
boolean result = wxPayNotifyRefundService.handleWxPayNotifyRefund(notifyResultMap);
return result ? MSG_SUCCESS : MSG_FAILURE;
} catch (Exception e) {
e.printStackTrace();
return MSG_FAILURE;
}
}
private final IWxPayNotifyScanpayService wxPayNotifyScanpayService;
@Operation(summary = "扫码支付回调通知处理")
@PostMapping("/notify/scanpay")
public String parseScanPayNotifyResult(String xmlData) throws WxPayException {
final WxScanPayNotifyResult result = this.wxService.parseScanPayNotifyResult(xmlData);
// TODO 根据自己业务场景需要构造返回对象
return WxPayNotifyResponse.success("成功");
final WxScanPayNotifyResult notifyResult = this.wxService.parseScanPayNotifyResult(xmlData);
// 根据自己业务场景需要构造返回对象
try {
Map<String, String> notifyResultMap = notifyResult.toMap();
boolean result = wxPayNotifyScanpayService.handleWxPayNotifyScanpay(notifyResultMap);
return result ? MSG_SUCCESS : MSG_FAILURE;
} catch (Exception e) {
e.printStackTrace();
return MSG_FAILURE;
}
}
/**

View File

@@ -0,0 +1,17 @@
package org.wfc.payment.wxpay.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.wfc.payment.wxpay.model.UWxPayOrderModel;
@Mapper
public interface UWxPayOrderMapper {
void insertOrder(UWxPayOrderModel uWxPayOrder);
void updateOrder(UWxPayOrderModel uWxPayOrder);
UWxPayOrderModel selectOrderByOutTradeNo(@Param("outTradeNo") String outTradeNo);
UWxPayOrderModel selectOrderById(@Param("id") Long id);
}

View File

@@ -0,0 +1,26 @@
package org.wfc.payment.wxpay.model;
import javax.persistence.*;
import java.util.Date;
import lombok.Data;
@Data
@Entity
public class UWxPayOrderModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;
private String outTradeNo;
private String trancationId;
private String tradeStatus;
private Long totalAmount;
private String subject;
private String body;
private String createBy;
private Date createTime;
private String updateBy;
private Date updateTime;
// Getters and Setters
}

View File

@@ -0,0 +1,7 @@
package org.wfc.payment.wxpay.service;
import java.util.Map;
public interface IWxPayNotifyOrderService {
boolean handleWxPayNotifyOrder(Map<String, String> params);
}

View File

@@ -0,0 +1,7 @@
package org.wfc.payment.wxpay.service;
import java.util.Map;
public interface IWxPayNotifyRefundService {
boolean handleWxPayNotifyRefund(Map<String, String> params);
}

View File

@@ -0,0 +1,7 @@
package org.wfc.payment.wxpay.service;
import java.util.Map;
public interface IWxPayNotifyScanpayService {
boolean handleWxPayNotifyScanpay(Map<String, String> params);
}

View File

@@ -0,0 +1,49 @@
package org.wfc.payment.wxpay.service.impl;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.wfc.payment.wxpay.mapper.UWxPayOrderMapper;
import org.wfc.payment.wxpay.model.UWxPayOrderModel;
import lombok.AllArgsConstructor;
import java.util.Date;
import org.wfc.payment.wxpay.utils.VerifySignUtils;
import org.wfc.payment.wxpay.service.IWxPayNotifyOrderService;
@AllArgsConstructor
@Service
public class WxPayNotifyOrderServiceImpl implements IWxPayNotifyOrderService {
private final UWxPayOrderMapper wxpayOrderMapper;
private final VerifySignUtils verifySignUtils;
public boolean handleWxPayNotifyOrder(Map<String, String> params) {
// 验证签名
boolean signVerified = verifySignUtils.verifySignature(params);
if (signVerified) {
// 处理业务逻辑,例如更新订单状态
String outTradeNo = params.get("out_trade_no");
String tradeStatus = params.get("trade_state");
Long totalAmount = new Long(params.get("total_fee")); // 微信支付金额单位为分
String subject = params.get("subject");
String body = params.get("body");
UWxPayOrderModel order = wxpayOrderMapper.selectOrderByOutTradeNo(outTradeNo);
if (order == null) {
order = new UWxPayOrderModel();
order.setOutTradeNo(outTradeNo);
order.setCreateTime(new Date());
wxpayOrderMapper.insertOrder(order);
}
order.setTradeStatus(tradeStatus);
order.setTotalAmount(totalAmount);
order.setSubject(subject);
order.setBody(body);
order.setUpdateTime(new Date());
wxpayOrderMapper.updateOrder(order);
return "SUCCESS".equals(tradeStatus);
}
return false;
}
}

View File

@@ -0,0 +1,49 @@
package org.wfc.payment.wxpay.service.impl;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.wfc.payment.wxpay.mapper.UWxPayOrderMapper;
import org.wfc.payment.wxpay.model.UWxPayOrderModel;
import lombok.AllArgsConstructor;
import java.util.Date;
import org.wfc.payment.wxpay.utils.VerifySignUtils;
import org.wfc.payment.wxpay.service.IWxPayNotifyRefundService;
@AllArgsConstructor
@Service
public class WxPayNotifyRefundServiceImpl implements IWxPayNotifyRefundService {
private final UWxPayOrderMapper wxpayOrderMapper;
private final VerifySignUtils verifySignUtils;
public boolean handleWxPayNotifyRefund(Map<String, String> params) {
// 验证签名
boolean signVerified = verifySignUtils.verifySignature(params);
if (signVerified) {
// 处理业务逻辑,例如更新订单状态
String outTradeNo = params.get("out_trade_no");
String tradeStatus = params.get("trade_status");
Long totalAmount = new Long(params.get("total_fee")); // 微信支付金额单位为分
String subject = params.get("subject");
String body = params.get("body");
UWxPayOrderModel order = wxpayOrderMapper.selectOrderByOutTradeNo(outTradeNo);
if (order == null) {
order = new UWxPayOrderModel();
order.setOutTradeNo(outTradeNo);
order.setCreateTime(new Date());
wxpayOrderMapper.insertOrder(order);
}
order.setTradeStatus(tradeStatus);
order.setTotalAmount(totalAmount);
order.setSubject(subject);
order.setBody(body);
order.setUpdateTime(new Date());
wxpayOrderMapper.updateOrder(order);
return "SUCCESS".equals(tradeStatus);
}
return false;
}
}

View File

@@ -0,0 +1,50 @@
package org.wfc.payment.wxpay.service.impl;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.wfc.payment.wxpay.mapper.UWxPayOrderMapper;
import org.wfc.payment.wxpay.model.UWxPayOrderModel;
import org.wfc.payment.wxpay.service.IWxPayNotifyScanpayService;
import lombok.AllArgsConstructor;
import java.util.Date;
import org.wfc.payment.wxpay.utils.VerifySignUtils;
@AllArgsConstructor
@Service
public class WxPayNotifyScanpayServiceImpl implements IWxPayNotifyScanpayService {
private final UWxPayOrderMapper wxpayOrderMapper;
private final VerifySignUtils verifySignUtils;
public boolean handleWxPayNotifyScanpay(Map<String, String> params) {
// 验证签名
boolean signVerified = verifySignUtils.verifySignature(params);
if (signVerified) {
// 处理业务逻辑,例如更新订单状态
String outTradeNo = params.get("out_trade_no");
String tradeStatus = params.get("trade_status");
Long totalAmount = new Long(params.get("total_fee")); // 微信支付金额单位为分
String subject = params.get("subject");
String body = params.get("body");
UWxPayOrderModel order = wxpayOrderMapper.selectOrderByOutTradeNo(outTradeNo);
if (order == null) {
order = new UWxPayOrderModel();
order.setOutTradeNo(outTradeNo);
order.setCreateTime(new Date());
wxpayOrderMapper.insertOrder(order);
}
order.setTradeStatus(tradeStatus);
order.setTotalAmount(totalAmount);
order.setSubject(subject);
order.setBody(body);
order.setUpdateTime(new Date());
wxpayOrderMapper.updateOrder(order);
return "SUCCESS".equals(tradeStatus);
}
return false;
}
}

View File

@@ -0,0 +1,102 @@
package org.wfc.payment.wxpay.utils;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import java.util.Collections;
import java.util.List;
import java.security.PublicKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.wfc.payment.wxpay.config.WxPayProperties;
@Component
public class VerifySignUtils {
private final WxPayProperties wxpayProperties;
@Autowired
public VerifySignUtils(WxPayProperties wxpayProperties) {
this.wxpayProperties = wxpayProperties;
}
public static String sign(String data, PrivateKey privateKey) throws SignatureException {
if (data == null || privateKey == null) {
throw new IllegalArgumentException("Data and private key cannot be null");
}
try {
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
byte[] signBytes = signature.sign();
return Base64.getEncoder().encodeToString(signBytes);
} catch (Exception e) {
throw new SignatureException("Failed to sign data", e);
}
}
public static boolean verify(String data, String sign, PublicKey publicKey) throws SignatureException {
if (data == null || sign == null || publicKey == null) {
throw new IllegalArgumentException("Data, sign and public key cannot be null");
}
try {
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initVerify(publicKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
byte[] signBytes = Base64.getDecoder().decode(sign);
return signature.verify(signBytes);
} catch (Exception e) {
throw new SignatureException("Failed to verify signature", e);
}
}
public boolean verifySignature(Map<String, String> params) {
if (params == null || params.isEmpty()) {
return false;
}
// Extract signature parameter
String sign = params.remove("sign");
if (sign == null) {
return false;
}
// Sort parameters
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
// Build parameter string
StringBuilder data = new StringBuilder();
for (String key : keys) {
String value = params.get(key);
if (value != null) {
data.append(key)
.append("=")
.append(value)
.append("&");
}
}
// Remove last '&'
if (data.length() > 0) {
data.setLength(data.length() - 1);
}
try {
byte[] keyBytes = Base64.getDecoder().decode(wxpayProperties.getMchKey());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return verify(data.toString(), sign, publicKey);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.wfc.payment.alipay.mapper.UAlipayOrderMapper">
<resultMap id="UAlipayOrderResultMap" type="org.wfc.payment.alipay.model.UAlipayOrderModel">
<id property="id" column="id"/>
<result property="userId" column="user_id"/>
<result property="outTradeNo" column="out_trade_no"/>
<result property="tradeStatus" column="trade_status"/>
<result property="totalAmount" column="total_amount"/>
<result property="subject" column="subject"/>
<result property="body" column="body"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
</resultMap>
<select id="selectOrderByOutTradeNo" resultMap="UAlipayOrderResultMap">
SELECT * FROM u_alipay_order WHERE out_trade_no = #{outTradeNo}
</select>
<insert id="insertOrder">
INSERT INTO u_alipay_order (out_trade_no, trade_status, total_amount, subject, body, create_time, update_time)
VALUES (#{outTradeNo}, #{tradeStatus}, #{totalAmount}, #{subject}, #{body}, #{createTime}, #{updateTime})
</insert>
<update id="updateOrder">
UPDATE u_alipay_order
SET trade_status = #{tradeStatus}, total_amount = #{totalAmount}, subject = #{subject}, body = #{body}, update_time = #{updateTime}
WHERE out_trade_no = #{outTradeNo}
</update>
<select id="selectOrderById" parameterType="long" resultType="org.wfc.payment.alipay.model.UAlipayOrderModel" resultMap="UAlipayOrderResultMap">
SELECT * FROM u_alipay_order WHERE id = #{id}
</select>
</mapper>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.wfc.payment.ccpay.mapper.UCreditCardOrderMapper">
<insert id="insertOrder" parameterType="org.wfc.payment.ccpay.model.UCreditCardOrder">
INSERT INTO u_credit_card_order (transaction_id, user_id, order_id, amount, currency, status, payment_time, card_type, card_holder_name, billing_address, card_last_four, gateway_response)
VALUES (#{transactionId}, #{userId}, #{orderId}, #{amount}, #{currency}, #{status}, #{paymentTime}, #{cardType}, #{cardHolderName}, #{billingAddress}, #{cardLastFour}, #{gatewayResponse})
</insert>
<select id="selectOrderById" parameterType="long" resultType="org.wfc.payment.ccpay.model.UCreditCardOrder">
SELECT * FROM u_credit_card_order WHERE id = #{id}
</select>
<!-- 其他需要的 SQL 映射 -->
</mapper>

View File

@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.wfc.payment.ccpay.mapper.UCreditCardPaymentMapper">
<insert id="insertPayment" parameterType="org.wfc.payment.ccpay.model.UCreditCardPayment">
INSERT INTO u_credit_card_payment (transaction_id, user_id, order_id, amount, currency, status, payment_time, card_type, card_holder_name, billing_address, card_last_four, gateway_response)
VALUES (#{transactionId}, #{userId}, #{orderId}, #{amount}, #{currency}, #{status}, #{paymentTime}, #{cardType}, #{cardHolderName}, #{billingAddress}, #{cardLastFour}, #{gatewayResponse})
</insert>
<select id="selectPaymentById" parameterType="long" resultType="org.wfc.payment.ccpay.model.UCreditCardPayment">
SELECT * FROM u_credit_card_payment WHERE id = #{id}
</select>
<!-- 其他需要的 SQL 映射 -->
</mapper>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.wfc.payment.wxpay.mapper.UWxPayOrderMapper">
<resultMap id="UWxPayOrderResultMap" type="org.wfc.payment.wxpay.model.UWxPayOrderModel">
<id property="id" column="id"/>
<result property="userId" column="user_id"/>
<result property="outTradeNo" column="out_trade_no"/>
<result property="trancationId" column="trancation_id"/>
<result property="tradeStatus" column="trade_status"/>
<result property="totalAmount" column="total_amount"/>
<result property="subject" column="subject"/>
<result property="body" column="body"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
</resultMap>
<select id="selectOrderByOutTradeNo" resultMap="UWxPayOrderResultMap">
SELECT * FROM u_wxpay_order WHERE out_trade_no = #{outTradeNo}
</select>
<insert id="insertOrder">out_trade_no,
INSERT INTO u_wxpay_order (out_trade_no, trancation_id, trade_status, total_amount, subject, body, create_time, update_time)
VALUES (#{outTradeNo}, #{trancationId}, #{tradeStatus}, #{totalAmount}, #{subject}, #{body}, #{createTime}, #{updateTime})
</insert>
<update id="updateOrder">
UPDATE u_wxpay_order
SET trade_status = #{tradeStatus}, total_amount = #{totalAmount}, subject = #{subject}, body = #{body}, update_time = #{updateTime}
WHERE out_trade_no = #{outTradeNo}
</update>
<select id="selectOrderById" parameterType="long" resultType="org.wfc.payment.wxpay.model.UWxPayOrderModel" resultMap="UWxPayOrderResultMap">
SELECT * FROM u_wxpay_order WHERE id = #{id}
</select>
</mapper>