(w http.ResponseWriter, r *http.Request)
| 43 | } |
| 44 | |
| 45 | func (o *orbWebhook) handleWebhook(w http.ResponseWriter, r *http.Request) error { |
| 46 | r.Body = http.MaxBytesReader(w, r.Body, maxBodyBytes) |
| 47 | payload, err := io.ReadAll(r.Body) |
| 48 | if err != nil { |
| 49 | return httputil.Errorf(http.StatusServiceUnavailable, "error reading request body: %w", err) |
| 50 | } |
| 51 | |
| 52 | // unmarshal event first before even verifying signature, so we can ignore unwanted events as Orb does not have an option to selectively deliver only required events |
| 53 | var e genericEvent |
| 54 | err = json.Unmarshal(payload, &e) |
| 55 | if err != nil { |
| 56 | return httputil.Errorf(http.StatusBadRequest, "error parsing event data: %w", err) |
| 57 | } |
| 58 | |
| 59 | if !slices.Contains(interestingEvents, e.Type) { |
| 60 | w.WriteHeader(http.StatusOK) |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | now := time.Now().UTC() |
| 65 | err = o.verifySignature(payload, r.Header, now) |
| 66 | if err != nil { |
| 67 | return httputil.Errorf(http.StatusBadRequest, "error verifying webhook signature: %w", err) |
| 68 | } |
| 69 | |
| 70 | switch e.Type { |
| 71 | case "invoice.payment_succeeded": |
| 72 | var ie invoiceEvent |
| 73 | err = json.Unmarshal(payload, &ie) |
| 74 | if err != nil { |
| 75 | return httputil.Errorf(http.StatusBadRequest, "error parsing event data: %w", err) |
| 76 | } |
| 77 | err = o.handleInvoicePaymentSucceeded(r.Context(), ie) |
| 78 | if err != nil { |
| 79 | return httputil.Errorf(http.StatusInternalServerError, "error handling event: %w", err) |
| 80 | } |
| 81 | case "invoice.payment_failed": |
| 82 | var ie invoiceEvent |
| 83 | err = json.Unmarshal(payload, &ie) |
| 84 | if err != nil { |
| 85 | return httputil.Errorf(http.StatusBadRequest, "error parsing event data: %w", err) |
| 86 | } |
| 87 | err = o.handleInvoicePaymentFailed(r.Context(), ie) |
| 88 | if err != nil { |
| 89 | return httputil.Errorf(http.StatusInternalServerError, "error handling event: %w", err) |
| 90 | } |
| 91 | case "invoice.issue_failed": |
| 92 | var ie invoiceEvent |
| 93 | err = json.Unmarshal(payload, &ie) |
| 94 | if err != nil { |
| 95 | return httputil.Errorf(http.StatusBadRequest, "error parsing event data: %w", err) |
| 96 | } |
| 97 | // inefficient one time conversion to named logger as its rare event and no need to log every thing else with named logger |
| 98 | o.orb.logger.Named("billing").Warn("invoice issue failed", zap.String("customer_id", ie.OrbInvoice.Customer.ExternalCustomerID), zap.String("invoice_id", ie.OrbInvoice.ID), zap.String("props", fmt.Sprintf("%v", ie.Properties))) |
| 99 | case "subscription.started": |
| 100 | var se subscriptionEvent |
| 101 | err = json.Unmarshal(payload, &se) |
| 102 | if err != nil { |
nothing calls this directly
no test coverage detected