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.AlipayObject;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.SettleDetailInfo;
import com.alipay.api.domain.SettleInfo;
import com.alipay.api.domain.SubMerchant;
import com.alipay.api.request.AlipayTradeAppPayRequest;
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.AliAppOrderRS;
import com.jeequan.jeepay.core.utils.AmountUtil;
import com.jeequan.jeepay.thirdparty.channel.alipay.AlipayKit;
import com.jeequan.jeepay.thirdparty.channel.alipay.AlipayPaymentService;
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;

import java.util.ArrayList;
import java.util.List;

/*
* 支付宝直付通
*/
@Service("zftpayPaymentByAliAppService")
public class AliApp extends ZftpayPaymentService {

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

    /**
     * product_code 为产品码，按实际使用修改。APP支付上送：QUICK_MSECURITY_PAY，
     * 手机网站支付上送：QUICK_WAP_WAY，
     * 电脑网站支付上送：FAST_INSTANT_TRADE_PAY，
     * JSAPI支付上送：JSAPI_PAY。
     */
    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        AlipayTradeAppPayRequest req = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = getPayModel(rq,payOrder,AlipayTradeAppPayModel.class);
        req.setNotifyUrl(getNotifyUrl());
        model.setProductCode("QUICK_MSECURITY_PAY");
        req.setBizModel(model);
        String payData = null;
        try {
            payData = configContextQueryService.getZftpayClientWrapper(mchAppConfigContext).getAlipayClient().sdkExecute(req).getBody();
        } catch (AlipayApiException e) {
            throw ChannelException.sysError(e.getMessage());
        }
        // 构造函数响应数据
        AliAppOrderRS res = ApiResBuilder.buildSuccess(AliAppOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        //放置 响应数据
        channelRetMsg.setChannelAttach(payData);
        channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
        res.setPayData(payData);
        return res;
    }

}
