无关风月
16 小时以前 5dc40fcd64b0513150f1d8335ab849e6d8cdc28e
cloud-server-competition/src/main/java/com/dsh/competition/service/impl/CompetitionServiceImpl.java
@@ -34,8 +34,10 @@
import com.dsh.competition.service.IPaymentCompetitionService;
import com.dsh.competition.service.UserCompetitionService;
import com.dsh.competition.util.*;
import com.dsh.competition.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;
@@ -299,11 +301,20 @@
        if (paymentCompetitionVo.getPayType() == 1) {//微信
            Competition byId = competitionService.getById(paymentCompetitionVo.getId());
            Integer operatorId = byId.getOperatorId();
            int temp = 0;
            if (operatorId!=null && operatorId != 0){
                temp =1;
                // 运营商
                String s = storeClient.getmerchantNumberByOperatorId(operatorId);
                if(!StringUtils.hasLength(s)){
                    return ResultUtil.error("运营商未配置微信商户号,获取支付失败!");
            }
            return weChatPaymentCompetition(code, money,temp,operatorId);
                return payMoneyUtil.weixinpayV3(s,"报名赛事",code,
                        "/base/competition/weChatPaymentCompetitionCallback1",
                        money.toString());
            }else{
                return    payMoneyUtil.weixinpay("报名赛事", "", code, money.toString(),
                "/base/competition/weChatPaymentCompetitionCallback", "APP", "");
            }
        }
        if (paymentCompetitionVo.getPayType() == 2) {//支付宝
@@ -315,7 +326,10 @@
                return aliPaymentCompetition(operatorId,smid,code, money);
            }
            // 获取该运营商的商户号
            String smid = storeClient.getSMIDByOperatorId(operatorId);
            String smid = storeClient.getmerchantNumberAliByOperatorId(operatorId);
            if (!StringUtils.hasLength(smid)){
                return ResultUtil.error("运营商未配置支付宝商户号,获取支付失败!");
            }
            return aliPaymentCompetition(operatorId,smid,code, money);
        }
        if (paymentCompetitionVo.getPayType() == 3) {//玩湃币
@@ -362,7 +376,6 @@
        return ResultUtil.success();
    }
    /**
     * 赛事微信支付
     *
@@ -372,69 +385,15 @@
     * @throws Exception
     */
    public ResultUtil weChatPaymentCompetition(String code, BigDecimal money,Integer isFenZhang,Integer operatorId) throws Exception {
//
//        payMoneyUtil.weixinpayV3(WxV3PayConfig.smidVx,"报名赛事",code,
//                "/base/competition/weChatPaymentCompetitionCallback1",
//                Long.valueOf(money.toString()));
        ResultUtil weixinpay = payMoneyUtil.weixinpay("报名赛事"+"-"+isFenZhang, "", code, money.toString(),
                "/base/competition/weChatPaymentCompetitionCallback", "APP", "");
        if (weixinpay.getCode() == 200) {
            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);
                            PaymentCompetition paymentCompetition = paymentCompetitionService.getOne(new QueryWrapper<PaymentCompetition>()
                                    .eq("code", code).eq("payType", 1));
                            if (paymentCompetition.getPayStatus() == 2) {
                                break;
                            }
                            ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryWXOrder(code, "");
                            if (resultUtil.getCode() == 200 && paymentCompetition.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) {
                                    paymentCompetition.setAppUserId(null);
                                    paymentCompetition.setState(3);
                                    userCompetitionService.remove(new QueryWrapper<UserCompetition>().eq("paymentCompetitionId", paymentCompetition.getId()));
                                    break;
                                }
                                if ("SUCCESS".equals(s)) {
                                    paymentCompetition.setAppUserId(null);
                                    paymentCompetition.setPayStatus(2);
                                    paymentCompetition.setPayTime(new Date());
                                    paymentCompetition.setPayOrderNo(transaction_id);
                                    paymentCompetitionService.updateById(paymentCompetition);
                                    Competition competition = cttService.getById(paymentCompetition.getCompetitionId());
                                    competition.setApplicantsNumber(competition.getApplicantsNumber() + 1);
                                    cttService.updateById(competition);
                                    break;
                                }
                                if ("USERPAYING".equals(s) || "NOTPAY".equals(s)) {
                                    num++;
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        return weixinpay;
    }
@@ -449,80 +408,80 @@
    public ResultUtil aliPaymentCompetition(Integer operatorId,String smid,String code, BigDecimal money) throws Exception {
        ResultUtil alipay = payMoneyUtil.alipay
                (smid,"报名赛事", "", "", code, money.toString(), "/base/competition/aliPaymentCompetitionCallback");
        if (alipay.getCode() == 200) {
            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);
                            PaymentCompetition paymentCompetition = paymentCompetitionService.getOne(new QueryWrapper<PaymentCompetition>().eq("code", code).eq("payType", 2));
                            if (paymentCompetition.getPayStatus() == 2) {
                                break;
                            }
                            AlipayTradeQueryResponse resultUtil = payMoneyUtil.queryALIOrder(code);
                            if (resultUtil.getCode().equals("10000") && paymentCompetition.getPayStatus() == 1) {
                                /**
                                 * WAIT_BUYER_PAY(交易创建,等待买家付款)、
                                 * TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
                                 * TRADE_SUCCESS(交易支付成功)、
                                 * TRADE_FINISHED(交易结束,不可退款)
                                 */
//                                Map<String, String> data1 = resultUtil.getData();
//                                String s = data1.get("tradeStatus");
//                                String tradeNo = data1.get("tradeNo");
                                String tradeNo = resultUtil.getTradeNo();
                                String s = resultUtil.getTradeStatus();
                                if ("TRADE_CLOSED".equals(s) || "TRADE_FINISHED".equals(s) || num == 10) {
                                    paymentCompetition.setAppUserId(null);
                                    paymentCompetition.setState(3);
                                    userCompetitionService.remove(new QueryWrapper<UserCompetition>().eq("paymentCompetitionId", paymentCompetition.getId()));
                                    break;
                                }
                                if ("TRADE_SUCCESS".equals(s)) {
                                    paymentCompetition.setAppUserId(null);
                                    paymentCompetition.setPayStatus(2);
                                    paymentCompetition.setPayTime(new Date());
                                    paymentCompetition.setPayOrderNo(tradeNo);
                                    paymentCompetitionService.updateById(paymentCompetition);
                                    Competition competition = cttService.getById(paymentCompetition.getCompetitionId());
                                    competition.setApplicantsNumber(competition.getApplicantsNumber() + 1);
                                    cttService.updateById(competition);
                                    // 结算资金到商户账号
                                    payMoneyUtil.confirm(smid,code,tradeNo,money.toString());
                                    if (operatorId == null || operatorId == 0){
                                        // 属于平台的赛事 资金不做冻结 不做分账处理
                                        payMoneyUtil.confirm1(smid,code,tradeNo,money.toString());
                                    }else{
                                        // 资金冻结
                                        payMoneyUtil.confirm(smid,code,tradeNo,money.toString());
                                        // 分账处理
                                        extracted(operatorId, money, tradeNo);
                                    }
                                    // 结算资金到商户账号
                                    //分账
//                                    moneyOut(tradeNo, tradeNo,code);
                                    break;
                                }
                                if ("WAIT_BUYER_PAY".equals(s)) {
                                    num++;
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
//        if (alipay.getCode() == 200) {
//            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);
//                            PaymentCompetition paymentCompetition = paymentCompetitionService.getOne(new QueryWrapper<PaymentCompetition>().eq("code", code).eq("payType", 2));
//                            if (paymentCompetition.getPayStatus() == 2) {
//                                break;
//                            }
//                            AlipayTradeQueryResponse resultUtil = payMoneyUtil.queryALIOrder(code);
//                            if (resultUtil.getCode().equals("10000") && paymentCompetition.getPayStatus() == 1) {
//                                /**
//                                 * WAIT_BUYER_PAY(交易创建,等待买家付款)、
//                                 * TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
//                                 * TRADE_SUCCESS(交易支付成功)、
//                                 * TRADE_FINISHED(交易结束,不可退款)
//                                 */
////                                Map<String, String> data1 = resultUtil.getData();
////                                String s = data1.get("tradeStatus");
////                                String tradeNo = data1.get("tradeNo");
//                                String tradeNo = resultUtil.getTradeNo();
//                                String s = resultUtil.getTradeStatus();
//
//                                if ("TRADE_CLOSED".equals(s) || "TRADE_FINISHED".equals(s) || num == 10) {
//                                    paymentCompetition.setAppUserId(null);
//                                    paymentCompetition.setState(3);
//                                    userCompetitionService.remove(new QueryWrapper<UserCompetition>().eq("paymentCompetitionId", paymentCompetition.getId()));
//
//                                    break;
//                                }
//                                if ("TRADE_SUCCESS".equals(s)) {
//                                    paymentCompetition.setAppUserId(null);
//                                    paymentCompetition.setPayStatus(2);
//                                    paymentCompetition.setPayTime(new Date());
//                                    paymentCompetition.setPayOrderNo(tradeNo);
//                                    paymentCompetitionService.updateById(paymentCompetition);
//
//                                    Competition competition = cttService.getById(paymentCompetition.getCompetitionId());
//                                    competition.setApplicantsNumber(competition.getApplicantsNumber() + 1);
//                                    cttService.updateById(competition);
//
//                                    // 结算资金到商户账号
//                                    payMoneyUtil.confirm(smid,code,tradeNo,money.toString());
//                                    if (operatorId == null || operatorId == 0){
//                                        // 属于平台的赛事 资金不做冻结 不做分账处理
//                                        payMoneyUtil.confirm1(smid,code,tradeNo,money.toString());
//                                    }else{
//                                        // 资金冻结
//                                        payMoneyUtil.confirm(smid,code,tradeNo,money.toString());
//                                        // 分账处理
//                                        extracted(operatorId, money, tradeNo);
//                                    }
//                                    // 结算资金到商户账号
//                                    //分账
////                                    moneyOut(tradeNo, tradeNo,code);
//                                    break;
//                                }
//                                if ("WAIT_BUYER_PAY".equals(s)) {
//                                    num++;
//                                }
//                            }
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
//        }
        return alipay;
    }
    private void extracted(Integer operatorId, BigDecimal money, String tradeNo) throws Exception {