|
|
|
@@ -0,0 +1,223 @@ |
|
|
|
package com.iformall.controller; |
|
|
|
|
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.common.IdWorker; |
|
|
|
import com.iformall.common.Result; |
|
|
|
import com.iformall.common.ResultData; |
|
|
|
import com.iformall.domain.po.*; |
|
|
|
import com.iformall.domain.po.tt.TtCUser; |
|
|
|
import com.iformall.enums.EnumCreditLockedStatus; |
|
|
|
import com.iformall.enums.EnumOrderStatus; |
|
|
|
import com.iformall.enums.EnumPayStatus; |
|
|
|
import com.iformall.enums.EnumPayWay; |
|
|
|
import com.iformall.exception.MallinkException; |
|
|
|
import com.iformall.service.*; |
|
|
|
import com.iformall.service.pay.entity.PayExtraParam; |
|
|
|
import com.iformall.utils.IPUtil; |
|
|
|
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.web.bind.annotation.*; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
@RestController |
|
|
|
@RequestMapping("/api/pay") |
|
|
|
@Api(description = "支付相关接口") |
|
|
|
public class TtPayOrderController extends BaseController { |
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxPayOrderService wxPayOrderService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxOrderService wxOrderService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxCUserBasicInfoService wxCUserBasicInfoService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxPayAccountService wxPayAccountService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxCouponService wxCouponService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxAppinfoService wxAppinfoService; |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信支付 |
|
|
|
* @param paramMap |
|
|
|
* @param request |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
//@ApiOperation(value = "发起微信小程序支付订单", notes = "{\"orderId\":\"string\"}") |
|
|
|
@RequestMapping(value = "/create", method = RequestMethod.POST) |
|
|
|
public ResultData _create(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws Exception { |
|
|
|
logger.info("/api/pay/create" + paramMap.toString()); |
|
|
|
String orderIdStr = paramMap.get("orderId"); |
|
|
|
if (StringUtils.isBlank(orderIdStr)) { |
|
|
|
logger.info("orderId不能为空: " + paramMap.toString()); |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空"); |
|
|
|
} |
|
|
|
|
|
|
|
TtCUser user = getTtCUser(); |
|
|
|
if (!user.basicInfoIs()) { |
|
|
|
logger.error("暂未成为会员,请授权手机号"); |
|
|
|
return new ResultData(ErrorCode.USER_IS_NOT_MEMBER.getCode(), "暂未成为会员,请授权手机号"); |
|
|
|
} |
|
|
|
|
|
|
|
return _create(orderIdStr,user.getUserId(),EnumPayWay.PAY_WAY_TT,user.getAppId(),new PayExtraParam("openId",user.getOpenId()),request); |
|
|
|
} |
|
|
|
|
|
|
|
private ResultData _create(String orderIdStr,Long userId, EnumPayWay payWay,String appId,PayExtraParam parm,HttpServletRequest request) { |
|
|
|
WxCUserBasicInfo wxCUserBasicInfo = wxCUserBasicInfoService.getById(userId,getTenantInfo().getFinalTenantId()); |
|
|
|
if ( null == wxCUserBasicInfo) { |
|
|
|
logger.error("会员用户不存在."+userId); |
|
|
|
return new ResultData(ErrorCode.USER_IS_EMPTY.getCode(),"会员用户不存在."+userId); |
|
|
|
} |
|
|
|
if (null != wxCUserBasicInfo && EnumCreditLockedStatus.CLOSE.getCode().equals(wxCUserBasicInfo.getStatus())) { |
|
|
|
return new ResultData(ErrorCode.MEMBER_IS_LOCKED); |
|
|
|
} |
|
|
|
WxAppinfo appInfo = wxAppinfoService.getByAppId(appId); |
|
|
|
if(appInfo == null) { |
|
|
|
return new ResultData(ErrorCode.APP_ID_NOT_FOUND); |
|
|
|
} |
|
|
|
Long orderId = 0L; |
|
|
|
try { |
|
|
|
orderId = Long.valueOf(orderIdStr); |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId参数不正确"); |
|
|
|
} |
|
|
|
WxPayOrder record = new WxPayOrder(); |
|
|
|
record.setOrderId(orderId); |
|
|
|
try { |
|
|
|
record.setIp(IPUtil.getIpAddr(request)); |
|
|
|
return wxPayOrderService.createPayOrder(appInfo, wxCUserBasicInfo, record, payWay,parm); |
|
|
|
} catch (MallinkException e) { |
|
|
|
logger.error("payment wechat, order create error, req 2: " + record.toString() + ", e:" + e.getMessage()); |
|
|
|
return new ResultData(e.getErrorCode(), e.getMessage()); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("payment wechat, order create error, req 3: " + record.toString() + ", e:" + e.getMessage()); |
|
|
|
return new ResultData(ErrorCode.PAY_ORDER_ERROR, e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ApiOperation(value = "更新支付订单状态", notes = "{\"payOrderId\":\"string\",\"orderId\":\"string\",\"status\":integer,\"reason\":\"string\"}") |
|
|
|
@PostMapping("/updatePayOrder") |
|
|
|
public ResultData updatePayOrder(@RequestBody Map<String, Object> paramMap) { |
|
|
|
logger.info("/api/pay/updatePayOrder" + paramMap.toString()); |
|
|
|
String payOrderIdStr = (String) paramMap.get("payOrderId"); |
|
|
|
String orderIdStr = (String) paramMap.get("orderId"); |
|
|
|
String reasonStr = (String) paramMap.get("reason"); |
|
|
|
Integer status = (Integer) paramMap.get("status"); |
|
|
|
if (StringUtils.isBlank(orderIdStr)) { |
|
|
|
logger.info("orderId不能为空: " + paramMap.toString()); |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空"); |
|
|
|
} |
|
|
|
Long payOrderId = 0L, orderId = 0L; |
|
|
|
try { |
|
|
|
orderId = Long.valueOf(orderIdStr); |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
logger.error("orderId参数不正确: " + paramMap.toString() + ", e:" + e.getMessage()); |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId参数不正确"); |
|
|
|
} |
|
|
|
if (!StringUtils.isBlank(payOrderIdStr)) { |
|
|
|
try { |
|
|
|
payOrderId = Long.valueOf(payOrderIdStr); |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
logger.error("payOrderId参数不正确: " + paramMap.toString() + ", e:" + e.getMessage()); |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "payOrderId参数不正确"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(payOrderIdStr.equalsIgnoreCase("0")) { |
|
|
|
// 免费券直接返回成功 |
|
|
|
return new ResultData(Result.SUCCESS, "免费券支付状态更新成功"); |
|
|
|
} |
|
|
|
|
|
|
|
WxPayOrder payOrder = new WxPayOrder(); |
|
|
|
payOrder.setId(payOrderId); |
|
|
|
payOrder.setPayOrderNo(String.valueOf(payOrderId)); |
|
|
|
payOrder.setOrderId(orderId); |
|
|
|
payOrder.setPayOrderStatus(status); |
|
|
|
payOrder.setFailReason(reasonStr); |
|
|
|
payOrder.updateTenantInfo(getTenantInfo()); |
|
|
|
payOrder.setPayVendor(EnumPayWay.PAY_WAY_WECHAT.getCode()); |
|
|
|
|
|
|
|
try { |
|
|
|
//有价券支付成功 |
|
|
|
if (status.equals(EnumPayStatus.PAY_STATUS_SUCCESS.getCode())) { |
|
|
|
// 前端提示支付成功, 不作为支付成功标志,只检查支付订单状态 |
|
|
|
// 有价券不走update, callback更新 |
|
|
|
if (payOrderId<=0) { |
|
|
|
logger.error("/api/pay/updatePayOrder error : payOrderId不能为空: " + paramMap.toString()); |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "payOrderId非法"+payOrderId+"[payOrderIdStr:"+payOrderIdStr+"]"); |
|
|
|
} |
|
|
|
payOrder = wxPayOrderService.getById(payOrderId,payOrder.getTenantId()); |
|
|
|
if (null == payOrder) { |
|
|
|
return new ResultData(ErrorCode.PAY_ORDER_ERROR, "未查询到payOrder."); |
|
|
|
} |
|
|
|
WxOrder order = wxOrderService.getById(orderId,payOrder.getTenantId()); |
|
|
|
if (null == order) { |
|
|
|
return new ResultData(ErrorCode.PAY_ORDER_ERROR, "未查询到Order["+orderId+"]."); |
|
|
|
} |
|
|
|
WxCoupon wxCoupon = wxCouponService.getById(order.getProductId(),order.getTenantId()); |
|
|
|
if (null == wxCoupon) { |
|
|
|
return new ResultData(ErrorCode.PAY_ORDER_ERROR, "未查询到优惠券[orderId:"+orderId+"]."); |
|
|
|
} |
|
|
|
|
|
|
|
//拼团的订单,如果查询到是拼团待付款的,则重新查询是否已经真的支付,其他的直接返回订单信息 |
|
|
|
if (wxPayOrderService.isOrderGroupCoupon(payOrder, wxCoupon)) { |
|
|
|
if (!order.getOrderStatus().equals(EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode())) { |
|
|
|
return new ResultData(Result.SUCCESS, "支付状态更新成功", order); |
|
|
|
} |
|
|
|
}else { |
|
|
|
//非拼团的订单,支付成功,关闭订单,撤销支付订单,支付转退款的,直接返回订单信息,其他需要查询真实支付状态 |
|
|
|
if (payOrder.getPayOrderStatus().equals(EnumPayStatus.PAY_STATUS_SUCCESS.getCode())|| |
|
|
|
payOrder.getPayOrderStatus().equals(EnumPayStatus.PAY_STATUS_CLOSE.getCode())|| |
|
|
|
payOrder.getPayOrderStatus().equals(EnumPayStatus.PAY_STATUS_REVERSE.getCode())|| |
|
|
|
payOrder.getPayOrderStatus().equals(EnumPayStatus.PAY_STATUS_REFUND.getCode())) { |
|
|
|
return new ResultData(Result.SUCCESS, "支付状态更新成功", order); |
|
|
|
} |
|
|
|
} |
|
|
|
//查询微信订单状态并更新 |
|
|
|
|
|
|
|
WxAppinfo appInfo = wxAppinfoService.getCAppInfo(payOrder, EnumPayWay.getEnum(payOrder.getPayVendor())); |
|
|
|
//WxCUser user = getUser(); |
|
|
|
//WxAppinfo appInfo = getAppInfo(user.getAppId()); |
|
|
|
if (null == appInfo) { |
|
|
|
return new ResultData(ErrorCode.PAY_ORDER_ERROR, "当前支付类型["+payOrder.getPayVendor()+"]找不到C端appInfo"); |
|
|
|
} |
|
|
|
WxPayAccount payAccount = wxPayAccountService.getById(appInfo.getPayId()); |
|
|
|
try { |
|
|
|
WxPayOrder wo = wxPayOrderService.handleWxOrderQuery(payOrder, order, appInfo, payAccount, IdWorker.get(),false); |
|
|
|
if (null != wo) { |
|
|
|
return new ResultData(Result.SUCCESS, "支付状态更新成功", order); |
|
|
|
} |
|
|
|
}catch(MallinkException e) { |
|
|
|
//已经同步了微信的支付状态,表示已经更新 |
|
|
|
return new ResultData(Result.SUCCESS, "支付状态更新成功", order); |
|
|
|
} |
|
|
|
|
|
|
|
return new ResultData(ErrorCode.PAY_ORDER_ERROR, "查询微信订单支付状态失败,请稍后!"); |
|
|
|
} else if (status.equals(EnumPayStatus.PAY_STATUS_FAIL.getCode())) { |
|
|
|
// 有价券支付失败 |
|
|
|
wxPayOrderService.handlePayOrderFail(payOrder, EnumPayStatus.PAY_STATUS_FAIL.getCode()); |
|
|
|
} |
|
|
|
return new ResultData(Result.SUCCESS, "支付状态更新成功"); |
|
|
|
} catch (MallinkException e) { |
|
|
|
logger.error("支付状态更新失败2: " + payOrder.toString() + ", e:" + e.getMessage()); |
|
|
|
return new ResultData(e.getErrorCode(), e.getMessage()); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("支付状态更新失败3: " + payOrder.toString() + ", e:" + e.getMessage()); |
|
|
|
return new ResultData(ErrorCode.PAY_ORDER_ERROR, "支付状态更新失败3: " + payOrder.toString() + ", e:" + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |