1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: return ok status even upon subscription event handling failure [skip ci]

This commit is contained in:
Ferdinand Mütsch 2023-04-10 18:49:29 +02:00
parent b33c71b41f
commit df25183035

View File

@ -219,8 +219,7 @@ func (h *SubscriptionHandler) PostWebhook(w http.ResponseWriter, r *http.Request
// example payload: https://pastr.de/p/k7bx3alx38b1iawo6amtx09k // example payload: https://pastr.de/p/k7bx3alx38b1iawo6amtx09k
subscription, err := h.parseSubscriptionEvent(w, r, event) subscription, err := h.parseSubscriptionEvent(w, r, event)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) return // status code already written
return
} }
logbuch.Info("received stripe subscription event of type '%s' for subscription '%s' (customer '%s').", event.Type, subscription.ID, subscription.Customer.ID) logbuch.Info("received stripe subscription event of type '%s' for subscription '%s' (customer '%s').", event.Type, subscription.ID, subscription.Customer.ID)
@ -233,14 +232,14 @@ func (h *SubscriptionHandler) PostWebhook(w http.ResponseWriter, r *http.Request
customer, err := stripeCustomer.Get(subscription.Customer.ID, nil) customer, err := stripeCustomer.Get(subscription.Customer.ID, nil)
if err != nil { if err != nil {
conf.Log().Request(r).Error("failed to fetch stripe customer with id '%s', %v", subscription.Customer.ID, err) conf.Log().Request(r).Error("failed to fetch stripe customer with id '%s', %v", subscription.Customer.ID, err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusOK) // don't make stripe retry the event
return return
} }
u, err := h.userSrvc.GetUserByEmail(customer.Email) u, err := h.userSrvc.GetUserByEmail(customer.Email)
if err != nil { if err != nil {
conf.Log().Request(r).Error("failed to get user with email '%s' as stripe customer '%s' for processing event for subscription %s, %v", customer.Email, subscription.Customer.ID, subscription.ID, err) conf.Log().Request(r).Error("failed to get user with email '%s' as stripe customer '%s' for processing event for subscription %s, %v", customer.Email, subscription.Customer.ID, subscription.ID, err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusOK) // don't make stripe retry the event
return return
} }
user = u user = u
@ -248,7 +247,7 @@ func (h *SubscriptionHandler) PostWebhook(w http.ResponseWriter, r *http.Request
if err := h.handleSubscriptionEvent(subscription, user); err != nil { if err := h.handleSubscriptionEvent(subscription, user); err != nil {
conf.Log().Request(r).Error("failed to handle subscription event %s (%s) for user %s, %v", event.ID, event.Type, user.ID, err) conf.Log().Request(r).Error("failed to handle subscription event %s (%s) for user %s, %v", event.ID, event.Type, user.ID, err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusOK) // don't make stripe retry the event
return return
} }
@ -256,16 +255,14 @@ func (h *SubscriptionHandler) PostWebhook(w http.ResponseWriter, r *http.Request
// example payload: https://pastr.de/p/d01iniw9naq9hkmvyqtxin2w // example payload: https://pastr.de/p/d01iniw9naq9hkmvyqtxin2w
checkoutSession, err := h.parseCheckoutSessionEvent(w, r, event) checkoutSession, err := h.parseCheckoutSessionEvent(w, r, event)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) return // status code already written
return
} }
logbuch.Info("received stripe checkout session event of type '%s' for session '%s' (customer '%s' with email '%s').", event.Type, checkoutSession.ID, checkoutSession.Customer.ID, checkoutSession.CustomerEmail) logbuch.Info("received stripe checkout session event of type '%s' for session '%s' (customer '%s' with email '%s').", event.Type, checkoutSession.ID, checkoutSession.Customer.ID, checkoutSession.CustomerEmail)
user, err := h.userSrvc.GetUserById(checkoutSession.ClientReferenceID) user, err := h.userSrvc.GetUserById(checkoutSession.ClientReferenceID)
if err != nil { if err != nil {
conf.Log().Request(r).Error("failed to find user with id '%s' to update associated stripe customer (%s)", user.ID, checkoutSession.Customer.ID) conf.Log().Request(r).Error("failed to find user with id '%s' to update associated stripe customer (%s)", user.ID, checkoutSession.Customer.ID)
w.WriteHeader(http.StatusInternalServerError) return // status code already written
return
} }
if user.StripeCustomerId == "" { if user.StripeCustomerId == "" {