package com.sonic.lion.controller.web;

import com.sonic.common.rpc.Result;
import com.sonic.lion.service.WebhookHandlerService;
import com.sonic.lion.utils.HttpUtil;
import com.sonic.sdk.api.annotation.IgnoreAuth;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.sonic.common.rpc.Result.success;

/**
 * Stripe回调
 * @author: code
 */
@Slf4j
@RestController
public class WebhookStripeController {

    @Autowired
    private WebhookHandlerService webhookHandlerService;

    @IgnoreAuth
    @ApiOperation(value = "stripe的充值回调", tags = {"Stripe回调"})
    @PostMapping(value = "/web/pay/webhooks/stripe/payment")
    public Result payment(@RequestHeader("Stripe-Signature") String signature, @ApiParam(hidden = true) HttpServletRequest request) throws Exception {
        webhookHandlerService.stripePaymentHandler(signature, HttpUtil.getBody(request));
        return success();
    }

    @IgnoreAuth
    @ApiOperation(value = "stripe的提现回调", tags = {"Stripe回调"})
    @PostMapping(value = "/web/pay/webhooks/stripe/payout")
    public Result payout(@RequestHeader("Stripe-Signature") String signature, @ApiParam(hidden = true) HttpServletRequest request) throws Exception {
        webhookHandlerService.stripePayoutHandler(signature, HttpUtil.getBody(request));
        return success();
    }

    @IgnoreAuth
    @ApiOperation(value = "stripe的订阅回调", tags = {"Stripe回调"})
    @PostMapping(value = "/web/pay/webhooks/stripe/subscription")
    public Result subscription(@RequestHeader("Stripe-Signature") String signature, @ApiParam(hidden = true) HttpServletRequest request) throws Exception {
        webhookHandlerService.stripeSubscriptionHandler(signature, HttpUtil.getBody(request));
        return success();
    }

    @IgnoreAuth
    @ApiOperation(value = "stripe的争议回调", tags = {"Stripe回调"})
    @PostMapping(value = "/web/pay/webhooks/stripe/dispute")
    public Result dispute(@RequestHeader("Stripe-Signature") String signature, @ApiParam(hidden = true) HttpServletRequest request) throws Exception {
        webhookHandlerService.stripeDisputeHandler(signature, HttpUtil.getBody(request));
        return success();
    }

}
