package com.xingxi.unifiedpay.open;

import com.xingxi.unifiedpay.bean.BiandanUnifiedpayCallbackRequest;
import com.xingxi.unifiedpay.bean.BiandanUnifiedpayCallbackResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.web.bind.annotation.PathVariable;
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;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/unifiedpay")
@ConditionalOnBean({XingxiUnifiedpayServiceI.class})
public class WXPayCallbackEndpoint {
    private final XingxiUnifiedpayServiceI unifiedpayServiceI;


    @PostMapping("/{mercId}/callback")
    public String callback(@PathVariable("mercId") Long mercId, HttpServletRequest request, HttpServletResponse response) {
        log.debug("MercId[" + mercId + "]WXPayCallback");

        BiandanUnifiedpayCallbackRequest callbackRequest = new BiandanUnifiedpayCallbackRequest();
        callbackRequest.setHttpServletRequest(request);
        callbackRequest.setSellerId(mercId);

        try {
            BiandanUnifiedpayCallbackResponse callbackResponse = unifiedpayServiceI.getResponse(callbackRequest);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("商户[" + mercId + "]处理微信支付回调通知失败!!!!");
            return "FAIL";
        }

        return "SUCCESS";
    }
}
