|
|
@@ -6,6 +6,12 @@ import java.util.*;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+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.core.domain.R;
|
|
|
@@ -19,6 +25,7 @@ import com.fs.core.utils.OrderCodeUtils;
|
|
|
import com.fs.course.domain.*;
|
|
|
import com.fs.course.mapper.*;
|
|
|
import com.fs.course.param.*;
|
|
|
+import com.fs.course.service.IFsCoursePlaySourceConfigService;
|
|
|
import com.fs.course.service.IFsUserCourseStudyService;
|
|
|
import com.fs.course.vo.FsUserCourseOrderListPVO;
|
|
|
import com.fs.course.vo.FsUserCourseOrderListUVO;
|
|
|
@@ -29,10 +36,7 @@ import com.fs.his.enums.FsUserIntegralLogTypeEnum;
|
|
|
import com.fs.his.mapper.*;
|
|
|
import com.fs.his.param.FsPackageOrderDoPayParam;
|
|
|
import com.fs.his.param.FsUserAddIntegralTemplateParam;
|
|
|
-import com.fs.his.service.IFsCouponService;
|
|
|
-import com.fs.his.service.IFsStorePaymentService;
|
|
|
-import com.fs.his.service.IFsUserCouponService;
|
|
|
-import com.fs.his.service.IFsUserIntegralLogsService;
|
|
|
+import com.fs.his.service.*;
|
|
|
import com.fs.huifuPay.domain.HuiFuCreateOrder;
|
|
|
import com.fs.huifuPay.domain.HuifuCreateOrderResult;
|
|
|
import com.fs.huifuPay.service.HuiFuService;
|
|
|
@@ -55,6 +59,7 @@ import com.github.binarywang.wxpay.config.WxPayConfig;
|
|
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
|
|
import com.github.binarywang.wxpay.service.WxPayService;
|
|
|
import com.google.gson.Gson;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.checkerframework.checker.units.qual.A;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -64,12 +69,15 @@ import com.fs.course.service.IFsUserCourseOrderService;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
/**
|
|
|
* 课程订单信息Service业务层处理
|
|
|
*
|
|
|
* @author fs
|
|
|
* @date 2024-05-21
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class FsUserCourseOrderServiceImpl implements IFsUserCourseOrderService
|
|
|
{
|
|
|
@@ -133,6 +141,14 @@ public class FsUserCourseOrderServiceImpl implements IFsUserCourseOrderService
|
|
|
private IFsUserIntegralLogsService userIntegralLogsService;
|
|
|
@Autowired
|
|
|
private FsUserWxMapper fsUserWxMapper;
|
|
|
+ @Autowired
|
|
|
+ private FsUserMapper userMapper;
|
|
|
+ @Autowired
|
|
|
+ private IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService;
|
|
|
+ @Autowired
|
|
|
+ private IMerchantAppConfigService merchantAppConfigService;
|
|
|
+ @Autowired
|
|
|
+ private IFsUserService fsUserService;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -367,6 +383,174 @@ public class FsUserCourseOrderServiceImpl implements IFsUserCourseOrderService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public R appPayment(FsUserCourseOrderDoPayParam param, HttpServletRequest request) {
|
|
|
+ FsUserCourseOrder order = fsUserCourseOrderMapper.selectFsUserCourseOrderByOrderId(param.getOrderId());
|
|
|
+ if (order == null) {
|
|
|
+ return R.error("订单不存在");
|
|
|
+ }
|
|
|
+ if (order.getStatus() != 1) {
|
|
|
+ return R.error("非法操作");
|
|
|
+ }
|
|
|
+
|
|
|
+ FsUser user = userMapper.selectFsUserByUserId(param.getUserId());
|
|
|
+ if (user == null) {
|
|
|
+ return R.error("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (order.getPayMoney().compareTo(new BigDecimal(0)) <= 0) {
|
|
|
+ this.payConfirm(order.getOrderCode(),"","","",2,null,null);
|
|
|
+ return R.ok().put("isPay",1);
|
|
|
+ }
|
|
|
+
|
|
|
+ FsCoursePlaySourceConfig fsCoursePlaySourceConfig = fsCoursePlaySourceConfigService.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 = merchantAppConfigService.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(param.getUserId().longValue());
|
|
|
+ openId = fsUser.getAppOpenId();
|
|
|
+ } else {
|
|
|
+ if (StringUtils.isNotBlank(appId)) {
|
|
|
+ //查询fs_user_wx的openId
|
|
|
+ Wrapper<FsUserWx> queryWrapper = Wrappers.<FsUserWx>lambdaQuery()
|
|
|
+ .eq(FsUserWx::getFsUserId, param.getUserId())
|
|
|
+ .eq(FsUserWx::getAppId, appId);
|
|
|
+ FsUserWx fsUserWx = fsUserWxMapper.selectOne(queryWrapper);
|
|
|
+ if (fsUserWx != null) {
|
|
|
+ openId = fsUserWx.getOpenId();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ appId = merchantAppConfig.getAppId();
|
|
|
+ openId = Objects.isNull(user) ? "" : user.getMaOpenId();
|
|
|
+ if (StringUtils.isBlank(openId)){
|
|
|
+ Wrapper<FsUserWx> queryWrapper = Wrappers.<FsUserWx>lambdaQuery()
|
|
|
+ .eq(FsUserWx::getFsUserId, param.getUserId())
|
|
|
+ .eq(FsUserWx::getAppId, appId);
|
|
|
+ FsUserWx fsUserWx = fsUserWxMapper.selectOne(queryWrapper);
|
|
|
+ if (Objects.nonNull(fsUserWx)){
|
|
|
+ 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("订单生成失败,请重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ FsStorePayment storePayment = new FsStorePayment();
|
|
|
+ storePayment.setStatus(0);
|
|
|
+ storePayment.setPayMode(merchantAppConfig.getMerchantType());
|
|
|
+ storePayment.setBusinessCode(order.getOrderCode());
|
|
|
+ storePayment.setPayCode(payCode);
|
|
|
+ storePayment.setPayMoney(order.getPayMoney());
|
|
|
+ storePayment.setCreateTime(new Date());
|
|
|
+ storePayment.setPayTypeCode("app");
|
|
|
+ storePayment.setBusinessType(5);
|
|
|
+ storePayment.setRemark("课程订单支付");
|
|
|
+ storePayment.setPayTypeCode(param.getPayType());
|
|
|
+ storePayment.setUserId(user.getUserId());
|
|
|
+ storePayment.setBusinessId(order.getOrderId().toString());
|
|
|
+ storePayment.setMerchantId(merchantAppConfig.getId());
|
|
|
+ storePayment.setAppId(param.getAppId());
|
|
|
+ if (storePaymentService.insertFsStorePayment(storePayment) > 0) {
|
|
|
+ if (!"hf".equals(merchantAppConfig.getMerchantType()) && !"appPay".equals(merchantAppConfig.getMerchantType())) {
|
|
|
+ return R.error("支付暂不可用!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("hf".equals(merchantAppConfig.getMerchantType())) {
|
|
|
+ HuiFuCreateOrder o = new HuiFuCreateOrder();
|
|
|
+ o.setTradeType(param.getPayType().equals("wx") ? "T_MINIAPP" : "A_NATIVE");
|
|
|
+ o.setOpenid(openId);
|
|
|
+ o.setAppId(appId);
|
|
|
+ o.setReqSeqId("course-"+storePayment.getPayCode());
|
|
|
+ o.setTransAmt(storePayment.getPayMoney().toString());
|
|
|
+ o.setAppId(param.getAppId());
|
|
|
+ o.setGoodsDesc("课程订单支付");
|
|
|
+ HuifuCreateOrderResult result = huiFuService.createOrder(o);
|
|
|
+ FsStorePayment mt=new FsStorePayment();
|
|
|
+ mt.setPaymentId(storePayment.getPaymentId());
|
|
|
+ mt.setTradeNo(result.getHf_seq_id());
|
|
|
+ mt.setAppId(appId);
|
|
|
+ storePaymentService.updateFsStorePayment(mt);
|
|
|
+ return R.ok().put("isPay",0).put("data",result).put("type","hf");
|
|
|
+ } 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("课程订单支付");
|
|
|
+ orderRequest.setOutTradeNo("course-"+storePayment.getPayCode());
|
|
|
+ orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));
|
|
|
+ orderRequest.setTradeType("APP");
|
|
|
+ orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
|
|
|
+ orderRequest.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
|
|
|
+ //调用统一下单接口,获取"预支付交易会话标识"
|
|
|
+ try {
|
|
|
+ log.info("会员开通订单支付 调用微信入参:{}", orderRequest.toString());
|
|
|
+ Object result = wxPayService.createOrder(orderRequest);
|
|
|
+ return R.ok().put("data",result).put("type","wxApp").put("isPay",0);
|
|
|
+ } catch (WxPayException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new CustomException("会员开通订单支付"+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("course-" + storePayment.getPayCode());
|
|
|
+ // 订单总金额
|
|
|
+ model.setTotalAmount(storePayment.getPayMoney().toString());
|
|
|
+ // 订单标题
|
|
|
+ model.setSubject("课程订单支付");
|
|
|
+ aliRequest.setBizModel(model);
|
|
|
+ aliRequest.setNotifyUrl(fsPayConfig.getAliNotifyUrl());
|
|
|
+
|
|
|
+ try {
|
|
|
+ log.info("会员开通订单支付 调用微信入参:{}", aliRequest.toString());
|
|
|
+ AlipayTradeAppPayResponse result = alipayClient.sdkExecute(aliRequest);
|
|
|
+ if (!result.isSuccess()) {
|
|
|
+ String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(result);
|
|
|
+ log.error("支付宝支付调用失败 诊断链接:{}", diagnosisUrl);
|
|
|
+ throw new CustomException(result.getSubMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.ok().put("isPay", 0).put("data", result).put("type", "aliPay");
|
|
|
+ } catch (Exception e){
|
|
|
+ log.info("支付宝支付异常: {}", e);
|
|
|
+ throw new CustomException("支付系统异常,请稍后重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.error();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public R payment(FsUserCourseOrderDoPayParam param) {
|
|
|
FsUserCourseOrder order = fsUserCourseOrderMapper.selectFsUserCourseOrderByOrderId(param.getOrderId());
|
|
|
@@ -900,3 +1084,5 @@ public class FsUserCourseOrderServiceImpl implements IFsUserCourseOrderService
|
|
|
return moneys;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|