package com.yonge.cooleshow.admin.controller;

import com.huifu.adapay.core.AdapayCore;
import com.huifu.adapay.core.util.AdapaySign;
import com.yonge.cooleshow.admin.service.OmsOrderReturnApplyService;
import com.yonge.cooleshow.common.controller.BaseController;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: liweifan
 * @Data: 2022/3/11 18:16
 */
@RestController
@RequestMapping("/payment")
@Api(value = "付款接口", tags = "付款接口")
public class PaymentController extends BaseController {
    private final static Logger log = LoggerFactory.getLogger(PaymentController.class);

    @Autowired
    private OmsOrderReturnApplyService orderReturnApplyService;

    /**
     * 汇付回调
     * 问题：1、成功失败需要回执什么信息 2、回调url地址是否需要加入白名单
     * @author liweifan
     * @param: request
     * @updateTime 2022/3/11 18:35
     */
    @PostMapping("/callback")
    public void callback(HttpServletRequest request) {
        try {
            //验签传参data
            String data = request.getParameter("data");
            //验签传参sign
            String sign = request.getParameter("sign");
            //验签传参publicKey
            String publicKey = AdapayCore.PUBLIC_KEY;
            //验签
            boolean checkSign = AdapaySign.verifySign(data, sign, publicKey);
            log.info("汇付回调，res is {}", data);
            if (checkSign) {
                //验签成功逻辑
                log.info("验签成功");
                String type = request.getParameter("type");
                if (type.contains("refund")) {
                    orderReturnApplyService.orderCallback(data);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
