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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.exception.ChannelException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.params.sandpay.SandpayConfig;
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.AliBarOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliBarOrderRS;
import com.jeequan.jeepay.thirdparty.channel.sandpay.SandPayKit;
import com.jeequan.jeepay.thirdparty.channel.sandpay.SandpayPaymentService;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/*
* 杉德 支付宝 条码支付
*
* @author pangxiaoyu
*
* @date 2021/6/8 18:11
*/
@Slf4j
@Service("sandPaymentByAliBarService") //Service Name需保持全局唯一性
public class AliBar extends SandpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {

        AliBarOrderRQ bizRQ = (AliBarOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getAuthCode())){
            throw new BizException("用户支付条码[authCode]不可为空");
        }

        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        String logPrefix = "【杉德支付宝("+payOrder.getWayCode()+")支付】";
        AliBarOrderRQ bizRQ = (AliBarOrderRQ) rq;
        AliBarOrderRS res = ApiResBuilder.buildSuccess(AliBarOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);

        JSONObject bodyJson = new JSONObject();
        // 通道类型
        bodyJson.put("payTool", "0401");
        // 付款码： 用户 APP 展示的付款条码或二维码
        bodyJson.put("authCode", bizRQ.getAuthCode().trim());
        // 杉德 统一参数赋值
        SandPayKit.setPayBody(bodyJson, payOrder, getNotifyUrl(), getReturnUrl());

        // 发送请求
        log.info("{}， 下单请求发送数据：{}", logPrefix, bodyJson);
        JSONObject resJson = SandPayKit.requestServer(bodyJson, SandpayConfig.ORDER_PAY, SandpayConfig.ORDERPAY_BARPAY, SandpayConfig.PRODUCTID_ALIPAY_QR, mchAppConfigContext);
        log.info("{}， 下单请求返回数据：{}", logPrefix, resJson);
        //请求 & 响应成功， 判断业务逻辑
        JSONObject resHead = resJson.getJSONObject("head");
        String status = resHead.getString("respCode");
        try {
            if(SandpayConfig.SUCCESS.equals(status)){

                JSONObject resBody = resJson.getJSONObject("body");
                String channelOrderNo = resBody.get("payOrderCode").toString();
                channelRetMsg.setChannelOrderId(channelOrderNo);

                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
                res.setPayData(resBody.toJSONString());
            }else {
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
                // 开启轮询查单
                channelRetMsg.setNeedQuery(true);
            }
        }catch (Exception e) {
            throw ChannelException.sysError(e.getMessage());
        }
        return res;
    }

}
