微信支付部分

This commit is contained in:
menxipeng
2025-11-24 23:42:09 +08:00
parent b29d2c696c
commit c7fd167167
17 changed files with 523 additions and 60 deletions

View File

@@ -41,4 +41,6 @@ public class CallBackController {
}
}
}

View File

@@ -0,0 +1,142 @@
package com.ruoyi.web.controller.back;
import com.ruoyi.common.core.domain.entity.OrderInfo;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.system.service.IOrderInfoService;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/call/back")
public class WechatPayNotifyController {
@Autowired
private IOrderInfoService orderService;
/**
* 微信支付结果回调接口
* POST /api/wechat/pay/notify
*/
@PostMapping("/wechat")
public Map<String, String> handleWechatPayNotify(HttpServletRequest request,
HttpServletResponse response) {
Map<String, String> result = new HashMap<>();
try {
System.out.println("收到微信支付回调通知");
// 1. 构建回调参数
RequestParam requestParam = buildRequestParam(request);
NotificationParser parser = new NotificationParser();
// 2. 解析并验证通知
Transaction transaction = parser.parse(requestParam, Transaction.class);
System.out.println("解析支付通知成功:");
System.out.println("商户订单号: " + transaction.getOutTradeNo());
System.out.println("微信支付订单号: " + transaction.getTransactionId());
System.out.println("交易状态: " + transaction.getTradeState());
System.out.println("交易类型: " + transaction.getTradeType());
System.out.println("金额: " + transaction.getAmount().getTotal());
// 3. 处理业务逻辑
orderService.processPaymentResult(transaction);
// if (processResult) {
// // 4. 返回成功响应
// result.put("code", "SUCCESS");
// result.put("message", "成功");
// System.out.println("支付回调处理成功");
// } else {
// result.put("code", "FAIL");
// result.put("message", "处理失败");
// System.err.println("支付回调处理失败");
// }
} catch (Exception e) {
System.err.println("支付回调处理异常: " + e.getMessage());
e.printStackTrace();
// 返回失败响应
result.put("code", "FAIL");
result.put("message", "处理异常: " + e.getMessage());
}
return result;
}
/**
* 从HttpServletRequest构建RequestParam
*/
private RequestParam buildRequestParam(HttpServletRequest request) throws Exception {
// 获取微信回调的头部信息
String wechatpaySerial = request.getHeader("Wechatpay-Serial");
String wechatpaySignature = request.getHeader("Wechatpay-Signature");
String wechatpayTimestamp = request.getHeader("Wechatpay-Timestamp");
String wechatpayNonce = request.getHeader("Wechatpay-Nonce");
String wechatpaySignatureType = request.getHeader("Wechatpay-Signature-Type");
System.out.println("回调头部信息:");
System.out.println("Wechatpay-Serial: " + wechatpaySerial);
System.out.println("Wechatpay-Timestamp: " + wechatpayTimestamp);
System.out.println("Wechatpay-Nonce: " + wechatpayNonce);
System.out.println("Wechatpay-Signature-Type: " + wechatpaySignatureType);
// 读取请求体
String body = getRequestBody(request);
System.out.println("回调请求体: " + body);
return new RequestParam.Builder()
.serialNumber(wechatpaySerial)
.nonce(wechatpayNonce)
.signature(wechatpaySignature)
.timestamp(wechatpayTimestamp)
.signType(wechatpaySignatureType)
.body(body)
.build();
}
/**
* 读取请求体内容
*/
private String getRequestBody(HttpServletRequest request) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
try (java.io.BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
}
return stringBuilder.toString();
}
//
//
// /**
// * 处理退款逻辑
// */
// private boolean handleRefund(Order order, Transaction transaction) {
// // 实现退款逻辑
// System.out.println("处理退款: " + order.getOutTradeNo());
// return true;
// }
//
// /**
// * 处理订单关闭逻辑
// */
// private boolean handleClosedPayment(Order order, Transaction transaction) {
// // 实现订单关闭逻辑
// System.out.println("处理订单关闭: " + order.getOutTradeNo());
// return true;
// }
}

View File

@@ -3,14 +3,17 @@ package com.ruoyi.web.controller.client;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.OrderInfo;
import com.ruoyi.common.core.domain.dto.OrderCreateRequest;
import com.ruoyi.common.core.domain.dto.PaymentRequest;
import com.ruoyi.common.core.domain.dto.RefundRequest;
import com.ruoyi.common.core.domain.entity.OrderInfo;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.IOrderInfoService;
import com.wechat.pay.java.service.payments.app.model.PrepayWithRequestPaymentResponse;
import org.aspectj.weaver.loadtime.Aj;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@@ -46,6 +49,12 @@ public class ClientOrderInfoController extends BaseController
}
}
// 调起微信支付参数
@GetMapping("/wechatPay")
public AjaxResult wechatPay(@RequestParam("orderId") String orderId) {
return orderInfoService.wechatPay(orderId);
}
/**
* 支付订单
*/

View File

@@ -53,14 +53,16 @@ public class ClientShopUserController {
ShopUser shopUser = new ShopUser();
shopUser.setUserId(userId);
shopUser.setStatus(2L);
String code = redisCache.getCacheObject("sms_code:"+shopUser.getPhone());
String code = redisCache.getCacheObject("sms_code:"+param.getPhone());
if (param.getCode() != null) {
if (Objects.equals(code, param.getCode())) {
shopUserService.updateShopUserId(shopUser);
return AjaxResult.success();
int delInt = shopUserService.removeById(userId);
if (delInt > 0){
return AjaxResult.success();
}
}else {
return AjaxResult.error("验证码错误");
}
}else {
return AjaxResult.error("验证码错误");
}
return AjaxResult.error("注销账号失败");
}

View File

@@ -156,3 +156,11 @@ umApp:
# androidKey: 687b2df479267e0210b79b6f
IOSKey: 68ef6aa88560e34872ca7b41
#Q1w2e3r4
wechat:
appId: wx87084c0b6aa7aed6
merchantId: 1732990772
privateKeyPath: D:\musicpro\apiclient_key.pem
merchantCertPath: D:\musicpro\apiclient_cert.pem
wechatPayCertPath: D:\musicpro\pub_key.pem
publicKeyId: PUB_KEY_ID_0117329907722025112100111619002400
apiV3Key: ASSSUNTvttuiwqazuu12tnftANtfb004