无关风月
12 小时以前 5dc40fcd64b0513150f1d8335ab849e6d8cdc28e
cloud-server-course/src/main/java/com/dsh/course/service/impl/TCoursePackageServiceImpl.java
@@ -36,8 +36,10 @@
import com.dsh.course.model.*;
import com.dsh.course.service.*;
import com.dsh.course.util.*;
import com.dsh.course.util.wx.WxV3PayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
@@ -945,80 +947,28 @@
        TCoursePackage byId = coursePackageService.getById(paymentCourseVo.getId());
        Integer storeId = byId.getStoreId();
        Store store = storeClient.queryStoreById(storeId);
        // 是否分账 0否1是
        int isFenZhang= 1;
        if (store.getOperatorId()==null || store.getOperatorId()==0){
            // 平台
            isFenZhang = 0;
            ResultUtil weixinpay = payMoneyUtil.weixinpay("报名运动营", "", code, coursePackagePaymentConfig.getId().toString(),
                    "/base/course/weChatPaymentCourseCallback", "APP", "");
            return weixinpay;
        }else{
            String s = storeClient.getmerchantNumberByOperatorId(store.getOperatorId());
            System.err.println("微信商户号"+s);
            if(!StringUtils.hasLength(s)){
                return ResultUtil.error("运营商未配置微信商户号,获取支付失败!");
            }
            return payMoneyUtil.weixinpayV3(s,"报名运动营",code,
                    "/base/course/weChatPaymentCourseCallback1",paymentPrice.toString(),"");
        }
        ResultUtil weixinpay = payMoneyUtil.weixinpay("报名运动营"+"-"+isFenZhang, coursePackagePaymentConfig.getId().toString(), code, paymentPrice.toString(),
                "/base/course/weChatPaymentCourseCallback", "APP", "");
        if (weixinpay.getCode() == 200) {
            String finalCode = code;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        int num = 1;
                        int wait = 0;
                        while (num <= 10) {
                            int min = 5000;
                            wait += (min * num);
                            Thread.sleep(wait);
                            CoursePackageOrder coursePackageOrder1 = coursePackageOrderService.getOne(new QueryWrapper<CoursePackageOrder>()
                                    .eq("code", finalCode).eq("state", 1));
                            if (coursePackageOrder1.getPayStatus() == 2) {
                                break;
                            }
                            ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryWXOrder(finalCode, "");
                            if (resultUtil.getCode() == 200 && coursePackageOrder1.getPayStatus() == 1) {
                                /**
                                 * SUCCESS—支付成功,
                                 * REFUND—转入退款,
                                 * NOTPAY—未支付,
                                 * CLOSED—已关闭,
                                 * REVOKED—已撤销(刷卡支付),
                                 * USERPAYING--用户支付中,
                                 * PAYERROR--支付失败(其他原因,如银行返回失败)
                                 */
                                Map<String, String> data1 = resultUtil.getData();
                                String s = data1.get("trade_state");
                                String transaction_id = data1.get("transaction_id");
                                if ("REFUND".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s) || num == 10) {
                                    //有待支付的订单,这里不处理
//                                    coursePackageOrder1.setState(3);
//                                    coursePackageOrderService.updateById(coursePackageOrder1);
                                    break;
                                }
                                if ("SUCCESS".equals(s)) {
                                    coursePackageOrder1.setPayStatus(2);
                                    coursePackageOrder1.setOrderNumber(transaction_id);
                                    coursePackageOrder1.setAppUserId(null);
                                    coursePackageOrderService.updateById(coursePackageOrder1);
                                    //修改课时有效期
                                    addCoursePackageOrderStudent(coursePackageOrder1.getId(), coursePackagePaymentConfig);
                                    break;
                                }
                                if ("USERPAYING".equals(s) || "NOTPAY".equals(s)) {
                                    num++;
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        return weixinpay;
//        return payMoneyUtil.weixinpayV3(WxV3PayConfig.smidVx,"报名运动营",code,
//                "/base/course/weChatPaymentCourseCallback1",Long.valueOf(paymentPrice.toString()),
//                coursePackagePaymentConfig.getId()+"");
    }
    private String smid = "2088330203191220";//平台支付宝商户号
    private String smidVx = "2088330203191220";//平台微信商户号
    /**
     * 课程支付宝支付
     *
@@ -1072,91 +1022,94 @@
            if (store.getOperatorId()==0){
                smid1 = smid;
            }else{
                smid1 = storeClient.getSMIDByOperatorId(store.getOperatorId());
                smid1 = storeClient.getmerchantNumberAliByOperatorId(store.getOperatorId());
            }
        }
        if (!StringUtils.hasLength(smid1)){
            return ResultUtil.error("运营商未配置支付宝商户号,获取支付失败!");
        }
        ResultUtil alipay = payMoneyUtil.alipay(smid1,"报名运动营", "报名运动营",
                coursePackagePaymentConfig.getId().toString(),
                code, paymentPrice.toString(), "/base/course/aliPaymentCourseCallback");
        if (alipay.getCode() == 200) {
            String finalCode = code;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        int num = 1;
                        int wait = 0;
                        while (num <= 10) {
                            int min = 5000;
                            wait += (min * num);
                            Thread.sleep(wait);
                            CoursePackageOrder coursePackageOrder1 = coursePackageOrderService.getOne(new QueryWrapper<CoursePackageOrder>()
                                    .eq("code", finalCode).eq("state", 1));
                            if (coursePackageOrder1.getPayStatus() == 2) {
                                break;
                            }
                            AlipayTradeQueryResponse alipayTradeQueryResponse = payMoneyUtil.queryALIOrder(finalCode);
                            if (null != alipayTradeQueryResponse) {
                                /**
                                 * WAIT_BUYER_PAY(交易创建,等待买家付款)、
                                 * TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
                                 * TRADE_SUCCESS(交易支付成功)、
                                 * TRADE_FINISHED(交易结束,不可退款)
                                 */
                                String s = alipayTradeQueryResponse.getTradeStatus();
                                String tradeNo = alipayTradeQueryResponse.getTradeNo();
                                if ("TRADE_CLOSED".equals(s) || "TRADE_FINISHED".equals(s) || num == 10) {
                                    //有待支付的订单,这里不处理
//                                    coursePackageOrder1.setState(3);
//        if (alipay.getCode() == 200) {
//            String finalCode = code;
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        Thread.sleep(1000);
//                        int num = 1;
//                        int wait = 0;
//                        while (num <= 10) {
//                            int min = 5000;
//                            wait += (min * num);
//                            Thread.sleep(wait);
//                            CoursePackageOrder coursePackageOrder1 = coursePackageOrderService.getOne(new QueryWrapper<CoursePackageOrder>()
//                                    .eq("code", finalCode).eq("state", 1));
//                            if (coursePackageOrder1.getPayStatus() == 2) {
//                                break;
//                            }
//                            AlipayTradeQueryResponse alipayTradeQueryResponse = payMoneyUtil.queryALIOrder(finalCode);
//                            if (null != alipayTradeQueryResponse) {
//                                /**
//                                 * WAIT_BUYER_PAY(交易创建,等待买家付款)、
//                                 * TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
//                                 * TRADE_SUCCESS(交易支付成功)、
//                                 * TRADE_FINISHED(交易结束,不可退款)
//                                 */
//                                String s = alipayTradeQueryResponse.getTradeStatus();
//
//                                String tradeNo = alipayTradeQueryResponse.getTradeNo();
//                                if ("TRADE_CLOSED".equals(s) || "TRADE_FINISHED".equals(s) || num == 10) {
//                                    //有待支付的订单,这里不处理
////                                    coursePackageOrder1.setState(3);
////                                    coursePackageOrderService.updateById(coursePackageOrder1);
//                                    break;
//                                }
//                                if ("TRADE_SUCCESS".equals(s)) {
//                                    coursePackageOrder1.setPayStatus(2);
//                                    coursePackageOrder1.setOrderNumber(tradeNo);
//                                    coursePackageOrder1.setAppUserId(null);
//                                    coursePackageOrderService.updateById(coursePackageOrder1);
                                    break;
                                }
                                if ("TRADE_SUCCESS".equals(s)) {
                                    coursePackageOrder1.setPayStatus(2);
                                    coursePackageOrder1.setOrderNumber(tradeNo);
                                    coursePackageOrder1.setAppUserId(null);
                                    coursePackageOrderService.updateById(coursePackageOrder1);
                                    addCoursePackageOrderStudent(coursePackageOrder1.getId(), coursePackagePaymentConfig);
                                    moneyOut(tradeNo,tradeNo,finalCode);
                                    // 根据课程id 查询这个课程属于哪个门店 属于哪个运营商
                                    Integer id = paymentCourseVo.getId();
                                    TCoursePackage byId = coursePackageService.getById(id);
                                    Integer storeId = byId.getStoreId();
                                    Store store = storeClient.queryStoreById(storeId);
                                    Integer operatorId = store.getOperatorId();
                                    if (operatorId==null){
                                        String smid ="2088330203191220";
                                            // 说明是平台的 不走分账
//                                            payMoneyUtil.confirm(smid,finalCode,tradeNo,paymentPrice.toString());
                                        payMoneyUtil.confirm1(smid,finalCode,tradeNo,paymentPrice.toString());
                                        break;
                                    }else if (operatorId==0){
                                        String smid ="2088330203191220";
                                        // 说明是平台的
                                        payMoneyUtil.confirm1(smid,finalCode,tradeNo,paymentPrice.toString());
                                        break;
                                    }else{
                                        // 当前课程不属于门店 查询这个课程属于哪个门店 属于哪个运营商
                                        String smidByOperatorId = storeClient.getSMIDByOperatorId(operatorId);
                                        payMoneyUtil.confirm(smidByOperatorId,finalCode,tradeNo,paymentPrice.toString());
                                        // 分账处理
                                        extracted(store, coursePackageOrder1, tradeNo);
                                        break;
                                    }
                                }
                                if ("WAIT_BUYER_PAY".equals(s)) {
                                    num++;
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
//                                    addCoursePackageOrderStudent(coursePackageOrder1.getId(), coursePackagePaymentConfig);
//                                    moneyOut(tradeNo,tradeNo,finalCode);
//                                    // 根据课程id 查询这个课程属于哪个门店 属于哪个运营商
//                                    Integer id = paymentCourseVo.getId();
//                                    TCoursePackage byId = coursePackageService.getById(id);
//                                    Integer storeId = byId.getStoreId();
//                                    Store store = storeClient.queryStoreById(storeId);
//                                    Integer operatorId = store.getOperatorId();
//                                    if (operatorId==null){
//                                        String smid ="2088330203191220";
//                                            // 说明是平台的 不走分账
////                                            payMoneyUtil.confirm(smid,finalCode,tradeNo,paymentPrice.toString());
//                                        payMoneyUtil.confirm1(smid,finalCode,tradeNo,paymentPrice.toString());
//                                        break;
//                                    }else if (operatorId==0){
//                                        String smid ="2088330203191220";
//                                        // 说明是平台的
//                                        payMoneyUtil.confirm1(smid,finalCode,tradeNo,paymentPrice.toString());
//                                        break;
//                                    }else{
//                                        // 当前课程不属于门店 查询这个课程属于哪个门店 属于哪个运营商
//                                        String smidByOperatorId = storeClient.getSMIDByOperatorId(operatorId);
//                                        payMoneyUtil.confirm(smidByOperatorId,finalCode,tradeNo,paymentPrice.toString());
//                                        // 分账处理
//                                        extracted(store, coursePackageOrder1, tradeNo);
//                                        break;
//                                    }
//                                }
//                                if ("WAIT_BUYER_PAY".equals(s)) {
//                                    num++;
//                                }
//                            }
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
//        }
        return alipay;
    }
@@ -1355,30 +1308,6 @@
        TCoursePackage byId = coursePackageService.getById(coursePackageOrder1.getCoursePackageId());
        Store store = storeClient.queryStoreById(byId.getStoreId());
//        moneyOut(trade_no,trade_no,code);
        if (store.getOperatorId()!=null && store.getOperatorId()!=0){
            // 休眠两分钟后再调用分账接口 避免提示订单正在处理中
            Thread.sleep(120000);
            // 根据运营商id获取对应运营商分账比例 返回格式: 微信分账比例,支付宝分账比例
            String proportionByOperatorId = storeClient.getProportionByOperatorId(store.getOperatorId());
            String[] split = proportionByOperatorId.split(",");
            String s1 = split[0];
            if (!s1.equals("未设置")){
                BigDecimal bigDecimal = new BigDecimal(s1);
                // 分账比例
                BigDecimal bigDecimal1 = bigDecimal.divide(new BigDecimal(100)).setScale(2);
                // 微信商户号
                String s2 = storeClient.getmerchantNumberByOperatorId(store.getOperatorId());
                String nonce_str = UUIDUtil.getRandomCode(16);
                ResultUtil fenzhang = payMoneyUtil.fenzhang(trade_no, coursePackageOrder1.getCashPayment().multiply(bigDecimal1), s2,nonce_str);
                if (!fenzhang.getCode().equals(200)){
                    System.err.println("分账失败 原因是:"+fenzhang.getData().toString());
                }else{
                    coursePackageOrder1.setFenzhangNo(fenzhang.getData().toString());
                    coursePackageOrder1.setFenzhangOrderNo(nonce_str);
                    coursePackageOrderService.updateById(coursePackageOrder1);
                }
            }
        }
        return ResultUtil.success();
    }