package controllers

import (
	"fmt"
	"net/http"

	"git.rosy.net.cn/baseapi/platformapi/wxpayapi"
	"git.rosy.net.cn/jx-callback/business/partner/purchase/jx/localjx"
	"git.rosy.net.cn/jx-callback/globals/api"
	"github.com/astaxie/beego/server/web"
)

type WXPayController struct {
	web.Controller
}

func (c *WXPayController) Msg() {
	if c.Ctx.Input.Method() == http.MethodPost {
		msg, callbackResponse := api.WxpayAPI.GetCallbackMsg(c.Ctx.Request)
		var err error
		if callbackResponse == nil {
			if msg.MsgType == wxpayapi.MsgTypeUnkown {
				err = fmt.Errorf("未知的微信支付回调类型:%d", msg.MsgType)
			} else {
				err = localjx.OnWxPayCallback(msg)
			}
		}
		if callbackResponse == nil {
			callbackResponse = wxpayapi.SuccessResponse
		} else if err != nil {
			callbackResponse = wxpayapi.Err2CallbackResponse(err, "")
		}
		c.Data["xml"] = callbackResponse
		c.ServeXML()
	} else {
		c.Abort("404")
	}
}
