You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
9.4 KiB
204 lines
9.4 KiB
package com.cweb.controller.pay;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.cweb.config.SysConst;
|
|
import com.hai.common.exception.ErrorCode;
|
|
import com.hai.common.exception.ErrorHelp;
|
|
import com.hai.common.exception.SysCode;
|
|
import com.hai.common.pay.WechatPayUtil;
|
|
import com.hai.common.pay.entity.OrderType;
|
|
import com.hai.common.pay.entity.WeChatPayReqInfo;
|
|
import com.hai.common.pay.util.MD5Util;
|
|
import com.hai.common.security.SessionObject;
|
|
import com.hai.common.security.UserCenter;
|
|
import com.hai.common.utils.MathUtils;
|
|
import com.hai.common.utils.ResponseMsgUtil;
|
|
import com.hai.entity.HighChildOrder;
|
|
import com.hai.entity.HighCoupon;
|
|
import com.hai.entity.HighOrder;
|
|
import com.hai.model.HighCouponModel;
|
|
import com.hai.model.ResponseData;
|
|
import com.hai.model.UserInfoModel;
|
|
import com.hai.service.HighCouponService;
|
|
import com.hai.service.HighOrderService;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.math.BigDecimal;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.SortedMap;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
/**
|
|
*
|
|
* @Title:
|
|
* @Description: 对订单的操作
|
|
* @author: 魏真峰
|
|
* @param:
|
|
* @return:
|
|
* @throws
|
|
*/
|
|
@Controller
|
|
@RequestMapping("/order")
|
|
@Api(value = "对订单的操作")
|
|
public class OrderController {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(OrderController.class);
|
|
|
|
@Autowired
|
|
private UserCenter userCenter;
|
|
|
|
@Resource
|
|
private WechatPayUtil wechatPayUtil;
|
|
|
|
@Resource
|
|
private HighOrderService highOrderService;
|
|
|
|
@Resource
|
|
private HighCouponService highCouponService;
|
|
|
|
/**
|
|
*
|
|
* @Title: orderToPay
|
|
* @Description: 订单支付发起支付
|
|
* @author: 魏真峰
|
|
* @param: [pageNum, pageSize]
|
|
* @return: com.shinwoten.haj.common.model.ResponseData
|
|
* @throws
|
|
*/
|
|
@RequestMapping(value="/orderToPay",method = RequestMethod.POST)
|
|
@ResponseBody
|
|
@ApiOperation(value = "订单支付发起支付")
|
|
public ResponseData orderToPay(@RequestBody String reqBodyStr) {
|
|
try {
|
|
if (StringUtils.isBlank(reqBodyStr)) {
|
|
log.error("orderToPay error!", "参数错误");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
|
|
}
|
|
JSONObject jsonObject = JSONObject.parseObject(reqBodyStr);
|
|
Long orderId = jsonObject.getLong("orderId");
|
|
String openId = jsonObject.getString("openId"); // openId
|
|
if ( orderId == null || StringUtils.isBlank(openId)) {
|
|
log.error("orderToPay error!", "参数错误");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
|
|
}
|
|
|
|
HighOrder order = highOrderService.getOrderById(orderId);
|
|
if(order == null) {
|
|
log.error("OrderController --> orderToPay() ERROR", "未找到订单信息");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_ORDER, "");
|
|
}
|
|
//校验订单状态 订单状态:1 待支付 2 已支付 3.已完成 4. 已退款 5.已取消
|
|
if(order.getOrderStatus() != 1) {
|
|
log.error("OrderController --> orderToPay() ERROR", "订单不处于待支付状态");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.ORDER_NO_STAY_PAY, "");
|
|
}
|
|
|
|
// 商品类型 1:卡卷 2:金币充值
|
|
for (HighChildOrder childOrder : order.getHighChildOrderList()) {
|
|
if (childOrder.getGoodsType() == 2) {
|
|
HighCouponModel coupon = highCouponService.getCouponById(childOrder.getGoodsId());
|
|
// 支付类型:1.微信支付 2.金币支付
|
|
if (coupon.getPayType() != 1) {
|
|
log.error("OrderController --> orderToPay() ERROR", "只支持微信支持");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, coupon.getCouponName() + ",只支持微信支付");
|
|
}
|
|
}
|
|
}
|
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
map.put("orderNo", order.getOrderNo());
|
|
map.put("payPrice", order.getPayPrice());
|
|
map.put("body","购买卡券");
|
|
map.put("subject","购买卡券");
|
|
|
|
//微信支付
|
|
String nonce_str = MD5Util.MD5Encode(String.valueOf(ThreadLocalRandom.current().nextInt(10000)), "UTF-8");
|
|
int total_fee = MathUtils.objectConvertBigDecimal(map.get("payPrice")).multiply(new BigDecimal("100")).intValue();
|
|
WeChatPayReqInfo weChatPayReqInfo = new WeChatPayReqInfo();
|
|
weChatPayReqInfo.setAppid(SysConst.getSysConfig().getWxAppId()); //公众号id
|
|
weChatPayReqInfo.setOpenid(openId);
|
|
weChatPayReqInfo.setMch_id(SysConst.getSysConfig().getWxMchId()); //商户号
|
|
weChatPayReqInfo.setNonce_str(nonce_str); //随机字符串
|
|
weChatPayReqInfo.setBody(map.get("body").toString()); //商品描述
|
|
weChatPayReqInfo.setOut_trade_no(map.get("orderNo").toString()); //商户订单号
|
|
weChatPayReqInfo.setTotal_fee(total_fee); //总金额
|
|
weChatPayReqInfo.setSpbill_create_ip("139.159.177.244"); //终端ip
|
|
weChatPayReqInfo.setNotify_url(SysConst.getSysConfig().getNotifyUrl()); //通知url
|
|
weChatPayReqInfo.setTrade_type("JSAPI"); //交易类型
|
|
// weChatPayReqInfo.setAttach(map.get("orderScene").toString()); //附加数据,区分订单类型
|
|
Map<String,String> payMap = new HashMap<>();
|
|
|
|
payMap.put("app_id",SysConst.getSysConfig().getWxAppId());
|
|
payMap.put("api_key",SysConst.getSysConfig().getWxApiKey());
|
|
payMap.put("unified_order_url",SysConst.getSysConfig().getWxUnifiedOrderUrl());
|
|
SortedMap<Object, Object> sortedMap = wechatPayUtil.goWechatPay(weChatPayReqInfo,payMap);
|
|
return ResponseMsgUtil.success(sortedMap);
|
|
} catch (Exception e) {
|
|
log.error("orderToPay error!", e);
|
|
return ResponseMsgUtil.exception(e);
|
|
}
|
|
}
|
|
|
|
|
|
@RequestMapping(value="/orderToGoldPay",method = RequestMethod.POST)
|
|
@ResponseBody
|
|
@ApiOperation(value = "订单支付发起金币支付")
|
|
public ResponseData orderToGoldPay(@RequestBody String reqBodyStr) {
|
|
try {
|
|
if (StringUtils.isBlank(reqBodyStr)) {
|
|
log.error("orderToPay error!", "参数错误");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
|
|
}
|
|
JSONObject jsonObject = JSONObject.parseObject(reqBodyStr);
|
|
Long orderId = jsonObject.getLong("orderId");
|
|
|
|
if (orderId == null) {
|
|
log.error("orderToPay error!", "参数错误");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
|
|
}
|
|
|
|
HighOrder order = highOrderService.getOrderById(orderId);
|
|
if(order == null) {
|
|
log.error("OrderController --> orderToPay() ERROR", "未找到订单信息");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_ORDER, "");
|
|
}
|
|
//校验订单状态 订单状态:1 待支付 2 已支付 3.已完成 4. 已退款 5.已取消
|
|
if(order.getOrderStatus() != 1) {
|
|
log.error("OrderController --> orderToPay() ERROR", "订单不处于待支付状态");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.ORDER_NO_STAY_PAY, "");
|
|
}
|
|
|
|
// 商品类型 1:卡卷 2:金币充值
|
|
for (HighChildOrder childOrder : order.getHighChildOrderList()) {
|
|
if (childOrder.getGoodsType() == 2) {
|
|
HighCouponModel coupon = highCouponService.getCouponById(childOrder.getGoodsId());
|
|
// 支付类型:1.微信支付 2.金币支付
|
|
if (coupon.getPayType() != 2) {
|
|
log.error("OrderController --> orderToPay() ERROR", "只支持金币支持");
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, coupon.getCouponName() + ",只支持金币支付");
|
|
}
|
|
}
|
|
}
|
|
|
|
highOrderService.goldPayOrder(order.getMemId(), order.getId());
|
|
|
|
return ResponseMsgUtil.success("支付成功");
|
|
} catch (Exception e) {
|
|
log.error("orderToPay error!", e);
|
|
return ResponseMsgUtil.exception(e);
|
|
}
|
|
}
|
|
}
|
|
|