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.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.AliLiteOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliLiteOrderRS;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
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.service.ConfigContextQueryService;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 支付宝 小程序
 *
 */
@Service("sxfpayPaymentByAliLiteService") //Service Name需保持全局唯一性
public class AliLite extends SxfpayPaymentService {

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

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        AliLiteOrderRS res = ApiResBuilder.buildSuccess(AliLiteOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        AliLiteOrderRQ bizRQ = (AliLiteOrderRQ) rq;
        JSONObject bizContent = new JSONObject();
        // 默认支付参数
        SxfpayKit.setPayParams(mchAppConfigContext, bizContent, payOrder, rq);
        bizContent.put("payWay", "02");
        // 商户订单号
        bizContent.put("notifyUrl", getNotifyUrl());
        // 买家支付宝用户ID
        bizContent.put("userId", bizRQ.getBuyerUserId());
        // 配置参数
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        SxfpayIsvParams isvParams = (SxfpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), getIfCode());
        JSONObject respBizData = SxfpayKit.reqApi(ReqMethod.Method.TRADE_JSAPI_PAY, isvParams, bizContent);
        initCommonResult(respBizData, channelRetMsg);
        res.setChannelRetMsg(channelRetMsg);
        if (channelRetMsg.getJsapiMsg() != null) {
            res.setAlipayTradeNo(channelRetMsg.getJsapiMsg().getTradeNo());
            res.setPayData(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
        }
        return res;
    }
}
