package controllers

import (
	"bytes"
	"io/ioutil"
	"net/http"

	"git.rosy.net.cn/jx-callback/globals/api"

	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/partner/purchase/jx/localjx"
	"git.rosy.net.cn/jx-callback/globals"

	"github.com/astaxie/beego/server/web"
	"github.com/astaxie/beego/server/web/context"
)

type TongLianController struct {
	web.Controller
}

func (c *TongLianController) Msg() {
	if c.Ctx.Input.Method() == http.MethodPost {
		call, err := api.TLpayAPI.GetCallbackMsg(getUsefulRequest2(c.Ctx))
		globals.SugarLogger.Debugf("tonglianapi callback callbackResponse:%s", utils.Format4Output(call, true))
		if err == nil {
			err = localjx.OnTLPayCallback(call)
		}
		c.Data["json"] = call
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}

func getUsefulRequest2(ctx *context.Context) *http.Request {
	ctx.Request.Body = ioutil.NopCloser(bytes.NewReader(ctx.Input.RequestBody))
	return ctx.Request
}
