新增微信支付结果通知中转接口/api/system/wx-pay/notify
This commit is contained in:
@@ -3,8 +3,10 @@ package com.gxwebsoft.common.core.utils;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
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.UserRole;
|
||||
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -17,15 +19,35 @@ public class RequestUtil {
|
||||
private static String ACCESS_TOKEN;
|
||||
private static String TENANT_ID;
|
||||
|
||||
public void setTenantId(String tenantId){
|
||||
public void setTenantId(String tenantId) {
|
||||
TENANT_ID = tenantId;
|
||||
}
|
||||
|
||||
public void setAccessToken(String token){
|
||||
public void setAccessToken(String 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;
|
||||
try {
|
||||
// 链式构建请求
|
||||
@@ -63,7 +85,7 @@ public class RequestUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
public User getUserByPhone(String phone){
|
||||
public User getUserByPhone(String phone) {
|
||||
String path = "/system/user/getByPhone/" + phone;
|
||||
try {
|
||||
// 链式构建请求
|
||||
@@ -81,23 +103,24 @@ public class RequestUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 新增用户
|
||||
public boolean saveUserByPhone(MerchantAccount merchantAccount) {
|
||||
String path = "/system/user/";
|
||||
try {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("nickname",merchantAccount.getRealName());
|
||||
map.put("nickname", merchantAccount.getRealName());
|
||||
map.put("username", merchantAccount.getPhone());
|
||||
map.put("realName",merchantAccount.getRealName());
|
||||
map.put("realName", merchantAccount.getRealName());
|
||||
map.put("phone", merchantAccount.getPhone());
|
||||
map.put("password",merchantAccount.getPassword());
|
||||
map.put("password", merchantAccount.getPassword());
|
||||
final ArrayList<Object> roles = new ArrayList<>();
|
||||
final UserRole userRole = new UserRole();
|
||||
userRole.setUserId(merchantAccount.getUserId());
|
||||
userRole.setRoleId(merchantAccount.getRoleId());
|
||||
userRole.setTenantId(merchantAccount.getTenantId());
|
||||
roles.add(userRole);
|
||||
map.put("roles",roles);
|
||||
map.put("roles", roles);
|
||||
map.put("tenantId", TENANT_ID);
|
||||
// 链式构建请求
|
||||
String result = HttpRequest.post(SERVER_HOST.concat(path))
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
package com.gxwebsoft.common.system.controller;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
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.system.entity.Order;
|
||||
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.NotificationParser;
|
||||
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 javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DecimalFormat;
|
||||
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 科技小王子
|
||||
* @since 2023-06-20 18:07:50
|
||||
@@ -39,10 +31,10 @@ public class WxPayNotifyController extends BaseController {
|
||||
@Value("${spring.profiles.active}")
|
||||
String active;
|
||||
@Resource
|
||||
private OrderService orderService;
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
@Resource
|
||||
private RequestUtil requestUtil;
|
||||
@Resource
|
||||
private ConfigProperties conf;
|
||||
// 本地证书
|
||||
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}")
|
||||
public String wxNotify(@RequestHeader Map<String, String> header, @RequestBody String body, @PathVariable("tenantId") Integer tenantId) {
|
||||
System.out.println("异步通知*************** = ");
|
||||
// System.out.println("request header = " + header);
|
||||
// System.out.println("request body = " + body);
|
||||
// System.out.println("tenantId = " + tenantId);
|
||||
System.out.println("request header = " + header);
|
||||
System.out.println("request body = " + body);
|
||||
System.out.println("tenantId = " + tenantId);
|
||||
|
||||
// 获取支付配置信息用于解密
|
||||
String key = "Payment:1:".concat(tenantId.toString());
|
||||
final Payment payment = redisUtil.get(key, Payment.class);
|
||||
System.out.println("payment = " + payment);
|
||||
String privateKey = payment.getApiKey();
|
||||
String apiclientCert = conf.getUploadPath().concat("/file").concat(payment.getApiclientCert());
|
||||
// 开发环境配置
|
||||
@@ -73,6 +66,7 @@ public class WxPayNotifyController extends BaseController {
|
||||
.body(body)
|
||||
.build();
|
||||
|
||||
|
||||
// 如果已经初始化了 RSAAutoCertificateConfig,可直接使用
|
||||
// 没有的话,则构造一个
|
||||
NotificationConfig config = new RSANotificationConfig.Builder()
|
||||
@@ -80,13 +74,15 @@ public class WxPayNotifyController extends BaseController {
|
||||
.certificatesFromPath(apiclientCert)
|
||||
.build();
|
||||
|
||||
|
||||
// 初始化 NotificationParser
|
||||
NotificationParser parser = new NotificationParser(config);
|
||||
|
||||
System.out.println("parser = " + parser);
|
||||
|
||||
// 以支付通知回调为例,验签、解密并转换成 Transaction
|
||||
try {
|
||||
Transaction transaction = parser.parse(requestParam, Transaction.class);
|
||||
System.out.println("transaction = " + transaction);
|
||||
final String outTradeNo = transaction.getOutTradeNo();
|
||||
final String transactionId = transaction.getTransactionId();
|
||||
final Integer total = transaction.getAmount().getTotal();
|
||||
@@ -101,26 +97,39 @@ public class WxPayNotifyController extends BaseController {
|
||||
System.out.println("amount = " + total);
|
||||
|
||||
if (StrUtil.equals("支付成功", tradeStateDesc)) {
|
||||
// 1. 查询要处理的订单
|
||||
Order order = orderService.getByOutTradeNo(outTradeNo);
|
||||
System.out.println("order = " + order);
|
||||
// 2. 已支付则跳过
|
||||
if (order.getPayStatus().equals(PAY_STATUS_SUCCESS)) {
|
||||
final String string = requestUtil.pushWxPayNotify(transaction, payment);
|
||||
System.out.println("string = " + string);
|
||||
return "SUCCESS";
|
||||
}
|
||||
// 2. 未支付则处理更新订单状态
|
||||
if (order.getPayStatus().equals(PAY_STATUS_NO_PAY)) {
|
||||
// 5. TODO 处理订单状态
|
||||
order.setPayTime(DateUtil.date());
|
||||
order.setPayStatus(PAY_STATUS_SUCCESS);
|
||||
order.setTransactionId(transactionId);
|
||||
// 实际付款金额:Integer除以100后转BigDecimal
|
||||
DecimalFormat df = new DecimalFormat("0.00");
|
||||
final String format = df.format(total / 100);
|
||||
order.setPayPrice(new BigDecimal(format));
|
||||
orderService.updateByOutTradeNo(order);
|
||||
return "SUCCESS";
|
||||
}
|
||||
// // 1. 查询要处理的订单
|
||||
// Order order = orderService.getByOutTradeNo(outTradeNo);
|
||||
// // 2. 已支付则跳过
|
||||
// if (order.getPayStatus().equals(1)) {
|
||||
// return "SUCCESS";
|
||||
// }
|
||||
// // 2. 未支付则处理更新订单状态
|
||||
// if (order.getPayStatus().equals(0)) {
|
||||
// // 5. TODO 处理订单状态
|
||||
// order.setPayTime(DateUtil.date());
|
||||
// order.setPayStatus(1);
|
||||
// order.setTransactionId(transactionId);
|
||||
// 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) {
|
||||
System.out.println($e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user