|
|
@ -12,7 +12,10 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import javax.servlet.ServletInputStream; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
@ -31,6 +34,8 @@ public class OrderService extends MybatisBaseService<OrderMapper, PayOrder> { |
|
|
|
String tradeType = "JSAPI"; |
|
|
|
//微信统一下单
|
|
|
|
String uniformorder = "https://api.mch.weixin.qq.com/pay/unifiedorder"; |
|
|
|
//订单查询
|
|
|
|
String orderquery = "https://api.mch.weixin.qq.com/pay/orderquery"; |
|
|
|
|
|
|
|
public ResultBean<String> createOrder(OrderDto dto) { |
|
|
|
ResultBean<String> rb = ResultBean.fireFail(); |
|
|
@ -191,9 +196,14 @@ public class OrderService extends MybatisBaseService<OrderMapper, PayOrder> { |
|
|
|
return rb; |
|
|
|
} |
|
|
|
|
|
|
|
public ResultBean wxPayNotify(ServletInputStream inputStream) { |
|
|
|
public ResultBean wxPayNotify(HttpServletRequest request, HttpServletResponse response) { |
|
|
|
ResultBean rb = ResultBean.fireFail(); |
|
|
|
try { |
|
|
|
request.setCharacterEncoding("UTF-8"); |
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
response.setContentType("text/html;charset=UTF-8"); |
|
|
|
response.setHeader("Access-Control-Allow-Origin", "*"); |
|
|
|
InputStream inputStream = request.getInputStream(); |
|
|
|
ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
int len = 0; |
|
|
@ -211,6 +221,9 @@ public class OrderService extends MybatisBaseService<OrderMapper, PayOrder> { |
|
|
|
// 业务处理
|
|
|
|
//根据订单编号查询
|
|
|
|
PayOrder payOrder = baseMapper.selectBySn(out_trade_no); |
|
|
|
if (payOrder == null) { |
|
|
|
return rb.setMsg("订单不存在"); |
|
|
|
} |
|
|
|
payOrder.setState(2); |
|
|
|
payOrder.setModifyTime(new Date()); |
|
|
|
payOrder.setPayTypeValue("微信"); |
|
|
@ -243,4 +256,66 @@ public class OrderService extends MybatisBaseService<OrderMapper, PayOrder> { |
|
|
|
map.put("payType", payOrder.getPayTypeValue()); |
|
|
|
return rb.success().setData(map); |
|
|
|
} |
|
|
|
|
|
|
|
public ResultBean orderQuery(OrderQuery query) { |
|
|
|
ResultBean rb = ResultBean.fireFail(); |
|
|
|
PayOrder payOrder = fetchBySid(query.getMainSid()); |
|
|
|
if (payOrder == null) { |
|
|
|
return rb.setMsg("订单不存在"); |
|
|
|
} |
|
|
|
WxPayVo wxPayVo = new WxPayVo(payOrder.getSource()); |
|
|
|
Map<Object, Object> parame = new TreeMap<Object, Object>(); |
|
|
|
parame.put("appid", wxPayVo.getAppId()); |
|
|
|
// 商家账号。
|
|
|
|
parame.put("mch_id", wxPayVo.getMchId()); |
|
|
|
String randomStr = CharUtil.getRandomNum(18).toUpperCase(); |
|
|
|
// 随机字符串
|
|
|
|
parame.put("nonce_str", randomStr); |
|
|
|
// 商户订单编号
|
|
|
|
parame.put("out_trade_no", payOrder.getOutTradeNo()); |
|
|
|
|
|
|
|
String sign = WechatUtil.arraySign(parame, wxPayVo.getSecret()); |
|
|
|
// 数字签证
|
|
|
|
parame.put("sign", sign); |
|
|
|
|
|
|
|
String xml = MapUtils.convertMap2Xml(parame); |
|
|
|
log.info("xml:" + xml); |
|
|
|
Map<String, Object> resultUn = null; |
|
|
|
try { |
|
|
|
resultUn = XmlUtil.xmlStrToMap(WechatUtil.requestOnce(orderquery, xml)); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return rb.setMsg("查询失败,error=" + e.getMessage()); |
|
|
|
} |
|
|
|
// 响应报文
|
|
|
|
String return_code = MapUtils.getString("return_code", resultUn); |
|
|
|
String return_msg = MapUtils.getString("return_msg", resultUn); |
|
|
|
|
|
|
|
if (!"SUCCESS".equals(return_code)) { |
|
|
|
return rb.setMsg("查询失败,error=" + return_msg); |
|
|
|
} |
|
|
|
|
|
|
|
String trade_state = MapUtils.getString("trade_state", resultUn); |
|
|
|
if ("SUCCESS".equals(trade_state)) { |
|
|
|
payOrder.setState(2); |
|
|
|
payOrder.setModifyTime(new Date()); |
|
|
|
baseMapper.updateById(payOrder); |
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
payOrder = fetchBySid(query.getMainSid()); |
|
|
|
map.put("createTime", DateUtils.format(payOrder.getCreateTime(), "yyyy-MM-dd HH:mm:ss")); |
|
|
|
map.put("payTime", DateUtils.format(payOrder.getModifyTime(), "yyyy-MM-dd HH:mm:ss")); |
|
|
|
map.put("outTradeNo", payOrder.getOutTradeNo()); |
|
|
|
map.put("mainSid", payOrder.getSid()); |
|
|
|
map.put("trade_type", payOrder.getPayTypeValue());//交易类型
|
|
|
|
return rb.success().setData(map); |
|
|
|
} else if ("USERPAYING".equals(trade_state)) { |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
// 失败
|
|
|
|
return rb.setMsg("查询失败,error=" + trade_state); |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|