handle out of time

This commit is contained in:
Cauchy
2023-10-10 16:24:03 +08:00
parent 6ab08f7a29
commit f3df8b326b
14 changed files with 636 additions and 495 deletions

View File

@@ -0,0 +1,59 @@
package com.peanut.config;
/**
* @Description: 常量类
* @Author: Cauchy
* @CreateTime: 2023/10/10
*/
public class Constants {
/**
* 订单状态 - 待支付
*/
public static final String ORDER_STATUS_TO_BE_PAID = "0";
/**
* 订单状态 - 待发货
*/
public static final String ORDER_STATUS_TO_BE_SHIPPED = "1";
/**
* 订单状态 - 已发货
*/
public static final String ORDER_STATUS_SHIPPED = "2";
/**
* 订单状态 - 已发货
*/
public static final String ORDER_STATUS_FINISHED = "3";
/**
* 订单状态 - 交易失败
*/
public static final String ORDER_STATUS_FAIL = "4";
/**
* 订单状态 - 订单超时
*/
public static final String ORDER_STATUS_OUT_OF_TIME = "5";
/**
* 支付方式 - 微信支付
*/
public static final String PAYMENT_METHOD_WECHAT_PAY = "1";
/**
* 支付方式 - 支付宝 - 支付宝支付
*/
public static final String PAYMENT_METHOD_ALI_PAY = "2";
/**
* 支付方式 - IOS 内购
*/
public static final String PAYMENT_METHOD_IOS = "3";
/**
* 支付方式 - 虚拟币支付
*/
public static final String PAYMENT_METHOD_VIRTUAL = "4";
/**
* 购买方式 - 直接购买
*/
public static final String BUY_TYPE_REDIRECT = "0";
/**
* 购买方式 - 购物车
*/
public static final String BUY_TYPE_CART = "1";
}

View File

@@ -11,6 +11,7 @@ import java.util.Map;
/**
* RabbitMq 延时队列实现
*
* @author AnYuan
*/
@@ -41,6 +42,19 @@ public class DelayQueueConfig {
public static final String DEAD_LETTER_QUEUE_ROUTING_KEY = "delay.queue.deadLetter.delay_10s.routingKey";
public static final String DEAD_LETTER_QUEUE = "delay.queue.deadLetter.queue";
/**
* 订单待支付交换机
*/
public static final String ORDER_TO_BE_PAY_EXCHANGE = "order_to_be_pay_exchange";
public static final String ORDER_TO_BE_PAY_ROUTING_KEY = "order_to_be_pay_routingKey";
public static final String ORDER_TO_BE_PAY_QUEUE = "order_to_be_pay_queue";
/**
* 订单取消队列
*/
public static final String ORDER_CANCEL_DEAD_LETTER_EXCHANGE = "order_cancel_dead_letter_exchange";
public static final String ORDER_CANCEL_DEAD_LETTER_ROUTING_KEY = "order_cancel_dead_letter_routingKey";
public static final String ORDER_CANCEL_DEAD_LETTER_QUEUE = "order_cancel_dead_letter_queue";
/**
* 快递队列
@@ -52,6 +66,7 @@ public class DelayQueueConfig {
/**
* 声明 死信交换机
*
* @return deadLetterExchange
*/
@Bean
@@ -62,6 +77,7 @@ public class DelayQueueConfig {
/**
* 声明 订单交换机
*
* @return deadLetterExchange
*/
@Bean
@@ -97,9 +113,9 @@ public class DelayQueueConfig {
}
/**
* 声明 死信队列 用于接收死信消息
*
* @return deadLetterQueueA
*/
@Bean
@@ -109,6 +125,7 @@ public class DelayQueueConfig {
/**
* 将 死信队列 绑定到死信交换机上
*
* @return deadLetterBindingA
*/
@Bean
@@ -121,6 +138,7 @@ public class DelayQueueConfig {
/**
* 声明 延时交换机
*
* @return delayExchange
*/
@Bean
@@ -130,6 +148,7 @@ public class DelayQueueConfig {
/**
* 将 延时队列 绑定参数
*
* @return Queue
*/
@Bean
@@ -146,6 +165,7 @@ public class DelayQueueConfig {
/**
* 将 延时队列 绑定到延时交换机上面
*
* @return delayBindingA
*/
@Bean
@@ -172,6 +192,7 @@ public class DelayQueueConfig {
public Queue FMSQueue() {
return new Queue(FMS_QUEUE);
}
@Bean
TopicExchange FMSExchange() {
return new TopicExchange(FMS_EXCHANGE);
@@ -181,5 +202,38 @@ public class DelayQueueConfig {
return BindingBuilder.bind(FMSQueue()).to(FMSExchange()).with(FMS_ROUTING_KEY);
}
@Bean
public DirectExchange orderCancelDeadLetterExchange() {
return new DirectExchange(ORDER_CANCEL_DEAD_LETTER_EXCHANGE);
}
@Bean
public Queue orderCancelDeadLetterQueue() {
return new Queue(ORDER_CANCEL_DEAD_LETTER_QUEUE);
}
@Bean
public DirectExchange orderExchange() {
return new DirectExchange(ORDER_TO_BE_PAY_EXCHANGE);
}
@Bean
public Queue orderQueue() {
Map<String, Object> arguments = new HashMap<>(2);
// 绑定死信交换机
arguments.put("x-dead-letter-exchange", ORDER_CANCEL_DEAD_LETTER_EXCHANGE);
// 绑定我们的路由key
arguments.put("x-dead-letter-routing-key", ORDER_CANCEL_DEAD_LETTER_ROUTING_KEY);
return new Queue(ORDER_TO_BE_PAY_QUEUE, true, false, false, arguments);
}
@Bean
public Binding orderBinding() {
return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_TO_BE_PAY_ROUTING_KEY);
}
@Bean
public Binding binding() {
return BindingBuilder.bind(orderCancelDeadLetterQueue()).to(orderCancelDeadLetterExchange()).with(ORDER_CANCEL_DEAD_LETTER_ROUTING_KEY);
}
}

View File

@@ -1,26 +1,29 @@
package com.peanut.modules.book.controller;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.peanut.common.utils.PageUtils;
import com.peanut.common.utils.R;
import com.peanut.config.Constants;
import com.peanut.config.DelayQueueConfig;
import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.service.*;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import com.peanut.modules.pay.weChatPay.service.WxpayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -54,44 +57,10 @@ public class BuyOrderController {
private UserEbookBuyService userEbookBuyService;
@Autowired
private UserRecordService userRecordService;
/**
* 订单状态 - 待支付
*/
private static final String ORDER_STATUS_TO_BE_PAID = "0";
/**
* 订单状态 - 待发货
*/
private static final String ORDER_STATUS_TO_BE_SHIPPED = "1";
/**
* 订单状态 - 待收货
*/
private static final String ORDER_STATUS_TO_BE_RECEIVED = "2";
/**
* 支付方式 - 支付宝 - 支付宝支付
*/
private static final String PAYMENT_METHOD_ALI_PAY = "1";
/**
* 支付方式 - 微信支付
*/
private static final String PAYMENT_METHOD_WECHAT_PAY = "2";
/**
* 支付方式 - IOS 内购
*/
private static final String PAYMENT_METHOD_IOS = "3";
/**
* 支付方式 - 虚拟币支付
*/
private static final String PAYMENT_METHOD_VIRTUAL = "4";
/**
* 购买方式 - 直接购买
*/
private static final String BUY_TYPE_REDIRECT = "0";
/**
* 购买方式 - 购物车
*/
private static final String BUY_TYPE_CART = "1";
@Autowired
private WxpayService wxpayService;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private ShopProudictBookService shopProudictBookService;
@@ -144,7 +113,7 @@ public class BuyOrderController {
*/
@RequestMapping("/buySave")
@Transactional
public R buySave(@RequestBody BuyOrderEntity buyOrder) {
public R buySave(@RequestBody BuyOrderEntity buyOrder) throws IOException {
// 获取订单详情
List<BuyOrderDetailEntity> products = buyOrder.getProducts();
// 订单总金额
@@ -163,7 +132,7 @@ public class BuyOrderController {
buyOrderDetail.setProductPrice(product.getPrice());
buyOrderDetail.setAddressId(buyOrder.getAddressId());
buyOrderDetail.setProductUrl(product.getProductImages());
buyOrderDetail.setOrderStatus(ORDER_STATUS_TO_BE_PAID);
buyOrderDetail.setOrderStatus(Constants.ORDER_STATUS_TO_BE_PAID);
}
totalPrice = totalPrice.subtract(useCouponAmount(buyOrder));
@@ -176,21 +145,38 @@ public class BuyOrderController {
for (BuyOrderDetailEntity buyOrderDetail : products) {
buyOrderDetail.setOrderId(buyOrder.getOrderId());
buyOrderDetail.setUserId(buyOrder.getUserId());
if (BUY_TYPE_CART.equals(buyOrder.getBuyType())) {
if (Constants.BUY_TYPE_CART.equals(buyOrder.getBuyType())) {
handleBuyCart(buyOrder, buyOrderDetail);
}
}
buyOrderDetailService.saveBatch(products);
if (PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) {
buyOrder.setOrderStatus(ORDER_STATUS_TO_BE_SHIPPED);
// 1. 虚拟币支付
if (Constants.PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) {
buyOrder.setOrderStatus(Constants.ORDER_STATUS_TO_BE_SHIPPED);
MyUserEntity user = this.myUserService.getById(buyOrder.getUserId());
if (usePeanutCoin(user, totalPrice)) {
// 更新订单状态
buyOrderService.updateOrderStatus(user.getId(), buyOrder.getOrderSn(), "0");
recordTransaction(buyOrder, user, totalPrice);
addEbookToUser(products, buyOrder);
} else {
return R.error(500, "花生币余额不足!");
}
}
// 2. 微信支付
if (Constants.PAYMENT_METHOD_WECHAT_PAY.equals(buyOrder.getPaymentMethod())) {
rabbitTemplate.convertAndSend(
DelayQueueConfig.ORDER_TO_BE_PAY_EXCHANGE,
DelayQueueConfig.ORDER_TO_BE_PAY_ROUTING_KEY,
buyOrder.getOrderId(),
messagePostProcessor()
);
WechatPaymentInfo paymentInfo = new WechatPaymentInfo();
paymentInfo.setOrderSn(orderSn);
paymentInfo.setBuyOrderId(buyOrder.getOrderId());
paymentInfo.setTotalAmount(totalPrice);
wxpayService.prepay(paymentInfo);
}
Map<String, Object> result = new HashMap<>();
result.put("orderSn", buyOrder.getOrderSn());
result.put("money", totalPrice);
@@ -557,4 +543,13 @@ public class BuyOrderController {
orderCartService.removeByIds(collect);
}
}
private MessagePostProcessor messagePostProcessor() {
return message -> {
//设置有效期30分钟
//message.getMessageProperties().setExpiration("1800000");
message.getMessageProperties().setExpiration("30000");
return message;
};
}
}

View File

@@ -113,4 +113,7 @@ public class BuyOrderDetailEntity implements Serializable {
@TableField("record_id")
private Integer recordId;
@TableField(exist = false)
private Long timestamp;
}

View File

@@ -21,9 +21,6 @@ import lombok.Data;
public class BuyOrderEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@TableId
private Integer orderId;
/**
@@ -47,8 +44,6 @@ public class BuyOrderEntity implements Serializable {
* 收货人手机号
*/
private String userPhone;
/**
* 省
*/
@@ -66,7 +61,7 @@ public class BuyOrderEntity implements Serializable {
*/
private String address;
/**
* 支付方式 1支付宝2微信3ios内购 4虚拟币
* 支付方式 1微信2支付宝3ios内购 4虚拟币
*/
private String paymentMethod;
/**
@@ -109,6 +104,7 @@ public class BuyOrderEntity implements Serializable {
* 2: 已发货
* 3已完成
* 4: 交易失败
* 5: 已过期
*/
private String orderStatus;
/**
@@ -153,19 +149,14 @@ public class BuyOrderEntity implements Serializable {
* 地址id
*/
private Integer addressId;
/**
* 订单备注
*/
private String remark;
/**
* 快递鸟订单编号
*/
private String orderCode;
/**
* 支付时间
*/
@@ -175,4 +166,7 @@ public class BuyOrderEntity implements Serializable {
@TableField("record_id")
private Integer recordId;
@TableField(exist = false)
private Long timestamp;
}

View File

@@ -16,5 +16,7 @@ import java.util.Map;
public interface PayWechatOrderService extends IService<PayWechatOrderEntity> {
PageUtils queryPage(Map<String, Object> params);
void add(String orderSn,String prepayId);
}

View File

@@ -1,6 +1,5 @@
package com.peanut.modules.book.service.impl;
import cn.com.marsoft.tool.ToolObject;
import com.alibaba.druid.mock.MockConnection;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@@ -30,9 +29,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.xml.soap.Text;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@@ -128,6 +124,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrderEntity
List<BuyOrderEntity> records = page.getRecords();
for (BuyOrderEntity buyOrderEntity : records) {
Integer orderId = buyOrderEntity.getOrderId();
buyOrderEntity.setTimestamp(buyOrderEntity.getCreateTime().getTime()/1000);
List<BuyOrderDetailEntity> entities = buyOrderDetailService.getBaseMapper().selectList(new QueryWrapper<BuyOrderDetailEntity>()
.eq("order_id", orderId));
if (entities != null) {
@@ -141,7 +138,6 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrderEntity
}
}
}
@@ -669,7 +665,6 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrderEntity
}
public static String getRandom(int len) {
Random r = new Random();
StringBuilder rs = new StringBuilder();
@@ -691,6 +686,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrderEntity
}
return R.ok(Xsode);
}
/*
打印时查重
*/
@@ -738,7 +734,6 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrderEntity
}
/**
* 计算仪器,预售快递费用
*
@@ -801,6 +796,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrderEntity
/**
* 计算快递费用
*
* @param param
*/
public int getTransPrice(Map<String, Object> param) {

View File

@@ -6,17 +6,23 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.common.utils.PageUtils;
import com.peanut.common.utils.Query;
import com.peanut.modules.book.dao.PayWechatOrderDao;
import com.peanut.modules.book.entity.BuyOrderEntity;
import com.peanut.modules.book.entity.PayWechatOrderEntity;
import com.peanut.modules.book.service.BuyOrderService;
import com.peanut.modules.book.service.PayWechatOrderService;
import com.peanut.modules.book.service.PayZfbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
@Service("payWechatOrderService")
public class PayWechatOrderServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOrderEntity> implements PayWechatOrderService {
@Autowired
BuyOrderService buyOrderService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<PayWechatOrderEntity> page = this.page(
@@ -27,4 +33,21 @@ public class PayWechatOrderServiceImpl extends ServiceImpl<PayWechatOrderDao, Pa
return new PageUtils(page);
}
@Override
public void add(String orderSn, String prepayId) {
QueryWrapper<BuyOrderEntity> wrapper = new QueryWrapper<>();
wrapper.eq("order_sn", orderSn);
BuyOrderEntity buyOrder = buyOrderService.getOne(wrapper);
PayWechatOrderEntity entity = new PayWechatOrderEntity();
entity.setCustomerId(buyOrder.getUserId());
entity.setCreateTime(new Date());
entity.setOrderSn(buyOrder.getOrderSn());
entity.setPrepayId(prepayId);
entity.setTotalAmount(buyOrder.getRealMoney());
entity.setSystemLog("预支付完成");
entity.setPayType(buyOrder.getOrderType());
entity.setOrderId(buyOrder.getOrderSn());
this.save(entity);
}
}

View File

@@ -0,0 +1,27 @@
package com.peanut.modules.mq.Consumer;
import com.peanut.config.Constants;
import com.peanut.config.DelayQueueConfig;
import com.peanut.modules.book.entity.BuyOrderEntity;
import com.peanut.modules.book.service.BuyOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Description: 消息超时取消死信队列消费者
* @Author: Cauchy
* @CreateTime: 2023/10/10
*/
@Component
public class OrderCancelConsumer {
@Autowired
BuyOrderService buyOrderService;
@RabbitListener(queues = DelayQueueConfig.ORDER_CANCEL_DEAD_LETTER_QUEUE)
public void orderConsumer(String orderId) {
BuyOrderEntity buyOrder = buyOrderService.getById(orderId);
buyOrder.setOrderStatus(Constants.ORDER_STATUS_OUT_OF_TIME);
buyOrderService.updateById(buyOrder);
}
}

View File

@@ -1,6 +1,5 @@
package com.peanut.modules.pay.weChatPay.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -8,26 +7,19 @@ import com.peanut.common.utils.R;
import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.service.*;
import com.peanut.modules.pay.weChatPay.config.WechatPayConfig;
import com.peanut.modules.pay.weChatPay.dto.WechatDto;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import com.peanut.modules.pay.weChatPay.util.HttpUtils;
import com.peanut.modules.pay.weChatPay.util.WechatPayValidator;
import com.peanut.modules.pay.weChatPay.util.WxPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
@@ -66,104 +58,46 @@ public class WeChatPayController {
@Autowired
private UserEbookBuyService userEbookBuyService;
//无需应答签名
@Autowired
private CloseableHttpClient wxPayClient;
@Autowired
private WxPayUtil wxPayUtil;
/**
* 生成预订单
*
* @param dto
* @param paymentInfo 微信支付信息
* @return
* @throws Exception
*/
@RequestMapping(value = "/placeAnOrder/shoppingPay")
@Transactional(rollbackFor = Exception.class)
public R shoppingPay(@RequestBody WechatDto dto) throws Exception {
log.info("生成预订单");
public R newShoppingPay(@RequestBody WechatPaymentInfo paymentInfo) {
QueryWrapper<BuyOrderEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("order_sn", dto.getOrderSn());
queryWrapper.eq("order_sn", paymentInfo.getOrderSn());
BuyOrderEntity order = buyOrderService.getOne(queryWrapper);
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("appid", wechatPayConfig.getAppId());
paramMap.put("mchid", wechatPayConfig.getMchId());
paramMap.put("description", "微信支付");
// 订单编号
paramMap.put("out_trade_no", order.getOrderSn());
// 微信回调地址
paramMap.put("notify_url", wechatPayConfig.getNotifyUrl());
BigDecimal totalAmount = dto.getTotalAmount();
// 这里 * 100 单位为 ‘分’
BigDecimal hand = new BigDecimal("100");
totalAmount = totalAmount.multiply(hand);
Map<String, Object> amountMap = new HashMap<>();
amountMap.put("total", totalAmount);
amountMap.put("currency", "CNY");
paramMap.put("amount", amountMap);
JSONObject json = JSONObject.parseObject(JSON.toJSONString(paramMap));
log.info("请求参数:{}", paramMap);
JSONObject responseJson = wxPayUtil.doPostWexinV3(wechatPayConfig.getPayUrl(), json.toJSONString());
String prepayId = responseJson.getString("prepay_id");
// 传入参数 payUrl 发送post请求
HttpPost httpPost = new HttpPost(wechatPayConfig.getPayUrl());
// 将json数据转换成字符串
StringEntity entity = new StringEntity(json.toString(), "utf-8");
// 设置该请求的Content-Type为application/json 都是json格式
entity.setContentType("application/json");
// 将实体对象设置到HttpPost表示要传递该数据到服务器端。
httpPost.setEntity(entity);
// 设置请求头部的Accept属性为"application/json"表示客户端希望接收的为json。
httpPost.setHeader("Accept", "application/json");
CloseableHttpResponse response = wxPayClient.execute(httpPost);
// 向微信支付平台发送请求,处理响应结果,并将订单信息保存到数据库中。
String bodyAsString = EntityUtils.toString(response.getEntity());
// 时间戳
long timestamp = System.currentTimeMillis() / 1000;
// 随机串
// 判断订单是否已经过期
if (order.getOrderStatus().equals("5")) {
return R.error("订单支付超时");
}
String nonceStr = UUID.randomUUID().toString().replace("-", "");
String sign = wxPayUtil.getSign(wechatPayConfig.getAppId(), timestamp, nonceStr, prepayId);
log.info("签名:{}", sign);
Map<String, Object> paramMap = new HashMap<>();
Map<String, Object> map = new HashMap<>();
map.put("prepayid", prepayId);
map.put("timestamp", timestamp);
paramMap.put("appid", wechatPayConfig.getAppId());
map.put("noncestr", nonceStr);
map.put("sign", sign);
map.put("appid", wechatPayConfig.getAppId());
map.put("package", "Sign=WXPay");
map.put("extData", "sign");
map.put("partnerid", wechatPayConfig.getMchId());
try {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
log.info("返回结果:{}", bodyAsString);
// 添加微信支付订单信息
PayWechatOrderEntity wechat = new PayWechatOrderEntity();
wechat.setCustomerId(order.getUserId());
wechat.setCreateTime(new Date());
wechat.setOrderSn(order.getOrderSn());
wechat.setPrepayId(prepayId);
wechat.setTotalAmount(order.getRealMoney());
wechat.setSystemLog(response.toString());
wechat.setPayType(order.getOrderType());
wechat.setOrderId(order.getOrderSn());
wechat.setBuyOrderId(dto.getBuyOrderId());
this.payWechatOrderService.save(wechat);
} else if (statusCode == 204) { //处理成功无返回Body
log.info("支付成功");
} else {
log.error("下单失败,响应码为:{},返回结果:{}", statusCode, bodyAsString);
throw new IOException("request failed");
}
} finally {
response.close();
}
return R.ok().put("paramMap", paramMap).put("Map", map);
paramMap.put("mchid", wechatPayConfig.getMchId());
QueryWrapper<PayWechatOrderEntity> wechatOrderQueryWrapper = new QueryWrapper<>();
wechatOrderQueryWrapper.eq("order_sn", paymentInfo.getOrderSn());
PayWechatOrderEntity payWechatOrder = payWechatOrderService.getOne(wechatOrderQueryWrapper);
String prepayId = payWechatOrder.getPrepayId();
map.put("prepayid", prepayId);
long timestamp = System.currentTimeMillis() / 1000;
map.put("timestamp", timestamp);
String sign = wxPayUtil.getSign(wechatPayConfig.getAppId(), timestamp, nonceStr, prepayId);
map.put("sign", sign);
Map<String, Object> result = new HashMap<>();
result.put("paramMap", paramMap);
result.put("Map", map);
return R.ok(result);
}
/**

View File

@@ -6,7 +6,7 @@ import java.io.Serializable;
import java.math.BigDecimal;
@Data
public class WechatDto implements Serializable {
public class WechatPaymentInfo implements Serializable {
/**
* 订单号

View File

@@ -1,15 +1,17 @@
package com.peanut.modules.pay.weChatPay.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.peanut.common.utils.R;
import com.peanut.modules.book.entity.PayWechatOrderEntity;
import com.peanut.modules.pay.weChatPay.dto.WechatDto;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import java.io.IOException;
import java.util.Map;
@Service
public interface WxpayService extends IService<PayWechatOrderEntity> {
void prepay(WechatPaymentInfo wechatDto) throws IOException;
}

View File

@@ -1,11 +1,63 @@
package com.peanut.modules.pay.weChatPay.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.book.dao.PayWechatOrderDao;
import com.peanut.modules.book.entity.PayWechatOrderEntity;
import com.peanut.modules.book.service.PayWechatOrderService;
import com.peanut.modules.pay.weChatPay.config.WechatPayConfig;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import com.peanut.modules.pay.weChatPay.service.WxpayService;
import com.peanut.modules.pay.weChatPay.util.WxPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOrderEntity> implements WxpayService {
@Autowired
private WxPayUtil wxPayUtil;
@Autowired
private WechatPayConfig wechatPayConfig;
@Autowired
private PayWechatOrderService payWechatOrderService;
@Override
public void prepay(WechatPaymentInfo paymentInfo) throws IOException {
Map<String, Object> paramMap = new HashMap<>();
// app id
paramMap.put("appid", wechatPayConfig.getAppId());
// 商户 id
paramMap.put("mchid", wechatPayConfig.getMchId());
// 描述
paramMap.put("description", "微信支付");
// 订单编号
paramMap.put("out_trade_no", paymentInfo.getOrderSn());
// 微信回调地址
paramMap.put("notify_url", wechatPayConfig.getNotifyUrl());
BigDecimal totalAmount = paymentInfo.getTotalAmount();
// 这里 * 100 单位为 ‘分’
BigDecimal hand = new BigDecimal("100");
totalAmount = totalAmount.multiply(hand);
Map<String, Object> amountMap = new HashMap<>();
amountMap.put("total", totalAmount.intValue());
amountMap.put("currency", "CNY");
paramMap.put("amount", amountMap);
JSONObject json = JSONObject.parseObject(JSON.toJSONString(paramMap));
log.info("请求参数:{}", paramMap);
JSONObject responseJson = wxPayUtil.doPostWexinV3(wechatPayConfig.getPayUrl(), json.toJSONString());
String prepayId = responseJson.getString("prepay_id");
payWechatOrderService.add(paymentInfo.getOrderSn(), prepayId);
}
}

View File

@@ -5,19 +5,19 @@ wxpay.mchId:1612860909
# ?? URL
wxpay.payUrl:https://api.mch.weixin.qq.com/v3/pay/transactions/app
# ????
wxpay.notifyUrl:https://testapi.nuttyreading.com/pay/payNotify
wxpay.notifyUrl:https://api.nuttyreading.com/pay/payNotify
# ?? url
wxpay.refundNotifyUrl:http://pjm6m9.natappfree.cc/pay/refundNotify
# key pem
wxpay.keyPemPath:/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem
#wxpay.keyPemPath:C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem
#wxpay.keyPemPath:/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem
wxpay.keyPemPath:C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem
# ???
wxpay.serialNo:679AECB2F7AC4183033F713828892BA640E4EEE3
# API v3 key
wxpay.apiV3Key:4aYFklzaULeGlr7oJPZ6rHWKcxjihZUF
# ????
wxpay.wechatPayCertificateUrl:/usr/local/hs/peanut_book/target/classes/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem
# wxpay.wechatPayCertificateUrl:C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem
#wxpay.wechatPayCertificateUrl:/usr/local/hs/peanut_book/target/classes/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem
wxpay.wechatPayCertificateUrl:C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem
# ?? url
wxpay.privateKeyUrl:/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem
#wxpay.privateKeyUrl:C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem
#wxpay.privateKeyUrl:/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem
wxpay.privateKeyUrl:C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem