package tiktok

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
)

const (
	PayStatus    = "payment" //  回调类型标记，支付成功回调为"payment"
	RefundStatus = "refund"  //  回调类型标记，退款回调为"refund"

	WxPayWay  = "1"  // 1-微信支付
	ZfbPayWay = "2"  // 2-支付宝支付
	TtPayWay  = "10" // 10-抖音支付
)

func (a *API) GetCallbackMsg(request *http.Request) (call *DetailCallBackMessage, refund *DetailCallBackMessage2Refund, payType string, err error) {
	data, err := ioutil.ReadAll(request.Body)
	if err != nil {
		return nil, nil, "", err
	}

	payInfo := &CallBackResult{}
	if err := json.Unmarshal(data, payInfo); err != nil {
		return nil, nil, "", err
	}

	// 支付回调
	if payInfo.TypePay == PayStatus {
		payDetailResult := &DetailCallBackMessage{}
		if err := json.Unmarshal([]byte(payInfo.Msg), payDetailResult); err != nil {
			return nil, nil, "", err
		}
		return payDetailResult, nil, payInfo.TypePay, err
	}

	// 退款回调
	if payInfo.TypePay == RefundStatus {
		refundDetailResult := &DetailCallBackMessage2Refund{}
		if err := json.Unmarshal([]byte(payInfo.Msg), refundDetailResult); err != nil {
			return nil, nil, "", err
		}
		return nil, refundDetailResult, payInfo.TypePay, err
	}

	return nil, nil, "", err
}
