新增微信支付结果通知中转接口/api/system/wx-pay/notify

This commit is contained in:
2024-07-31 02:32:42 +08:00
parent b1366a391d
commit 8811894d17
2 changed files with 84 additions and 52 deletions

View File

@@ -3,8 +3,10 @@ package com.gxwebsoft.common.core.utils;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.common.system.entity.MerchantAccount; import com.gxwebsoft.common.system.entity.MerchantAccount;
import com.gxwebsoft.common.system.entity.Payment;
import com.gxwebsoft.common.system.entity.User; import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.entity.UserRole; import com.gxwebsoft.common.system.entity.UserRole;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
@@ -17,15 +19,35 @@ public class RequestUtil {
private static String ACCESS_TOKEN; private static String ACCESS_TOKEN;
private static String TENANT_ID; private static String TENANT_ID;
public void setTenantId(String tenantId){ public void setTenantId(String tenantId) {
TENANT_ID = tenantId; TENANT_ID = tenantId;
} }
public void setAccessToken(String token){ public void setAccessToken(String token) {
ACCESS_TOKEN = token; ACCESS_TOKEN = token;
} }
public User getMerchantAccountByPhone(String phone){ // 微信支付通知
public String pushWxPayNotify(Transaction transaction, Payment payment){
// 设置租户ID
setTenantId(payment.getTenantId().toString());
// 推送支付通知地址
String path = payment.getNotifyUrl();
try {
// 链式构建请求
return HttpRequest.post(path)
.header("Tenantid", TENANT_ID)
.body(JSONUtil.toJSONString(transaction))//表单内容
.timeout(20000)//超时,毫秒
.execute().body();
} catch (Exception e) {
e.printStackTrace();
}
return "支付失败";
}
public User getMerchantAccountByPhone(String phone) {
String path = "/shop/merchant-account/getMerchantAccountByPhone/" + phone; String path = "/shop/merchant-account/getMerchantAccountByPhone/" + phone;
try { try {
// 链式构建请求 // 链式构建请求
@@ -63,7 +85,7 @@ public class RequestUtil {
return null; return null;
} }
public User getUserByPhone(String phone){ public User getUserByPhone(String phone) {
String path = "/system/user/getByPhone/" + phone; String path = "/system/user/getByPhone/" + phone;
try { try {
// 链式构建请求 // 链式构建请求
@@ -81,23 +103,24 @@ public class RequestUtil {
} }
return null; return null;
} }
// 新增用户 // 新增用户
public boolean saveUserByPhone(MerchantAccount merchantAccount) { public boolean saveUserByPhone(MerchantAccount merchantAccount) {
String path = "/system/user/"; String path = "/system/user/";
try { try {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("nickname",merchantAccount.getRealName()); map.put("nickname", merchantAccount.getRealName());
map.put("username", merchantAccount.getPhone()); map.put("username", merchantAccount.getPhone());
map.put("realName",merchantAccount.getRealName()); map.put("realName", merchantAccount.getRealName());
map.put("phone", merchantAccount.getPhone()); map.put("phone", merchantAccount.getPhone());
map.put("password",merchantAccount.getPassword()); map.put("password", merchantAccount.getPassword());
final ArrayList<Object> roles = new ArrayList<>(); final ArrayList<Object> roles = new ArrayList<>();
final UserRole userRole = new UserRole(); final UserRole userRole = new UserRole();
userRole.setUserId(merchantAccount.getUserId()); userRole.setUserId(merchantAccount.getUserId());
userRole.setRoleId(merchantAccount.getRoleId()); userRole.setRoleId(merchantAccount.getRoleId());
userRole.setTenantId(merchantAccount.getTenantId()); userRole.setTenantId(merchantAccount.getTenantId());
roles.add(userRole); roles.add(userRole);
map.put("roles",roles); map.put("roles", roles);
map.put("tenantId", TENANT_ID); map.put("tenantId", TENANT_ID);
// 链式构建请求 // 链式构建请求
String result = HttpRequest.post(SERVER_HOST.concat(path)) String result = HttpRequest.post(SERVER_HOST.concat(path))

View File

@@ -1,14 +1,11 @@
package com.gxwebsoft.common.system.controller; package com.gxwebsoft.common.system.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.gxwebsoft.common.core.config.ConfigProperties; import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.utils.RedisUtil; import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.utils.RequestUtil;
import com.gxwebsoft.common.core.web.BaseController; import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.Order;
import com.gxwebsoft.common.system.entity.Payment; import com.gxwebsoft.common.system.entity.Payment;
import com.gxwebsoft.common.system.mapper.SettingMapper;
import com.gxwebsoft.common.system.service.OrderService;
import com.wechat.pay.java.core.notification.NotificationConfig; import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser; import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RSANotificationConfig; import com.wechat.pay.java.core.notification.RSANotificationConfig;
@@ -19,15 +16,10 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map; import java.util.Map;
import static com.gxwebsoft.common.core.constants.OrderConstants.PAY_STATUS_NO_PAY;
import static com.gxwebsoft.common.core.constants.OrderConstants.PAY_STATUS_SUCCESS;
/** /**
* 微信支付结果通知 * 会员特权购买记录表控制器
* *
* @author 科技小王子 * @author 科技小王子
* @since 2023-06-20 18:07:50 * @since 2023-06-20 18:07:50
@@ -39,10 +31,10 @@ public class WxPayNotifyController extends BaseController {
@Value("${spring.profiles.active}") @Value("${spring.profiles.active}")
String active; String active;
@Resource @Resource
private OrderService orderService;
@Resource
private RedisUtil redisUtil; private RedisUtil redisUtil;
@Resource @Resource
private RequestUtil requestUtil;
@Resource
private ConfigProperties conf; private ConfigProperties conf;
// 本地证书 // 本地证书
public static String wechatpayCertPath = "/Users/gxwebsoft/JAVA/com.gxwebsoft.core/src/main/resources/cert/wechatpay_4A3231584E93B6AE77820074D07EADEACCB7E223.pem"; // 平台证书 public static String wechatpayCertPath = "/Users/gxwebsoft/JAVA/com.gxwebsoft.core/src/main/resources/cert/wechatpay_4A3231584E93B6AE77820074D07EADEACCB7E223.pem"; // 平台证书
@@ -51,13 +43,14 @@ public class WxPayNotifyController extends BaseController {
@PostMapping("/notify/{tenantId}") @PostMapping("/notify/{tenantId}")
public String wxNotify(@RequestHeader Map<String, String> header, @RequestBody String body, @PathVariable("tenantId") Integer tenantId) { public String wxNotify(@RequestHeader Map<String, String> header, @RequestBody String body, @PathVariable("tenantId") Integer tenantId) {
System.out.println("异步通知*************** = "); System.out.println("异步通知*************** = ");
// System.out.println("request header = " + header); System.out.println("request header = " + header);
// System.out.println("request body = " + body); System.out.println("request body = " + body);
// System.out.println("tenantId = " + tenantId); System.out.println("tenantId = " + tenantId);
// 获取支付配置信息用于解密 // 获取支付配置信息用于解密
String key = "Payment:1:".concat(tenantId.toString()); String key = "Payment:1:".concat(tenantId.toString());
final Payment payment = redisUtil.get(key, Payment.class); final Payment payment = redisUtil.get(key, Payment.class);
System.out.println("payment = " + payment);
String privateKey = payment.getApiKey(); String privateKey = payment.getApiKey();
String apiclientCert = conf.getUploadPath().concat("/file").concat(payment.getApiclientCert()); String apiclientCert = conf.getUploadPath().concat("/file").concat(payment.getApiclientCert());
// 开发环境配置 // 开发环境配置
@@ -73,6 +66,7 @@ public class WxPayNotifyController extends BaseController {
.body(body) .body(body)
.build(); .build();
// 如果已经初始化了 RSAAutoCertificateConfig可直接使用 // 如果已经初始化了 RSAAutoCertificateConfig可直接使用
// 没有的话,则构造一个 // 没有的话,则构造一个
NotificationConfig config = new RSANotificationConfig.Builder() NotificationConfig config = new RSANotificationConfig.Builder()
@@ -80,13 +74,15 @@ public class WxPayNotifyController extends BaseController {
.certificatesFromPath(apiclientCert) .certificatesFromPath(apiclientCert)
.build(); .build();
// 初始化 NotificationParser // 初始化 NotificationParser
NotificationParser parser = new NotificationParser(config); NotificationParser parser = new NotificationParser(config);
System.out.println("parser = " + parser);
// 以支付通知回调为例,验签、解密并转换成 Transaction // 以支付通知回调为例,验签、解密并转换成 Transaction
try { try {
Transaction transaction = parser.parse(requestParam, Transaction.class); Transaction transaction = parser.parse(requestParam, Transaction.class);
System.out.println("transaction = " + transaction);
final String outTradeNo = transaction.getOutTradeNo(); final String outTradeNo = transaction.getOutTradeNo();
final String transactionId = transaction.getTransactionId(); final String transactionId = transaction.getTransactionId();
final Integer total = transaction.getAmount().getTotal(); final Integer total = transaction.getAmount().getTotal();
@@ -101,26 +97,39 @@ public class WxPayNotifyController extends BaseController {
System.out.println("amount = " + total); System.out.println("amount = " + total);
if (StrUtil.equals("支付成功", tradeStateDesc)) { if (StrUtil.equals("支付成功", tradeStateDesc)) {
// 1. 查询要处理的订单 final String string = requestUtil.pushWxPayNotify(transaction, payment);
Order order = orderService.getByOutTradeNo(outTradeNo); System.out.println("string = " + string);
System.out.println("order = " + order);
// 2. 已支付则跳过
if (order.getPayStatus().equals(PAY_STATUS_SUCCESS)) {
return "SUCCESS"; return "SUCCESS";
} // // 1. 查询要处理的订单
// 2. 未支付则处理更新订单状态 // Order order = orderService.getByOutTradeNo(outTradeNo);
if (order.getPayStatus().equals(PAY_STATUS_NO_PAY)) { // // 2. 已支付则跳过
// 5. TODO 处理订单状态 // if (order.getPayStatus().equals(1)) {
order.setPayTime(DateUtil.date()); // return "SUCCESS";
order.setPayStatus(PAY_STATUS_SUCCESS); // }
order.setTransactionId(transactionId); // // 2. 未支付则处理更新订单状态
// 实际付款金额Integer除以100后转BigDecimal // if (order.getPayStatus().equals(0)) {
DecimalFormat df = new DecimalFormat("0.00"); // // 5. TODO 处理订单状态
final String format = df.format(total / 100); // order.setPayTime(DateUtil.date());
order.setPayPrice(new BigDecimal(format)); // order.setPayStatus(1);
orderService.updateByOutTradeNo(order); // order.setTransactionId(transactionId);
return "SUCCESS"; // order.setPayPrice(new BigDecimal(NumberUtil.decimalFormat("0.00", total * 0.01)));
} // order.setExpirationTime(DateUtil.offset(DateUtil.date(), DateField.YEAR, 10));
// System.out.println("实际付款金额 = " + order.getPayPrice());
// orderService.updateByOutTradeNo(order);
// // 6. TODO 同步更新订单明细的支付状态
// orderInfoService.update(new LambdaUpdateWrapper<OrderInfo>().eq(OrderInfo::getOrderId,order.getOrderId()).set(OrderInfo::getPayStatus,1));
//// orderInfoService.updateByOutTradeNo(order);
// // 7.同步会员卡状态
// if(order.getType().equals(2)){
// final UserCard userCard = userCardService.getById(order.getCardId());
// userCard.setStatus(1);
// userCardService.updateById(userCard);
// }
// // 8.发放积分
// usersService.addIntegral(order);
//
// return "SUCCESS";
// }
} }
} catch (Exception $e) { } catch (Exception $e) {
System.out.println($e.getMessage()); System.out.println($e.getMessage());