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

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.dgpay.DgPayKit;
import com.jeequan.jeepay.thirdparty.channel.dgpay.DgpayPaymentService;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import org.springframework.stereotype.Service;

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

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

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){

        AliQrOrderRQ aliQrOrderRQ = (AliQrOrderRQ)rq;
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        // 构造函数响应数据
        AliQrOrderRS res = ApiResBuilder.buildSuccess(AliQrOrderRS.class);
        res.setChannelRetMsg(channelRetMsg);

        JSONObject dataParams = DgPayKit.dataParams(payOrder, getNotifyUrl());
        dataParams.put("trade_type", "A_NATIVE");
        // 设置订单是否需要分账
        if(isDivisionOrder(payOrder)) {
            dataParams.put("delay_acct_flag", "Y");
        }

        JSONObject response = DgPayKit.payRequest(mchAppConfigContext, dataParams, "/trade/payment/jspay", payOrder.getIfCode());
        JSONObject resData = response.getJSONObject("data");
        String code = resData.getString("resp_code");
        channelRetMsg.setChannelBizData(resData);
        if (DgPayKit.STATE_ING.equals(code)) {

            String channelOrderNo = resData.get("hf_seq_id").toString();
            //付款信息
            String qrCdLink = resData.getString("qr_code");
            channelRetMsg.setChannelOrderId(channelOrderNo);
            ChannelJsapiMsg jsapiMsg = new ChannelJsapiMsg();
            if(CS.PAY_DATA_TYPE.CODE_IMG_URL.equals(aliQrOrderRQ.getPayDataType())){ //二维码地址
                res.setCodeImgUrl(sysConfigService.getDBApplicationConfig().genScanImgUrl(qrCdLink));
                jsapiMsg.setQrCodeUrl(sysConfigService.getDBApplicationConfig().genScanImgUrl(qrCdLink));
            }else{ //默认都为跳转地址方式
                jsapiMsg.setQrCodeUrl(qrCdLink);
                res.setCodeUrl(qrCdLink);
            }
            res.setPayData(JSON.toJSONString(jsapiMsg));
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
        }else {
            DgPayKit.channelMsgError(resData, channelRetMsg);
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
        }
        return res;
    }

}
