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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.MchApplyment;
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.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.lklspay.model.LklPayMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 关闭订单接口实现类
 *
 * @author crystal
 *
 */
@Slf4j
@Service
public class LklspayPayOrderCloseService implements IPayOrderCloseService {

    @Override
    public ChannelRetMsg close(PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        try {
            JSONObject bizContent = new JSONObject();
            LklspayKit.setPayParams(mchAppConfigContext,bizContent,payOrder.getClientIp());
            bizContent.put("origin_out_trade_no", payOrder.getPayOrderId());
            bizContent.put("origin_trade_no", payOrder.getPlatformMchOrderNo());

            MchApplyment mchApplyment = mchAppConfigContext.getMchApplyment();
            JSONObject respBizData = LklspayKit.payRequest(LklPayMethod.Api.ORDER_CLOSE, mchApplyment, bizContent);
            String trade_no = respBizData.getString("origin_trade_no");
            return ChannelRetMsg.confirmSuccess(trade_no);
        }catch (Exception e) {
            return ChannelRetMsg.sysError(e.getMessage());
        }
    }
}
