package com.jeequan.jeepay.thirdparty.channel.sxfpay.payway;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.exception.ChannelException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.params.sxfpay.SxfpayIsvParams;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.YsfJsapiOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.YsfJsapiOrderRS;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.SxfpayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.model.ReqMethod;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.SxfpayKit;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 云闪付jsapi
 *
 */
@Service("sxfpayPaymentByYsfJsapiService") //Service Name需保持全局唯一性
public class YsfJsapi extends SxfpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        YsfJsapiOrderRQ bizRQ = (YsfJsapiOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getUserId())){
            throw new BizException("[userId]不可为空");
        }
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        YsfJsapiOrderRS res = ApiResBuilder.buildSuccess(YsfJsapiOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        YsfJsapiOrderRQ bizRQ = (YsfJsapiOrderRQ) rq;
        try {
            JSONObject bizContent = new JSONObject();
            SxfpayKit.setPayParams(mchAppConfigContext, bizContent, payOrder, rq);
            bizContent.put("userId", bizRQ.getUserId());
            bizContent.put("notifyUrl", getNotifyUrl());
            if(StringUtils.isNotEmpty(bizRQ.getFrontUrl())){
                bizContent.put("frontUrl",bizRQ.getFrontUrl());
            }
            if(StringUtils.isNotEmpty(bizRQ.getFrontFailUrl())){
                bizContent.put("frontFailUrl",bizRQ.getFrontFailUrl());
            }
            SxfpayIsvParams isvParams = (SxfpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), getIfCode());
            JSONObject respBizData = SxfpayKit.reqApi(ReqMethod.Method.TRADE_JSAPI_PAY, isvParams, bizContent);
            initCommonResult(respBizData,channelRetMsg);
            if(channelRetMsg.getJsapiMsg() != null){
                res.setRedirectUrl(channelRetMsg.getJsapiMsg().getRedirectUrl());
                res.setPayData(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
            }
            return res;
        }catch (Exception e) {
            throw ChannelException.sysError(e.getMessage());
        }
    }
}
