package com.jeequan.jeepay.thirdparty.channel.ryxpay;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.interfaces.paychannel.IPayOrderCloseService;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.params.ruipay.RyxpayIsvParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.PayRespEntity;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.ReqMethod;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 关闭订单接口实现类
 *
 * @author xiaoyu
 *
 * @date 2022/5/16 9:09
 */
@Slf4j
@Service
public class RyxpayPayOrderCloseService implements IPayOrderCloseService {

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    @Override
    public ChannelRetMsg close(PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        try {
            RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(),payOrder.getIfCode());
            JSONObject bizContent = new JSONObject();
            bizContent.put("merchantCode", payOrder.getChannelMchNo());
            bizContent.put("oriReqMsgId", payOrder.getPayOrderId());
            PayRespEntity response = RyxpayKit.payReqApi(ReqMethod.ServiceCode.SMZF015,isvParams ,bizContent, payOrder.getPayOrderId(), null);
            String respType = response.getRespType();
            if (!RyxpayKit.RESP_BIZ_SUCCESS.equals(respType)) {
                return ChannelRetMsg.confirmFail(response.getSmzfMsgId(),response.getRespCode(),response.getRespMsg());
            }
            return ChannelRetMsg.confirmSuccess(response.getSmzfMsgId());
        }catch (Exception e) {
            return ChannelRetMsg.sysError(e.getMessage());
        }
    }
}
