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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.exception.ChannelException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
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.AliWapOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliWapOrderRS;
import com.jeequan.jeepay.core.utils.AmountUtil;
import com.jeequan.jeepay.thirdparty.channel.alipay.AlipayKit;
import com.jeequan.jeepay.thirdparty.channel.zftpay.ZftpayKit;
import com.jeequan.jeepay.thirdparty.channel.zftpay.ZftpayPaymentService;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import org.springframework.stereotype.Service;

/*
* 支付宝直付通 wap支付
*
* @author terrfly
*
* @date 2021/6/8 17:21
*/
@Service("zftpayPaymentByAliWapService") //Service Name需保持全局唯一性
public class AliWap extends ZftpayPaymentService {

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

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        AliWapOrderRQ bizRQ = (AliWapOrderRQ)rq;
        AlipayTradeWapPayRequest req = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = getPayModel(rq, payOrder, AlipayTradeWapPayModel.class);
        req.setNotifyUrl(getNotifyUrl());
        model.setProductCode("QUICK_WAP_WAY");
        // 构造函数响应数据
        req.setBizModel(model);
        AliWapOrderRS res = ApiResBuilder.buildSuccess(AliWapOrderRS.class);
        try {
            if(CS.PAY_DATA_TYPE.FORM.equals(bizRQ.getPayDataType())){ //表单方式
                res.setFormContent(configContextQueryService.getZftpayClientWrapper(mchAppConfigContext).getAlipayClient().pageExecute(req).getBody());
            }else if (CS.PAY_DATA_TYPE.CODE_IMG_URL.equals(bizRQ.getPayDataType())){ //二维码图片地址
                String payUrl = configContextQueryService.getZftpayClientWrapper(mchAppConfigContext).getAlipayClient().pageExecute(req, "GET").getBody();
                res.setCodeImgUrl(sysConfigService.getDBApplicationConfig().genScanImgUrl(payUrl));
            }else{ // 默认都为 payUrl方式
                res.setPayUrl(configContextQueryService.getZftpayClientWrapper(mchAppConfigContext).getAlipayClient().pageExecute(req, "GET").getBody());
            }
        }catch (AlipayApiException e) {
            throw ChannelException.sysError(e.getMessage());
        }
        res.setPayDataType(res.buildPayDataType());
        res.setPayData(res.buildPayData());
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        //放置 响应数据
        channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
        return res;
    }

}
