handle out of time
This commit is contained in:
@@ -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;
|
||||
// 随机串
|
||||
String nonceStr = UUID.randomUUID().toString().replace("-", "");
|
||||
String sign = wxPayUtil.getSign(wechatPayConfig.getAppId(), timestamp, nonceStr, prepayId);
|
||||
log.info("签名:{}", sign);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("prepayid", prepayId);
|
||||
map.put("timestamp", timestamp);
|
||||
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();
|
||||
// 判断订单是否已经过期
|
||||
if (order.getOrderStatus().equals("5")) {
|
||||
return R.error("订单支付超时");
|
||||
}
|
||||
return R.ok().put("paramMap", paramMap).put("Map", map);
|
||||
String nonceStr = UUID.randomUUID().toString().replace("-", "");
|
||||
Map<String, Object> paramMap = new HashMap<>();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
paramMap.put("appid", wechatPayConfig.getAppId());
|
||||
map.put("noncestr", nonceStr);
|
||||
map.put("package", "Sign=WXPay");
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Data
|
||||
public class WechatDto implements Serializable {
|
||||
public class WechatPaymentInfo implements Serializable {
|
||||
|
||||
/**
|
||||
* 订单号
|
||||
@@ -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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user