package com.hc.web.controller.browser.front;

import com.alipay.api.AlipayApiException;
import com.hc.business.service.AliPayService;
import com.hc.core.utils.pay.browser.alipay.bean.AliPayBean;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author 10696
 */

@RestController
@RequestMapping("/new/api/front/ali")
@Slf4j
public class FrontAliPayController {

    /**
     * 服务对象
     */
    private final AliPayService aliPayService;

    public FrontAliPayController(AliPayService aliPayService) {
        this.aliPayService = aliPayService;
    }

    @PostMapping("/pay")
    @ApiOperation(value = "阿里云支付", response = Result.class)
    public Result alipay(@Valid @RequestBody AliPayBean aliPayBean) throws AlipayApiException {
        return Result.success(aliPayService.alipay(aliPayBean));
    }

    @PostMapping("/notify")
    public void returnNotifyUrlInfo(HttpServletRequest request) throws ServletRequestBindingException {
        String outTradeNo = ServletRequestUtils.getRequiredStringParameter(request, "out_trade_no");
        String tradeNo = ServletRequestUtils.getRequiredStringParameter(request, "trade_no");
        String tradeStatus = ServletRequestUtils.getRequiredStringParameter(request, "trade_status");
        aliPayService.returnNotifyUrlInfo(outTradeNo, tradeNo, tradeStatus);
    }


}
