fusenapi/server/pay/internal/handler/stripewebhookhandler.go

58 lines
1.2 KiB
Go
Raw Normal View History

2023-07-26 11:23:16 +00:00
package handler
import (
2023-07-28 03:15:42 +00:00
"io"
2023-07-26 11:23:16 +00:00
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/pay/internal/logic"
"fusenapi/server/pay/internal/svc"
"fusenapi/server/pay/internal/types"
)
func StripeWebhookHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2023-07-28 03:15:42 +00:00
const MaxBodyBytes = int64(65536)
r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes)
defer r.Body.Close()
payload, err := io.ReadAll(r.Body)
2023-07-26 11:23:16 +00:00
if err != nil {
return
}
2023-07-28 03:15:42 +00:00
var req types.StripeWebhookReq
2023-08-08 06:20:57 +00:00
// userinfo, err := basic.RequestParse(w, r, svcCtx.SharedState, &req)
2023-07-28 03:15:42 +00:00
// if err != nil {
// return
// }
2023-07-28 11:03:36 +00:00
IPAddress := r.Header.Get("X-Real-Ip")
if IPAddress == "" {
IPAddress = r.Header.Get("X-Forwarded-For")
}
if IPAddress == "" {
IPAddress = r.RemoteAddr
}
req.RemoteAddr = IPAddress
2023-07-28 03:15:42 +00:00
req.Payload = payload
req.StripeSignature = r.Header.Get("Stripe-Signature")
2023-07-26 11:23:16 +00:00
// 创建一个业务逻辑层实例
l := logic.NewStripeWebhookLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
2023-07-28 03:15:42 +00:00
resp := l.StripeWebhook(&req, nil)
2023-07-26 11:23:16 +00:00
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}