123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- package com.ym.mec.collectfee.controller;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.ym.mec.collectfee.common.sms.SmsExample;
- import com.ym.mec.collectfee.common.web.BaseController;
- import com.ym.mec.collectfee.entity.*;
- import com.ym.mec.collectfee.service.*;
- import com.ym.mec.collectfee.utils.Constants;
- import com.ym.mec.collectfee.utils.GenerateNum;
- import com.ym.mec.collectfee.utils.ShortUrlUtil;
- import com.ym.mec.collectfee.utils.yqpay.*;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.IOUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.scheduling.annotation.EnableScheduling;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import sun.plugin2.util.NativeLibLoader;
- import java.io.InputStreamReader;
- import java.math.BigDecimal;
- import java.time.LocalDateTime;
- import java.util.*;
- //@Api("支付")
- @Slf4j
- @RestController
- @RequestMapping("yqpay")
- @EnableScheduling // 2.开启定时任务
- public class YqPayController extends BaseController {
- @Autowired
- private YqPayService yqPayService;
- @Autowired
- private YqQueryService yqQueryService;
- @Autowired
- private OrderService orderService;
- @Autowired
- private AccountService accountService;
- @Autowired
- private CourseGroupInfoService CourseGroupInfoService;
- @Autowired
- private ApplyInfoService applyInfoService;
- @Autowired
- private RenewalsService renewalsService;
- @Autowired
- private SchoolService schoolService;
- /**
- * 统一下单(乐团缴费)
- *
- * @return String
- * @throws Exception
- */
- // @ApiOperation(value = "提交支付", notes = "易乾支付统一下单")
- @PostMapping("/toPay")
- @Transactional
- public Object toPay(@ModelAttribute @Validated Order order) throws Exception {
- BigDecimal amount = new BigDecimal("0");
- School school = schoolService.get(order.getClassId());
- //计划招生人数有更新,更新
- orderService.getSchoolDetail(school.getSchoolId(), order.getClassId());
- //1、判断已报名人数
- CourseGroupInfo courseGroupInfo = CourseGroupInfoService.get(order.getCourseId());
- if (courseGroupInfo.getRegNum().compareTo(courseGroupInfo.getPlanNum()) >= 0) {
- return failed("乐团人数暂时已满,请稍后再试");
- }
- //课程组价格
- BigDecimal courseFee = courseGroupInfo.getFeeAmount();
- amount = amount.add(courseFee);
- //获取乐器的价格
- ClassPathResource classPathResource = new ClassPathResource("instruments.json"); //解析乐器数据
- BigDecimal instrumentPrice = new BigDecimal("0");//乐器价格
- String instrumentName = "";//乐器名称
- String instrumentId = order.getInstrument();
- String jsonString = IOUtils.toString(new InputStreamReader(classPathResource.getInputStream(), "UTF-8"));
- Instrument instrument = JSONObject.parseObject(jsonString, Instrument.class);
- if (order.getInstrument() != null && !order.getInstrument().isEmpty()) {
- instrumentPrice = new BigDecimal(instrument.getInstruments().get(instrumentId).get("referencePrice"));
- instrumentName = (String) instrument.getInstruments().get(instrumentId).get("index") + "-" +
- (String) instrument.getInstruments().get(instrumentId).get("name");
- }
- //2 版本为3.0( 26),不收乐器费用,收押金800放乐器费用
- if (courseGroupInfo.getFeeType().equals(26)) {
- instrumentPrice = new BigDecimal("800");
- }
- amount = amount.add(instrumentPrice);
- //辅件价格
- String adjunctIds = order.getAdjunct();
- BigDecimal adjunctPrice = new BigDecimal("0");//辅件价格
- String adjunctName = "";//辅件名称
- if (adjunctIds != null && !adjunctIds.isEmpty()) {
- String[] adjunctIdArr = adjunctIds.split(",");
- for (String adjunctId : adjunctIdArr) {
- adjunctPrice = adjunctPrice.add(new BigDecimal(instrument.getAuxiliaries().get(adjunctId).get("referencePrice")));
- adjunctName += (String) instrument.getAuxiliaries().get(adjunctId).get("name") + "|";
- }
- }
- amount = amount.add(adjunctPrice);
- ApplyInfo applyInfo = applyInfoService.get(order.getUserId());
- order.setPoName(school.getName());
- order.setVoicyPart(courseGroupInfo.getSubName());
- order.setGroupId(courseGroupInfo.getId());
- order.setAmount(amount);
- order.setRemark(instrumentName);
- order.setTuiFee(courseFee);
- order.setGoodsFee(instrumentPrice);
- order.setSdName(adjunctName + "教材|琴谱");
- order.setSdFee(adjunctPrice);
- order.setType(1);
- order.setUserName(applyInfo.getName());
- String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号
- order.setOrderNo(orderNo);
- order.setCreateTime(new Date()); //订单提交时间
- order.setStatus(1); //订单状态
- //获取分佣账户
- Integer branchId = 1001;//order.getBranchId();
- Account routingAccount = accountService.getRoutingAccount(branchId);
- order.setAccount(routingAccount.getSellerNo());
- order.setUAccount(routingAccount.getId().toString());
- //1、插入订单
- orderService.insert(order);
- //2、修改已报名人数
- courseGroupInfo.setRegNum(courseGroupInfo.getRegNum() + 1);
- CourseGroupInfoService.upByIdAndVersion(courseGroupInfo);
- //3、修改分佣账户已收金额
- BigDecimal HasRouting = routingAccount.getHasRouting().add(order.getAmount());
- routingAccount.setHasRouting(HasRouting);
- accountService.upByIdAndVersion(routingAccount);
- order.setAmount(new BigDecimal("2")); //测试账户设置金额
- Map rqMap = orderService.getPayMap(routingAccount, order, school); //获取支付map
- return succeed(rqMap);
- }
- /**
- * 续费支付
- *
- * @return String
- * @throws Exception
- */
- // @ApiOperation(value = "续费支付", notes = "续费支付")
- @PostMapping("/renewalsPay")
- public Object renewalsPay(@ModelAttribute @Validated Renewals renewals) throws Exception {
- MecUser mecUser = applyInfoService.findMecUser(renewals.getUserId());
- if (mecUser == null) {
- return failed("续费用户不存在");
- }
- renewals.setBranchId(mecUser.getBranchId());
- //课程组价格
- List<MecCourse> courses = applyInfoService.queryUserCourse(renewals.getUserId());//获取续费课程
- if (courses == null) {
- return failed("您没有续费的课程");
- }
- MecCourse mecCourse4json = JSON.parseObject(renewals.getCourses(), MecCourse.class);
- if (mecCourse4json == null) {
- return failed("请选择续费课程");
- }
- //classType 小课1 大课2
- Integer buyCount = mecCourse4json.getBuyCount();
- if (buyCount <= 0 || buyCount > 20) {
- return failed("购买课程次数不能小于1,大于20");
- }
- BigDecimal amount = new BigDecimal("0"); //课程总价
- String remark = "";
- List<MecCourse> pickCourses = new ArrayList<>();
- for (int i = 0; i < courses.size(); i++) {
- MecCourse course = courses.get(i);
- if (mecCourse4json.getCourseId().equals(course.getCourseId().intValue())) {
- BigDecimal price = course.getClassType().equals(1) ? course.getPrice().multiply(BigDecimal.valueOf(buyCount)) : course.getPrice().multiply(BigDecimal.valueOf(course.getBuyCount()));
- amount = amount.add(price);
- remark += course.getClassName();
- if (course.getClassType().equals(1)) {
- course.setBuyCount(buyCount);
- }
- pickCourses.add(course);
- }
- }
- if (pickCourses.size() == 0) {
- return failed("请选择续费课程");
- }
- String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号
- //获取分佣账户
- Integer branchId = 1001;//order.getBranchId();
- Account routingAccount = accountService.getRoutingAccount(branchId);
- Order order = renewalsService.addRenewalsOrder(renewals, amount, orderNo, pickCourses, routingAccount, remark);
- order.setAmount(new BigDecimal("2")); //测试账户设置金额
- Map rqMap = orderService.getPayMap(routingAccount, order, null); //获取支付map
- return succeed(rqMap);
- }
- /**
- * 交易查询
- *
- * @param merOrderNoList 用户订单号
- * @return
- * @throws Exception
- */
- //@PostMapping("/query")
- //@Scheduled(cron = "0/3 40 11 * * ?")
- public Object query(String merOrderNoList) throws Exception {
- String notifyUrl = ""; //回调地址
- Map<String, Object> resultMap = new LinkedHashMap<>();
- resultMap.put("merOrderNoList", merOrderNoList);
- Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
- return yqQueryService.orderQuery(requestMap);
- }
- /**
- * 用户信息(商户)查询
- *
- * @param sonMerNo 子商户号
- * @return
- * @throws Exception
- */
- @PostMapping("/queryaccount")
- public String queryAccount(String sonMerNo) throws Exception {
- String notifyUrl = ""; //回调地址
- Map<String, Object> resultMap = new LinkedHashMap<>();
- resultMap.put("merOrderNoList", sonMerNo);
- Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
- return yqQueryService.queryAccount(requestMap);
- }
- /**
- * 对账查询(定时任务每天对账)
- *
- * @return
- */
- public String queryBill() throws Exception {
- String notifyUrl = ""; //回调地址
- Map<String, Object> resultMap = new LinkedHashMap<>();
- resultMap.put("tradeDate", ""); //交易日期
- resultMap.put("payState", ""); //订单状态
- resultMap.put("tradeType", ""); //交易类型,不填为全部
- resultMap.put("channelType", ""); //通道类型,不填为全部
- Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
- return yqQueryService.billQuery(requestMap);
- }
- /**
- * 平台转账
- *
- * @return
- * @throws Exception
- */
- @PostMapping("/platformtransferacc")
- public String platformTransferAcc() throws Exception {
- String notifyUrl = ""; //回调地址
- Map<String, Object> resultMap = new LinkedHashMap<>();
- resultMap.put("payeeNo", ""); //收款方商户号
- resultMap.put("payeeName", ""); //收款方姓名
- resultMap.put("amount", ""); //金额
- resultMap.put("merOrderNo", ""); //商户订单号
- resultMap.put("remarks", ""); //备注
- Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
- return yqPayService.platformTransferAcc(requestMap);
- }
- /**
- * 提现短信
- *
- * @return
- * @throws Exception
- */
- @PostMapping("/sendsms")
- public Msg sendSms(@ModelAttribute Intfc intfc) throws Exception {
- String notifyUrl = ""; //回调地址
- String merMerOrderNo = GenerateNum.getInstance().GenerateOrderNo();
- Map<String, Object> resultMap = new LinkedHashMap<>();
- resultMap.put("wdMerNo", intfc.getWdMerNo()); //提现商户号
- resultMap.put("merMerOrderNo", merMerOrderNo); //商户订单号
- resultMap.put("amount", intfc.getAmount()); //提现金额
- resultMap.put("cardNo", intfc.getCardNo()); //提现银行卡号
- // resultMap.put("phone", intfc.getPhone()); //预留手机号(选填)
- // resultMap.put("cardType", intfc.getCardType()); //卡类型 0-个人银行卡,1-企业银行卡。(选填)
- Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
- return yqPayService.intfc(requestMap);
- }
- /**
- * 提现
- *
- * @return
- * @throws Exception
- */
- @PostMapping("/intfc")
- public Msg intfc(@ModelAttribute Intfc intfc) throws Exception {
- String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //回调地址
- String merMerOrderNo = GenerateNum.getInstance().GenerateOrderNo();
- Map<String, Object> resultMap = new LinkedHashMap<>();
- resultMap.put("wdMerNo", intfc.getWdMerNo()); //提现商户号
- resultMap.put("merMerOrderNo", merMerOrderNo); //商户订单号
- resultMap.put("amount", intfc.getAmount()); //提现金额
- resultMap.put("cardNo", intfc.getCardNo()); //提现银行卡号
- // resultMap.put("phone", intfc.getPhone()); //预留手机号(选填)
- // resultMap.put("cardType", intfc.getCardType()); //卡类型 0-个人银行卡,1-企业银行卡。(选填)
- resultMap.put("seqNo", ""); //流水号(选填)
- resultMap.put("smsCode", ""); //验证码(选填)
- Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
- return yqPayService.intfc(requestMap);
- }
- /**
- * 易乾异步通知接口
- *
- * @param msg
- * @return String
- * @throws Exception
- */
- @PostMapping("/notify")
- public Msg notify(@ModelAttribute Msg msg) throws Exception {
- // log.info(msg.toString());K
- Map<String, Object> rqMap = new LinkedHashMap<String, Object>();
- rqMap.put("code", msg.getCode());
- rqMap.put("msg", msg.getMsg());
- rqMap.put("responseType", msg.getResponseType());
- rqMap.put("responseParameters", msg.getResponseParameters());
- rqMap.put("sign", msg.getSign());
- boolean rs = YqPayUtil.verify(rqMap);
- msg.setCode("");
- msg.setMsg("fail");
- Order order = null;
- NotifyMsg notifyMsg = null;
- if (rs) {
- Map<String, String> notifyMap = JSON.parseObject(msg.getResponseParameters(), Map.class);
- String merMerOrderNo = notifyMsg.getMerMerOrderNo(); //自己系统订单号
- order = orderService.getOrderByOrderNo(merMerOrderNo);
- }
- //支付中订单存在,更新状态
- if (order != null && msg.getResponseType().equals("1")) {
- //更新订单状态
- HashMap<String, Object> upMap = new HashMap<>();
- upMap.put("oldStatus", order.getStatus());
- int status = msg.getCode().equals("88") ? 2 : 0;
- upMap.put("status", status);
- upMap.put("bank", notifyMsg.getChannelType());//支付通道
- if (order.getOrderNo().isEmpty()) {
- upMap.put("payId", notifyMsg.getOrderNo());//更新易乾付订单号
- }
- if (status == 2) {
- upMap.put("pay", notifyMsg.getPayAmount());
- upMap.put("payTime", new Date());//付款时间
- }
- // orderService.updateByIdAndStatus(upMap);
- //ApplyInfo applyInfo = applyInfoService.get(order.getUserId());
- //applyInfoService.userRegister(applyInfo.getPatriarchPhone(), order.getId()); //推送mec
- msg.setCode("000000");
- msg.setMsg("success");
- }
- return msg;
- }
- @Scheduled(cron = "0/5 * * * * ?")
- //@RequestMapping("/getOrderStatus")
- public void getOrderStatus() throws Exception {
- System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
- List<Order> payingOrders = orderService.findPayingOrders();
- String merOrderNos = ""; //
- for (int i = 0; i < payingOrders.size(); i++) {
- merOrderNos += payingOrders.get(i).getOrderNo() + ",";
- }
- if (merOrderNos.isEmpty()) {
- return;
- }
- merOrderNos = merOrderNos.substring(0, merOrderNos.length() - 1);
- String notifyUrl = ""; //回调地址
- Map<String, Object> resultMap = new LinkedHashMap<>();
- resultMap.put("merOrderNoList", merOrderNos);
- Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
- Msg queryRs = yqQueryService.orderQuery(requestMap);
- if (queryRs.getCode().equals("88")) {
- //更新订单状态
- String[] statusArr = {"0", "1", "7"};
- String responseParameters = queryRs.getResponseParameters();
- List<Map<String, String>> responseList = JSON.parseObject(responseParameters, List.class);
- for (int i = 0; i < responseList.size(); i++) {
- Map<String, String> rpMap = responseList.get(i);
- if (Arrays.asList(statusArr).contains(rpMap.get("tradeState"))) {
- this.updateOrder(rpMap);
- }
- }
- }
- }
- @Transactional
- public void updateOrder(Map<String, String> rpMap) {
- int status = rpMap.get("tradeState").equals("1") ? 2 : 0;
- Order order = orderService.getOrderByOrderNo(rpMap.get("merOrderNo"));
- if (order == null) {
- return;
- }
- HashMap<String, Object> upMap = new HashMap<>();
- upMap.put("oldStatus", 1);
- upMap.put("status", status);
- upMap.put("bank", rpMap.get("channelType"));
- if (order.getPayId() == null) {
- upMap.put("payId", rpMap.get("orderNo"));
- }
- if (status == 2) {
- upMap.put("pay", rpMap.get("totalMoney"));
- upMap.put("payTime", new Date());
- //推送mec
- if (order.getTuiFee() != null) { //乐团报名
- ApplyInfo applyInfo = applyInfoService.get(order.getUserId());
- applyInfo.setStatus(1);
- applyInfoService.update(applyInfo);
- applyInfoService.userRegister(applyInfo.getPatriarchPhone(), order.getId()); //推送mec
- schoolService.sendPayMsg(applyInfo.getPatriarchPhone(), order.getAmount().toString());
- } else {
- Renewals renewals = renewalsService.getRenewalsByOrderId(order.getId());
- RenewBean renewBean = new RenewBean();
- renewBean.setUserId(renewals.getUserId());
- renewBean.setClassId(renewals.getClassId());
- renewBean.setWay(renewals.getWay());
- renewBean.setPay(renewals.getPay());
- renewBean.setChargeMode(renewals.getChangeMode());
- renewBean.setBuy(new BigDecimal(renewals.getBuy()));
- renewBean.setPrice(renewals.getPrice());
- applyInfoService.pushRenew(renewBean);
- }
- }
- if (status == 0) {
- //失败减去已收款金额
- Account account = accountService.getAccountByBranchId(Integer.parseInt(order.getUAccount()));
- BigDecimal HasRouting = account.getHasRouting().subtract(order.getAmount());
- account.setHasRouting(HasRouting);
- accountService.upByIdAndVersion(account);
- //减去报名人数
- if (order.getTuiFee() != null) {
- CourseGroupInfo courseGroupInfo = CourseGroupInfoService.get(order.getGroupId());
- courseGroupInfo.setRegNum(courseGroupInfo.getRegNum() - 1);
- CourseGroupInfoService.upByIdAndVersion(courseGroupInfo);
- }
- }
- orderService.updateByIdAndStatus(upMap);
- }
- }
|