package com.jeequan.jeepay.thirdparty.channel.lklspay.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.UpQrOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.UpQrOrderRS;
import com.jeequan.jeepay.thirdparty.channel.lklspay.LklspayKit;
import com.jeequan.jeepay.thirdparty.channel.lklspay.LklspayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.lklspay.model.LklPayMethod;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 银联NATIVE支付
 * @author crystal
 */
@Service("lklspayPaymentByUpQrService") //Service Name需保持全局唯一性
@Slf4j
public class UpQr extends LklspayPaymentService {

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

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        UpQrOrderRQ bizRQ = (UpQrOrderRQ)rq;
        // 构造函数响应数据
        UpQrOrderRS res = ApiResBuilder.buildSuccess(UpQrOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        JSONObject bizContent = new JSONObject();
        LklspayKit.setPayParams(mchAppConfigContext, bizContent,payOrder.getClientIp());
        bizContent.put("out_trade_no",payOrder.getPayOrderId());
        bizContent.put("total_amount",payOrder.getFindAmt());
        bizContent.put("account_type", "NATIVE");
        bizContent.put("trans_type","41");
        bizContent.put("subject",payOrder.getSubject());
        bizContent.put("notify_url",getNotifyUrl());
        bizContent.put("settle_type","0");
        bizContent.put("remark",payOrder.getBuyerRemark());
        JSONObject response = LklspayKit.payRequest(LklPayMethod.Api.TRANS_PREORDER, mchAppConfigContext.getMchApplyment(), bizContent);
        JSONObject payInfo = this.initResult(response, channelRetMsg);
        if(payInfo != null){
            String qrCdUrl = payInfo.getString("code");
            ChannelJsapiMsg jsapiMsg = new ChannelJsapiMsg();
            if(CS.PAY_DATA_TYPE.CODE_IMG_URL.equals(bizRQ.getPayDataType())){
                jsapiMsg.setQrCodeUrl(sysConfigService.getDBApplicationConfig().genScanImgUrl(qrCdUrl));
                res.setCodeImgUrl(sysConfigService.getDBApplicationConfig().genScanImgUrl(qrCdUrl));
            }else{
                jsapiMsg.setQrCodeUrl(qrCdUrl);
                res.setCodeUrl(qrCdUrl);
            }
            res.setPayData(JSON.toJSONString(jsapiMsg));
        }
        res.setChannelRetMsg(channelRetMsg);
        return res;
    }
}
