配置、记录请求、支付

This commit is contained in:
2024-12-17 18:54:07 +08:00
parent d70bfbc605
commit 5249cc2cc8
12 changed files with 311 additions and 31 deletions

View File

@ -0,0 +1,11 @@
package com.ycwl.basic.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
public @interface RequestToFile {
}

View File

@ -0,0 +1,63 @@
package com.ycwl.basic.aspectj;
import com.ycwl.basic.annotation.RequestToFile;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Enumeration;
import java.util.stream.Collectors;
@Aspect
@Component
@Slf4j
public class HttpSaver {
@Pointcut("@annotation(com.ycwl.basic.annotation.RequestToFile)")
public void requestToFilePointCut() {
}
@After("requestToFilePointCut()")
public void requestToFile() throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return;
}
HttpServletRequest request = attributes.getRequest();
StringBuilder rawReq = new StringBuilder();
rawReq.append(request.getMethod()).append(" ").append(request.getRequestURL());
String queryString = request.getQueryString();
if (queryString != null) {
rawReq.append("?").append(queryString);
}
rawReq.append("\r\n");
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
rawReq.append(headerName).append(": ").append(request.getHeader(headerName)).append("\r\n");
}
rawReq.append("\r\n");
// 获取body
rawReq.append(request.getReader().lines().collect(Collectors.joining("\r\n")));
rawReq.append("\r\n");
// 写入文件
File file = new File("./request/"+System.currentTimeMillis()+".http");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
try (java.io.FileWriter writer = new java.io.FileWriter(file, true)) {
writer.write(rawReq.toString());
}
}
}

View File

@ -2,6 +2,8 @@ package com.ycwl.basic.controller.mobile;
import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.annotation.RequestToFile;
import com.ycwl.basic.aspectj.HttpSaver;
import com.ycwl.basic.enums.BizCodeEnum;
import com.ycwl.basic.model.wx.WXPayOrderReqVO;
import com.ycwl.basic.model.wx.WxPayRespVO;
@ -42,6 +44,7 @@ public class AppWxPayController {
@ApiOperation(value = "微信支付回调", notes = "微信支付回调")
@PostMapping("/payNotify")
@IgnoreToken
@RequestToFile
public ApiResponse<?> payNotify(HttpServletRequest request) {
wxPayService.payNotify(request);
return ApiResponse.success(BizCodeEnum.REQUEST_OK);
@ -58,6 +61,7 @@ public class AppWxPayController {
@ApiOperation(value = "微信支付退款回调", notes = "微信支付退款回调")
@PostMapping("/refundNotify")
@IgnoreToken
@RequestToFile
public ApiResponse<?> refundNotify(@RequestBody String refundResult) throws GeneralSecurityException, IOException {
return ApiResponse.buildResult(wxPayService.refundNotify(refundResult) ?
BizCodeEnum.SUCCESS :

View File

@ -26,4 +26,6 @@ public class WXPayOrderReqVO {
@ApiModelProperty(value = "商品订单号",required = true)
private Long orderSn;
private String description;
}

View File

@ -1,5 +1,6 @@
package com.ycwl.basic.service.impl.mobile;
import com.ycwl.basic.constant.BaseContextHandler;
import com.ycwl.basic.mapper.*;
import com.ycwl.basic.model.jwt.JwtInfo;
import com.ycwl.basic.model.mobile.goods.*;
@ -43,14 +44,13 @@ public class GoodsServiceImpl implements GoodsService {
private FaceMapper faceMapper;
public ApiResponse<List<GoodsPageVO>> goodsList(GoodsReqQuery query) {
JwtInfo worker = JwtTokenUtil.getWorker();
//查询原素材
List<GoodsPageVO> goodsList = new ArrayList<>();
VideoReqQuery videoReqQuery = new VideoReqQuery();
videoReqQuery.setScenicId(query.getScenicId());
videoReqQuery.setIsBuy(query.getIsBuy());
videoReqQuery.setMemberId(worker.getUserId());
videoReqQuery.setMemberId(Long.valueOf(BaseContextHandler.getUserId()));
//查询成片vlog
List<VideoRespVO> videoList = videoMapper.list(videoReqQuery);
videoList.forEach(videoRespVO -> {

View File

@ -90,13 +90,14 @@ public class WxPayServiceImpl implements WxPayService {
// request.setXxx(val)设置所需参数具体参数可见Request定义
PrepayRequest request = new PrepayRequest();
Amount amount = new Amount();
amount.setTotal(req.getTotalPrice());
amount.setTotal(1);
request.setAmount(amount);
request.setAppid(wechatConfig.getAppId());
request.setAppid(wechatConfig.getMiniProgramAppId());
request.setMchid(wechatConfig.getMchId());
request.setDescription(req.getGoodsName());
request.setNotifyUrl(wechatConfig.getPayNotifyUrl());
request.setOutTradeNo(req.getOrderSn().toString());
request.setDescription(req.getDescription());
Payer payer = new Payer();
payer.setOpenid(req.getOpenId());
@ -109,7 +110,7 @@ public class WxPayServiceImpl implements WxPayService {
vo.setTimeStamp(timeStamp);
String substring = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
vo.setNonceStr(substring);
String signatureStr = Stream.of(wechatConfig.getAppId(), String.valueOf(timeStamp), substring, "prepay_id=" + response.getPrepayId())
String signatureStr = Stream.of(wechatConfig.getMiniProgramAppId(), String.valueOf(timeStamp), substring, "prepay_id=" + response.getPrepayId())
.collect(Collectors.joining("\n", "", "\n"));
String sign = WXPayUtil.getSign(signatureStr, wechatConfig.getKeyPath());
vo.setPaySign(sign);
@ -137,6 +138,7 @@ public class WxPayServiceImpl implements WxPayService {
stringBuffer.append(s);
}
String s1 = stringBuffer.toString();
log.warn("微信支付回调:{}", s1);
String timestamp = request.getHeader(WECHAT_PAY_TIMESTAMP);
String nonce = request.getHeader(WECHAT_PAY_NONCE);
String signType = request.getHeader(WeiXinConstant.WECHATPAY_SIGNATURE_TYPE);
@ -145,7 +147,7 @@ public class WxPayServiceImpl implements WxPayService {
NotificationConfig config = new RSAAutoCertificateConfig.Builder()
.merchantId(wechatConfig.getMchId())
.privateKeyFromPath(wechatConfig.getKeyPath())
.privateKey(wechatConfig.getKeyPath())
.merchantSerialNumber(wechatConfig.getMchSerialNo())
.apiV3Key(wechatConfig.getApiV3())
.build();
@ -401,7 +403,7 @@ public class WxPayServiceImpl implements WxPayService {
// 对参数进行加密
byte[] bytes = parameter.getBytes(String.valueOf(StandardCharsets.UTF_8));
Signature sign = Signature.getInstance("SHA256withRSA");
PrivateKey privateKey = PemUtil.loadPrivateKeyFromPath(wechatConfig.getKeyPath()); // privateKeyPath是商户证书密钥的位置apiclient_key.pem
PrivateKey privateKey = PemUtil.loadPrivateKeyFromString(wechatConfig.getKeyPath()); // privateKeyPath是商户证书密钥的位置apiclient_key.pem
sign.initSign(privateKey); // 商户密钥文件路径
sign.update(bytes);
String signature = Base64.getEncoder().encodeToString(sign.sign());
@ -425,7 +427,7 @@ public class WxPayServiceImpl implements WxPayService {
static void init(WechatConfig wechatConfig) {
instance = new RSAAutoCertificateConfig.Builder()
.merchantId(wechatConfig.getMchId())
.privateKeyFromPath(wechatConfig.getKeyPath())
.privateKey(wechatConfig.getKeyPath())
.merchantSerialNumber(wechatConfig.getMchSerialNo())
.apiV3Key(wechatConfig.getApiV3())
.build();

View File

@ -130,17 +130,17 @@ public class OrderServiceImpl implements OrderService {
orderItems.add(orderItemEntity);
//修改商品状态
if (Objects.equals(goodsType, GoodsTypeEnum.VIDEO.code)) {
VideoEntity videoEntity = new VideoEntity();
videoEntity.setId(goodsId);
videoEntity.setIsBuy(1);
videoMapper.update(videoEntity);
}else if (Objects.equals(goodsType, GoodsTypeEnum.SOURCE.code)) {
SourceEntity sourceEntity = new SourceEntity();
sourceEntity.setId(goodsId);
sourceEntity.setIsBuy(1);
sourceMapper.update(sourceEntity);
}
// if (Objects.equals(goodsType, GoodsTypeEnum.VIDEO.code)) {
// VideoEntity videoEntity = new VideoEntity();
// videoEntity.setId(goodsId);
// videoEntity.setIsBuy(1);
// videoMapper.update(videoEntity);
// }else if (Objects.equals(goodsType, GoodsTypeEnum.SOURCE.code)) {
// SourceEntity sourceEntity = new SourceEntity();
// sourceEntity.setId(goodsId);
// sourceEntity.setIsBuy(1);
// sourceMapper.update(sourceEntity);
// }
});
int addOrderItems = orderMapper.addOrderItems(orderItems);
if (addOrderItems == NumberConstant.ZERO) {
@ -157,12 +157,10 @@ public class OrderServiceImpl implements OrderService {
statisticsMapper.addStatisticsRecord(statisticsRecordAddReq);
//TODO 封装微信支付请求
// WxPayRespVO wxPayRespVO = initiatePayment(order, goodsDetailVO);
// return ApiResponse.success(wxPayRespVO);
WxPayRespVO wxPayRespVO = initiatePayment(order, goodsDetailVO);
return ApiResponse.success(wxPayRespVO);
return ApiResponse.success(null);
}
}
@ -187,7 +185,8 @@ public class OrderServiceImpl implements OrderService {
wxPayOrderReqVO.setOpenId(order.getOpenid())
.setOrderSn(order.getId())
.setTotalPrice(BigDecimalUtil.convertToCents(order.getPrice()))
.setGoodsName(goodsName);
.setGoodsName(goodsName)
.setDescription("VLOG视频支付");
return wxPayService.createOrder(wxPayOrderReqVO);
}

View File

@ -18,7 +18,7 @@ public interface WxPayService {
/**
* 微信支付回调
*/
void payNotify(HttpServletRequest request);
void payNotify(HttpServletRequest xml);
/**
* 微信支付结果查询

View File

@ -1,6 +1,7 @@
package com.ycwl.basic.utils;
import cn.hutool.core.util.XmlUtil;
import com.wechat.pay.java.core.util.PemUtil;
import org.springframework.util.Base64Utils;
@ -8,6 +9,7 @@ import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Map;
import java.util.Random;
/**
@ -26,7 +28,7 @@ public class WXPayUtil {
public static String getSign(String signatureStr,String privateKey) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IOException, URISyntaxException {
//replace 根据实际情况,不一定都需要
String replace = privateKey.replace("\\n", "\n");
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKeyFromPath(replace);
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKeyFromString(replace);
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(merchantPrivateKey);
sign.update(signatureStr.getBytes(StandardCharsets.UTF_8));
@ -46,4 +48,7 @@ public class WXPayUtil {
return new String(nonceChars);
}
public static Map<String, Object> xmlToMap(String xmlData) {
return XmlUtil.xmlToMap(xmlData);
}
}