|
|
@@ -28,6 +28,12 @@ import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.alipay.api.AlipayClient;
|
|
|
+import com.alipay.api.DefaultAlipayClient;
|
|
|
+import com.alipay.api.diagnosis.DiagnosisUtils;
|
|
|
+import com.alipay.api.domain.AlipayTradeAppPayModel;
|
|
|
+import com.alipay.api.request.AlipayTradeAppPayRequest;
|
|
|
+import com.alipay.api.response.AlipayTradeAppPayResponse;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.fs.common.config.FSSysConfig;
|
|
|
@@ -83,6 +89,7 @@ import com.fs.common.utils.SnowflakeUtil;
|
|
|
import com.fs.huifuPay.sdk.opps.core.utils.HuiFuUtils;
|
|
|
import com.fs.his.domain.MerchantAppConfig;
|
|
|
import com.fs.his.domain.FsPayConfig;
|
|
|
+import com.fs.his.service.IFsUserService;
|
|
|
import com.fs.system.domain.SysConfig;
|
|
|
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
|
|
|
import com.google.gson.Gson;
|
|
|
@@ -137,6 +144,7 @@ import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.PreDestroy;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import static com.fs.hisStore.constants.StoreConstants.DELIVERY;
|
|
|
|
|
|
@@ -193,6 +201,9 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
@Autowired
|
|
|
FsUserScrmMapper userMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IFsUserService fsUserService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private IErpOrderService erpOrderService;
|
|
|
|
|
|
@@ -3731,6 +3742,234 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
|
|
|
+ public R appPayment(LiveOrderAppPayParam param, HttpServletRequest request) {
|
|
|
+ LiveOrder order = baseMapper.selectLiveOrderByOrderId(String.valueOf(param.getOrderId()));
|
|
|
+ if (order == null) {
|
|
|
+ return R.error("订单不存在");
|
|
|
+ }
|
|
|
+ if (!order.getStatus().equals(OrderInfoEnum.STATUS_0.getValue())) {
|
|
|
+ return R.error("订单状态不正确");
|
|
|
+ }
|
|
|
+ String payingOrderId = redisCache.getCacheObject("isPaying:" + order.getOrderId());
|
|
|
+ if (StringUtils.isNotEmpty(payingOrderId) && order.getOrderId().toString().equals(payingOrderId)) {
|
|
|
+ return R.error(501, "正在支付中...");
|
|
|
+ }
|
|
|
+ FsUserScrm user = userMapper.selectFsUserById(Long.valueOf(order.getUserId()));
|
|
|
+ if (user == null) {
|
|
|
+ return R.error("未找到用户信息,请联系管理员!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (order.getIsEditMoney() == null || order.getIsEditMoney() != 1) {
|
|
|
+ String config = configService.selectConfigByKey("his.store");
|
|
|
+ com.fs.store.config.StoreConfig storeConfig = JSONUtil.toBean(config, com.fs.store.config.StoreConfig.class);
|
|
|
+ if (param.getOrderPayType().equals(1)) {
|
|
|
+ order.setPayType("1");
|
|
|
+ order.setPayMoney(order.getPayPrice());
|
|
|
+ order.setPayDelivery(BigDecimal.ZERO);
|
|
|
+ } else if (param.getOrderPayType().equals(2)) {
|
|
|
+ order.setPayType("2");
|
|
|
+ BigDecimal payMoney = order.getPayPrice().multiply(new BigDecimal(storeConfig.getPayRate())).divide(new BigDecimal(100));
|
|
|
+ payMoney = new BigDecimal(payMoney.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
|
|
|
+ if (payMoney.compareTo(new BigDecimal("0.01")) < 0) {
|
|
|
+ return R.error("物流代收计算支付金额为0,不允许选择物流代收");
|
|
|
+ }
|
|
|
+ order.setPayDelivery(order.getPayPrice().subtract(payMoney));
|
|
|
+ order.setPayMoney(payMoney);
|
|
|
+ } else if (param.getOrderPayType().equals(3)) {
|
|
|
+ order.setPayType("3");
|
|
|
+ BigDecimal amount = redisCache.getCacheObject("orderAmount:" + order.getOrderId());
|
|
|
+ BigDecimal payMoney = BigDecimal.ZERO;
|
|
|
+ if (amount != null) {
|
|
|
+ payMoney = amount;
|
|
|
+ }
|
|
|
+ BigDecimal payPostage = order.getPayPostage();
|
|
|
+ if (payPostage == null || payPostage.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ payPostage = storeConfig.getPayPostage();
|
|
|
+ if (payPostage == null) {
|
|
|
+ payPostage = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ order.setPayPrice(order.getPayPrice().add(payPostage));
|
|
|
+ }
|
|
|
+ order.setPayPostage(payPostage);
|
|
|
+ payMoney = payMoney.add(payPostage);
|
|
|
+ order.setPayMoney(payMoney);
|
|
|
+ order.setPayDelivery(order.getPayPrice().subtract(payMoney));
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(param.getAppId())) {
|
|
|
+ order.setAppId(param.getAppId());
|
|
|
+ }
|
|
|
+ baseMapper.updateLiveOrder(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (order.getPayType().equals("3") && order.getPayMoney().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ this.payConfirm(2, order.getOrderId(), null, null, null, null);
|
|
|
+ return R.ok().put("payType", param.getOrderPayType()).put("isPay", 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (order.getPayMoney().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ this.payConfirm(2, order.getOrderId(), null, null, null, null);
|
|
|
+ return R.ok().put("isPay", 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(param.getAppId())) {
|
|
|
+ throw new IllegalArgumentException("appId不能为空");
|
|
|
+ }
|
|
|
+ FsCoursePlaySourceConfig fsCoursePlaySourceConfig = fsCoursePlaySourceConfigMapper.selectCoursePlaySourceConfigByAppId(param.getAppId());
|
|
|
+ if (fsCoursePlaySourceConfig == null) {
|
|
|
+ throw new CustomException("未找到appId对应的配置: " + param.getAppId());
|
|
|
+ }
|
|
|
+ Long merchantConfigId = fsCoursePlaySourceConfig.getMerchantConfigId();
|
|
|
+ if (merchantConfigId == null || merchantConfigId <= 0) {
|
|
|
+ throw new CustomException("没有配置商户信息");
|
|
|
+ }
|
|
|
+ MerchantAppConfig merchantAppConfig = merchantAppConfigMapper.selectMerchantAppConfigById(fsCoursePlaySourceConfig.getMerchantConfigId());
|
|
|
+ FsPayConfig fsPayConfig = JSON.parseObject(merchantAppConfig.getDataJson(), FsPayConfig.class);
|
|
|
+
|
|
|
+ String openId = null;
|
|
|
+ String appId = param.getAppId();
|
|
|
+ if (request.getHeader("sourcetype") != null && "APP".equals(request.getHeader("sourcetype"))) {
|
|
|
+ FsUser fsUser = fsUserService.selectFsUserByUserId(Long.valueOf(order.getUserId()));
|
|
|
+ if (fsUser != null) {
|
|
|
+ openId = fsUser.getAppOpenId();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (StringUtils.isNotBlank(appId)) {
|
|
|
+ Wrapper<FsUserWx> queryWrapper = Wrappers.<FsUserWx>lambdaQuery()
|
|
|
+ .eq(FsUserWx::getFsUserId, order.getUserId())
|
|
|
+ .eq(FsUserWx::getAppId, appId);
|
|
|
+ FsUserWx fsUserWx = fsUserWxMapper.selectOne(queryWrapper);
|
|
|
+ if (fsUserWx != null) {
|
|
|
+ openId = fsUserWx.getOpenId();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ appId = merchantAppConfig.getAppId();
|
|
|
+ openId = user.getMaOpenId();
|
|
|
+ if (StringUtils.isBlank(openId)) {
|
|
|
+ Wrapper<FsUserWx> queryWrapper = Wrappers.<FsUserWx>lambdaQuery()
|
|
|
+ .eq(FsUserWx::getFsUserId, order.getUserId())
|
|
|
+ .eq(FsUserWx::getAppId, appId);
|
|
|
+ FsUserWx fsUserWx = fsUserWxMapper.selectOne(queryWrapper);
|
|
|
+ if (fsUserWx != null) {
|
|
|
+ openId = fsUserWx.getOpenId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("hf".equals(merchantAppConfig.getMerchantType()) && "wx".equals(param.getPayType()) && StringUtils.isBlank(openId)) {
|
|
|
+ return R.error("用户OPENID不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ String payCode = OrderCodeUtils.getOrderSn();
|
|
|
+ if (StringUtils.isEmpty(payCode)) {
|
|
|
+ return R.error("订单生成失败,请重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ LiveOrderPayment storePayment = new LiveOrderPayment();
|
|
|
+ storePayment.setCompanyId(order.getCompanyId());
|
|
|
+ storePayment.setCompanyUserId(order.getCompanyUserId());
|
|
|
+ storePayment.setPayMode(merchantAppConfig.getMerchantType());
|
|
|
+ storePayment.setStatus(0);
|
|
|
+ storePayment.setPayCode(payCode);
|
|
|
+ storePayment.setPayMoney(order.getPayMoney());
|
|
|
+ storePayment.setCreateTime(new Date());
|
|
|
+ storePayment.setPayTypeCode(param.getPayType());
|
|
|
+ storePayment.setBusinessType(9);
|
|
|
+ storePayment.setRemark("直播订单app支付");
|
|
|
+ storePayment.setOpenId(openId);
|
|
|
+ storePayment.setUserId(user.getUserId());
|
|
|
+ storePayment.setBusinessId(String.valueOf(order.getOrderId()));
|
|
|
+ storePayment.setBusinessCode(order.getOrderCode());
|
|
|
+ storePayment.setAppId(appId);
|
|
|
+ storePayment.setMerConfigId(merchantAppConfig.getId());
|
|
|
+ liveOrderPaymentMapper.insertLiveOrderPayment(storePayment);
|
|
|
+
|
|
|
+ if (!"hf".equals(merchantAppConfig.getMerchantType()) && !"appPay".equals(merchantAppConfig.getMerchantType())) {
|
|
|
+ return R.error("支付暂不可用!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("hf".equals(merchantAppConfig.getMerchantType())) {
|
|
|
+ HuiFuCreateOrder o = new HuiFuCreateOrder();
|
|
|
+ o.setTradeType("wx".equals(param.getPayType()) ? "T_MINIAPP" : "A_NATIVE");
|
|
|
+ o.setOpenid(openId);
|
|
|
+ o.setAppId(appId);
|
|
|
+ o.setReqSeqId("applive-" + storePayment.getPayCode());
|
|
|
+ o.setTransAmt(storePayment.getPayMoney().toString());
|
|
|
+ o.setGoodsDesc("直播订单app支付");
|
|
|
+ HuifuCreateOrderResult result = huiFuService.createOrder(o);
|
|
|
+ if (result.getResp_code() != null && (result.getResp_code().equals("00000000") || result.getResp_code().equals("00000100"))) {
|
|
|
+ LiveOrderPayment mt = new LiveOrderPayment();
|
|
|
+ mt.setPaymentId(storePayment.getPaymentId());
|
|
|
+ mt.setTradeNo(result.getHf_seq_id());
|
|
|
+ mt.setAppId(appId);
|
|
|
+ liveOrderPaymentMapper.updateLiveOrderPayment(mt);
|
|
|
+ redisCache.setCacheObject("isPaying:" + order.getOrderId(), order.getOrderId().toString(), 1, TimeUnit.MINUTES);
|
|
|
+ Map<String, Object> resultMap = JSON.parseObject(result.getPay_info(), new TypeReference<Map<String, Object>>() {
|
|
|
+ });
|
|
|
+ if (resultMap != null && resultMap.get("package") != null) {
|
|
|
+ resultMap.put("packageValue", resultMap.get("package"));
|
|
|
+ }
|
|
|
+ return R.ok().put("payType", param.getOrderPayType()).put("data", resultMap).put("type", "hf").put("isPay", 0);
|
|
|
+ }
|
|
|
+ return R.error(result.getResp_desc());
|
|
|
+ } else if ("appPay".equals(merchantAppConfig.getMerchantType()) && "wx".equals(param.getPayType())) {
|
|
|
+ WxPayConfig payConfig = new WxPayConfig();
|
|
|
+ payConfig.setAppId(appId);
|
|
|
+ payConfig.setMchId(fsPayConfig.getWxAppMchId());
|
|
|
+ payConfig.setMchKey(fsPayConfig.getWxAppMchKey());
|
|
|
+ payConfig.setSubAppId(org.apache.commons.lang3.StringUtils.trimToNull(null));
|
|
|
+ payConfig.setSubMchId(org.apache.commons.lang3.StringUtils.trimToNull(null));
|
|
|
+ payConfig.setKeyPath(null);
|
|
|
+ payConfig.setNotifyUrl(fsPayConfig.getWxAppNotifyUrl());
|
|
|
+ wxPayService.setConfig(payConfig);
|
|
|
+ WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
|
|
|
+ orderRequest.setBody("直播订单app支付");
|
|
|
+ orderRequest.setOutTradeNo("applive-" + storePayment.getPayCode());
|
|
|
+ orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));
|
|
|
+ orderRequest.setTradeType("APP");
|
|
|
+ orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
|
|
|
+ orderRequest.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
|
|
|
+ try {
|
|
|
+ log.info("直播订单app支付 调用微信入参:{}", orderRequest);
|
|
|
+ Object result = wxPayService.createOrder(orderRequest);
|
|
|
+ redisCache.setCacheObject("isPaying:" + order.getOrderId(), order.getOrderId().toString(), 1, TimeUnit.MINUTES);
|
|
|
+ return R.ok().put("data", result).put("type", "wxApp").put("isPay", 0).put("payType", param.getOrderPayType());
|
|
|
+ } catch (WxPayException e) {
|
|
|
+ log.error("直播订单app支付失败", e);
|
|
|
+ throw new CustomException("直播订单app支付" + e.getMessage());
|
|
|
+ }
|
|
|
+ } else if ("appPay".equals(merchantAppConfig.getMerchantType()) && "ali".equals(param.getPayType())) {
|
|
|
+ AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", fsPayConfig.getAliAppId(), fsPayConfig.getAliPrivateKey(), "json", "utf-8", fsPayConfig.getAliPublicKey(), "RSA2");
|
|
|
+ AlipayTradeAppPayRequest aliRequest = new AlipayTradeAppPayRequest();
|
|
|
+ AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
|
|
|
+ model.setOutTradeNo("applive-" + storePayment.getPayCode());
|
|
|
+ model.setTotalAmount(storePayment.getPayMoney().toString());
|
|
|
+ model.setSubject("直播订单app支付");
|
|
|
+ aliRequest.setBizModel(model);
|
|
|
+ aliRequest.setNotifyUrl(fsPayConfig.getAliNotifyUrl());
|
|
|
+ try {
|
|
|
+ log.info("直播订单app支付 调用支付宝入参:{}", aliRequest);
|
|
|
+ AlipayTradeAppPayResponse result = alipayClient.sdkExecute(aliRequest);
|
|
|
+ if (!result.isSuccess()) {
|
|
|
+ String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(result);
|
|
|
+ log.error("支付宝支付调用失败 诊断链接:{}", diagnosisUrl);
|
|
|
+ throw new CustomException(result.getSubMsg());
|
|
|
+ }
|
|
|
+ redisCache.setCacheObject("isPaying:" + order.getOrderId(), order.getOrderId().toString(), 1, TimeUnit.MINUTES);
|
|
|
+ return R.ok().put("isPay", 0).put("data", result).put("type", "aliPay").put("payType", param.getOrderPayType());
|
|
|
+ } catch (CustomException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("支付宝支付异常", e);
|
|
|
+ throw new CustomException("支付系统异常,请稍后重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.error("支付暂不可用!");
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<LiveOrder> selectLiveOrderItemJson() {
|
|
|
return baseMapper.selectLiveOrderItemJson();
|