package com.jeequan.jeepay.thirdparty.channel.dgpay.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.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelJsapiMsg;
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.dgpay.DgPayKit;
import com.jeequan.jeepay.thirdparty.channel.dgpay.DgpayPaymentService;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/*
 * 银联 jsapi
 *
 * @author zx
 *
 * @date 2021/6/8 18:11
 */
@Slf4j
@Service("dgpayPaymentByYsfJsapiService") //Service Name需保持全局唯一性
public class YsfJsapi extends DgpayPaymentService {

    @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);

        try {
            YsfJsapiOrderRQ bizRQ = (YsfJsapiOrderRQ) rq;
            JSONObject dataParams = DgPayKit.dataParams(payOrder, getNotifyUrl());
            dataParams.put("trade_type", "U_JSAPI");

            JSONObject jsapiJson = new JSONObject();
            jsapiJson.put("customer_ip",  payOrder.getClientIp());
            jsapiJson.put("user_id",  bizRQ.getUserId());
            dataParams.put("unionpay_data", jsapiJson.toJSONString());

            // 设置订单是否需要分账
            if(isDivisionOrder(payOrder)) {
                dataParams.put("delay_acct_flag", "Y");
            }

            JSONObject response = DgPayKit.payRequest(mchAppConfigContext, dataParams, "/trade/payment/jspay", payOrder.getIfCode());
            JSONObject resData = response.getJSONObject("data");
            channelRetMsg.setChannelBizData(resData);
            String code = resData.getString("resp_code");
            if (DgPayKit.STATE_ING.equals(code)) {
                String channelOrderNo = resData.getString("hf_seq_id");
                //付款信息
                String payInfo = resData.getString("pay_info");
                channelRetMsg.setChannelOrderId(channelOrderNo);
                res.setRedirectUrl(payInfo);
                ChannelJsapiMsg channelJsapiMsg = new ChannelJsapiMsg();
                channelJsapiMsg.setRedirectUrl(payInfo);
                res.setPayData(JSON.toJSONString(channelJsapiMsg));
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
            }else {
                DgPayKit.channelMsgError(resData, channelRetMsg);
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
            }
            return res;

        }catch (Exception e) {
            log.error(e.getMessage());
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
            channelRetMsg.setChannelErrMsg("系统异常");
            return res;
        }
    }

}
