package com.kym.miniapp.controller;

import com.alibaba.fastjson2.JSONObject;
import com.kym.common.R;
import com.kym.common.annotation.ApiLog;
import com.kym.service.wechat.WxPayService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author skyline
 * @description
 * @date 2023-07-22 20:36
 */
@Controller
@RequestMapping("/payment")
public class PaymentController {
    private final WxPayService wxPayService;

    public PaymentController(WxPayService wxPayService) {
        this.wxPayService = wxPayService;
    }

    @ApiLog("微信支付")
    @PostMapping("/wxPay")
    @ResponseBody
    R<?> prepay(@RequestBody JSONObject params) {
        return R.success(wxPayService.wxPay(params));
    }

    @ApiLog(value = "微信回调推送", ignoreParams = true)
    @SneakyThrows
    @PostMapping("/notify")
    ResponseEntity<Object> notify(HttpServletRequest request) {
        return wxPayService.wxNotify(request);
    }


    @ApiLog("用户申请退款")
    @PostMapping("/wxApplyRefund")
    @ResponseBody
    R<?> wxAppRefund(@RequestBody String reason) {
        wxPayService.applyWxRefund(reason);
        return R.success();
    }

    @ApiLog(value = "微信退款回调推送", ignoreParams = true)
    @PostMapping("/refundNotify")
    ResponseEntity<Object> refundNotify(HttpServletRequest request) {
        return wxPayService.wxRefundNotify(request);
    }
}
