package com.ghy.web.controller.pay;

import com.ghy.common.adapay.model.Event;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.utils.spring.SpringUtils;
import com.ghy.payment.service.CallBackService;
import com.huifu.adapay.core.AdapayCore;
import com.huifu.adapay.core.util.AdapaySign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 提供给 Adapay 的回调接口
 *
 * @author HH 2022/3/25
 */
@RestController
public class AdapayCallbackController extends BaseController {

    @PostMapping("/adapay/callback")
    public String callback(Event event) {
        logger.info(" 收单返回消息event" + event);
        //验签请参data
        String data = event.getData();
        logger.info(" 收单返回消息data" + data);
        //验签请参sign
        String sign = event.getSign();
        boolean verifySign = false;
        try {
            verifySign = verifySign(data, sign);
        } catch (Exception e) {
            logger.error("签名验证失败 {}", e.getMessage());
        }
        verifySign = true;
        if (verifySign) {
            //Event事件类型
            String type = event.getType();
            String beanName;
            if (StringUtils.isBlank(type)) {
                return "NG";
            }
            switch (type) {
                case "payment.succeeded":
                case "payment.failed":
                    beanName = "payCallbackService";
                    break;
                case "payment_reverse.succeeded":
                case "payment_reverse.failed":
                    beanName = "payReverseCallbackService";
                    break;
                case "payment.close.succeeded":
                case "payment.close.failed":
                    beanName = "refundCallbackService";
                    break;
                case "cash.succeeded":
                case "cash.failed":
                    beanName = "drawCashCallbackService";
                    break;
                default:
                    logger.warn("UNKNOWN EVENT TYPE [{}]", type);
                    return "NG";
            }
            logger.info("handler bean is {} ", beanName);

            CallBackService callBackService = SpringUtils.getBean(beanName);
            callBackService.onCallback(event);
            return "OK";
        }
        logger.warn("签名验证失败 data=[{}], sign=[{}]", data, sign);
        return "NG";
    }

    @GetMapping("/adapay/callback")
    @ResponseBody
    public String test() {
        return "NG";
    }

    /**
     * 校验签名
     *
     * @param data AdapayCallback.data
     * @param sign AdapayCallback.sign
     * @return TRUE=校验成功
     */
    private boolean verifySign(String data, String sign) throws Exception {
        Assert.hasText(data, "data is blank!");
        Assert.hasText(sign, "sign is blank!");
        //验签请参publicKey
        String publicKey = AdapayCore.PUBLIC_KEY;
        logger.debug("验签请参：data={}sign={}", data, sign);
        //验签
        return AdapaySign.verifySign(data, sign, publicKey);
    }
}
