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

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
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.oauth2.AlipayOauth2Params;
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.AliJsapiOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliJsapiOrderRS;
import com.jeequan.jeepay.thirdparty.channel.kqpay.KqpayKit;
import com.jeequan.jeepay.thirdparty.channel.kqpay.KqpayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.kqpay.model.KqpayResp;
import com.jeequan.jeepay.thirdparty.channel.kqpay.util.KqpayConst;
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 xiaoyu
 *
 * @date 2022/6/13 17:20
 */
@Slf4j
@Service("kqpayPaymentByAliJsapiService") //Service Name需保持全局唯一性
public class AliJsapi extends KqpayPaymentService {

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

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        AliJsapiOrderRS res = ApiResBuilder.buildSuccess(AliJsapiOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        AliJsapiOrderRQ bizRQ = (AliJsapiOrderRQ) rq;
        JSONObject bizContent = new JSONObject();
        // 默认支付参数
        KqpayKit.setPayParams(mchAppConfigContext, bizContent);

        bizContent.put("amount",payOrder.getFindAmt());
        bizContent.put("userAgent","03");
        AlipayOauth2Params oauth2Params = (AlipayOauth2Params) configContextQueryService.queryIsvOauth2Params(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.ALIPAY);
        bizContent.put("subAppId",oauth2Params.getPid());
        bizContent.put("tr3Url",getNotifyUrl());
        // 买家支付宝用户ID
        bizContent.put("openId", bizRQ.getBuyerUserId());
        long between = DateUtil.between(payOrder.getCreatedAt(), payOrder.getExpiredTime(), DateUnit.SECOND);
        bizContent.put("txnExpireTime",between + "");
        bizContent.put("terminalIp",payOrder.getClientIp());
        bizContent.put("frontUrl",getReturnUrl(payOrder.getPayOrderId()));
        KqpayResp resp = KqpayKit.payRequest(KqpayConst.ReqMethod.A7009, mchAppConfigContext, bizContent,payOrder.getPayOrderId());
        this.initResult(resp, channelRetMsg);
        ChannelJsapiMsg jsapiMsg = channelRetMsg.getJsapiMsg();
        if(jsapiMsg != null ){
            // 获取payInfo
            res.setAlipayTradeNo(jsapiMsg.getTradeNo());
            res.setPayData(JSON.toJSONString(jsapiMsg));
        }
        return res;
    }

}
