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.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.AliQrOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliQrOrderRS;
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 org.springframework.stereotype.Service;

/**
 * 银盛 支付宝扫码
 *
 * @author xiaoyu
 *
 * @date 2022/5/24 17:38
 */
@Service("kqpayPaymentByAliQrService") //Service Name需保持全局唯一性
public class AliQr extends KqpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        AliQrOrderRQ bizRQ = (AliQrOrderRQ) rq;
        AliQrOrderRS res = ApiResBuilder.buildSuccess(AliQrOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        JSONObject bizContent = new JSONObject();
        // 默认支付参数
        KqpayKit.setPayParams(mchAppConfigContext, bizContent);
        bizContent.put("cur","CNY");
        bizContent.put("amount",payOrder.getFindAmt());
        bizContent.put("tr3Url",getNotifyUrl());
        String expiredTime = DateUtil.between(payOrder.getCreatedAt(), payOrder.getExpiredTime(), DateUnit.SECOND) + "";
        bizContent.put("qrCodeTimeout",expiredTime);
        bizContent.put("terminalIp",payOrder.getClientIp());
        KqpayResp resp = KqpayKit.payRequest(KqpayConst.ReqMethod.A7007, mchAppConfigContext, bizContent,payOrder.getPayOrderId());
        this.initResult(resp, channelRetMsg);
        String qrCode = resp.getResponseBody().getString("qrCode");
        ChannelJsapiMsg channelJsapiMsg = new ChannelJsapiMsg();
        if(CS.PAY_DATA_TYPE.CODE_IMG_URL.equals(bizRQ.getPayDataType())){
            channelJsapiMsg.setQrCodeUrl(sysConfigService.getDBApplicationConfig().genScanImgUrl(qrCode));
            res.setCodeImgUrl(sysConfigService.getDBApplicationConfig().genScanImgUrl(qrCode));
        }else{ //默认都为跳转地址方式
            channelJsapiMsg.setQrCodeUrl(qrCode);
            res.setCodeUrl(qrCode);
        }
        res.setPayData(JSON.toJSONString(channelJsapiMsg));
        res.setChannelRetMsg(channelRetMsg);
        return res;
    }
}
