package cn.sikey.hmd.controller.app.razorpay;

import cn.sikey.framework.common.exception.ServiceException;
import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.pay.api.razorpay.RazorpayApi;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author: nelson
 * @Date: 2025/3/31
 * @Description: 网关
 */
@Controller
@RequestMapping("/app-api/hmd/razorpay/call")
@Slf4j
public class RazorpayPaymentCallBackController {

    @Resource
    private RazorpayApi razorpayApi;

    /**
     * 手机网站支付，回调
     *
     * @param request 参数
     * @return
     */
    @PostMapping("/tradeWapPayGateway")
    public String tradeWapPayGateway(HttpServletRequest request) {
        Map<String, String> paramsMap = request.getParameterMap().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> String.join(",", entry.getValue())));
        log.info("回调 进入 paramsMap:{}", paramsMap);
        CommonResult<Void> tradeWapPayGateway = razorpayApi.tradeWapPayGateway(paramsMap);
        if (!tradeWapPayGateway.getCode().equals(HttpStatus.SC_OK)) {
            log.warn("[手机网站支付-razorpay]调用razorpay支付服务失败");
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "调用支付服务失败");
        }
        log.info("回调 返回");
        return "razorpayResult";
    }

}
