ChannelOrderReissueService.java 6.0 KB
Newer Older
D
dingzhiwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2021-2031, 河北计全科技有限公司 (https://www.jeequan.com & jeequan@126.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jeequan.jeepay.pay.service;

import com.jeequan.jeepay.core.entity.PayOrder;
19
import com.jeequan.jeepay.core.entity.RefundOrder;
D
dingzhiwei 已提交
20 21
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.pay.channel.IPayOrderQueryService;
22
import com.jeequan.jeepay.pay.channel.IRefundService;
23
import com.jeequan.jeepay.pay.model.MchAppConfigContext;
D
dingzhiwei 已提交
24 25
import com.jeequan.jeepay.pay.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.service.impl.PayOrderService;
26
import com.jeequan.jeepay.service.impl.RefundOrderService;
D
dingzhiwei 已提交
27 28 29 30 31 32 33 34 35 36 37 38
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
* 查询上游订单, &  补单服务实现类
*
* @author terrfly
* @site https://www.jeepay.vip
* @date 2021/6/8 17:40
*/
39

D
dingzhiwei 已提交
40 41 42 43 44 45
@Service
@Slf4j
public class ChannelOrderReissueService {

    @Autowired private ConfigContextService configContextService;
    @Autowired private PayOrderService payOrderService;
46
    @Autowired private RefundOrderService refundOrderService;
D
dingzhiwei 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    @Autowired private PayMchNotifyService payMchNotifyService;


    /** 处理订单 **/
    public ChannelRetMsg processPayOrder(PayOrder payOrder){

        try {

            String payOrderId = payOrder.getPayOrderId();

            //查询支付接口是否存在
            IPayOrderQueryService queryService = SpringBeansUtil.getBean(payOrder.getIfCode() + "PayOrderQueryService", IPayOrderQueryService.class);

            // 支付通道接口实现不存在
            if(queryService == null){
                log.error("{} interface not exists!", payOrder.getIfCode());
                return null;
            }

66 67
            //查询出商户应用的配置信息
            MchAppConfigContext mchAppConfigContext = configContextService.getMchAppConfigContext(payOrder.getMchNo(), payOrder.getAppId());
D
dingzhiwei 已提交
68

69
            ChannelRetMsg channelRetMsg = queryService.query(payOrder, mchAppConfigContext);
D
dingzhiwei 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
            if(channelRetMsg == null){
                log.error("channelRetMsg is null");
                return null;
            }

            log.info("补单[{}]查询结果为:{}", payOrderId, channelRetMsg);

            // 查询成功
            if(channelRetMsg.getChannelState() == ChannelRetMsg.ChannelState.CONFIRM_SUCCESS) {
                if (payOrderService.updateIng2Success(payOrderId, channelRetMsg.getChannelOrderId())) {

                    // 通知商户系统
                    if(StringUtils.isNotEmpty(payOrder.getNotifyUrl())){
                        payMchNotifyService.payOrderNotify(payOrderService.getById(payOrderId));
                    }

                }
            }else if(channelRetMsg.getChannelState() == ChannelRetMsg.ChannelState.CONFIRM_FAIL){  //确认失败

                //1. 更新支付订单表为失败状态
                payOrderService.updateIng2Fail(payOrderId, channelRetMsg.getChannelOrderId(), channelRetMsg.getChannelErrCode(), channelRetMsg.getChannelErrMsg());

            }

            return channelRetMsg;

        } catch (Exception e) {  //继续下一次迭代查询
            log.error("error payOrderId = {}", payOrder.getPayOrderId(), e);
            return null;
        }

    }

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    /** 处理退款订单 **/
    public ChannelRetMsg processRefundOrder(RefundOrder refundOrder){

        try {

            String refundOrderId = refundOrder.getRefundOrderId();

            //查询支付接口是否存在
            IRefundService queryService = SpringBeansUtil.getBean(refundOrder.getIfCode() + "RefundService", IRefundService.class);

            // 支付通道接口实现不存在
            if(queryService == null){
                log.error("退款补单:{} interface not exists!", refundOrder.getIfCode());
                return null;
            }

            //查询出商户应用的配置信息
            MchAppConfigContext mchAppConfigContext = configContextService.getMchAppConfigContext(refundOrder.getMchNo(), refundOrder.getAppId());

            ChannelRetMsg channelRetMsg = queryService.query(refundOrder, mchAppConfigContext);
            if(channelRetMsg == null){
                log.error("退款补单:channelRetMsg is null");
                return null;
            }

            log.info("退款补单:[{}]查询结果为:{}", refundOrderId, channelRetMsg);

            // 查询成功
            if(channelRetMsg.getChannelState() == ChannelRetMsg.ChannelState.CONFIRM_SUCCESS) {
                if (refundOrderService.updateIng2Success(refundOrderId, channelRetMsg.getChannelOrderId())) {

                    // 通知商户系统
                    if(StringUtils.isNotEmpty(refundOrder.getNotifyUrl())){
                        payMchNotifyService.refundOrderNotify(refundOrderService.getById(refundOrderId));
                    }

                }
            }else if(channelRetMsg.getChannelState() == ChannelRetMsg.ChannelState.CONFIRM_FAIL){  //确认失败

                //1. 更新支付订单表为失败状态
                refundOrderService.updateIng2Fail(refundOrderId, channelRetMsg.getChannelOrderId(), channelRetMsg.getChannelErrCode(), channelRetMsg.getChannelErrMsg());

            }

            return channelRetMsg;

        } catch (Exception e) {  //继续下一次迭代查询
            log.error("退款补单:error refundOrderId = {}", refundOrder.getRefundOrderId(), e);
            return null;
        }

    }

D
dingzhiwei 已提交
156 157

}