package controllers

import (
	"bytes"
	"git.rosy.net.cn/baseapi/platformapi/tonglianpayapi"
	"io/ioutil"
	"net/http"

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

	"git.rosy.net.cn/jx-callback/business/partner/purchase/jx/localjx"
	"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))
		if err == nil {
			err = localjx.OnTLPayCallback(call)
		}
		c.Data["json"] = call
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}

//通联同一扫码支付回调接口
func (c *TongLianController) OnLinePay() {
	if c.Ctx.Input.Method() == http.MethodPost {
		call, err := api.TLpayAPI.GetCallbackOnlinePayMsg(getUsefulRequest2(c.Ctx))
		if err == nil {
			err = localjx.OnTLOnlinePayCallback(call)
		}
		if err == nil {
			c.Data["json"] = tonglianpayapi.OnlinePayStatusSuccess
		} else {
			c.Data["json"] = tonglianpayapi.OnlinePayStatusFail
		}
		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
}
