package com.njyunzhi.pet_identity.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyV3Result;
import com.github.binarywang.wxpay.service.WxPayService;
import com.njyunzhi.pet_identity.common.BaseController;
import com.njyunzhi.pet_identity.common.WxUtils;
import com.njyunzhi.pet_identity.service.ITaOrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "微信小程序")
@RestController
@RequestMapping("/wxpay")
public class WxPayController extends BaseController {

    @Autowired
    WxUtils wxUtils;

    @Autowired
    ITaOrderService iTaOrderService;

    @ResponseBody
    @RequestMapping("/notify/order")
    public String payNotify(@RequestBody String jsonData) {
        WxPayService payService = wxUtils.getPayService();
        WxPayOrderNotifyV3Result result = null;
        try {
            result = payService.parseOrderNotifyV3Result(jsonData, null);
            iTaOrderService.notifyOrderPay(result.getResult());
            return WxPayNotifyResponse.success("OK");
        } catch (Exception e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail(e.getMessage());
        }


    }

    @ResponseBody
    @RequestMapping("/notify/refund")
    public String refundNotify(@RequestBody String jsonData) {
        WxPayService payService = wxUtils.getPayService();
        WxPayRefundNotifyV3Result result = null;
        try {
            result = payService.parseRefundNotifyV3Result(jsonData, null);
            iTaOrderService.refundNotifyOrder(result.getResult());
            return WxPayNotifyResponse.success("OK");
        } catch (Exception e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail(e.getMessage());
        }
    }

}
